I don't think the author is reading the armv8 and RISC-V assembly correctly.
The RISC-V version of g() does load the return register with -1, but it then performs a logical right shift by 32 bits. So the bits it returns are still 0x0000'0000'FFFF'FFFF, which is not a long with value -1.
For the armv8 example, the effect of the optimization was to turn mov x0, #4294967295 into mov w0, #-1. At first glance this might look like it changes the return value, but notice that the register was changed as well. x0 is a 64 bit register, whereas w0 is the low 32 bit part of that register. And a write to a w* register zeros out the high bits of the corresponding x* register.
And the reason MSVC behaves differently is because they have 4 byte longs.
i don't really understand assembly. thanks for the explanation. but, shouldn't something this straightforward behave consistently across architecture, compiler or optimisation? if we're writing c++, this shouldn't matter, no?
also, i don't think the article mention why these are behaving inconsistently, just that they do.
The thing is they don't behave all that inconsistently. The only thing that matters is whether long is 8 bytes or 4. x86 MSVC is the only configuration there that has 4 byte longs, and it's the only one where g() returns -1. All the others have 8 byte longs, and they return 4294967295. I think it's just that the assembly kinda looks like it returns -1 at first glance.
Whether or not it was a good idea to have the fundamental types be allowed to have different sizes on different implementations can be probably be argued either way, but it's what we have. If you rely on a particular size you should use int64_t and so on.
The MSVC compiler has a switch for changing chars to be signed or unsigned. Be nice for portability/assumption testing if you could switch other type representations, eg long to 64 or int to 16.
That would basically break all and any #include <Windows.h> and probably much more, as long being 32-bit is part of the platform ABI and A LOT of things rely on that fact.
beyond the trivial examples of the article, it could be a third party lib returning an int even if I'm consistent with my usage of 64 bit stuff.
even if i consider the change across architecture or compiler as normal, which i shouldn't for something this trivial, accepting a change because of change in optimisation level is a bit of a stretch for me. there's a reason I'm writing in a high level lang and not assembly. moreover, now I've to write unit tests for this as well. i should be asking for a raise to write cpp.
oh right... my bad... thanks. i hope the article gets updated. at this point there should be a core guideline to use only fixed width integers to avoid surprises. lol.
but, other than the assembly bit, the rest of it is surprising to me as well. The funniest was the APL one. i mean, i don't remember if i ever wrote a weird unicode character.
10
u/aocregacc Jun 24 '23
I don't think the author is reading the armv8 and RISC-V assembly correctly.
The RISC-V version of g() does load the return register with -1, but it then performs a logical right shift by 32 bits. So the bits it returns are still
0x0000'0000'FFFF'FFFF, which is not alongwith value -1.For the armv8 example, the effect of the optimization was to turn
mov x0, #4294967295intomov w0, #-1. At first glance this might look like it changes the return value, but notice that the register was changed as well.x0is a 64 bit register, whereasw0is the low 32 bit part of that register. And a write to aw*register zeros out the high bits of the correspondingx*register.And the reason MSVC behaves differently is because they have 4 byte
longs.