Browse code

mathops: remove undefined behaviour from sign_extend()

This function intentionally overflows the signed range on
the left shift. Using this type-punning avoids errors from
the overflow checker without disabling this test globally.

Signed-off-by: Mans Rullgard <mans@mansr.com>

Mans Rullgard authored on 2011/10/09 20:57:08
Showing 1 changed files
... ...
@@ -116,7 +116,9 @@ static inline av_const int mid_pred(int a, int b, int c)
116 116
 #ifndef sign_extend
117 117
 static inline av_const int sign_extend(int val, unsigned bits)
118 118
 {
119
-    return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
119
+    unsigned shift = 8 * sizeof(int) - bits;
120
+    union { unsigned u; int s; } v = { (unsigned) val << shift };
121
+    return v.s >> shift;
120 122
 }
121 123
 #endif
122 124