Skip to content

Instantly share code, notes, and snippets.

@c00kiemon5ter
Created February 23, 2012 01:53
Show Gist options
  • Save c00kiemon5ter/1889147 to your computer and use it in GitHub Desktop.
Save c00kiemon5ter/1889147 to your computer and use it in GitHub Desktop.
absolute function in c that does not branch
// 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