include/SDL_bits.h
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Tue, 12 Feb 2013 11:47:31 -0800
changeset 6866 e74a4b282450
child 6873 f042ae287155
permissions -rw-r--r--
Don't clobber refcounting in SDL_Init.

- Fixes bug 1712 by not overwriting SDL_SubsystemRefCount in SDL_Init.
- Removes the SDL_initialized variable, and makes SDL_SubsystemRefCount
the canonical source of truth for whether or not a subsystem has been
initialized.
- Refactors SDL_InitSubSystem and SDL_QuitSubSystem to use helper
functions to manage refcount.
- Adds automated tests for SDL_Init/Quit*.
- Adds SDL_bits.h which contains SDL_MostSignificantBitIndex.
jorgen@6866
     1
/*
jorgen@6866
     2
  Simple DirectMedia Layer
jorgen@6866
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
jorgen@6866
     4
jorgen@6866
     5
  This software is provided 'as-is', without any express or implied
jorgen@6866
     6
  warranty.  In no event will the authors be held liable for any damages
jorgen@6866
     7
  arising from the use of this software.
jorgen@6866
     8
jorgen@6866
     9
  Permission is granted to anyone to use this software for any purpose,
jorgen@6866
    10
  including commercial applications, and to alter it and redistribute it
jorgen@6866
    11
  freely, subject to the following restrictions:
jorgen@6866
    12
jorgen@6866
    13
  1. The origin of this software must not be misrepresented; you must not
jorgen@6866
    14
     claim that you wrote the original software. If you use this software
jorgen@6866
    15
     in a product, an acknowledgment in the product documentation would be
jorgen@6866
    16
     appreciated but is not required.
jorgen@6866
    17
  2. Altered source versions must be plainly marked as such, and must not be
jorgen@6866
    18
     misrepresented as being the original software.
jorgen@6866
    19
  3. This notice may not be removed or altered from any source distribution.
jorgen@6866
    20
*/
jorgen@6866
    21
jorgen@6866
    22
/**
jorgen@6866
    23
 *  \file SDL_bits.h
jorgen@6866
    24
 *
jorgen@6866
    25
 *  Functions for fiddling with bits and bitmasks.
jorgen@6866
    26
 */
jorgen@6866
    27
jorgen@6866
    28
#ifndef _SDL_bits_h
jorgen@6866
    29
#define _SDL_bits_h
jorgen@6866
    30
jorgen@6866
    31
#include "SDL_stdinc.h"
jorgen@6866
    32
jorgen@6866
    33
#include "begin_code.h"
jorgen@6866
    34
/* Set up for C function definitions, even when using C++ */
jorgen@6866
    35
#ifdef __cplusplus
jorgen@6866
    36
/* *INDENT-OFF* */
jorgen@6866
    37
extern "C" {
jorgen@6866
    38
/* *INDENT-ON* */
jorgen@6866
    39
#endif
jorgen@6866
    40
jorgen@6866
    41
/**
jorgen@6866
    42
 *  \file SDL_bits.h
jorgen@6866
    43
 *
jorgen@6866
    44
 *  Uses inline functions for compilers that support them, and static
jorgen@6866
    45
 *  functions for those that do not.  Because these functions become
jorgen@6866
    46
 *  static for compilers that do not support inline functions, this
jorgen@6866
    47
 *  header should only be included in files that actually use them.
jorgen@6866
    48
 */
jorgen@6866
    49
jorgen@6866
    50
/**
jorgen@6866
    51
 *  Get the index of the most significant bit. Result is undefined when called
jorgen@6866
    52
 *  with 0. This operation can also be stated as "count leading zeroes" and
jorgen@6866
    53
 *  "log base 2".
jorgen@6866
    54
 *
jorgen@6866
    55
 *  \return Index of the most significant bit.
jorgen@6866
    56
 */
jorgen@6866
    57
static __inline__ Sint8
jorgen@6866
    58
SDL_MostSignificantBitIndex32(Uint32 x)
jorgen@6866
    59
{
jorgen@6866
    60
#if defined(__GNUC__)
jorgen@6866
    61
    /* Count Leading Zeroes builtin in GCC.
jorgen@6866
    62
     * http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gcc/Other-Builtins.html
jorgen@6866
    63
     */
jorgen@6866
    64
    return 31 - __builtin_clz(x);
jorgen@6866
    65
#else
jorgen@6866
    66
    /* Based off of Bit Twiddling Hacks by Sean Eron Anderson
jorgen@6866
    67
     * <seander@cs.stanford.edu>, released in the public domain.
jorgen@6866
    68
     * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup
jorgen@6866
    69
     */
jorgen@6866
    70
    static const Sint8 LogTable256[256] =
jorgen@6866
    71
    {
jorgen@6866
    72
    #define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n
jorgen@6866
    73
        -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
jorgen@6866
    74
        LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),
jorgen@6866
    75
        LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)
jorgen@6866
    76
    #undef LT
jorgen@6866
    77
    };
jorgen@6866
    78
jorgen@6866
    79
    register unsigned int t, tt;
jorgen@6866
    80
jorgen@6866
    81
    if (tt = x >> 16)
jorgen@6866
    82
    {
jorgen@6866
    83
      return ((t = tt >> 8) ? 24 + LogTable256[t] : 16 + LogTable256[tt]);
jorgen@6866
    84
    }
jorgen@6866
    85
    else
jorgen@6866
    86
    {
jorgen@6866
    87
      return ((t = x >> 8) ? 8 + LogTable256[t] : LogTable256[x]);
jorgen@6866
    88
    }
jorgen@6866
    89
#endif
jorgen@6866
    90
}
jorgen@6866
    91
jorgen@6866
    92
/* Ends C function definitions when using C++ */
jorgen@6866
    93
#ifdef __cplusplus
jorgen@6866
    94
/* *INDENT-OFF* */
jorgen@6866
    95
}
jorgen@6866
    96
/* *INDENT-ON* */
jorgen@6866
    97
#endif
jorgen@6866
    98
#include "close_code.h"
jorgen@6866
    99
jorgen@6866
   100
#endif /* _SDL_bits_h */
jorgen@6866
   101
jorgen@6866
   102
/* vi: set ts=4 sw=4 expandtab: */