qemu-devel
[Top][All Lists]
Advanced

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

Re: [Qemu-devel] [PATCH] hw/audio/fmopl.c: Avoid clang warning about shi


From: Paolo Bonzini
Subject: Re: [Qemu-devel] [PATCH] hw/audio/fmopl.c: Avoid clang warning about shifting negative number
Date: Tue, 17 Nov 2015 11:42:02 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0


On 17/11/2015 11:17, Peter Maydell wrote:
> If clang is
> warning about it they obviously don't want to guarantee that they
> aren't ever going to rely on this UB for optimisation.

My interpretation is just that clang's diagnostics are not that much
better than GCC anymore, and they feel the need to pump their release
notes with oh so many new -W flags.

Just look at the 3.5 release notes:

   New warning -Wabsolute-value: Clang warns about incorrect or useless
   usage of the absolute functions (abs, fabsf, etc).

   #include <stdlib.h>
   void foo() {
     unsigned int i=0;
     abs(i);
   }

   returns warning: taking the absolute value of unsigned type
   ‘unsigned int’ has no effect [-Wabsolute-value]

Wow, haven't you wanted that since kindergarten? :-D

There's no reason not to put this under the existing -Wunused-value,
other than politics.

> I agree that the rephrasing isn't great; if you have a
> preferred non-UB way to write it I'm open to suggestions.

I don't think there's a good way to express it.  I guess -0x8000u would
look mildly better, but it is more dangerous so I think overall it's
even worse.

The point is that left shifting of a negative number _is not overflow_
unless that number is less than ("has fewer leading ones than") INT_MIN
>> n.  It makes no sense to forbid anything else, if at the same time
you allow signed right shift to be arithmetic shift.

I'm all okay with compilers treating shifts of negative numbers as
undefined behavior if a 0 is shifted into the sign bit.

Paolo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]