Truthiness in C
dxuuu.xyz
Truthiness in C
1–10 of 26 posts
Re: Truthiness in C
#2I'm quite confident the compiler will optimize that test-and-move away as soon as you use the bool for something, giving you the same assembly as a last century's `typedef bool int`.
Re: Truthiness in C
#3https://www.felixcloutier.com/x86/aam
Who said that curiousity was bad?
Re: Truthiness in C
#4Yeah, C now has bool natively... I'm quite confident the compiler will optimize that test-and-move away as soon as you use the bool for something, giving you the same assembly as a last century's `typedef bool int`.
Re: Truthiness in C
#5> Note that the top 56 bits in rax are not zeroed – they contain junk. This is fine b/c the compiler will only make callers check the lowest bit of a register for boolean operations. This is why changing the compiler’s “understanding” (ie the cast) is necessary.
... and yet, the function ABI is clearly i386 (fetching arguments from stack) and indeed everything is compiled with -m32 [2] (i.e. 32-bit SysV ABI). This is a strange contradiction in 2023. On the Intel/AMD side, x86_64 has been prevalent (and the default) for... more than 15 years?
It does not invalidate the article's point. But it is slightly confusing....
Re: Truthiness in C
#6Yeah, C now has bool natively... I'm quite confident the compiler will optimize that test-and-move away as soon as you use the bool for something, giving you the same assembly as a last century's `typedef bool int`.
I think it has to keep it for externally visible functions, right?
Re: Truthiness in C
#7 return (bool)x;
is equivalent to return 0!=x;
and return !!x;
https://godbolt.org/z/Gq5j8j66aSorry, this is the C link:
Re: Truthiness in C
#8The article is very recent (September 2023 [1]), mentions that "rax is used to return integer values" in the SysV ABI (hence implicitly the 64-bit SysV ABI). Also confirmed in this excerpt: > Note that the top 56 bits in rax are not zeroed – they contain junk. This is fine b/c the compiler will only make callers check the lowest bit of a register for boolean operations. This is why changing the compiler’s “understand…
On such architecture "casting to bool" would be free, but other, narrowing type conversions would not.
Re: Truthiness in C
#9The article is very recent (September 2023 [1]), mentions that "rax is used to return integer values" in the SysV ABI (hence implicitly the 64-bit SysV ABI). Also confirmed in this excerpt: > Note that the top 56 bits in rax are not zeroed – they contain junk. This is fine b/c the compiler will only make callers check the lowest bit of a register for boolean operations. This is why changing the compiler’s “understand…
The article has too many x86 assumptions. The casts are most definitely _not_ free, because there are multiple operations like sign-extending, zero-extending, etc. which depend on the ABI. The fact that these are all almost free in x86 is irrelevant since some architectures will have to go out of their way to implement the operation (albeit the ABI is usually designed to make these operations simple). Similarly, I co…
And thanks for pointing out the x86/ABI assumptions. Had not considered that. It's certainly interesting to think about.
Re: Truthiness in C
#10Navigating down the hyperlinks rabbit hole, I discovered native BCD asm instructions im x86 https://www.felixcloutier.com/x86/aam Who said that curiousity was bad?