zlacker

[parent] [thread] 14 comments
1. dahart+(OP)[view] [source] 2024-01-16 15:22:16
It shouldn’t be scary. Any library that is sensitive to order of operations will hopefully have a big fat warning on it. And it can be compiled separately with fast-math disabled. I don’t know of any such libraries off the top of my head, and it’s quite rare to find situations that result in orders of magnitude more error, though I grant you it can happen, and it can be contrived pretty easily.
replies(2): >>planed+v >>mort96+m7
2. planed+v[view] [source] 2024-01-16 15:24:50
>>dahart+(OP)
You can't fully disable fast-math per-library, moreover a library compiled with fast-math might also introduce inaccuracies in a seemingly unrelated library or application code in the same executable. The reason is that fast-math enables some dynamic initialization of the library that changes the floating point environment in some ways.
replies(2): >>london+H3 >>dahart+i6
◧◩
3. london+H3[view] [source] [discussion] 2024-01-16 15:37:15
>>planed+v
you're gonna hive to give us a concrete real world example to convince most of us...
◧◩
4. dahart+i6[view] [source] [discussion] 2024-01-16 15:46:14
>>planed+v
> You can’t fully disable fast-math per library

Can you elaborate? What fast-math can sneak into a library that disabled fast-math at compile time?

> fast-math enables some dynamic initialization of the library that changes the floating point environment in some ways.

I wasn’t aware of this, I would love to see some documentation discussing exactly what happens, can you send a link?

replies(2): >>mort96+L7 >>jcranm+nc
5. mort96+m7[view] [source] 2024-01-16 15:51:17
>>dahart+(OP)
I don't typically thoroughly read through the documentation for all the dependencies which my dependencies are using.

But you're correct that it's probably usually fine in practice.

replies(1): >>dahart+Ja
◧◩◪
6. mort96+L7[view] [source] [discussion] 2024-01-16 15:53:07
>>dahart+i6
> Can you elaborate? What fast-math can sneak into a library that disabled fast-math at compile time?

A lot of library code is in headers (especially in C++!). The code in headers is compiled by your compiler using your compile options.

replies(1): >>dahart+n8
◧◩◪◨
7. dahart+n8[view] [source] [discussion] 2024-01-16 15:55:06
>>mort96+L7
Ah, of course, very good point. A header-only library doesn’t have separate compile options. This is a great reason for a float-sensitive library to not be header-only, right?
replies(1): >>mort96+2a
◧◩◪◨⬒
8. mort96+2a[view] [source] [discussion] 2024-01-16 16:02:17
>>dahart+n8
It's not just about being header-only, lots of libraries which aren't header-only still have code in headers. The library may choose to put certain functions in headers for performance reasons (to let compiler inline them), or, in C++, function templates and class templates generally have to be in headers.

But yeah, it's probably a good idea to not put code which breaks under -ffast-math in headers if possible.

◧◩
9. dahart+Ja[view] [source] [discussion] 2024-01-16 16:05:41
>>mort96+m7
That’s fair. Ideally transitive dependencies should be completely hidden from you. Hopefully the author of the library you include directly has heeded the instructions of libraries they depend on.

Hey I grant and acknowledge that using fast-math carries a little risk of surprises, we don’t necessarily need to try to think of corner cases. I’m mostly pushing back a little because using floats at all carries almost as much risk. A lot of people seem to use floats without knowing how inaccurate floats are, and a lot of people aren’t doing precision analysis or handling the exceptional cases… and don’t really need to.

replies(1): >>ska+HA
◧◩◪
10. jcranm+nc[view] [source] [discussion] 2024-01-16 16:12:20
>>dahart+i6
https://github.com/llvm/llvm-project/issues/57589

Turn on fast-math, it flips the FTZ/DAZ bit for the entire application. Even if you turned it on for just a shared library!

replies(1): >>accoun+jZ2
◧◩◪
11. ska+HA[view] [source] [discussion] 2024-01-16 18:09:04
>>dahart+Ja
> A lot of people seem to use floats without knowing how inaccurate floats are,

Small nit, but floats aren't inaccurate, they have non uniform precision. Some float operations can be inaccurate, but that's rather path dependent...

One problem with -ffast-math is that a) it sounds appealing and b) people don't understand floats, so lots of people turn it on without understanding what it does, and that can introduce subtle problems in code they didn't write.

Sometimes in computational code it makes sense e.g. to get rid of denorms, but a very small fraction of programmers understand this properly, or ever will.

I wish they had named it something scary sounding.

replies(2): >>dahart+U41 >>accoun+r23
◧◩◪◨
12. dahart+U41[view] [source] [discussion] 2024-01-16 20:08:13
>>ska+HA
I am talking about float operations, of course. And they’re all inaccurate, generally speaking, because they round. Fast math rounding error is not much larger than rounding error without fast mast.
◧◩◪◨
13. accoun+jZ2[view] [source] [discussion] 2024-01-17 10:02:51
>>jcranm+nc
That's only one small part of -ffast-math/-Ofast though and not a very scary one at that.
replies(1): >>mort96+Ds5
◧◩◪◨
14. accoun+r23[view] [source] [discussion] 2024-01-17 10:27:47
>>ska+HA
> Sometimes in computational code it makes sense e.g. to get rid of denorms, but a very small fraction of programmers understand this properly, or ever will.

"Some times" here being almost all the time. It is rare that your code will break without denormals if it doesn't already have precision problems with them.

◧◩◪◨⬒
15. mort96+Ds5[view] [source] [discussion] 2024-01-17 23:39:43
>>accoun+jZ2
But it's an example of -ffast-math affecting separately compiled libraries.
[go to top]