Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Does Rust not have the equivalent of GCC's "-ffast-math"?




No, because as I commented in another subthread, `-ffast-math` is:

1. dangerous assumptions hidden behind a simple, attractive-looking option [1]. It should be called -fwrong-math or -fdangerous-math or something (GCC does have the funnily named switch -funsafe-math-optimizations – what could go wrong with fun, safe math optimizations?!)

2. Translation-unit scoped, which means that dependencies not consented to "fast math" can break your code (as in UB land) or make the optimizations pointless, and your code can break your dependencies' semantics too via inlining. On the other hand, a library author must think very carefully what float opts to enable in order to be compatible with client code.

Deciding how the scoping of non-IEEE float math operations should work is a very nontrivial question. The scope could be a translation unit, a module, a type, a function, a block, or every individual operation, and none of those is without issues, particularly regarding questions like inlining and interprocedural and link-time-optimization, as well as ergonomics. In other ways, it's yet another function coloring problem.

There are currently-unstable "algebraic_add/mul/etc" methods for floats for letting LLVM treat those particular operations as if floats were real numbers [2]. They're the first step towards safe UB-free float optimizations, but of course those names are rather awkward to use in math-heavy code, and a wrapper type overloading the normal operators would be good to have.

---

[1] See, eg. https://simonbyrne.github.io/notes/fastmath/

[2] In terms of associativity and such, not in eg. assuming the nonexistence of NaNs, which would be very unsafe.


As a student of floating point math idiosyncrasies, I had always thought -ffast-math should be renamed -fsloppy-math.

How does one become a "student of floating point math idiosyncracies"?

By enrolling at “Floating Point Math Idiosyncrasy University” I suppose

No it doesn't. A global flag is a no-go as it breaks modularity. A local opt-in through dedicated types or methods is being designed but it's not stable.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: