include/SDL_bits.h
changeset 6873 f042ae287155
parent 6866 e74a4b282450
child 7004 18d4df6942c2
equal deleted inserted replaced
6872:2f4c95464651 6873:f042ae287155
    63      */
    63      */
    64     return 31 - __builtin_clz(x);
    64     return 31 - __builtin_clz(x);
    65 #else
    65 #else
    66     /* Based off of Bit Twiddling Hacks by Sean Eron Anderson
    66     /* Based off of Bit Twiddling Hacks by Sean Eron Anderson
    67      * <seander@cs.stanford.edu>, released in the public domain.
    67      * <seander@cs.stanford.edu>, released in the public domain.
    68      * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
    68      * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
    69      */
    69      */
    70     static const Sint8 LogTable256[256] =
    70     const Uint32 b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
       
    71     const Uint8  S[] = {1, 2, 4, 8, 16};
       
    72 
       
    73     Uint8 msbIndex = 0;
       
    74     int i;
       
    75 
       
    76     for (i = 4; i >= 0; i--)
    71     {
    77     {
    72     #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
    78         if (x & b[i])
    73         -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
    79         {
    74         LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
    80             x >>= S[i];
    75         LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)
    81             msbIndex |= S[i];
    76     #undef LT
    82         }
    77     };
    83     }
    78 
    84 
    79     register unsigned int t, tt;
    85     return msbIndex;
    80 
       
    81     if (tt = x >> 16)
       
    82     {
       
    83       return ((t = tt >> 8) ? 24 + LogTable256[t] : 16 + LogTable256[tt]);
       
    84     }
       
    85     else
       
    86     {
       
    87       return ((t = x >> 8) ? 8 + LogTable256[t] : LogTable256[x]);
       
    88     }
       
    89 #endif
    86 #endif
    90 }
    87 }
    91 
    88 
    92 /* Ends C function definitions when using C++ */
    89 /* Ends C function definitions when using C++ */
    93 #ifdef __cplusplus
    90 #ifdef __cplusplus