Sept. 30, 2003
4:59 a.m.
If your machine does not have an instruction for computing the absolute value, this computation can usually be done in three or four branch-free instructions. First, compute: signed int x signed int y = x >> 31 and then one of the following: abs = (x ^ y) - y abs = (x + y) ^ y abs = x - ((x<<1) & y)