Re: [math-fun] absolute value (branchless)
Ship the number off to your GPU (Graphics Processing Unit), which is incredibly deeply pipelined, and either simply do abs(x) or sqrt(x^2) or isqrt(x^2). The only 'real' (pun intended) problem with abs(x) is the possibility of overflow if x is the most negative 2's complement integer. There still is no good way for deep pipelines to deal with problems like overflow. To a first approximation, most arithmetic calculations are _free_ compared with the cost of fetching & storing results; i.e., fetching a result can cost hundreds of cycles waiting for the answer. Loops in hardware now have _inertia_, in the sense that it may be cheaper to fetch hundreds of items in advance; as a result of this, it may take hundreds of cycles to 'slow down' or 'stop' from a tight loop. I've pondered whether it would be possible to make this analogy with inertia (and mass) more accurate... At 01:25 PM 8/23/2013, Warren D Smith wrote:
suppose my computer wants |x| but it refuses to perform an "if" statement.
What to do?
participants (1)
-
Henry Baker