C++20 volatile warning

Good day! :3

I am using GCC 10.2.0 from MSYS2 on Windows.
Recently I moved one C++ project from C++17 to C++20, and the problem with GLM library occurred.

Library:
1
2
https://github.com/g-truc/glm
GLM is a header only C++ mathematics library for graphics software based on the OpenGL Shading Language (GLSL) specifications.


Compiler output:
1
2
3
/glm/detail/type_half.inl:9:6: warning: compound assignment with 'volatile'-qualified left operand is deprecated [-Wvolatile]
9 |    f *= f; // this will overflow before the for loop terminates
  |    ~~^~~~


Library function body:
1
2
3
4
5
6
7
8
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;
}


What is the correct way to fix that?
Last edited on
At a guess
f = f * f;

Not sure what making it volatile does for you, except to force the machine to always load/store a float (thus washing away any improved accuracy by keeping the result in extended floating point format until the calculation is over).
I have no idea why library creators made it volatile... (:
But your fix worked for me, thank you! =)
I will mark this thread as solved in a few days.
Making it volatile stops the loop from being optimized away.
Although I don't know what the point of that is here.
The authors postulate that the library works with a wide range of compilers, perhaps this loop optimization is causing problems somewhere...
1
2
3
4
5
6
7
8
GCC 4.7 and higher
Intel C++ Compose XE 2013 and higher
Clang 3.4 and higher
Apple Clang 6.0 and higher
Visual C++ 2013 and higher
CUDA 9.0 and higher (experimental)
SYCL (experimental: only ComputeCpp implementation has been tested).
Any C++11 compiler
Note in C++20, some previous uses of volatile have been deprecated. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1152r4.html
Topic archived. No new replies allowed.