I tried gcc 13.2 and clang 18.1.0 with -O3 -march=core-avx2. They both do what the CUDA compiler does: convert integer to float, add 1.538461565971f, convert float-to-int; this instruction sequence repeated ten times.
Intel icx 2024.0.0 with -O3 -march=core-avx2 is a tiny bit smarter. It converts *count to float once, then adds 1.538461565971f followed by a trunc operation (vroundss mode 11); rinse and repeat 10 times, then converts float to int once at the end.
The refusal of these highly-optimizing compilers to propagate 1.538461565971f through the float-to-int conversion likely means something, but I don’t know what that could be. If you file a performance bug with NVIDIA you may find out.
[Later:]
I think I understand it now, and the compiler behavior follows from plain C++ semantics.
(*counter) += 2 / 1.3f;
means
(*counter) = (*counter) + 2 / 1.3f;
means
(*counter) = (int)((float)(*counter) + ((float)2 / 1.3f));
which can be optimized to (as reflected in the generated machine code):
(*counter) = (int)((float)(*counter) + 1.538461565971f);
Basically we have the addition of an int and a float on the RHS, causing the int to be promoted to float by the rules for implicit conversions applied during expression evaluation. To get the desired code optimization, you would need to write
(*counter) += (int)(2 / 1.3f);
[Even later]
What seems to be missing from the CUDA compiler is a potential peephole optimization that contracts
int i;
float f, r;
i = f;
r = i;
into
int i;
float f, r;
r = truncf (f);
Analogous for double and long long int. The ISO-C++11 standard specifies in section 4.9:
A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type.
Since the only cases where the behavior of the two code variants above could differ is for those inputs where the “truncated value cannot be represented in the destination type”, and the compiler assumes the absence of undefined behavior when optimizing, this seems like a legitimate transformation.
Unfortunately, not under strict IEEE-754 floating-point semantics, as the two code variants differ for an input of -0.0f, for which truncf() delivers a negative zero but the conversion through the intermediate integer delivers a positive zero. So the optimization is not suitable by default but may be suitable when -use_fast_math is specified, or when the compiler can prove that negative zero is not one of the possible inputs.