Article View: comp.lang.c++
Article #980Re: What does this code
From: Chris M. Thomasson
Date: Sat, 21 Nov 2026 22:26
Date: Sat, 21 Nov 2026 22:26
94 lines
4712 bytes
4712 bytes
On 11/21/2026 3:43 AM, David Brown wrote:
> On 20/11/2026 23:45, Chris M. Thomasson wrote:
>> On 11/20/2026 7:05 AM, David Brown wrote:
>>> On 20/11/2026 13:43, Michael S wrote:
>>>> On Fri, 20 Nov 2026 12:56:25 +0100
>>>> David Brown <david.brown@hesbynett.no> wrote:
>>>>
>>>>> On 20/11/2026 12:22, Michael S wrote:
>>>
>>>>>>
>>>>>> As to what is good result, it's not that simple. When condition is
>>>>>> predictable, i.e. when p is ==0 >95% of time or <5% of time then
>>>>>> code with branch will be faster than code without branch. Esp. so
>>>>>> when p is originated outside of L1D cache.
>>>>>
>>>>> Indeed. The speed of code on "big" processors is always hard to
>>>>> predict, and can depend on many factors. (That's one reason I like
>>>>> microcontrollers - they are a lot more predictable.) Real-world
>>>>> speed depends on the values you give this kind of function, and all
>>>>> the surrounding code - a function like this is likely to be inlined.
>>>>>
>>>>
>>>> It is possible and relatively easy to convince clang to generate
>>>> branchy code that is similar to MSVC /Ox and likely the fastest in
>>>> practice:
>>>>
>>>> int baz(_Bool shared, void* p)
>>>> {
>>>> return __builtin_expect( (_Bool)p, 1) ? shared : -1;
>>>> }
>>>> https://godbolt.org/z/GdTxoqf4a
>>>>
>>>> It is much harder to convince gcc to do the same or similar.
>>>> I see such stubbornness as a serious disadvantage of gcc optimizer.
>>>>
>>> A technique for forcing particular code generation is to use volatile
>>> do-nothing inline assembly.
>>>
>>> int baz2(bool shared, void* p)
>>> {
>>> if (p) {
>>> asm volatile("");
>>> return shared;
>>> }
>>> return -1;
>>> }
>>>
>>> gcc:
>>>
>>> "baz2(bool, void*)":
>>> mov eax, -1
>>> test rsi, rsi
>>> je .L1
>>> movzx eax, dil
>>> .L1:
>>> ret
>>>
>>>
>>> clang:
>>>
>>> baz2(bool, void*):
>>> test rsi, rsi
>>> je .LBB0_1
>>> movzx eax, dil
>>> ret
>>> .LBB0_1:
>>> mov eax, -1
>>> ret
>>>
>>>
>>> The inline assembly is not standard C++, but neither is
>>> __builtin_expect. And it is highly portable assembly :-)
>>>
>>>
>>> If you prefer to have a single expression, you can have :
>>>
>>> inline int volatile_unspecified_int() {
>>> int x;
>>> asm volatile("" : "=r" (x));
>>
>> Can we use std::atomic_signal_fence for it? Iirc, its compiler only.
>>
>
> I don't think so, but I can't say for sure. The point of the volatile
> assembly here is to introduce some "observable behaviour" to the code,
> thus constraining the possible code flow combining the compiler can make
> using "as if" logic - but without adding any actual extra work.
>
> But try some fences if you like, and see how it affects the code
> generation.
>
I would need to check it out on goldbolt. std::atomic_signal_fence is
just for the compiler and not for the arch.
Technical Details
Message-ID:
<11dt5ra$fjb5$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11dou16$2vlr8$1@raubtier-asyl.eternal-september.org> <20261120114427.00002e24@yahoo.com> <11dp7lk$330qr$1@dont-email.me> <20261120132229.00004fe9@yahoo.com> <11dpcha$34p2n$1@dont-email.me> <20261120144337.00002bc5@yahoo.com> <11dpnjf$393gv$1@dont-email.me> <11dqihi$3k3aq$1@dont-email.me> <11ds04h$12fg$3@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> Newsgroups: comp.lang.c++ Subject: Re: What does this code Date: Sat, 21 Nov 2026 14:26:47 -0800 Organization: A noiseless patient Spider Lines: 94 Message-ID: <11dt5ra$fjb5$1@dont-email.me> References: <11dou16$2vlr8$1@raubtier-asyl.eternal-september.org> <20261120114427.00002e24@yahoo.com> <11dp7lk$330qr$1@dont-email.me> <20261120132229.00004fe9@yahoo.com> <11dpcha$34p2n$1@dont-email.me> <20261120144337.00002bc5@yahoo.com> <11dpnjf$393gv$1@dont-email.me> <11dqihi$3k3aq$1@dont-email.me> <11ds04h$12fg$3@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 21 Nov 2026 22:26:51 +0000 (UTC) Injection-Info: dont-email.me; logging-data="511333"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18lczMt1E6oN/qx8HIPcJWVqawS+N9G6us="; posting-host="693562abaf6a81ac59b0dad544e916f8" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:101DyO9Bu55bo+kegr+h3JyU0eE= sha256:JDRMYjl55b1Mc6+hmrTbE+sWK1ZrTa9L3TsY+STnHLU= sha1:C6GIlUtA30+p/jxkXp4bhRQQCk4= sha256:JkfkAMvsE7CM3Qrvf04Mql0m9j0FUuv0VqStgHsQ+CQ= Content-Language: en-US In-Reply-To: <11ds04h$12fg$3@dont-email.me>