src/macros.h
author rubidium
Fri, 23 Nov 2007 16:59:30 +0000
branchnoai
changeset 9722 ebf0ece7d8f6
parent 9704 197cb8c6ae17
permissions -rw-r--r--
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     2
6527
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6437
diff changeset
     3
/** @file macros.h */
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6437
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#ifndef MACROS_H
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     6
#define MACROS_H
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     7
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
     8
#include "core/math_func.hpp"
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
     9
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    10
/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    11
 * Fetch n bits from x, started at bit s.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    12
 *
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    13
 * This function can be used to fetch n bits from the value x. The
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    14
 * s value set the startposition to read. The startposition is
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    15
 * count from the LSB and starts at 0. The result starts at a
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    16
 * LSB, as this isn't just an and-bitmask but also some
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    17
 * bit-shifting operations. GB(0xFF, 2, 1) will so
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    18
 * return 0x01 (0000 0001) instead of
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    19
 * 0x04 (0000 0100).
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    20
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    21
 * @param x The value to read some bits.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    22
 * @param s The startposition to read some bits.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    23
 * @param n The number of bits to read.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    24
 * @return The selected bits, aligned to a LSB.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    25
 */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    26
template<typename T> static inline uint GB(const T x, const uint8 s, const uint8 n)
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    27
{
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    28
	return (x >> s) & ((1U << n) - 1);
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    29
}
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    30
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    31
/** Set n bits from x starting at bit s to d
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    32
 *
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    33
 * This function sets n bits from x which started as bit s to the value of
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    34
 * d. The parameters x, s and n works the same as the parameters of
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    35
 * #GB. The result is saved in x again. Unused bits in the window
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    36
 * provided by n are set to 0 if the value of b isn't "big" enough.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    37
 * This is not a bug, its a feature.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    38
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    39
 * @note Parameter x must be a variable as the result is saved there.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    40
 * @note To avoid unexpecting results the value of b should not use more
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    41
 *       space as the provided space of n bits (log2)
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    42
 * @param x The variable to change some bits
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    43
 * @param s The startposition for the new bits
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    44
 * @param n The size/window for the new bits
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    45
 * @param d The actually new bits to save in the defined position.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    46
 * @return The new value of x
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    47
 */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    48
template<typename T, typename U> static inline T SB(T& x, const uint8 s, const uint8 n, const U d)
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    49
{
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    50
	x &= (T)(~(((1U << n) - 1) << s));
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    51
	x |= (T)(d << s);
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    52
	return x;
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    53
}
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    54
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    55
/** Add i to n bits of x starting at bit s.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    56
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    57
 * This add the value of i on n bits of x starting at bit s. The parameters x,
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    58
 * s, i are similar to #GB besides x must be a variable as the result are
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    59
 * saved there. An overflow does not affect the following bits of the given
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    60
 * bit window and is simply ignored.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    61
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    62
 * @note Parameter x must be a variable as the result is saved there.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    63
 * @param x The variable to add some bits at some position
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    64
 * @param s The startposition of the addition
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    65
 * @param n The size/window for the addition
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    66
 * @param i The value to add at the given startposition in the given window.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    67
 * @return The new value of x
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    68
 */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    69
