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: