Using sizeof(unsigned int) is not equal to using a constant in some cases
A
alexcones@gmail.com
Sat Aug 18 15:56:00 GMT 2018
Hello,
during the use of gcc I noticed the following thing: sometimes when I use
sizeof(<type>) the compiler might generate other assembler code than
when using just a number instead.
The example of this: ( online version: https://gcc.godbolt.org/z/-KCHqM )
return val >> n | val << (32 - n);
unrolls to
mov eax, DWORD PTR [rbp-8]
mov edx, DWORD PTR [rbp-4]
mov ecx, eax
ror edx, cl ← fast ror here
mov eax, edx
but
return val >> n | val << (sizeof(val) * 8 - n);
unrolls to
mov eax, DWORD PTR [rbp-8] ─┐
mov edx, DWORD PTR [rbp-4] │
mov esi, edx │
mov ecx, eax │
shr esi, cl │
mov eax, 32 ├─ no ror
sub eax, DWORD PTR [rbp-8] │
mov edx, DWORD PTR [rbp-4] │
mov ecx, eax │
sal edx, cl │
mov eax, edx │
or eax, esi ─┘
Is it a bug or a feature? I see this problem at least
from 5.3, it also is present at the current version.
The problem is noticeable at -O0 level, unfortunately we have to use -O0
for smoother debug.
With best regards,
Alex.
More information about the Gcc-help
mailing list