Just out of curiosity. If I have something like:
if(x < 0) x = 0; if(x > some_maximum) x = some_maximum; return x;
Is there a way to not branch? This is c++.
Addendum: I mean no branch instructions in the assembly. It's a MIPS architecture.
The INTEGER value is stored as a signed binary integer and is typically used to store counts, quantities, and so on. Arithmetic operations and sort comparisons are performed more efficiently on integer data than on float or decimal data. INTEGER columns, however, cannot store absolute values beyond (2 31-1).
I attempted using the boolean operator && to check the two < and > values if they were true, so that anything between the two numbers would activate the cout statement to print the appropriate message for the numerical value entered.
There are bit-tricks to find the minimum or maximum of two numbers, so you could use those to find min(max(x, 0), some_maximum)
. From here:
y ^ ((x ^ y) & -(x < y)); // min(x, y) x ^ ((x ^ y) & -(x < y)); // max(x, y)
As the source states though, it's probably faster to do it the normal way, despite the branch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With