src/core/bitmath_func.hpp
author peter1138
Tue, 22 Jan 2008 07:27:06 +0000
changeset 8374 7a1b6c89cb89
parent 8132 f4c7a8e4f25a
child 9111 48ce04029fe4
permissions -rw-r--r--
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
7971
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
     1
/* $Id$ */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
     2
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
     3
/** @file bitmath_func.hpp */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
     4
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
     5
#ifndef BITMATH_FUNC_HPP
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
     6
#define BITMATH_FUNC_HPP
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
     7
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
     8
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
     9
 * Fetch n bits from x, started at bit s.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    10
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    11
 * This function can be used to fetch n bits from the value x. The
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    12
 * s value set the startposition to read. The startposition is
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    13
 * count from the LSB and starts at 0. The result starts at a
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    14
 * LSB, as this isn't just an and-bitmask but also some
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    15
 * bit-shifting operations. GB(0xFF, 2, 1) will so
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    16
 * return 0x01 (0000 0001) instead of
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    17
 * 0x04 (0000 0100).
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    18
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    19
 * @param x The value to read some bits.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    20
 * @param s The startposition to read some bits.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    21
 * @param n The number of bits to read.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    22
 * @return The selected bits, aligned to a LSB.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    23
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    24
template<typename T> static inline uint GB(const T x, const uint8 s, const uint8 n)
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    25
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    26
	return (x >> s) & ((1U << n) - 1);
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    27
}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    28
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    29
/** Set n bits from x starting at bit s to d
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    30
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    31
 * This function sets n bits from x which started as bit s to the value of
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    32
 * d. The parameters x, s and n works the same as the parameters of
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    33
 * #GB. The result is saved in x again. Unused bits in the window
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    34
 * provided by n are set to 0 if the value of b isn't "big" enough.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    35
 * This is not a bug, its a feature.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    36
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    37
 * @note Parameter x must be a variable as the result is saved there.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    38
 * @note To avoid unexpecting results the value of b should not use more
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    39
 *       space as the provided space of n bits (log2)
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    40
 * @param x The variable to change some bits
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    41
 * @param s The startposition for the new bits
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    42
 * @param n The size/window for the new bits
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    43
 * @param d The actually new bits to save in the defined position.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    44
 * @return The new value of x
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    45
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    46
template<typename T, typename U> static inline T SB(T& x, const uint8 s, const uint8 n, const U d)
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    47
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    48
	x &= (T)(~(((1U << n) - 1) << s));
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    49
	x |= (T)(d << s);
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    50
	return x;
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    51
}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    52
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    53
/** Add i to n bits of x starting at bit s.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    54
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    55
 * This add the value of i on n bits of x starting at bit s. The parameters x,
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    56
 * s, i are similar to #GB besides x must be a variable as the result are
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    57
 * saved there. An overflow does not affect the following bits of the given
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    58
 * bit window and is simply ignored.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    59
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    60
 * @note Parameter x must be a variable as the result is saved there.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    61
 * @param x The variable to add some bits at some position
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    62
 * @param s The startposition of the addition
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    63
 * @param n The size/window for the addition
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    64
 * @param i The value to add at the given startposition in the given window.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    65
 * @return The new value of x
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    66
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    67
template<typename T, typename U> static inline T AB(T& x, const uint8 s, const uint8 n, const U i)
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    68
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    69
	const T mask = (T)(((1U << n) - 1) << s);
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    70
	x = (T)((x & ~mask) | ((x + (i << s)) & mask));
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    71
	return x;
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    72
}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    73
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    74
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    75
 * Checks if a bit in a value is set.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    76
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    77
 * This function checks if a bit inside a value is set or not.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    78
 * The y value specific the position of the bit, started at the
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    79
 * LSB and count from 0.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    80
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    81
 * @param x The value to check
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    82
 * @param y The position of the bit to check, started from the LSB
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    83
 * @return True if the bit is set, false else.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    84
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    85
template<typename T> static inline bool HasBit(const T x, const uint8 y)
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    86
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    87
	return (x & ((T)1U << y)) != 0;
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    88
}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    89
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    90
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    91
 * Check several bits in a value.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    92
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    93
 * This macro checks if a value contains at least one bit of an other
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    94
 * value.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    95
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    96
 * @param x The first value
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    97
 * @param y The second value
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    98
 * @return True if at least one bit is set in both values, false else.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    99
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   100
#define HASBITS(x, y) ((x) & (y))
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   101
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   102
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   103
 * Set a bit in a variable.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   104
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   105
 * This function sets a bit in a variable. The variable is changed
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   106
 * and the value is also returned. Parameter y defines the bit and
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   107
 * starts at the LSB with 0.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   108
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   109
 * @param x The variable to set a bit
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   110
 * @param y The bit position to set
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   111
 * @return The new value of the old value with the bit set
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   112
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   113
template<typename T> static inline T SetBit(T& x, const uint8 y)
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   114
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   115
	return x = (T)(x | (T)(1U << y));
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   116
}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   117
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   118
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   119
 * Sets several bits in a variable.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   120
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   121
 * This macro sets several bits in a variable. The bits to set are provided
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   122
 * by a value. The new value is also returned.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   123
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   124
 * @param x The variable to set some bits
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   125
 * @param y The value with set bits for setting them in the variable
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   126
 * @return The new value of x
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   127
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   128
#define SETBITS(x, y) ((x) |= (y))
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   129
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   130
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   131
 * Clears a bit in a variable.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   132
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   133
 * This function clears a bit in a variable. The variable is
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   134
 * changed and the value is also returned. Parameter y defines the bit
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   135
 * to clear and starts at the LSB with 0.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   136
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   137
 * @param x The variable to clear the bit
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   138
 * @param y The bit position to clear
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   139
 * @return The new value of the old value with the bit cleared
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   140
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   141
template<typename T> static inline T ClrBit(T& x, const uint8 y)
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   142
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   143
	return x = (T)(x & ~((T)1U << y));
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   144
}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   145
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   146
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   147
 * Clears several bits in a variable.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   148
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   149
 * This macro clears several bits in a variable. The bits to clear are
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   150
 * provided by a value. The new value is also returned.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   151
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   152
 * @param x The variable to clear some bits
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   153
 * @param y The value with set bits for clearing them in the variable
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   154
 * @return The new value of x
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   155
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   156
#define CLRBITS(x, y) ((x) &= ~(y))
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   157
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   158
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   159
 * Toggles a bit in a variable.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   160
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   161
 * This function toggles a bit in a variable. The variable is
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   162
 * changed and the value is also returned. Parameter y defines the bit
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   163
 * to toggle and starts at the LSB with 0.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   164
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   165
 * @param x The varliable to toggle the bit
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   166
 * @param y The bit position to toggle
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   167
 * @return The new value of the old value with the bit toggled
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   168
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   169
template<typename T> static inline T ToggleBit(T& x, const uint8 y)
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   170
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   171
	return x = (T)(x ^ (T)(1U << y));
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   172
}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   173
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   174
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   175
/** Lookup table to check which bit is set in a 6 bit variable */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   176
extern const uint8 _ffb_64[64];
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   177
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   178
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   179
 * Returns the first occure of a bit in a 6-bit value (from right).
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   180
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   181
 * Returns the position of the first bit that is not zero, counted from the
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   182
 * LSB. Ie, 110100 returns 2, 000001 returns 0, etc. When x == 0 returns
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   183
 * 0.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   184
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   185
 * @param x The 6-bit value to check the first zero-bit
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   186
 * @return The first position of a bit started from the LSB or 0 if x is 0.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   187
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   188
#define FIND_FIRST_BIT(x) _ffb_64[(x)]
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   189
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   190
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   191
 * Finds the position of the first bit in an integer.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   192
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   193
 * This function returns the position of the first bit set in the
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   194
 * integer. It does only check the bits of the bitmask
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   195
 * 0x3F3F (0011111100111111) and checks only the
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   196
 * bits of the bitmask 0x3F00 if and only if the
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   197
 * lower part 0x00FF is 0. This results the bits at 0x00C0 must
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   198
 * be also zero to check the bits at 0x3F00.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   199
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   200
 * @param value The value to check the first bits
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   201
 * @return The position of the first bit which is set
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   202
 * @see FIND_FIRST_BIT
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   203
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   204
static inline uint8 FindFirstBit2x64(const int value)
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   205
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   206
	if ((value & 0xFF) == 0) {
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   207
		return FIND_FIRST_BIT((value >> 8) & 0x3F) + 8;
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   208
	} else {
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   209
		return FIND_FIRST_BIT(value & 0x3F);
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   210
	}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   211
}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   212
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   213
uint8 FindFirstBit(uint32 x);
8055
49cf1521d591 (svn r11616) -Fix [FS#1526]: sometimes large values could go off the chart.
rubidium
parents: 8000
diff changeset
   214
uint8 FindLastBit(uint64 x);
7971
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   215
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   216
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   217
 * Clear the first bit in an integer.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   218
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   219
 * This function returns a value where the first bit (from LSB)
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   220
 * is cleared.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   221
 * So, 110100 returns 110000, 000001 returns 000000, etc.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   222
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   223
 * @param value The value to clear the first bit
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   224
 * @return The new value with the first bit cleared
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   225
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   226
template<typename T> static inline T KillFirstBit(T value)
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   227
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   228
	return value &= (T)(value - 1);
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   229
}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   230
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   231
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   232
 * Counts the number of set bits in a variable.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   233
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   234
 * @param value the value to count the number of bits in.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   235
 * @return the number of bits.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   236
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   237
template<typename T> static inline uint CountBits(T value)
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   238
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   239
	uint num;
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   240
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   241
	/* This loop is only called once for every bit set by clearing the lowest
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   242
	 * bit in each loop. The number of bits is therefore equal to the number of
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   243
	 * times the loop was called. It was found at the following website:
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   244
	 * http://graphics.stanford.edu/~seander/bithacks.html */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   245
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   246
	for (num = 0; value != 0; num++) {
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   247
		value &= (T)(value - 1);
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   248
	}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   249
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   250
	return num;
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   251
}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   252
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   253
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   254
 * ROtate x Left by n
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   255
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   256
 * @note Assumes a byte has 8 bits
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   257
 * @param x The value which we want to rotate
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   258
 * @param n The number how many we waht to rotate
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   259
 * @return A bit rotated number
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   260
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   261
template<typename T> static inline T ROL(const T x, const uint8 n)
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   262
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   263
	return (T)(x << n | x >> (sizeof(x) * 8 - n));
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   264
}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   265
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   266
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   267
 * ROtate x Right by n
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   268
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   269
 * @note Assumes a byte has 8 bits
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   270
 * @param x The value which we want to rotate
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   271
 * @param n The number how many we waht to rotate
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   272
 * @return A bit rotated number
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   273
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   274
template<typename T> static inline T ROR(const T x, const uint8 n)
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   275
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   276
	return (T)(x >> n | x << (sizeof(x) * 8 - n));
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   277
}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   278
8113
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   279
/**
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   280
 * Do an operation for each set set bit in a value.
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   281
 *
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   282
 * This macros is used to do an operation for each set
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   283
 * bit in a variable. The first variable can be reused
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   284
 * in the operation due to it's the bit position counter.
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   285
 * The second variable will be cleared during the usage
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   286
 *
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   287
 * @param i The position counter
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   288
 * @param b The value which we check for set bits
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   289
 */
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   290
#define FOR_EACH_SET_BIT(i, b)      \
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   291
	for (i = 0; b != 0; i++, b >>= 1) \
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   292
		if (b & 1)
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   293
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   294
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   295
#if defined(__APPLE__)
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   296
	/* Make endian swapping use Apple's macros to increase speed
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   297
	 * (since it will use hardware swapping if available).
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   298
	 * Even though they should return uint16 and uint32, we get
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   299
	 * warnings if we don't cast those (why?) */
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   300
	#define BSWAP32(x) ((uint32)Endian32_Swap(x))
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   301
	#define BSWAP16(x) ((uint16)Endian16_Swap(x))
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   302
#else
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   303
	/**
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   304
	 * Perform a 32 bits endianness bitswap on x.
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   305
	 * @param x the variable to bitswap
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   306
	 * @return the bitswapped value.
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   307
	 */
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   308
	static inline uint32 BSWAP32(uint32 x)
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   309
	{
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   310
		return ((x >> 24) & 0xFF) | ((x >> 8) & 0xFF00) | ((x << 8) & 0xFF0000) | ((x << 24) & 0xFF000000);
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   311
	}
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   312
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   313
	/**
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   314
	 * Perform a 16 bits endianness bitswap on x.
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   315
	 * @param x the variable to bitswap
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   316
	 * @return the bitswapped value.
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   317
	 */
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   318
	static inline uint16 BSWAP16(uint16 x)
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   319
	{
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   320
		return (x >> 8) | (x << 8);
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   321
	}
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   322
#endif /* __APPLE__ */
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   323
7971
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   324
#endif /* BITMATH_FUNC_HPP */