🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: What does this code

#958
From: Michael S
Date: Fri, 20 Nov 2026 12:43
162 lines
7093 bytes
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:
> > On Fri, 20 Nov 2026 11:33:24 +0100
> > David Brown <david.brown@hesbynett.no> wrote:
> >   
> >> On 20/11/2026 10:44, Michael S wrote:  
> >>> On Fri, 20 Nov 2026 08:48:56 +0100
> >>> Bonita Montero <Bonita.Montero@gmail.com> wrote:
> >>>      
> >>>> Cool code without any branches and conditional
> >>>> moves if the compiler does it as I intended.
> >>>>
> >>>> inline xsm_lock::operator int() const noexcept
> >>>> {
> >>>> 	return (int)m_shared | -(int)!m_pMtx;
> >>>> }
> >>>>     
> >>
> >> That's the kind of hack "manual optimisation" code that most C and
> >> C++ programmers were able to leave behind at the turn of the
> >> century as compilers got better at optimising well-written and
> >> clear source code.
> >>  
> >>>> m_pMtx is a pointer.
> >>>> Chris, I'm sure *you* would use relacy with that.
> >>>>     
> >>>
> >>> The code that people could read and understand is compiled to
> >>> exactly the same asm on RISC-V:  
> >>
> >> (I'm copying in the results so that others don't have to follow the
> >> godbolt links unless they want to play with things themselves.)
> >>
> >> int bar(bool shared, void* p)
> >> {
> >>     return p ? shared : -1;
> >> }
> >>  
> >>> https://godbolt.org/z/vbvxTGMv5
> >>> And on ARM64:
> >>> https://godbolt.org/z/rv9GT7fxM
> >>> And on x86-64:
> >>> https://godbolt.org/z/fsGb4Keq3  
> >>
> >> foo(bool, void*):
> >>           xor     eax, eax
> >>           cmp     rsi, 1
> >>           sbb     eax, eax
> >>           or      eax, edi
> >>           ret
> >>  
> >>>
> >>> That's clang.
> >>>
> >>> With gcc readable code is compiled to better x86-64 asm than the
> >>> tricky one:
> >>> https://godbolt.org/z/7zhMdrEzo
> >>>
> >>> The same number of instructions in bot cases, but in readable
> >>> vesrsion dependency chain is shorter (2 vs 3).
> >>>      
> >>
> >> "foo(bool, void*)":
> >>           cmp     rsi, 1
> >>           movzx   edi, dil
> >>           sbb     eax, eax
> >>           or      eax, edi
> >>           ret
> >> "bar(bool, void*)":
> >>           movzx   edi, dil
> >>           test    rsi, rsi
> >>           mov     eax, -1
> >>           cmovne  eax, edi
> >>           ret
> >>
> >>
> >> MSVC, however, is not as smart - and that's the compiler Bonita
> >> uses AFAIK.
> >>
> >> shared$ = 8
> >> p$ = 16
> >> int foo(bool,void *) PROC                              ; foo,
> >> COMDAT movzx   eax, cl
> >>           test    rdx, rdx
> >>           mov     ecx, -1
> >>           cmove   eax, ecx
> >>           ret     0
> >> int foo(bool,void *) ENDP                              ; foo
> >>
> >> shared$ = 8
> >> p$ = 16
> >> int bar(bool,void *) PROC                              ; bar,
> >> COMDAT movzx   eax, cl
> >>           test    rdx, rdx
> >>           jne     SHORT $LN4@bar
> >>           mov     eax, -1
> >> $LN4@bar:
> >>           ret     0
> >> int bar(bool,void *) ENDP
> >>
> >>
> >> That is, MSVC needs that ugly source code to give as good results
> >> as gcc does with the much simpler code.
> >>  
> > 
> > Pay attention that -O in MSVC does not have the same meaning as -O
> > in in gcc or clang. gcc -O is approximately the same as MSVC -O1
> > https://godbolt.org/z/sojq94P4n
> > The same code for foo and bar.
> > Bonita, being Bonita, probably uses -Ox, which is similar to gcc
> > -O3.  
> 
> I am not very familiar with the flags for MSVC - indeed, I have
> barely ever used the tool outside of brief checks on godbolt.  So I
> don't know quite how they compare.
> 
> My usual choice for gcc is "-O2", but for small functions like this
> you typically get the same results for -O1, -O2 or -O3.
> Interestingly, for MSVC the /O2 optimisation (which is what I used)
> and /Ox (which I did not know about until now) gives the results
> above.  Using /O1, on the other hand, gives basically the same code
> for "bar" as for "foo" - branchless code object code from the clearer
> source code.  (That's just with godbolt "x64 msvc v19.latest" - I
> don't haven't bothered checking other versions.)
> 
> > 
> > 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.

> It's this kind of thing that makes the OP's code so pointless.  Code 
> written in a way that is clear to human readers is usually easier for 
> compilers to optimise in different ways in different circumstances.
> And if you really need the absolute highest speeds, you have to
> measure it and profile it with real use-cases, targeting the specific
> processors you want to use (not just generic x86-64), and perhaps use 
> profile-guided optimisation.
> 
Technical Details
Message-ID: <20261120144337.00002bc5@yahoo.com>
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
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>

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: Michael S <already5chosen@yahoo.com>
Newsgroups: comp.lang.c++
Subject: Re: What does this code
Date: Fri, 20 Nov 2026 14:43:37 +0200
Organization: A noiseless patient Spider
Lines: 162
Message-ID: <20261120144337.00002bc5@yahoo.com>
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>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 20 Nov 2026 12:43:36 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2785104"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/sLu29k6Hb4IZmaEgzjY4zhCwVKrjdqM0="; posting-host="c9277e243488dd36750115eaaca8b400"
Cancel-Lock: sha1:c7xtE0ocHCocEZDbnZymTrpllsc= sha256:eU3HJF6LIdF5mFg/+jYDcxcJLv/e1l4mMEuLZbOKHd0=
	sha1:NYA/eNiF8FZo1HxxvCBbvC3G79M= sha256:fG6rb+2CiI+oM0BwHLDlyYePQprFyLIPzmUC7xYssug=
X-Newsreader: Claws Mail 4.1.1 (GTK 3.24.34; x86_64-w64-mingw32)