Live data from Hacker News

Someone’s Been Messing with My Subnormals

moyix.blogspot.com

21–30 of 132 posts

Re: Someone’s Been Messing with My Subnormals

#22
The Julia package ecosystem has a lot of safeguards against silent incorrect behavior like this. For example, if you try to add a package binary build which would use fast math flags, it will throw an error and tell you to repent:

https://github.com/JuliaPackaging/BinaryBuilderBase.jl/blob/...

In user codes you can do `@fastmath`, but it's at the semantic level so it will change `sin` to `sin_fast` but not recurse down into other people's functions, because at that point you're just asking for trouble. There's also calls to rename it `@unsafemath` in Julia, just to make it explicit. In summary, "Fastmath" is overused and many times people actually want other optimizations (automatic FMA), and people really need to stop throwing global changes around willy-nilly, and programming languages need to force people to avoid such global issues both semantically and within its package ecosystems norms.

Re: Someone’s Been Messing with My Subnormals

#24

> it turns out that when you use -Ofast, -fno-fast-math does not, in fact, disable fast math. lol. lmao. What about -fno-unsafe-math-optimizations?

Nope, it still links in crtfastmath:

    $ gcc -Ofast -fno-unsafe-math-optimizations -fpic -shared foo.c -o foo.so
    $ objdump -j .text --disassemble=set_fast_math foo.so

    foo.so:     file format elf64-x86-64


    Disassembly of section .text:

    0000000000001040 :
        1040: f3 0f 1e fa           endbr64 
        1044: 0f ae 5c 24 fc        stmxcsr -0x4(%rsp)
        1049: 81 4c 24 fc 40 80 00  orl    $0x8040,-0x4(%rsp)
        1050: 00 
        1051: 0f ae 54 24 fc        ldmxcsr -0x4(%rsp)
        1056: c3                    retq

Re: Someone’s Been Messing with My Subnormals

#25
post #21

-Ofast isn't a good name for the option, but in GCC's defense the manual is pretty clear about all this, and there's no excuse for blindly turning on compiler options - they literally change the semantics of your code.

I agree. I think --ffast-math should actually be called --finexact-math. One would also hope that explicitly disabling an option on the command line would, you know, explicitly disable the option, but maybe that's too much to ask.

Re: Someone’s Been Messing with My Subnormals

#26
post #21

-Ofast isn't a good name for the option, but in GCC's defense the manual is pretty clear about all this, and there's no excuse for blindly turning on compiler options - they literally change the semantics of your code.

I wholeheartedly disagree.

    -Ofast
    
    Disregard strict standards compliance. ...
There's strict standards compliance and then there's the crazy grab bag of code changes that is `-ffast-math`. Further, I'd say gevent can defensibly say that -ffast-math is okay for them given what the manual says:

    -ffast-math
    
    ... it can result in incorrect output for programs that depend on an exact
    implementation of IEEE or ISO rules/specifications for math functions.
    It may, however, yield faster code for programs that do not require the
    guarantees of these specifications.
This is 100% on the compiler people. For the option name, the documentation, and the behavior.

https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/Optimize-Optio...

Re: Someone’s Been Messing with My Subnormals

#27
post #16

I ran Gentoo back in the good old days. The biggest draw was that after about a week of compiling my system ran a lot faster because of all the compiler optimisations one could enable because it only had to work on your CPU. I might be misremembering, but I think fastmath was one of the flags explicitly warned against in the Gentoo manual.

ChromeOS is sort of the successor to Gentoo. The images are built with profile-guided, link-time, and post-link optimization, and they are targeted to the specific CPU in a given Chromebook. Every other Linux leaves a large amount of performance on the table by targeting a common denominator CPU that's 20 years old and not having PGO.

It's not a successor, it's a derivative. And yes, if you're only targeting specific known hardware than you can and probably should optimize for it, but most distributions fully intend to be usable on very nearly any x86(_64) hardware so they can't do that.

Re: Someone’s Been Messing with My Subnormals

#28

The problem here is that enabling FTZ/DAZ flags involves modifying global (technically thread-local) state that is relatively expensive to do. Ideally, you'd want to twiddle these flags only for code that wants to work in this mode, but given the relative expense of this operation, it's not entirely practicable to auto-add twiddling to every function call, and doing it manually is somewhat challenging because compile…

[deleted]

Re: Someone’s Been Messing with My Subnormals

#29
post #21

-Ofast isn't a good name for the option, but in GCC's defense the manual is pretty clear about all this, and there's no excuse for blindly turning on compiler options - they literally change the semantics of your code.

It's a quirk of language, that for compiler writers and other algorithmic people "fast" often means "ballpark, but damn quick".

It's hard to come up with a similar name that isn't long.

Re: Someone’s Been Messing with My Subnormals

#30
post #21

-Ofast isn't a good name for the option, but in GCC's defense the manual is pretty clear about all this, and there's no excuse for blindly turning on compiler options - they literally change the semantics of your code.

I agree. I think --ffast-math should actually be called --finexact-math. One would also hope that explicitly disabling an option on the command line would, you know, explicitly disable the option, but maybe that's too much to ask.

I don't think it should exist at all. It's such a crazy grab bag of code changes disguised as "optimizations" that it's completely impossible to reason about, even for folks that "don't care" about the exact floating point arithmetic.

It has global effects like those in TFA, and even locally you no longer know if a line or two of arithmetic will become more precise (e.g., by using higher precision intermediate results), less precise, or become complete gibberish (e.g., because it thinks it can prove you're now dividing by zero and thus can just return whatever it wants).

Post reply on HN