Dynamic -ffast-math

Is it possible to selectively enable / disable runtime at runtime? For example, by creating FastMath and AccurateMath classes with a common Math base class so that you can use both implementations at run time? Also for blinking subnormal numbers to zero, etc.

In particular, I don’t know if compiling with -ffast-math will emit an instruction that, after executing it, will affect all numerical calculations in the stream (for example, setting a flag to align trims to zero).

+6
source share
2 answers

Try the following:

gcc -ffast-math -c first.c gcc -c second.c gcc -o dyn_fast_math first.o second.o 

Entering unique functions in first.c and second.c. That should do the trick. There is rarely any β€œglobal” impact of compiler optimization. If this does exist, the binding will most likely fail due to conflict.

I tried a small sample without any problems.

Here is an example.

first.c

 extern double second(); double first () { double dbl; dbl = 1.0; dbl /= 10.0; return dbl; } int main () { printf("first = %f\n", first()); printf("second = %f\n", second()); return 0; } 

second.c

 double second () { double ddbl; ddbl = 1.0; ddbl /= 10.0; return ddbl; } 

compilation

 gcc -S first.c gcc -c first.s gcc -ffast-math -S second.c gcc -ffast-math -c second.s gcc -o prog first.o second.o 

Check the difference between first.s and second.s and you will find the following:

 movapd %xmm1, %xmm2 divsd %xmm0, %xmm2 movapd %xmm2, %xmm0 

changes to this:

 mulsd %xmm1, %xmm0 

Both functions are called, and both return the expected result.

+3
source

If you do not want to guess with the assembly, you can do the following:

 #pragma fast-math push #pragma fast-math on [..] #pragma fast-math pop 

GCC may have slightly different syntax, but I expect this to be possible too.

+4
source

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


All Articles