AW: optimizer discards sign information

Xi Ruoyao xry111@xry111.site
Wed Apr 10 09:54:25 GMT 2024


On Wed, 2024-04-10 at 11:49 +0200, stefan@franke.ms wrote:

> 
> But I keep considering this as a bug. And clang behaves correctly!

No it is not.  Both compilers are correct as anything can happen for an
undefined behavior.

> 
> https://godbolt.org/z/az8WqboET
> 
> typedef unsigned long long int u64;
> typedef unsigned int u32;
> typedef unsigned short u16;
> 
> u64 foo(u16 *a, u16 *b) {
>     u32 x = *a * *b;
>     u64 r = x;
>     return r >> 31;
> }
> 
> gcc yields
> 
> foo:
>         xor     eax, eax
>         ret

In this case GCC can assume (*a * *b) must be in [0, 2147483647] because
otherwise there is an undefined behavior.  Thus r is in [0, 2147483647],
so r >> 31 must be 0.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University


More information about the Gcc-help mailing list