include/SDL_bits.h
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Jan 2018 10:03:25 -0800
changeset 11811 5d94cb6b24d3
parent 11433 b5e2b88d5193
child 12422 9091b20040cf
permissions -rw-r--r--
Updated copyright for 2018
jorgen@6866
     1
/*
jorgen@6866
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 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
slouken@10638
    28
#ifndef SDL_bits_h_
slouken@10638
    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
extern "C" {
jorgen@6866
    37
#endif
jorgen@6866
    38
jorgen@6866
    39
/**
jorgen@6866
    40
 *  \file SDL_bits.h
jorgen@6866
    41
 */
jorgen@6866
    42
jorgen@6866
    43
/**
jorgen@6866
    44
 *  Get the index of the most significant bit. Result is undefined when called
jorgen@6866
    45
 *  with 0. This operation can also be stated as "count leading zeroes" and
jorgen@6866
    46
 *  "log base 2".
jorgen@6866
    47
 *
slouken@7597
    48
 *  \return Index of the most significant bit, or -1 if the value is 0.
jorgen@6866
    49
 */
sezeroz@11314
    50
#if defined(__WATCOMC__) && defined(__386__)
sezeroz@11314
    51
extern _inline int _SDL_clz_watcom (Uint32);
sezeroz@11314
    52
#pragma aux _SDL_clz_watcom = \
sezeroz@11314
    53
    "bsr eax, eax" \
sezeroz@11314
    54
    "xor eax, 31" \
sezeroz@11314
    55
    parm [eax] nomemory \
sezeroz@11314
    56
    value [eax] \
sezeroz@11314
    57
    modify exact [eax] nomemory;
sezeroz@11314
    58
#endif
sezeroz@11314
    59
slouken@7597
    60
SDL_FORCE_INLINE int
jorgen@6866
    61
SDL_MostSignificantBitIndex32(Uint32 x)
jorgen@6866
    62
{
slouken@11433
    63
#if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
jorgen@6866
    64
    /* Count Leading Zeroes builtin in GCC.
jorgen@6866
    65
     * http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gcc/Other-Builtins.html
slouken@7605
    66
     */
slouken@7605
    67
    if (x == 0) {
slouken@7605
    68
        return -1;
slouken@7605
    69
    }
jorgen@6866
    70
    return 31 - __builtin_clz(x);
sezeroz@11314
    71
#elif defined(__WATCOMC__) && defined(__386__)
sezeroz@11314
    72
    if (x == 0) {
sezeroz@11314
    73
        return -1;
sezeroz@11314
    74
    }
sezeroz@11314
    75
    return 31 - _SDL_clz_watcom(x);
jorgen@6866
    76
#else
jorgen@6866
    77
    /* Based off of Bit Twiddling Hacks by Sean Eron Anderson
jorgen@6866
    78
     * <seander@cs.stanford.edu>, released in the public domain.
jorgen@6873
    79
     * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
jorgen@6866
    80
     */
jorgen@6873
    81
    const Uint32 b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
slouken@7597
    82
    const int    S[] = {1, 2, 4, 8, 16};
jorgen@6873
    83
slouken@7597
    84
    int msbIndex = 0;
slouken@7605
    85
    int i;
slouken@7597
    86
slouken@7605
    87
    if (x == 0) {
slouken@7605
    88
        return -1;
slouken@7605
    89
    }
jorgen@6873
    90
jorgen@6873
    91
    for (i = 4; i >= 0; i--)
jorgen@6866
    92
    {
jorgen@6873
    93
        if (x & b[i])
jorgen@6873
    94
        {
jorgen@6873
    95
            x >>= S[i];
jorgen@6873
    96
            msbIndex |= S[i];
jorgen@6873
    97
        }
jorgen@6873
    98
    }
jorgen@6866
    99
jorgen@6873
   100
    return msbIndex;
jorgen@6866
   101
#endif
jorgen@6866
   102
}
jorgen@6866
   103
jorgen@6866
   104
/* Ends C function definitions when using C++ */
jorgen@6866
   105
#ifdef __cplusplus
jorgen@6866
   106
}
jorgen@6866
   107
#endif
jorgen@6866
   108
#include "close_code.h"
jorgen@6866
   109
slouken@10638
   110
#endif /* SDL_bits_h_ */
jorgen@6866
   111
jorgen@6866
   112
/* vi: set ts=4 sw=4 expandtab: */