r/cpp • • Feb 16 '26

Favorite optimizations ??

I'd love to hear stories about people's best feats of optimization, or something small you are able to use often!

136 Upvotes

185 comments sorted by

View all comments

Show parent comment

4

u/James20k P2005R0 Feb 16 '26

I agree that the rule is surprising/wrong (and it makes portability a nightmare), the issue is that this:

Rust did the right thing, and made FMA explicitly opt-in (f32::mul_add()).

Isn't really a fix either unfortunately. The problem is that its extremely difficult to hand-write FMAs as well as the compiler does

In my case, I was doing code generation where I had an AST on-hand which was deliberately structured to maximise fma-ability, as it was absolutely critical to performance. But I've literally never been able to process a chain of adds/muls into FMAs as well as the compiler can, and I've had a lot of goes trying to get it right. I can't really justify a 10% drop in performance for it

I think ideally, I'd love to see a mode where we're able to mandate to the compiler that within a block or region, the maximum fma contraction is applied to all expressions within it - so that where you actively want this behaviour you can get a guarantee that it'll be used

The only thing we have in C++ currently is the #pragma for fp contraction, which is optional (and nvidia don't implement it on cuda unfortunately)

0

u/[deleted] Feb 16 '26

[deleted] — view deleted comment

3

u/James20k P2005R0 Feb 16 '26

In C++, compilers can and do reorder operations to produce FMAs, pretty arbitrarily. Its not a massively widely known fact, but they actively don't evaluate code according to ieee (and never have done). You have to do some extreme convolutions if you want your code to compile to the equivalent ieee sequence as what you'd expect

The relevant part of the spec is called fp contraction. I wrote up a pretty massive post about how this breaks numerical computing and found examples in the wild of this, but I haven't published it yet

1

u/tialaramex Feb 17 '26

Ultimately I think the outcome is that we bake Herbie-like capabilities into a programming language. You specify the real mathematical operation you want to approximate - with parameters on the accuracy and performance desired and the compiler spits out machine code to get as close as possible for your target hardware or an error because you want the impossible.

0

u/[deleted] Feb 17 '26

[deleted] — view deleted comment

2

u/James20k P2005R0 Feb 17 '26

This is unfortunately a common misconception, its simply not true in C++. C++ doesn't even mandate that floats are ieee

I'd recommend looking up floating point contraction in C++, a lot of people think that C++ gives much stronger guarantees than it actually does

https://godbolt.org/z/hMGbjoWz7

I modified one of the examples from the reproducible floating point paper, without -ffast-math being enabled, the compiler automatically generates an fma, and this results in cross platform divergence. Its completely legal in C++

0

u/[deleted] Feb 17 '26

[deleted] — view deleted comment

2

u/James20k P2005R0 Feb 17 '26

nvc++ follows the spec just fine here

This is the specific segment of the spec that allows this behaviour:

https://eel.is/c++draft/expr#pre-6

The values of the floating-point operands and the results of floating-point expressions may be represented in greater precision and range than that required by the type; the types are not changed thereby.37

This is the MSVC documentation:

https://learn.microsoft.com/en-us/cpp/preprocessor/fp-contract?view=msvc-170

The C/C++ spec permits floating point contraction to be on by default

If you pass -fno-fast-math into clang, it sets:

-ffp-contract=on

https://clang.llvm.org/docs/UsersManual.html on x64, but:

-fno-fast-math sets -ffp-contract to on (fast for CUDA and HIP).

Which is why you see divergence between nvcc (which is clang based), and clang. In fact, the clang docs say this:

on: enable C and C++ standard compliant fusion in the same statement unless dictated by pragmas (default for languages other than CUDA/HIP)

GCC says this:

https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

-ffp-contract=off disables floating-point expression contraction. -ffp-contract=fast enables floating-point expression contraction such as forming of fused multiply-add operations if the target has native support for them. -ffp-contract=on enables floating-point expression contraction if allowed by the language standard. This is implemented for C and C++, where it enables contraction within one expression, but not across different statements.

The default is -ffp-contract=off for C in a standards compliant mode (-std=c11 or similar), -ffp-contract=fast otherwise.

It is absolutely permitted by the spec, and the big 3 compilers

0

u/[deleted] Feb 18 '26

[deleted] — view deleted comment

1

u/James20k P2005R0 Feb 18 '26 edited Feb 20 '26

not replace mul+add with FMA unless allowed explicitly

I've linked explicit documentation that indicates that clang defaults this to on, I'd ask that you at least read the comments you reply to

This permits operation fusing, and Clang takes advantage of this by default (on)

if the HW cannot do that, you are out of spec anyway.

Where does it say in the C++ standard that floats must be IEEE?