Article View: comp.lang.c++
Article #969Re: What does this code
From: David Brown
Date: Sat, 21 Nov 2026 11:38
Date: Sat, 21 Nov 2026 11:38
45 lines
2771 bytes
2771 bytes
On 21/11/2026 06:39, Bonita Montero wrote:
> Am 20.11.2026 um 12:39 schrieb David Brown:
>
>> Maybe you are doing something wrong. A quick check on godbolt shows
>> gcc 8 onwards generates branch-free code for each of these variants
>> of source code - with better code for the clearer source.
> https://godbolt.org/z/TfEnTr3T7
It's helpful to copy the actual code, not just the godbolt link - then
group readers can see it immediately:
struct strct
{
explicit operator int() const noexcept;
void *p;
bool b;
};
strct::operator int() const noexcept
{
if( !p )
return -1;
return (int)b;
}
"strct::operator int() const":
cmp QWORD PTR [rdi], 0
je .L3
movzx eax, BYTE PTR [rdi+8]
ret
.L3:
mov eax, -1
ret
That is very different code, and the compiler has different heuristics
for generating object code. Here the compiler is generating a
conditional jump to avoid a load from memory. I would expect that to be
a win, at least if the branch gets reasonably good prediction hit
ratios. I am not an expert on the timing of modern x86 processors, but
I believe that even if "b" is in the L1 cache, loading it (which would
be necessary for branchless code) would take a lot more cycles than a
predicted jump.
Technical Details
Message-ID:
<11drvr5$12fg$2@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> <11dp9fv$33khk$1@raubtier-asyl.eternal-september.org> <11dpbgr$34a3s$1@dont-email.me> <11draqa$3qusi$1@raubtier-asyl.eternal-september.org>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:38:13 +0100 Organization: A noiseless patient Spider Lines: 45 Message-ID: <11drvr5$12fg$2@dont-email.me> References: <11dou16$2vlr8$1@raubtier-asyl.eternal-september.org> <20261120114427.00002e24@yahoo.com> <11dp7lk$330qr$1@dont-email.me> <11dp9fv$33khk$1@raubtier-asyl.eternal-september.org> <11dpbgr$34a3s$1@dont-email.me> <11draqa$3qusi$1@raubtier-asyl.eternal-september.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 21 Nov 2026 11:38:14 +0000 (UTC) Injection-Info: dont-email.me; logging-data="35312"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18txHEgwNBsYlLQWON1DK5vOSKCbNdajcU="; posting-host="e9cc8568760c122c84a8bb7d362e291d" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:yqcXJ4EChDBJbmTHyRyyrG/7tn0= sha256:nhYQOiZBWqth/yqRfbEpIXThrqbJnQMgjPEvlxsUMqY= sha1:njewRD+njuI6/VRLWkt1FSud96Q= sha256:hnVg3xTf5KTNL96BjwlwA3Cmxb1V67tHnsLT2rLvxO8= Content-Language: en-GB In-Reply-To: <11draqa$3qusi$1@raubtier-asyl.eternal-september.org>