Article View: comp.lang.c++
Article #959Re: What does this code
From: David Brown
Date: Fri, 20 Nov 2026 15:05
Date: Fri, 20 Nov 2026 15:05
90 lines
3817 bytes
3817 bytes
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));
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:
<11dpnjf$393gv$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.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>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.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: Fri, 20 Nov 2026 16:05:19 +0100 Organization: A noiseless patient Spider Lines: 90 Message-ID: <11dpnjf$393gv$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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 20 Nov 2026 15:05:20 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3444255"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19+PsaYoVedwRH/dEM4d069SwzwDXlACnA="; posting-host="ee084c1d78ff4d8f9e431b8c5465c129" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:utGzYVR8ge4dp3U+QQUTVkzX+20= sha256:hZFSV3ZurEB3RX/d3qpSp7FhAoGQD8SFagSuSQ/1e28= sha1:0ITJ4P6PPzNqWwbT9BZE86cJsKE= sha256:QLKFAaf03gb0wQDGMfNvotzFCI8XZ/cAdl0qnGef0LM= In-Reply-To: <20261120144337.00002bc5@yahoo.com> Content-Language: en-GB, nb-NO