Wednesday, October 8, 2014

Compute the integer absolute value without branching operation

int input_value;                    // Find the absolute value of input_value
unsigned int abs_value;       // the result value
#define CHAR_BIT           8
int const mask = input_value >> sizeof(int) * CHAR_BIT - 1;

abs_value = (input_value + mask) ^ mask;

No comments:

Post a Comment