2-4. Absolute Value Function

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 and then one of the following:

By “2x” we mean, of course, x + x or x << 1.

If you have a fast multiply by a variable whose value is ±1, the following will do:

Get Hacker's Delight now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.