Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast branchless max for unsigned integers

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.

like image 203
plasmacel Avatar asked Jul 30 '13 12:07

plasmacel


1 Answers

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);
}
like image 132
cmaster - reinstate monica Avatar answered Sep 18 '22 13:09

cmaster - reinstate monica