src/core/bitmath_func.hpp
author terom@frrb.lan
Fri, 19 Dec 2008 01:38:09 +0200
changeset 10439 50f056aa3024
parent 10065 2bf4d74bbf28
permissions -rw-r--r--
industries, unmoveables... everything but the landscape
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
9111
48ce04029fe4 (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: 8132
diff changeset
     3
/** @file bitmath_func.hpp Functions related to bit mathematics. */
7971
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
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
    24
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
    25
static FORCEINLINE uint GB(const T x, const uint8 s, const uint8 n)
7971
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    26
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    27
	return (x >> s) & ((1U << n) - 1);
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
9773
fd7309d22bc6 (svn r13910) -Document: string drawing related functions and types (Alberth)
rubidium
parents: 9575
diff changeset
    30
/** Set \a n bits in \a x starting at bit \a s to \a d
7971
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    31
 *
9773
fd7309d22bc6 (svn r13910) -Document: string drawing related functions and types (Alberth)
rubidium
parents: 9575
diff changeset
    32
 * This function sets \a n bits from \a x which started as bit \a s to the value of
fd7309d22bc6 (svn r13910) -Document: string drawing related functions and types (Alberth)
rubidium
parents: 9575
diff changeset
    33
 * \a d. The parameters \a x, \a s and \a n works the same as the parameters of
fd7309d22bc6 (svn r13910) -Document: string drawing related functions and types (Alberth)
rubidium
parents: 9575
diff changeset
    34
 * #GB. The result is saved in \a x again. Unused bits in the window
fd7309d22bc6 (svn r13910) -Document: string drawing related functions and types (Alberth)
rubidium
parents: 9575
diff changeset
    35
 * provided by n are set to 0 if the value of \a d isn't "big" enough.
7971
8509d595142a (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.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    37
 *
9773
fd7309d22bc6 (svn r13910) -Document: string drawing related functions and types (Alberth)
rubidium
parents: 9575
diff changeset
    38
 * @note Parameter \a x must be a variable as the result is saved there.
fd7309d22bc6 (svn r13910) -Document: string drawing related functions and types (Alberth)
rubidium
parents: 9575
diff changeset
    39
 * @note To avoid unexpecting results the value of \a d should not use more
fd7309d22bc6 (svn r13910) -Document: string drawing related functions and types (Alberth)
rubidium
parents: 9575
diff changeset
    40
 *       space as the provided space of \a n bits (log2)
7971
8509d595142a (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
8509d595142a (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
8509d595142a (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
8509d595142a (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.
9773
fd7309d22bc6 (svn r13910) -Document: string drawing related functions and types (Alberth)
rubidium
parents: 9575
diff changeset
    45
 * @return The new value of \a x
7971
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    46
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
    47
template <typename T, typename U>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
    48
static FORCEINLINE T SB(T &x, const uint8 s, const uint8 n, const U d)
7971
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    49
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    50
	x &= (T)(~(((1U << n) - 1) << s));
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    51
	x |= (T)(d << s);
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    52
	return x;
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    53
}
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
/** 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
    56
 *
8509d595142a (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,
8509d595142a (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
8509d595142a (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
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    60
 * bit window and is simply ignored.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    61
 *
8509d595142a (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.
8509d595142a (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
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    64
 * @param s The startposition of the addition
8509d595142a (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
8509d595142a (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.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    67
 * @return The new value of x
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    68
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
    69
template <typename T, typename U>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
    70
static FORCEINLINE T AB(T &x, const uint8 s, const uint8 n, const U i)
7971
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    71
{
8509d595142a (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);
8509d595142a (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));
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    74
	return x;
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    75
}
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
/**
8509d595142a (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.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    79
 *
8509d595142a (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.
8509d595142a (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
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    82
 * LSB and count from 0.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    83
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    84
 * @param x The value to check
8509d595142a (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
8509d595142a (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.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    87
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
    88
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
    89
static FORCEINLINE bool HasBit(const T x, const uint8 y)
7971
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
	return (x & ((T)1U << y)) != 0;
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
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    94
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    95
 * Check several bits in a value.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    96
 *
8509d595142a (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
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
    98
 * value.
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
 * @param x The first value
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   101
 * @param y The second value
8509d595142a (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.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   103
 */
9370
7e64238f2dbd (svn r13273) -Fix [FS#2042]: MSVC warnings (again)
glx
parents: 9111
diff changeset
   104
#define HASBITS(x, y) (((x) & (y)) != 0)
7971
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   105
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   106
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   107
 * Set a bit in a variable.
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
 * 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
   110
 * 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
   111
 * starts at the LSB with 0.
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
 * @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
   114
 * @param y The bit position to set
8509d595142a (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
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   116
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   117
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   118
static FORCEINLINE T SetBit(T &x, const uint8 y)
7971
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   119
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   120
	return x = (T)(x | (T)(1U << y));
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   121
}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   122
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
 * Sets several bits in a variable.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   125
 *
8509d595142a (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
8509d595142a (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.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   128
 *
8509d595142a (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
8509d595142a (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
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   131
 * @return The new value of x
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
#define SETBITS(x, y) ((x) |= (y))
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   134
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   135
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   136
 * Clears a bit in a variable.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   137
 *
8509d595142a (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
8509d595142a (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
8509d595142a (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.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   141
 *
8509d595142a (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
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   143
 * @param y The bit position to clear
8509d595142a (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
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   145
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   146
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   147
static FORCEINLINE T ClrBit(T &x, const uint8 y)
7971
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
	return x = (T)(x & ~((T)1U << y));
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   150
}
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
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   153
 * Clears several bits in a variable.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   154
 *
8509d595142a (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
8509d595142a (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.
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
 * @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
   159
 * @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
   160
 * @return The new value of x
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   161
 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   162
#define CLRBITS(x, y) ((x) &= ~(y))
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   163
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
 * Toggles a bit in a variable.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   166
 *
8509d595142a (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
8509d595142a (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
8509d595142a (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.
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
 * @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
   172
 * @param y The bit position to toggle
8509d595142a (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
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   174
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   175
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   176
static FORCEINLINE T ToggleBit(T &x, const uint8 y)
7971
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
	return x = (T)(x ^ (T)(1U << y));
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   179
}
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
8509d595142a (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 */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   183
extern const uint8 _ffb_64[64];
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
/**
8509d595142a (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).
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
 * 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
   189
 * 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
   190
 * 0.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   191
 *
8509d595142a (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
8509d595142a (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.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   194
 */
8509d595142a (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)]
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   196
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   197
/**
8509d595142a (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.
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
 * 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
   201
 * 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
   202
 * 0x3F3F (0011111100111111) and checks only the
8509d595142a (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
8509d595142a (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
8509d595142a (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.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   206
 *
8509d595142a (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
8509d595142a (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
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   209
 * @see FIND_FIRST_BIT
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   210
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   211
static FORCEINLINE uint8 FindFirstBit2x64(const int value)
7971
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
	if ((value & 0xFF) == 0) {
8509d595142a (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;
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   215
	} else {
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   216
		return FIND_FIRST_BIT(value & 0x3F);
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   217
	}
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
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   220
uint8 FindFirstBit(uint32 x);
8055
49cf1521d591 (svn r11616) -Fix [FS#1526]: sometimes large values could go off the chart.
rubidium
parents: 8000
diff changeset
   221
uint8 FindLastBit(uint64 x);
7971
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
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   224
 * Clear the first bit in an integer.
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
 * 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
   227
 * is cleared.
8509d595142a (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.
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
 * @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
   231
 * @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
   232
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   233
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   234
static FORCEINLINE T KillFirstBit(T value)
7971
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   235
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   236
	return value &= (T)(value - 1);
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   237
}
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
/**
8509d595142a (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.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   241
 *
8509d595142a (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.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   243
 * @return the number of bits.
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   244
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   245
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   246
static inline uint CountBits(T value)
7971
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   247
{
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   248
	uint num;
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
	/* 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
   251
	 * 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
   252
	 * 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
   253
	 * http://graphics.stanford.edu/~seander/bithacks.html */
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   254
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   255
	for (num = 0; value != 0; num++) {
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   256
		value &= (T)(value - 1);
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   257
	}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   258
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   259
	return num;
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
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
 * ROtate x Left by 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
 * @note Assumes a byte has 8 bits
8509d595142a (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
8509d595142a (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
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   268
 * @return A bit rotated number
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   269
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   270
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   271
static FORCEINLINE T ROL(const T x, const uint8 n)
7971
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   272
{
8509d595142a (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));
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   274
}
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
/**
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   277
 * ROtate x Right by n
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   278
 *
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   279
 * @note Assumes a byte has 8 bits
8509d595142a (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
8509d595142a (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
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   282
 * @return A bit rotated number
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   283
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   284
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   285
static FORCEINLINE T ROR(const T x, const uint8 n)
7971
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   286
{
8509d595142a (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));
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   288
}
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   289
8113
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   290
/**
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   291
 * 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
   292
 *
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   293
 * 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
   294
 * 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
   295
 * 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
   296
 * 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
   297
 *
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   298
 * @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
   299
 * @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
   300
 */
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   301
#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
   302
	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
   303
		if (b & 1)
31b7784db761 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8055
diff changeset
   304
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   305
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   306
#if defined(__APPLE__)
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   307
	/* 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
   308
	 * (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
   309
	 * 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
   310
	 * 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
   311
	#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
   312
	#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
   313
#else
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   314
	/**
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   315
	 * 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
   316
	 * @param x the variable to bitswap
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   317
	 * @return the bitswapped value.
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   318
	 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   319
	static FORCEINLINE uint32 BSWAP32(uint32 x)
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   320
	{
10065
2bf4d74bbf28 (svn r14232) -Codechange: use builtin for byte swapping for gcc >= 4.3
smatz
parents: 9773
diff changeset
   321
#if defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4)  && __GNUC_MINOR__ >= 3))
2bf4d74bbf28 (svn r14232) -Codechange: use builtin for byte swapping for gcc >= 4.3
smatz
parents: 9773
diff changeset
   322
		/* GCC >= 4.3 provides a builtin, resulting in faster code */
2bf4d74bbf28 (svn r14232) -Codechange: use builtin for byte swapping for gcc >= 4.3
smatz
parents: 9773
diff changeset
   323
		return (uint32)__builtin_bswap32((int32)x);
2bf4d74bbf28 (svn r14232) -Codechange: use builtin for byte swapping for gcc >= 4.3
smatz
parents: 9773
diff changeset
   324
#else
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   325
		return ((x >> 24) & 0xFF) | ((x >> 8) & 0xFF00) | ((x << 8) & 0xFF0000) | ((x << 24) & 0xFF000000);
10065
2bf4d74bbf28 (svn r14232) -Codechange: use builtin for byte swapping for gcc >= 4.3
smatz
parents: 9773
diff changeset
   326
#endif /* defined(__GNUC__) */
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   327
	}
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   328
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   329
	/**
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   330
	 * 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
   331
	 * @param x the variable to bitswap
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   332
	 * @return the bitswapped value.
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   333
	 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9370
diff changeset
   334
	static FORCEINLINE uint16 BSWAP16(uint16 x)
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   335
	{
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   336
		return (x >> 8) | (x << 8);
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   337
	}
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   338
#endif /* __APPLE__ */
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 8113
diff changeset
   339
7971
8509d595142a (svn r11527) -Codechange: Split the bitmath functions of to their own files
skidd13
parents:
diff changeset
   340
#endif /* BITMATH_FUNC_HPP */