shithub: opus

Download patch

ref: 1ee139bca076a1a3606a8a924a698d356d40cbb8
parent: 44ce46dc129f136c93da492299562c1f0fcd519d
author: Jean-Marc Valin <[email protected]>
date: Fri Sep 23 09:08:04 EDT 2011

Making the left shift macros use unsigned to avoid undefined behaviour

Result should be bit-identical on most machines/compilers, minus the undefined
behaviour

--- a/celt/fixed_generic.h
+++ b/celt/fixed_generic.h
@@ -64,11 +64,11 @@
 /** Arithmetic shift-right of a 16-bit value */
 #define SHR16(a,shift) ((a) >> (shift))
 /** Arithmetic shift-left of a 16-bit value */
-#define SHL16(a,shift) ((a) << (shift))
+#define SHL16(a,shift) ((opus_int16)((opus_uint16)(a)<<(shift)))
 /** Arithmetic shift-right of a 32-bit value */
 #define SHR32(a,shift) ((a) >> (shift))
 /** Arithmetic shift-left of a 32-bit value */
-#define SHL32(a,shift) ((opus_val32)(a) << (shift))
+#define SHL32(a,shift) ((opus_int32)((opus_uint32)(a)<<(shift)))
 
 /** 32-bit arithmetic shift right with rounding-to-nearest instead of rounding down */
 #define PSHR32(a,shift) (SHR32((a)+((EXTEND32(1)<<((shift))>>1)),shift))
@@ -77,7 +77,7 @@
 
 /** "RAW" macros, should not be used outside of this header file */
 #define SHR(a,shift) ((a) >> (shift))
-#define SHL(a,shift) ((opus_val32)(a) << (shift))
+#define SHL(a,shift) SHL32(a,shift)
 #define PSHR(a,shift) (SHR((a)+((EXTEND32(1)<<((shift))>>1)),shift))
 #define SATURATE(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x)))
 
--- a/silk/NSQ_del_dec.c
+++ b/silk/NSQ_del_dec.c
@@ -439,7 +439,7 @@
 
             /* Flip sign depending on dither */
             r_Q10 = r_Q10 ^ dither;
-            r_Q10 = silk_LIMIT_32( r_Q10, -31 << 10, 30 << 10 );
+            r_Q10 = silk_LIMIT_32( r_Q10, -(31 << 10), 30 << 10 );
 
             /* Find two quantization level candidates and measure their rate-distortion */
             q1_Q10 = silk_SUB32( r_Q10, offset_Q10 );
--- a/silk/SigProc_FIX.h
+++ b/silk/SigProc_FIX.h
@@ -494,10 +494,10 @@
 #define silk_ADD_POS_SAT32(a, b)            ((((a)+(b)) & 0x80000000)           ? silk_int32_MAX : ((a)+(b)))
 #define silk_ADD_POS_SAT64(a, b)            ((((a)+(b)) & 0x8000000000000000LL) ? silk_int64_MAX : ((a)+(b)))
 
-#define silk_LSHIFT8(a, shift)              ((a)<<(shift))                /* shift >= 0, shift < 8  */
-#define silk_LSHIFT16(a, shift)             ((a)<<(shift))                /* shift >= 0, shift < 16 */
-#define silk_LSHIFT32(a, shift)             ((a)<<(shift))                /* shift >= 0, shift < 32 */
-#define silk_LSHIFT64(a, shift)             ((a)<<(shift))                /* shift >= 0, shift < 64 */
+#define silk_LSHIFT8(a, shift)              ((opus_int8)((opus_uint8)(a)<<(shift)))                /* shift >= 0, shift < 8  */
+#define silk_LSHIFT16(a, shift)             ((opus_int16)((opus_uint16)(a)<<(shift)))                /* shift >= 0, shift < 16 */
+#define silk_LSHIFT32(a, shift)             ((opus_int32)((opus_uint32)(a)<<(shift)))                /* shift >= 0, shift < 32 */
+#define silk_LSHIFT64(a, shift)             ((opus_int64)((opus_uint64)(a)<<(shift)))                /* shift >= 0, shift < 64 */
 #define silk_LSHIFT(a, shift)               silk_LSHIFT32(a, shift)        /* shift >= 0, shift < 32 */
 
 #define silk_RSHIFT8(a, shift)              ((a)>>(shift))                /* shift >= 0, shift < 8  */
@@ -512,7 +512,6 @@
 #define silk_LSHIFT_SAT32(a, shift)         (silk_LSHIFT32( silk_LIMIT( (a), silk_RSHIFT32( silk_int32_MIN, (shift) ),    \
                                                                           silk_RSHIFT32( silk_int32_MAX, (shift) ) ), (shift) ))
 
-#define silk_LSHIFT_ovflw(a, shift)        ((a)<<(shift))        /* shift >= 0, allowed to overflow */
 #define silk_LSHIFT_uint(a, shift)         ((a)<<(shift))        /* shift >= 0 */
 #define silk_RSHIFT_uint(a, shift)         ((a)>>(shift))        /* shift >= 0 */