[pypy-dev] OT: abs(x) with 4 assembly insns

Damien Morton damien-morton at nyc.rr.com
Tue Sep 30 06:59:16 CEST 2003


>From http://www.hackersdelight.org/basics.pdf

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)



More information about the Pypy-dev mailing list