CUDA 6.0 const int warning: division by zero

The following code compiles and the compiler gives warning: division by zero

const int gggp_thread = blockIdx.x*blockIdx.x+threadIdx.x;
const int gggp_I = gggp_thread/(0+0);

When I print gggp_I it has the value 1.
Is this what you would expect?

                        Thank you


    Dr. W. B. Langdon,
    Department of Computer Science,
    University College London
    Gower Street, London WC1E 6BT, UK

GI 2015
EuroGP 2015
choose your background
A Field Guide to Genetic Programming
GP Bibliography

Since division by zero is essentially undefined (integers cannot represent infinity), it is about as good an answer as you would expect to get. Whether the compiler should throw a warning or an error is pretty much up to the compiler writer. As usual, check your compiler warnings! They can often hide mistakes.

I suppose the bigger question is what would you expect it to do as presumably that is not what you want to happen.


Thanks Tiomat. (To confirm the nvidia nvcc compiler does indeed give a warning:-).
I was wondering if I might get a run time error?
Or if CUDA int divide by zero will always give 1?

Undefined means exactly that. One cannot rely on knowledge of implementation artifacts. From the C99 standard:

“The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.”

From the 1998 C++ standard:

“The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined; […]”

In your code above the result of 1 is produced by the compiler. The actual integer division operations in CUDA return a pattern of all 1s for a divisor of zero. I wrote the integer division code currently used by CUDA for Fermi and Kepler (no idea whether it was subsequently rewritten for Maxwell), and as I recall the “all 1s” result in case of division by zero was specified for compatibility with Direct X.

Dear njuffa,
Thank you for your rapid and helpful reply.