Avatar ·

Why does replacing 0.1f with 0 slow down code by 10 times in Visual Studio 2010?

Hello everyone! I'm writing in C++ in Visual Studio 2010. I wrote a simple loop for floating-point calculations, but noticed strange behavior. If the loop condition uses the number 0.1f, the code runs fast. If I replace it with 0 (or any other integer, like 1), performance drops by about 10 times. Here's an example:\n\n```cpp\nconst float x = 1.0f;\nfor (int i = 0; i < 100000000; ++i) {\n // Fast version: if (x < 0.1f)\n // Slow version: if (x < 0)\n}\n```\n\nI'm compiling in Release with /O2 optimizations. Why such a difference? Is the compiler doing something wrong? Or is this an SSE/FPU feature? I'd appreciate an explanation because I'm stumped. Thanks in advance!\n<<>>\n

Root of the problem: denormalized numbers and the penalty for handling them

\n

The performance difference