The return value of SDL_MostSignificantBitIndex32(0) is defined as -1.
authorSam Lantinga <slouken@libsdl.org>
Wed, 07 Aug 2013 16:32:51 -0700
changeset 7597 b7219f6cf67a
parent 7596 45e5c263c096
child 7598 4ea0b0177a57
The return value of SDL_MostSignificantBitIndex32(0) is defined as -1.
include/SDL_bits.h
--- a/include/SDL_bits.h	Wed Aug 07 16:29:28 2013 -0700
+++ b/include/SDL_bits.h	Wed Aug 07 16:32:51 2013 -0700
@@ -45,15 +45,18 @@
  *  with 0. This operation can also be stated as "count leading zeroes" and
  *  "log base 2".
  *
- *  \return Index of the most significant bit.
+ *  \return Index of the most significant bit, or -1 if the value is 0.
  */
-SDL_FORCE_INLINE Sint8
+SDL_FORCE_INLINE int
 SDL_MostSignificantBitIndex32(Uint32 x)
 {
 #if defined(__GNUC__) && __GNUC__ >= 4
     /* Count Leading Zeroes builtin in GCC.
      * http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gcc/Other-Builtins.html
-     */
+	 */
+	if (x == 0) {
+		return -1;
+	}
     return 31 - __builtin_clz(x);
 #else
     /* Based off of Bit Twiddling Hacks by Sean Eron Anderson
@@ -61,10 +64,14 @@
      * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
      */
     const Uint32 b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
-    const Uint8  S[] = {1, 2, 4, 8, 16};
+    const int    S[] = {1, 2, 4, 8, 16};
 
-    Uint8 msbIndex = 0;
-    int i;
+    int msbIndex = 0;
+	int i;
+
+	if (x == 0) {
+		return -1;
+	}
 
     for (i = 4; i >= 0; i--)
     {