Re: AW: optimizer discards sign information

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2024-04-10 at 11:49 +0200, stefan@xxxxxxxxx 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@xxxxxxxxxxx>
School of Aerospace Science and Technology, Xidian University




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux