Truthiness in C
dxuuu.xyz
dxuuu.xyz
> 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....
On such architecture "casting to bool" would be free, but other, narrowing type conversions would not.
And thanks for pointing out the x86/ABI assumptions. Had not considered that. It's certainly interesting to think about.
`mov` can be "free" in two ways:
- the compiler can frequently avoid emitting the `mov` when part of a larger operation
- modern CPUs handle `mov` instructions earlier in the pipeline
Neither of those are true for `movsx`.
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`.
The upcoming C23 standard will define "bool" as a keyword.
return (bool)x;
is equivalent to return 0!=x;
and return !!x;
https://godbolt.org/z/Gq5j8j66aSorry, this is the C link:
https://www.felixcloutier.com/x86/aam
Who said that curiousity was bad?
But when the cat followed, a car came :(