C++ Logo

std-proposals

Advanced search

Re: [std-proposals] No shortcut logical operands-functions

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Thu, 15 Aug 2024 21:15:23 +0200
How to tackle this:   a) One could just try to improve the optimizers: Create a lot of examples (unit tests) and file reports to the compiler vendors, where the code with -O3 is not optimal.   b) Request the speed improvement with an attribute; if the optimizer does not manage, a warning is output at that line of code.   c) If you want to change the code to not create conditional jumps, I would not start with the condition, but with the if clause. Change it to return (a > 0 && b != 0) ? 0 : 1;   d) Only if that is not enough and perhaps Marcin's hint of using &, then I would think of introducing new ways to state the condition.     The think is, the logic of the code stays the same; a good compiler should not care how the logic is formulated, if it basically is identical in meaning.   Introducing new ways, which enforce some kind of translation into machine code, would limit a lot, what the optimizer may still do. Your idea could lead to a pessimization, if e.g. a > 0 does not have to be tested for at all at that point and the new construct enforces a certain translation. Examples could be more extreme, even spanning function calls - perhaps the function can be inlined, and the return would never be executed. Or one conditional jump is enough to avoid 5 others and you prevent it.   So I would start with performance hints before going into stricter enforcements.   Best, Sebastian

Received on 2024-08-15 19:15:25