Consistent Float Code Behavior with GCC

I do some numerical calculations, and I often had problems with floating point calculations when using GCC. For my current purpose, I don't care too much about the real accuracy of the results, but I want this proprietary property:

no matter where the same code is in my program, when it runs on SAME inputs, I want it to produce GAME outputs.

How can I get GCC to do this? And specifically, what is the behavior of -fast-math and various -O optimization?

I heard that GCC can try to be smart and sometimes load floats into registers and sometimes read them directly from memory, and this can change the accuracy of the floats, resulting in a different conclusion. How can i avoid this?

Again, I want:

  • my calculations will be fast
  • my calculations are reliable (i.e. the same input -> the same result)
  • I don't care about the accuracy for this particular code, so I can be fine-tuned if it brings reliability.

Can someone tell me what is the way to solve this problem?

+3
source share
2 answers

, GCC , , ​​ . , . , GCC, , , (, , , , , -).

, , GCC, , . , , , , , , , . (?) , , , , , f-p, - . , , , , , , usd GCC.

, , ,

- () ? , ; , , . , . (, 5 , .)

, : , (, , , , , ), ​​ , , , 64- fp. . , , f-p, , ; , , .

, , - , , . f-p. , .

: , .

-1

x86, , gcc, SSE2 ( ), .

PowerPC, , gcc not, fmadd ( , ), .

--fast-math: , . Gcc , , , .

(exp, sin,...) , , .

, , ( C99 ), , C99 , . , , - . , , double, x86. : , .

+1

Source: https://habr.com/ru/post/1734524/


All Articles