How does the GPU handle divides by zero?
On the CPU we often waste time checking to eliminate unwanted infinities or prevent slow exception handling.
What is the behavior and our options here? Section ‘C.2.1 Single-Precision Floating-Point Functions’ of the Programming Guide was not clear on this.
That’s a good question.
The floating-point division behaves as IEEE-754 requires. That is, it returns an infinity that has the same sign as the zero divider.
(And in single-precision, all denormal numbers are considered as zero, so they generate an infinity as well.)
There are (currently) no arithmetic exceptions on the GPU, and special cases are handled in hardware, so no slowdown due to exceptions/microcode.
so how do we handle it without using if conditional operators. Is there anyway (faster way) to detect it ??
I cannot think of anything faster than if (isinf()), isinf() is probably 1 clock cycle, so I doubt you can get faster than that.
You don’t have to add a test after every operation. If you get exceptional values at some point, they will propagate in the rest of the computation and your results will eventually be NaNs (as long as you don’t use fmin and fmax). So you can just check periodically that your results aren’t NaNs, and stop when it happens. You won’t know where exactly it occurred first, but you can’t miss it.
Or wait for the day when GPUs will support IEEE flags, which I suspect might not be so far away. External Media