Created
February 23, 2012 01:53
-
-
Save c00kiemon5ter/1889147 to your computer and use it in GitHub Desktop.
absolute function in c that does not branch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// branching function: | |
// inline int abs(int x) | |
// { | |
// return (x<0) ? -x : x; | |
// } | |
inline int abs_no_branch(int x) | |
{ | |
int m = (x >> (8 * sizeof(int)-1)); | |
return ((x ^ m) - m); | |
} | |
inline int abs_no_branch_xor_and(int x) | |
{ | |
return (~(1<<(8*sizeof(int)-1)))&n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment