C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Thu, 15 Aug 2024 15:31:20 -0700
On Thursday 15 August 2024 12:22:43 GMT-7 Jens Maurer via Std-Proposals wrote:
> If "a" and "b" are simple enough, the optimizer can often
> fold the two checks.

Clang and GCC do for this example:
https://gcc.godbolt.org/z/aGWYnvr1G

In my experience, Clang aggressively prefers branchless codegen, to the point
where I think it sometimes generates worse code than GCC. But it should run
with the same timing in all cases. (this case is such an example: it should
run in 6 uops / 4 cycles in all cases, but the ICC code to the right runs in
2, 3 or 4 uops / 2, 2, 3 cycles because of macro-fusion, maybe even 2 cycles
in all cases due to the zero-idiom at the end).

I think this is a QoI problem, first and foremost. If you want the compiler to
generate code that always runs in the same cycle count, you want the
*compiler* to know what it is doing.

And I'm not convinced this is needed even for current compilers in real-time
conditions. If you need to know what the worst case runtime is, then it's not
a C++ or compiler problem. Run a code-coverage tool that inspects the branches
and figures out the slowest path given all possibilities. I assume you are
already testing all branches anyway.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel DCAI Platform & System Engineering

Received on 2024-08-15 22:31:24