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 graphics/02icon47.gifand 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 ?, the following will do: