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 7597b7219f6cf67a
parent 7596 45e5c263c096
child 7598 4ea0b0177a57
The return value of SDL_MostSignificantBitIndex32(0) is defined as -1.
include/SDL_bits.h
     1.1 --- a/include/SDL_bits.h	Wed Aug 07 16:29:28 2013 -0700
     1.2 +++ b/include/SDL_bits.h	Wed Aug 07 16:32:51 2013 -0700
     1.3 @@ -45,15 +45,18 @@
     1.4   *  with 0. This operation can also be stated as "count leading zeroes" and
     1.5   *  "log base 2".
     1.6   *
     1.7 - *  \return Index of the most significant bit.
     1.8 + *  \return Index of the most significant bit, or -1 if the value is 0.
     1.9   */
    1.10 -SDL_FORCE_INLINE Sint8
    1.11 +SDL_FORCE_INLINE int
    1.12  SDL_MostSignificantBitIndex32(Uint32 x)
    1.13  {
    1.14  #if defined(__GNUC__) && __GNUC__ >= 4
    1.15      /* Count Leading Zeroes builtin in GCC.
    1.16       * http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gcc/Other-Builtins.html
    1.17 -     */
    1.18 +	 */
    1.19 +	if (x == 0) {
    1.20 +		return -1;
    1.21 +	}
    1.22      return 31 - __builtin_clz(x);
    1.23  #else
    1.24      /* Based off of Bit Twiddling Hacks by Sean Eron Anderson
    1.25 @@ -61,10 +64,14 @@
    1.26       * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
    1.27       */
    1.28      const Uint32 b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
    1.29 -    const Uint8  S[] = {1, 2, 4, 8, 16};
    1.30 +    const int    S[] = {1, 2, 4, 8, 16};
    1.31  
    1.32 -    Uint8 msbIndex = 0;
    1.33 -    int i;
    1.34 +    int msbIndex = 0;
    1.35 +	int i;
    1.36 +
    1.37 +	if (x == 0) {
    1.38 +		return -1;
    1.39 +	}
    1.40  
    1.41      for (i = 4; i >= 0; i--)
    1.42      {