🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: What does this code

#953
From: David Brown
Date: Fri, 20 Nov 2026 10:33
121 lines
4813 bytes
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.


I have no idea what the original code is intended to be, but using an 
"int" for a three-way classification like this sounds like a really 
obtuse way to write the code.  For comparison, using an enum like this 
generates identical code to "bar" with gcc :

enum class classifications {
	invalid_pointer = -1, unshared_null = 0, shared_null = 1
};
classifications foobar(bool shared, void* p) {
	if (!p) return classifications::invalid_pointer;
	if (shared) return classifications::shared_null;
	return classifications::unshared_null;
}

Without knowing the point of the code, or how it might be changed or 
expanded in the future, or what C++ code would clearly follow the 
specifications of the original function, it is impossible to say what 
style of C++ code would be clearest.  But what we can see from this is 
that a good compiler can handle clear source code written in various 
ways, and generates the best results when given clear source code rather 
than obscure hacks.

Technical Details
Message-ID: <11dp7lk$330qr$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-mail
References: <11dou16$2vlr8$1@raubtier-asyl.eternal-september.org> <20261120114427.00002e24@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 11:33:24 +0100
Organization: A noiseless patient Spider
Lines: 121
Message-ID: <11dp7lk$330qr$1@dont-email.me>
References: <11dou16$2vlr8$1@raubtier-asyl.eternal-september.org>
 <20261120114427.00002e24@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 10:33:25 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3244891"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18qKf/kxdl/hqyskG2UcRfqaOPklOz9TBM="; posting-host="ee084c1d78ff4d8f9e431b8c5465c129"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:arsEnstFLAlIKN+Kv5koJepdrDg= sha256:1GxrBLevbDOwQx+dXXuVoGgy8F1EE5qXmj6GADxCeEg=
	sha1:lkFRdNCV7vGqmZ2QHUK4qsrsVXM= sha256:DZ6jIyWmbytjt6I0JupHaMA4KaDyjWY4Va2rWs0rqbM=
In-Reply-To: <20261120114427.00002e24@yahoo.com>
Content-Language: en-GB, nb-NO