core: Fix GCC 6 compiler warning regarding left shift of negative value
authorPeter Meerwald <pmeerw@pmeerw.net>
Mon, 15 Feb 2016 22:05:03 +0000 (23:05 +0100)
committerPeter Meerwald-Stadler <pmeerw@pmeerw.net>
Thu, 18 Feb 2016 13:08:13 +0000 (14:08 +0100)
In file included from pulse/timeval.c:32:0:
pulse/timeval.c: In function 'pa_timeval_add':
./pulsecore/macro.h:303:28: warning: left shift of negative value [-Wshift-negative-value]
              ? ~(~(type) 0 << (8*sizeof(type)-1))

reported by Ubuntu gcc-6

gcc-6 adds -Wshift-negative-value (enabled by -Wextra) which warns
about left shifting a negative value. Such shifts are undefined
because they depend on the representation of negative values.

also works with -Wshift-overflow=2

Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net>
src/pulsecore/macro.h

index 1716f810bb0ae042d0d1d6f37160eb8a940805fa..1e15f4fa2c1eb6885617f1d88e4dbbd165798203 100644 (file)
@@ -298,14 +298,16 @@ static inline size_t PA_PAGE_ALIGN(size_t l) {
 
 #define PA_INT_TYPE_SIGNED(type) (!!((type) 0 > (type) -1))
 
+#define PA_INT_TYPE_HALF(type) ((type) 1 << (sizeof(type)*8 - 2))
+
 #define PA_INT_TYPE_MAX(type)                                          \
     ((type) (PA_INT_TYPE_SIGNED(type)                                  \
-             ? ~(~(type) 0 << (8*sizeof(type)-1))                      \
+             ? (PA_INT_TYPE_HALF(type) - 1 + PA_INT_TYPE_HALF(type))   \
              : (type) -1))
 
 #define PA_INT_TYPE_MIN(type)                                          \
     ((type) (PA_INT_TYPE_SIGNED(type)                                  \
-             ? (~(type) 0 << (8*sizeof(type)-1))                       \
+             ? (-1 - PA_INT_TYPE_MAX(type))                            \
              : (type) 0))
 
 /* We include this at the very last place */