include/SDL_bits.h
changeset 6866 e74a4b282450
child 6873 f042ae287155
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/include/SDL_bits.h	Tue Feb 12 11:47:31 2013 -0800
     1.3 @@ -0,0 +1,102 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +/**
    1.26 + *  \file SDL_bits.h
    1.27 + *
    1.28 + *  Functions for fiddling with bits and bitmasks.
    1.29 + */
    1.30 +
    1.31 +#ifndef _SDL_bits_h
    1.32 +#define _SDL_bits_h
    1.33 +
    1.34 +#include "SDL_stdinc.h"
    1.35 +
    1.36 +#include "begin_code.h"
    1.37 +/* Set up for C function definitions, even when using C++ */
    1.38 +#ifdef __cplusplus
    1.39 +/* *INDENT-OFF* */
    1.40 +extern "C" {
    1.41 +/* *INDENT-ON* */
    1.42 +#endif
    1.43 +
    1.44 +/**
    1.45 + *  \file SDL_bits.h
    1.46 + *
    1.47 + *  Uses inline functions for compilers that support them, and static
    1.48 + *  functions for those that do not.  Because these functions become
    1.49 + *  static for compilers that do not support inline functions, this
    1.50 + *  header should only be included in files that actually use them.
    1.51 + */
    1.52 +
    1.53 +/**
    1.54 + *  Get the index of the most significant bit. Result is undefined when called
    1.55 + *  with 0. This operation can also be stated as "count leading zeroes" and
    1.56 + *  "log base 2".
    1.57 + *
    1.58 + *  \return Index of the most significant bit.
    1.59 + */
    1.60 +static __inline__ Sint8
    1.61 +SDL_MostSignificantBitIndex32(Uint32 x)
    1.62 +{
    1.63 +#if defined(__GNUC__)
    1.64 +    /* Count Leading Zeroes builtin in GCC.
    1.65 +     * http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gcc/Other-Builtins.html
    1.66 +     */
    1.67 +    return 31 - __builtin_clz(x);
    1.68 +#else
    1.69 +    /* Based off of Bit Twiddling Hacks by Sean Eron Anderson
    1.70 +     * <seander@cs.stanford.edu>, released in the public domain.
    1.71 +     * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
    1.72 +     */
    1.73 +    static const Sint8 LogTable256[256] =
    1.74 +    {
    1.75 +    #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
    1.76 +        -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
    1.77 +        LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
    1.78 +        LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)
    1.79 +    #undef LT
    1.80 +    };
    1.81 +
    1.82 +    register unsigned int t, tt;
    1.83 +
    1.84 +    if (tt = x >> 16)
    1.85 +    {
    1.86 +      return ((t = tt >> 8) ? 24 + LogTable256[t] : 16 + LogTable256[tt]);
    1.87 +    }
    1.88 +    else
    1.89 +    {
    1.90 +      return ((t = x >> 8) ? 8 + LogTable256[t] : LogTable256[x]);
    1.91 +    }
    1.92 +#endif
    1.93 +}
    1.94 +
    1.95 +/* Ends C function definitions when using C++ */
    1.96 +#ifdef __cplusplus
    1.97 +/* *INDENT-OFF* */
    1.98 +}
    1.99 +/* *INDENT-ON* */
   1.100 +#endif
   1.101 +#include "close_code.h"
   1.102 +
   1.103 +#endif /* _SDL_bits_h */
   1.104 +
   1.105 +/* vi: set ts=4 sw=4 expandtab: */