mctools / ncrystal

NCrystal : a library for thermal neutron transport in crystals and other materials
https://mctools.github.io/ncrystal/
Other
38 stars 17 forks source link

Compile with -fno-math-errno #169

Open tkittel opened 3 months ago

tkittel commented 3 months ago

As explained here:

https://stackoverflow.com/questions/57673825/how-to-force-gcc-to-assume-that-a-floating-point-expression-is-non-negative/57674631#57674631

we should compile with -fno-math-errno for more efficient code (not sure if this only affects gcc, or also clang?). Obviously as a private compilation flag for libNCrystal only.

Specifically I discovered the need for this when I was trying to get simd auto-vectorisation to work on a large number of neutrons, and no matter when I simply could not get a simple loop like the following to vectorise:

for ( unsigned i = 0; i < n; ++i)
   v[i] = std::sqrt(x[i])

The problem is that the errno handling adds a branch inside the sqrt implementation. Using __builtin_sqrt or sqrt(fabs(x[i])) didn't work either. However compiling with -fno-math-errno did the trick.

tkittel commented 3 months ago

Clang accepts this flag, but it might be mostly for the case where it is using the gnu libc. In any case, it seems to do no harm for clang.

tkittel commented 2 weeks ago

This will be in the next release.