template<typename T, typename U> static inline T AB(T& x, const uint8 s, const uint8 n, const U i)
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9601
diff changeset
    70
{
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    71
	const T mask = (T)(((1U << n) - 1) << s);
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    72
	x = (T)((x & ~mask) | ((x + (i << s)) & mask));
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    73
	return x;
1400
b8709b34afce (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
    74
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    76
/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    77
 * Checks if a bit in a value is set.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    78
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    79
 * This function checks if a bit inside a value is set or not.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    80
 * The y value specific the position of the bit, started at the
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    81
 * LSB and count from 0.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    82
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    83
 * @param x The value to check
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    84
 * @param y The position of the bit to check, started from the LSB
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    85
 * @return True if the bit is set, false else.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    86
 */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    87
template<typename T> static inline bool HasBit(const T x, const uint8 y)
6336
16879925b259 (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6332
diff changeset
    88
{
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
    89
	return (x & ((T)1U << y)) != 0;
6336
16879925b259 (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6332
diff changeset
    90
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    92
/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    93
 * Set a bit in a variable.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    94
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    95
 * This function sets a bit in a variable. The variable is changed
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    96
 * and the value is also returned. Parameter y defines the bit and
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    97
 * starts at the LSB with 0.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    98
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
    99
 * @param x The variable to set a bit
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   100
 * @param y The bit position to set
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   101
 * @return The new value of the old value with the bit set
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   102
 */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   103
template<typename T> static inline T SetBit(T& x, const uint8 y)
6336
16879925b259 (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6332
diff changeset
   104
{
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   105
	return x = (T)(x | (T)(1U << y));
6336
16879925b259 (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6332
diff changeset
   106
}
16879925b259 (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6332
diff changeset
   107
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   108
/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   109
 * Clears a bit in a variable.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   110
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   111
 * This function clears a bit in a variable. The variable is
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   112
 * changed and the value is also returned. Parameter y defines the bit
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   113
 * to clear and starts at the LSB with 0.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   114
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   115
 * @param x The variable to clear the bit
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   116
 * @param y The bit position to clear
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   117
 * @return The new value of the old value with the bit cleared
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   118
 */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   119
template<typename T> static inline T ClrBit(T& x, const uint8 y)
6336
16879925b259 (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6332
diff changeset
   120
{
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   121
	return x = (T)(x & ~((T)1U << y));
6336
16879925b259 (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6332
diff changeset
   122
}
16879925b259 (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6332
diff changeset
   123
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   124
/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   125
 * Toggles a bit in a variable.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   126
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   127
 * This function toggles a bit in a variable. The variable is
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   128
 * changed and the value is also returned. Parameter y defines the bit
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   129
 * to toggle and starts at the LSB with 0.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   130
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   131
 * @param x The varliable to toggle the bit
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   132
 * @param y The bit position to toggle
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   133
 * @return The new value of the old value with the bit toggled
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   134
 */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   135
template<typename T> static inline T ToggleBit(T& x, const uint8 y)
6336
16879925b259 (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6332
diff changeset
   136
{
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   137
	return x = (T)(x ^ (T)(1U << y));
6336
16879925b259 (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6332
diff changeset
   138
}
16879925b259 (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6332
diff changeset
   139
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
6527
f584ab6d87f8 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6437
diff changeset
   141
/* checking more bits. Maybe unneccessary, but easy to use */
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   142
/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   143
 * Check several bits in a value.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   144
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   145
 * This macro checks if a value contains at least one bit of an other
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   146
 * value.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   147
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   148
 * @param x The first value
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   149
 * @param y The second value
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   150
 * @return True if at least one bit is set in both values, false else.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   151
 */
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 6527
diff changeset
   152
#define HASBITS(x, y) ((x) & (y))
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   153
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   154
/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   155
 * Sets several bits in a variable.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   156
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   157
 * This macro sets several bits in a variable. The bits to set are provided
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   158
 * by a value. The new value is also returned.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   159
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   160
 * @param x The variable to set some bits
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   161
 * @param y The value with set bits for setting them in the variable
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   162
 * @return The new value of x
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   163
 */
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 6527
diff changeset
   164
#define SETBITS(x, y) ((x) |= (y))
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   165
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   166
/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   167
 * Clears several bits in a variable.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   168
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   169
 * This macro clears several bits in a variable. The bits to clear are
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   170
 * provided by a value. The new value is also returned.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   171
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   172
 * @param x The variable to clear some bits
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   173
 * @param y The value with set bits for clearing them in the variable
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   174
 * @return The new value of x
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   175
 */
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 6527
diff changeset
   176
#define CLRBITS(x, y) ((x) &= ~(y))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
5919
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5856
diff changeset
   178
#define GENERAL_SPRITE_COLOR(color) ((color) + PALETTE_RECOLOR_START)
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5856
diff changeset
   179
#define PLAYER_SPRITE_COLOR(owner) (GENERAL_SPRITE_COLOR(_player_colors[owner]))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
9704
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9701
diff changeset
   181
/**
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9701
diff changeset
   182
 * Whether a sprite comes from the original graphics files or a new grf file
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9701
diff changeset
   183
 * (either supplied by OpenTTD or supplied by the user).
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9701
diff changeset
   184
 *
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9701
diff changeset
   185
 * @param sprite The sprite to check
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9701
diff changeset
   186
 * @return True if it is a new sprite, or false if it is original.
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9701
diff changeset
   187
 */
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9701
diff changeset
   188
#define IS_CUSTOM_SPRITE(sprite) ((sprite) >= SPR_SIGNALS_BASE)
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9701
diff changeset
   189
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   190
extern const byte _ffb_64[64];
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   191
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   192
/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   193
 * Returns the first occure of a bit in a 6-bit value (from right).
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   194
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   195
 * Returns the position of the first bit that is not zero, counted from the
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   196
 * LSB. Ie, 110100 returns 2, 000001 returns 0, etc. When x == 0 returns
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
   197
 * 0.
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   198
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   199
 * @param x The 6-bit value to check the first zero-bit
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   200
 * @return The first position of a bit started from the LSB or 0 if x is 0.
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
   201
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   202
#define FIND_FIRST_BIT(x) _ffb_64[(x)]
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   203
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   204
/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   205
 * Finds the position of the first bit in an integer.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   206
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   207
 * This function returns the position of the first bit set in the
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   208
 * integer. It does only check the bits of the bitmask
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   209
 * 0x3F3F (0011111100111111) and checks only the
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   210
 * bits of the bitmask 0x3F00 if and only if the
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   211
 * lower part 0x00FF is 0. This results the bits at 0x00C0 must
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   212
 * be also zero to check the bits at 0x3F00.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   213
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   214
 * @param value The value to check the first bits
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   215
 * @return The position of the first bit which is set
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   216
 * @see FIND_FIRST_BIT
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   217
 */
500
ef288590e096 (svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents: 239
diff changeset
   218
static inline int FindFirstBit2x64(int value)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
{
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   220
	if ((value & 0xFF) == 0) {
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   221
		return FIND_FIRST_BIT((value >> 8) & 0x3F) + 8;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   222
	} else {
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   223
		return FIND_FIRST_BIT(value & 0x3F);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   224
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   225
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   227
/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   228
 * Clear the first bit in an integer.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   229
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   230
 * This function returns a value where the first bit (from LSB)
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   231
 * is cleared.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   232
 * So, 110100 returns 110000, 000001 returns 000000, etc.
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   233
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   234
 * @param value The value to clear the first bit
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   235
 * @return The new value with the first bit cleared
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   236
 */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   237
template<typename T> static inline T KillFirstBit(T value)
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   238
{
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   239
	return value &= (T)(value - 1);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   240
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   241
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   242
/**
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   243
 * Counts the number of set bits in a variable.
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   244
 *
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   245
 * @param value the value to count the number of bits in.
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   246
 * @return the number of bits.
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   247
 */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   248
template<typename T> static inline uint CountBits(T value)
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   249
{
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   250
	uint num;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   251
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   252
	/* This loop is only called once for every bit set by clearing the lowest
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   253
	 * bit in each loop. The number of bits is therefore equal to the number of
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   254
	 * times the loop was called. It was found at the following website:
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   255
	 * http://graphics.stanford.edu/~seander/bithacks.html */
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   256
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   257
	for (num = 0; value != 0; num++) {
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   258
		value &= (T)(value - 1);
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   259
	}
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   260
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   261
	return num;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   262
}
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   263
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   264
/**
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   265
 * Flips a coin with a given probability.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   266
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   267
 * This macro can be used to get true or false randomized according to a
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   268
 * given probability. The parameter a and b create a percent value with
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   269
 * (a/b). The macro returns true in (a/b) percent.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   270
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   271
 * @param a The numerator of the fraction
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   272
 * @param b The denominator of the fraction, must of course not be null
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   273
 * @return True in (a/b) percent
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   274
 */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   275
#define CHANCE16(a, b) CHANCE16I(a, b, Random())
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   276
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   277
/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   278
 * Flips a coin with a given probability and saves the randomize-number in a variable.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   279
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   280
 * This macro uses the same parameters as the CHANCE16 marco. The third parameter
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   281
 * must be a variable the randomize-number from Random() is saved in.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   282
 *
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   283
 * The low 16 bits of r will already be used and can therefor not be passed to
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   284
 * CHANCE16I. One can only send the high 16 bits to CHANCE16I.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   285
 *
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   286
 * @param a The numerator of the fraction, see CHANCE16
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   287
 * @param b The denominator of the fraction, see CHANCE16
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   288
 * @param r The variable to save the randomize-number from Random()
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   289
 * @return True in (a/b) percent
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   290
 */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   291
#define CHANCE16R(a, b, r) CHANCE16I(a, b, r = Random())
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   292
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   293
/**
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   294
 * Checks if a given randomize-number is below a given probability.
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   295
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   296
 * This macro is used to check if the given probability by the fraction of (a/b)
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   297
 * is greater than low 16 bits of the given randomize-number v.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   298
 *
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   299
 * Do not use this function twice on the same random 16 bits as it will yield
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   300
 * the same result. One can use a random number for two calls to CHANCE16I,
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   301
 * where one call sends the low 16 bits and the other the high 16 bits.
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   302
 *
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   303
 * @param a The numerator of the fraction, see CHANCE16
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   304
 * @param b The denominator of the fraction, see CHANCE16
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   305
 * @param r The given randomize-number
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   306
 * @return True if v is less or equals (a/b)
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9629
diff changeset
   307
 */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   308
static inline bool CHANCE16I(const uint a, const uint b, const uint32 r)
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   309
{
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   310
	return (uint16)r < (uint16)((65536 * a) / b);
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   311
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   312
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   313
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2775
diff changeset
   314
#define for_each_bit(_i, _b)            \
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2775
diff changeset
   315
	for (_i = 0; _b != 0; _i++, _b >>= 1) \
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2775
diff changeset
   316
		if (_b & 1)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   317
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   318
2966
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   319
static inline uint16 ReadLE16Aligned(const void* x)
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   320
{
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   321
	return FROM_LE16(*(const uint16*)x);
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   322
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   323
2966
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   324
static inline uint16 ReadLE16Unaligned(const void* x)
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   325
{
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   326
#ifdef OTTD_ALIGNMENT
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   327
	return ((const byte*)x)[0] | ((const byte*)x)[1] << 8;
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   328
#else
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   329
	return FROM_LE16(*(const uint16*)x);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   330
#endif
2966
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   331
}
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   332
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
2086
dbe5faa270e0 (svn r2596) Add macros ROL and ROR to ROtate values Left/Right. Also shorten the parameter names for GB and SB to increase readability
tron
parents: 1981
diff changeset
   334
/**
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   335
 * ROtate x Left by n
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   336
 *
2086
dbe5faa270e0 (svn r2596) Add macros ROL and ROR to ROtate values Left/Right. Also shorten the parameter names for GB and SB to increase readability
tron
parents: 1981
diff changeset
   337
 * @note Assumes a byte has 8 bits
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   338
 * @param x The value which we want to rotate
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   339
 * @param n The number how many we waht to rotate
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   340
 * @return A bit rotated number
2086
dbe5faa270e0 (svn r2596) Add macros ROL and ROR to ROtate values Left/Right. Also shorten the parameter names for GB and SB to increase readability
tron
parents: 1981
diff changeset
   341
 */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   342
template<typename T> static inline T ROL(const T x, const uint8 n)
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   343
{
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   344
	return (T)(x << n | x >> (sizeof(x) * 8 - n));
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   345
}
1852
9cfa8bf9d39f (svn r2358) Add macros for getting (GB) and setting (SB) a range of bits
tron
parents: 1556
diff changeset
   346
2398
70de6626d65f (svn r2924) Introduce the ALIGN() macro which aligns values to multiples of a power of 2, for exact semantics see the commment in macros.h
tron
parents: 2238
diff changeset
   347
/**
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   348
 * ROtate x Right by n
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   349
 *
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   350
 * @note Assumes a byte has 8 bits
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   351
 * @param x The value which we want to rotate
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   352
 * @param n The number how many we waht to rotate
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   353
 * @return A bit rotated number
2398
70de6626d65f (svn r2924) Introduce the ALIGN() macro which aligns values to multiples of a power of 2, for exact semantics see the commment in macros.h
tron
parents: 2238
diff changeset
   354
 */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   355
template<typename T> static inline T ROR(const T x, const uint8 n)
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   356
{
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   357
	return (T)(x >> n | x << (sizeof(x) * 8 - n));
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9704
diff changeset
   358
}
2398
70de6626d65f (svn r2924) Introduce the ALIGN() macro which aligns values to multiples of a power of 2, for exact semantics see the commment in macros.h
tron
parents: 2238
diff changeset
   359
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4274
diff changeset
   360
/** return the largest value that can be entered in a variable.
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4274
diff changeset
   361
 */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4274
diff changeset
   362
#define MAX_UVALUE(type) ((type)~(type)0)
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4274
diff changeset
   363
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   364
#endif /* MACROS_H */