r/cpp • u/Little-Reflection986 • 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
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:
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)