🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: What does this code

#962
From: Chris M. Thomasson
Date: Fri, 20 Nov 2026 22:45
96 lines
4298 bytes
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.


>      return x;
> }
> 
> int baz3(bool shared, void* p)
> {
>    return p ? (volatile_unspecified_int(), shared) : -1;
> }
> 
> 
> Of course it is difficult to give guarantees about exactly how code is 
> generated, and things may change between versions, so this is all 
> somewhat fragile and probably not a great idea in practice.  But it is 
> fun to play around with!
> 
Technical Details
Message-ID: <11dqihi$3k3aq$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.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>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.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: Fri, 20 Nov 2026 14:45:03 -0800
Organization: A noiseless patient Spider
Lines: 96
Message-ID: <11dqihi$3k3aq$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 20 Nov 2026 22:45:07 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3804506"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/SrMyMekHjtgmXdf1wPCmLvNJEuz3YFMk="; posting-host="fa5c50218976ec21e33b4cef826fe31b"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:1h03tSnfPl9kXdrZN37LcQ5kZJE= sha256:nEenxa7eQ+4DoARwjRzKg6FE8UbT976druviSnmFTAU=
	sha1:UehquguqDuSf0I5jXriS8dNQSsA= sha256:AmDL4VL8Rml40klCJ1RH29LRbk7UIiq38Bv4kC+Pc1M=
Content-Language: en-US
In-Reply-To: <11dpnjf$393gv$1@dont-email.me>