🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c++
Article #970

Re: What does this code

#970
From: David Brown
Date: Sat, 21 Nov 2026 11:43
89 lines
4404 bytes
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.
Technical Details
Message-ID: <11ds04h$12fg$3@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-mail
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>

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: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c++
Subject: Re: What does this code
Date: Sat, 21 Nov 2026 12:43:13 +0100
Organization: A noiseless patient Spider
Lines: 89
Message-ID: <11ds04h$12fg$3@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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 21 Nov 2026 11:43:14 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="35312"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+YLiKUK1EZ/dRWrq+IapajSqzxotzFPoI="; posting-host="e9cc8568760c122c84a8bb7d362e291d"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:vP+a+np0M3QbsPxitU35eqdOC2w= sha256:FCekavFIo4M0+boFX121F1O2p9sxzKD/6VO3q2i9eZ4=
	sha1:xLZOjScKPdMajNp378pao0HQO9k= sha256:pjPNm/23ZTqsv70NtxAAmTMWSiyA3GC6YgrpdQRu3uM=
In-Reply-To: <11dqihi$3k3aq$1@dont-email.me>
Content-Language: en-GB