[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
this computation can usually be done in three or four branch-free
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