JuliaLang/julia — pull request #9406 by eschnett (2014-12-19 03:49:59+00:00)
Title: Implement `@fastmath`

This implements a macro `@fastmath` that works similar to `@inbounds`: expressions evaluated this way have LLVM's "fast-math" flag set, which is equivalent to calling clang with the `-ffast-math` option.

I experimented with two different ways to implement this. Both work, but obviously only one of them should be used, the other should be removed again.

**Implementation 1:** Math functions such as `+`, `-` have fast-math equivalent called `add_fast`, `sub_fast`, etc. These have their own intrinsics. A macro `@fastmath` (easy to implement, but not yet done) would convert math operations to their fast form. Advantage: This gives programmers full control over when to use fast math operations, and when not. Disadvantage: The macro needs to convert a parse tree, which may be slow. I believe `@simd` goes this way.

**Implementation 2:** The Julia compiler maintains a global state variable that describes whether fast-math is in effect or not. The `@fastmath` macro (this is how it is currently implemented) toggles this flag. Advantage: Very fast. Disadvantage: Uses global state, I can't tell whether the toggling works at the right times. This is equivalent to how `@inbounds` is working.

Please comment, both on the general idea of having `@fastmath` (I believe there is consensus that this is a good and necessary thing), as well as on the implementations. I will continue to update the branch with suggestions.