Skip to contentSkip to conversation

Remove left shifts of negative signed integers

PATCH In review John Snow 3 participants 8 messages Jun 30, 2016 – Aug 16, 2016

lore
Share & tools
lore

John Snow

author
#0 lore raw

PATCH 1/1 Remove left shifts of negative signed integers

Another exercise in placating Clang's increasingly strict -Werror mode. Technically, this is undefined behavior. In practice, -N<<M is the same as -(N<<M).

---
 hw/audio/fmopl.c      | 2 +-
 target-i386/monitor.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
hw/audio/fmopl.c +1 -1
index 731110f..de9338b 100644
@@ -69,7 +69,7 @@ static int opl_dbg_maxchip,opl_dbg_chip;
 /* final output shift , limit minimum and maximum */
 #define OPL_OUTSB   (TL_BITS+3-16)		/* OPL output final shift 16bit */
 #define OPL_MAXOUT (0x7fff<<OPL_OUTSB)
-#define OPL_MINOUT (-0x8000<<OPL_OUTSB)
+#define OPL_MINOUT (-(0x8000<<OPL_OUTSB))
 
 /* -------------------- quality selection --------------------- */
 
target-i386/monitor.c +1 -1
index fccfe40..94e9871 100644
@@ -36,7 +36,7 @@ static void print_pte(Monitor *mon, hwaddr addr,
 {
 #ifdef TARGET_X86_64
     if (addr & (1ULL << 47)) {
-        addr |= -1LL << 48;
+        addr |= -(1LL << 48);
     }
 #endif
     monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
Signature · 1 line
2.5.5
#1 lore raw
Quoted 15 lines · John Snow

Another exercise in placating Clang's increasingly strict -Werror mode. Technically, this is undefined behavior. In practice, -N<<M is the same as -(N<<M).

---
 hw/audio/fmopl.c      | 2 +-
 target-i386/monitor.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
hw/audio/fmopl.c +0 -0
index 731110f..de9338b 100644

hw/audio/fmopl.c static int opl_dbg_maxchip,opl_dbg_chip; · line 72

Quoted 5 lines
hw/audio/fmopl.c +1 -1
@@ -69,7 +69,7 @@ static int opl_dbg_maxchip,opl_dbg_chip;
 /* final output shift , limit minimum and maximum */
 #define OPL_OUTSB   (TL_BITS+3-16)             /* OPL output final shift 16bit */
 #define OPL_MAXOUT (0x7fff<<OPL_OUTSB)
-#define OPL_MINOUT (-0x8000<<OPL_OUTSB)
+#define OPL_MINOUT (-(0x8000<<OPL_OUTSB))

We have been down this path before:
http://patchwork.ozlabs.org/patch/545238/

Paolo will doubtless be along with the rant shortly.

-- PMM

author
#2 lore raw
Quoted 25 lines · Peter Maydell
Quoted 15 lines · John Snow

Another exercise in placating Clang's increasingly strict -Werror mode. Technically, this is undefined behavior. In practice, -N<<M is the same as -(N<<M).

---
 hw/audio/fmopl.c      | 2 +-
 target-i386/monitor.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
hw/audio/fmopl.c +0 -0
index 731110f..de9338b 100644

hw/audio/fmopl.c static int opl_dbg_maxchip,opl_dbg_chip; · line 72

Quoted 5 lines
hw/audio/fmopl.c +1 -1
@@ -69,7 +69,7 @@ static int opl_dbg_maxchip,opl_dbg_chip;
 /* final output shift , limit minimum and maximum */
 #define OPL_OUTSB   (TL_BITS+3-16)             /* OPL output final shift 16bit */
 #define OPL_MAXOUT (0x7fff<<OPL_OUTSB)
-#define OPL_MINOUT (-0x8000<<OPL_OUTSB)
+#define OPL_MINOUT (-(0x8000<<OPL_OUTSB))

We have been down this path before:
http://patchwork.ozlabs.org/patch/545238/

Quoted 4 lines

Paolo will doubtless be along with the rant shortly.

-- PMM

I figured, so I CC'd him. Nobody can say I didn't try.

--js

#4 lore raw
In reply to John Snow

----- Original Message -----

Quoted 7 lines

From: "John Snow" <[email protected]>
To: [email protected]
Cc: [email protected], [email protected], [email protected], "peter maydell" <[email protected]>, "John Snow" <[email protected]>
Sent: Thursday, June 30, 2016 9:36:36 PM
Subject: [PATCH] Remove left shifts of negative signed integers

Quoted 5 lines

Another exercise in placating Clang's increasingly strict -Werror mode. Technically, this is undefined behavior. In practice, -N<<M is the same as -(N<<M).

There's been discussions on this in the past; sorry but this is a super-duper NACK.

GCC correctly puts this warning under -Wextra, and promises not to ever make use of this facet of undefined behavior. The only correct patch is the one that disables the warning for clang, and possibly adds -fwrapv. In GCC, -fwrapv correctly silences ubsan's left-shift and signed-overflow warnings. In Clang, this is reported at
https://llvm.org/bugs/show_bug.cgi?id=25552. It's a heavy hammer but it's the safest options as compiler evolve.

Paolo

Quoted 35 lines
---
 hw/audio/fmopl.c      | 2 +-
 target-i386/monitor.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
hw/audio/fmopl.c +1 -1
index 731110f..de9338b 100644
@@ -69,7 +69,7 @@ static int opl_dbg_maxchip,opl_dbg_chip;
 /* final output shift , limit minimum and maximum */
 #define OPL_OUTSB   (TL_BITS+3-16)		/* OPL output final shift 16bit */
 #define OPL_MAXOUT (0x7fff<<OPL_OUTSB)
-#define OPL_MINOUT (-0x8000<<OPL_OUTSB)
+#define OPL_MINOUT (-(0x8000<<OPL_OUTSB))
 
 /* -------------------- quality selection --------------------- */
 
target-i386/monitor.c +1 -2
index fccfe40..94e9871 100644
@@ -36,7 +36,7 @@ static void print_pte(Monitor *mon, hwaddr addr,
 {
 #ifdef TARGET_X86_64
     if (addr & (1ULL << 47)) {
-        addr |= -1LL << 48;
+        addr |= -(1LL << 48);
     }
 #endif
     monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
--

2.5.5

author
#5 lore raw
In reply to Paolo Bonzini
Quoted 27 lines · Paolo Bonzini

----- Original Message -----

Quoted 7 lines

From: "John Snow" <[email protected]>
To: [email protected]
Cc: [email protected], [email protected], [email protected], "peter maydell" <[email protected]>, "John Snow" <[email protected]>
Sent: Thursday, June 30, 2016 9:36:36 PM
Subject: [PATCH] Remove left shifts of negative signed integers

Quoted 5 lines

Another exercise in placating Clang's increasingly strict -Werror mode. Technically, this is undefined behavior. In practice, -N<<M is the same as -(N<<M).

There's been discussions on this in the past; sorry but this is a super-duper NACK.

GCC correctly puts this warning under -Wextra, and promises not to ever make use of this facet of undefined behavior. The only correct patch is the one that disables the warning for clang, and possibly adds -fwrapv. In GCC, -fwrapv correctly silences ubsan's left-shift and signed-overflow warnings. In Clang, this is reported at
https://llvm.org/bugs/show_bug.cgi?id=25552. It's a heavy hammer but it's the safest options as compiler evolve.

Quoted 2 lines

Paolo

Where /exactly/ do you propose we canonicalize the clang invocation you would like to support?

--js

#6 lore raw
Quoted 11 lines
Quoted 8 lines

GCC correctly puts this warning under -Wextra, and promises not to ever make use of this facet of undefined behavior. The only correct patch is the one that disables the warning for clang, and possibly adds -fwrapv. In GCC, -fwrapv correctly silences ubsan's left-shift and signed-overflow warnings. In Clang, this is reported at
https://llvm.org/bugs/show_bug.cgi?id=25552. It's a heavy hammer but it's the safest options as compiler evolve.

Where /exactly/ do you propose we canonicalize the clang invocation you would like to support?

In configure where we look for other warning flags. Add -fwrapv and -Wno-shift-negative-value so that they're enabled if the compilers supports them.

Paolo

#7 lore raw
In reply to Paolo Bonzini
Quoted 7 lines · Paolo Bonzini

GCC correctly puts this warning under -Wextra, and promises not to ever make use of this facet of undefined behavior. The only correct patch is the one that disables the warning for clang, and possibly adds -fwrapv. In GCC, -fwrapv correctly silences ubsan's left-shift and signed-overflow warnings. In Clang, this is reported at
https://llvm.org/bugs/show_bug.cgi?id=25552. It's a heavy hammer but it's the safest options as compiler evolve.

James Molloy kindly wrote a patch for clang which fixes PR25552 (so -fwrapv silences the compiler warning and the sanitizer warning), and that has now been committed to clang. So I'm now happy that both clang and gcc agree that -fwrapv is the way to give you a well-defined C dialect with the shift semantics we want. Post-2.7 we should add -fwrapv to our compiler flags (we'll want to keep -Wno-shift-negative-value for the benefit of clang versions without the 25552 bugfix).

thanks
-- PMM