Search code examples
c++floating-pointc++20volatileglm-math

GLM library deprecation warnings around volatile in C++20


Many operations with volatile were deprecated in c++20 (see https://en.cppreference.com/w/cpp/language/cv)

So.... when I used very popular glm library(https://github.com/g-truc/glm) in my project I got warnings around this code (file type_half.inl):

GLM_FUNC_QUALIFIER float overflow()
{
    volatile float f = 1e10;

    for(int i = 0; i < 10; ++i)
        f *= f; // this will overflow before the for loop terminates
    return f;
}

This code used to force causing floating point overflow, and obviously volatile here used to turn off optimization around f. Otherwise compiler can just throw out this cycle as useless.

How this code may be fixed for modern standards of c++, like c++20 and above?


Solution

  • The obvious solution is to remove the *=: f = f * f;. The code doesn't specifically need any special behavior out of *=; it just wants to make the compiler not optimize the multiplying operations. To the extent that volatile makes the compiler do that, it would work just as well with f = f * f;.