SDL Library

Dependents:   H261_decoder

Committer:
miruga27
Date:
Thu Sep 22 00:03:09 2016 +0000
Revision:
0:7fb6877b5d7c
SDL

Who changed what in which revision?

UserRevisionLine numberNew contents of line
miruga27 0:7fb6877b5d7c 1 /*
miruga27 0:7fb6877b5d7c 2 Simple DirectMedia Layer
miruga27 0:7fb6877b5d7c 3 Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
miruga27 0:7fb6877b5d7c 4
miruga27 0:7fb6877b5d7c 5 This software is provided 'as-is', without any express or implied
miruga27 0:7fb6877b5d7c 6 warranty. In no event will the authors be held liable for any damages
miruga27 0:7fb6877b5d7c 7 arising from the use of this software.
miruga27 0:7fb6877b5d7c 8
miruga27 0:7fb6877b5d7c 9 Permission is granted to anyone to use this software for any purpose,
miruga27 0:7fb6877b5d7c 10 including commercial applications, and to alter it and redistribute it
miruga27 0:7fb6877b5d7c 11 freely, subject to the following restrictions:
miruga27 0:7fb6877b5d7c 12
miruga27 0:7fb6877b5d7c 13 1. The origin of this software must not be misrepresented; you must not
miruga27 0:7fb6877b5d7c 14 claim that you wrote the original software. If you use this software
miruga27 0:7fb6877b5d7c 15 in a product, an acknowledgment in the product documentation would be
miruga27 0:7fb6877b5d7c 16 appreciated but is not required.
miruga27 0:7fb6877b5d7c 17 2. Altered source versions must be plainly marked as such, and must not be
miruga27 0:7fb6877b5d7c 18 misrepresented as being the original software.
miruga27 0:7fb6877b5d7c 19 3. This notice may not be removed or altered from any source distribution.
miruga27 0:7fb6877b5d7c 20 */
miruga27 0:7fb6877b5d7c 21
miruga27 0:7fb6877b5d7c 22 /**
miruga27 0:7fb6877b5d7c 23 * \file SDL_bits.h
miruga27 0:7fb6877b5d7c 24 *
miruga27 0:7fb6877b5d7c 25 * Functions for fiddling with bits and bitmasks.
miruga27 0:7fb6877b5d7c 26 */
miruga27 0:7fb6877b5d7c 27
miruga27 0:7fb6877b5d7c 28 #ifndef _SDL_bits_h
miruga27 0:7fb6877b5d7c 29 #define _SDL_bits_h
miruga27 0:7fb6877b5d7c 30
miruga27 0:7fb6877b5d7c 31 #include "SDL_stdinc.h"
miruga27 0:7fb6877b5d7c 32
miruga27 0:7fb6877b5d7c 33 #include "begin_code.h"
miruga27 0:7fb6877b5d7c 34 /* Set up for C function definitions, even when using C++ */
miruga27 0:7fb6877b5d7c 35 #ifdef __cplusplus
miruga27 0:7fb6877b5d7c 36 extern "C" {
miruga27 0:7fb6877b5d7c 37 #endif
miruga27 0:7fb6877b5d7c 38
miruga27 0:7fb6877b5d7c 39 /**
miruga27 0:7fb6877b5d7c 40 * \file SDL_bits.h
miruga27 0:7fb6877b5d7c 41 */
miruga27 0:7fb6877b5d7c 42
miruga27 0:7fb6877b5d7c 43 /**
miruga27 0:7fb6877b5d7c 44 * Get the index of the most significant bit. Result is undefined when called
miruga27 0:7fb6877b5d7c 45 * with 0. This operation can also be stated as "count leading zeroes" and
miruga27 0:7fb6877b5d7c 46 * "log base 2".
miruga27 0:7fb6877b5d7c 47 *
miruga27 0:7fb6877b5d7c 48 * \return Index of the most significant bit, or -1 if the value is 0.
miruga27 0:7fb6877b5d7c 49 */
miruga27 0:7fb6877b5d7c 50 SDL_FORCE_INLINE int
miruga27 0:7fb6877b5d7c 51 SDL_MostSignificantBitIndex32(Uint32 x)
miruga27 0:7fb6877b5d7c 52 {
miruga27 0:7fb6877b5d7c 53 #if defined(__GNUC__) && __GNUC__ >= 4
miruga27 0:7fb6877b5d7c 54 /* Count Leading Zeroes builtin in GCC.
miruga27 0:7fb6877b5d7c 55 * http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gcc/Other-Builtins.html
miruga27 0:7fb6877b5d7c 56 */
miruga27 0:7fb6877b5d7c 57 if (x == 0) {
miruga27 0:7fb6877b5d7c 58 return -1;
miruga27 0:7fb6877b5d7c 59 }
miruga27 0:7fb6877b5d7c 60 return 31 - __builtin_clz(x);
miruga27 0:7fb6877b5d7c 61 #else
miruga27 0:7fb6877b5d7c 62 /* Based off of Bit Twiddling Hacks by Sean Eron Anderson
miruga27 0:7fb6877b5d7c 63 * <seander@cs.stanford.edu>, released in the public domain.
miruga27 0:7fb6877b5d7c 64 * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog
miruga27 0:7fb6877b5d7c 65 */
miruga27 0:7fb6877b5d7c 66 const Uint32 b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
miruga27 0:7fb6877b5d7c 67 const int S[] = {1, 2, 4, 8, 16};
miruga27 0:7fb6877b5d7c 68
miruga27 0:7fb6877b5d7c 69 int msbIndex = 0;
miruga27 0:7fb6877b5d7c 70 int i;
miruga27 0:7fb6877b5d7c 71
miruga27 0:7fb6877b5d7c 72 if (x == 0) {
miruga27 0:7fb6877b5d7c 73 return -1;
miruga27 0:7fb6877b5d7c 74 }
miruga27 0:7fb6877b5d7c 75
miruga27 0:7fb6877b5d7c 76 for (i = 4; i >= 0; i--)
miruga27 0:7fb6877b5d7c 77 {
miruga27 0:7fb6877b5d7c 78 if (x & b[i])
miruga27 0:7fb6877b5d7c 79 {
miruga27 0:7fb6877b5d7c 80 x >>= S[i];
miruga27 0:7fb6877b5d7c 81 msbIndex |= S[i];
miruga27 0:7fb6877b5d7c 82 }
miruga27 0:7fb6877b5d7c 83 }
miruga27 0:7fb6877b5d7c 84
miruga27 0:7fb6877b5d7c 85 return msbIndex;
miruga27 0:7fb6877b5d7c 86 #endif
miruga27 0:7fb6877b5d7c 87 }
miruga27 0:7fb6877b5d7c 88
miruga27 0:7fb6877b5d7c 89 /* Ends C function definitions when using C++ */
miruga27 0:7fb6877b5d7c 90 #ifdef __cplusplus
miruga27 0:7fb6877b5d7c 91 }
miruga27 0:7fb6877b5d7c 92 #endif
miruga27 0:7fb6877b5d7c 93 #include "close_code.h"
miruga27 0:7fb6877b5d7c 94
miruga27 0:7fb6877b5d7c 95 #endif /* _SDL_bits_h */
miruga27 0:7fb6877b5d7c 96
miruga27 0:7fb6877b5d7c 97 /* vi: set ts=4 sw=4 expandtab: */