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