I found a trick from the AGGREGATE Magic for fast computing max values. The only problem that this is for integers, and however I have tried some things, have no idea how to make a version for unsigned integers.
inline int32_t max(int32_t a, int32_t b)
{
return a - ((a-b) & (a-b)>>31);
}
Any advice?
EDIT
Do not use this, because as others stated it produces undefined behavior. For any modern architecture the compiler will able to emit a branchless conditional move instruction from return (a > b) ? a : b
, that will be faster than the function in question.
What does this code do? It takes the value of a and the difference a - b
. Of course, a - (a - b)
is b
. And (a - b) >> 31
simply creates a mask of ones iff a - b
is negative.
This code is incorrect, iff you get an overflow on the subtraction. That, however is the same story as for unsigned integers. So iff you are content with the fact, that your code is not correct for the entire value range, you can simply ignore unsignedness and use this:
inline uint32_t umax(uint32_t a, uint32_t b) {
return (uint32_t)max((int32_t)a, (int32_t)b);
}
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