src/macros.h
author truelight
Sun, 04 Nov 2007 22:47:34 +0000
changeset 8328 6909973c8359
parent 8325 1552eb5e5430
child 8329 77e50bd6ad67
permissions -rw-r--r--
(svn r11382) -Codechange: renamed COUNTBITS to CountBits, as it is no longer a macro (skidd13)
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
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
     8
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
     9
 * Fetch n bits from x, started at bit s.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    10
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    11
 * This macro can be used to fetch n bits from the value x. The
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    12
 * s value set the startposition to read. The startposition is
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    13
 * count from the LSB and starts at 0. The result starts at a
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    14
 * LSB, as this isn't just an and-bitmask but also some
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    15
 * bit-shifting operations. GB(0xFF, 2, 1) will so
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    16
 * return 0x01 (0000 0001) instead of
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    17
 * 0x04 (0000 0100).
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    18
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    19
 * @param x The value to read some bits.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    20
 * @param s The startposition to read some bits.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    21
 * @param n The number of bits to read.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    22
 * @return The selected bits, aligned to a LSB.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    23
 */
2663
f3e7d6d3e3a1 (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    24
#define GB(x, s, n) (((x) >> (s)) & ((1U << (n)) - 1))
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    25
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    26
/** Set n bits from x starting at bit s to d
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    27
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    28
 * This macro sets n bits from x which started as bit s to the value of
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    29
 * d. The parameters x, s and n works the same as the parameters of
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    30
 * #GB. The result is saved in x again. Unused bits in the window
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    31
 * provided by n are set to 0 if the value of b isn't "big" enough.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    32
 * This is not a bug, its a feature.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    33
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    34
 * @note Parameter x must be a variable as the result is saved there.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    35
 * @note To avoid unexpecting results the value of b should not use more
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    36
 *       space as the provided space of n bits (log2)
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    37
 * @param x The variable to change some bits
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    38
 * @param s The startposition for the new bits
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    39
 * @param n The size/window for the new bits
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    40
 * @param d The actually new bits to save in the defined position.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    41
 * @return The new value of x
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    42
 */
2663
f3e7d6d3e3a1 (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    43
#define SB(x, s, n, d) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | ((d) << (s)))
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    44
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    45
/** Add i to n bits of x starting at bit s.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    46
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    47
 * This add the value of i on n bits of x starting at bit s. The parameters x,
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    48
 * s, i are similar to #GB besides x must be a variable as the result are
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    49
 * saved there. An overflow does not affect the following bits of the given
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    50
 * bit window and is simply ignored.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    51
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    52
 * @note Parameter x must be a variable as the result is saved there.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    53
 * @param x The variable to add some bits at some position
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    54
 * @param s The startposition of the addition
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    55
 * @param n The size/window for the addition
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    56
 * @param i The value to add at the given startposition in the given window.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    57
 * @return The new value of x
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    58
 */
2663
f3e7d6d3e3a1 (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    59
#define AB(x, s, n, i) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | (((x) + ((i) << (s))) & (((1U << (n)) - 1) << (s))))
f3e7d6d3e3a1 (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    60
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 22
diff changeset
    61
#ifdef min
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
#undef min
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    64
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    65
#ifdef max
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    66
#undef max
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    69
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    70
 * Returns the maximum of two values.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    71
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    72
 * This function returns the greater value of two given values.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    73
 * If they are equal the value of a is returned.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    74
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    75
 * @param a The first value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    76
 * @param b The second value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    77
 * @return The greater value or a if equals
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    78
 */
8315
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
    79
template<typename T> static inline T max(const T a, const T b)
5854
da4502a497d3 (svn r8057) -Codechange: Declare the "new" max template as static line.
celestar
parents: 5853
diff changeset
    80
{
da4502a497d3 (svn r8057) -Codechange: Declare the "new" max template as static line.
celestar
parents: 5853
diff changeset
    81
	return a >= b ? a : b;
da4502a497d3 (svn r8057) -Codechange: Declare the "new" max template as static line.
celestar
parents: 5853
diff changeset
    82
}
5852
cb3f71b16e1a (svn r8055) -Codechange: Replace the different max, dmax, maxu whatever macros by a simple template function max(), that requires two arguments of the same type. While I'm at it change a variable called "max" to "maxval" in a function that calls max().
celestar
parents: 5838
diff changeset
    83
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    84
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    85
 * Returns the minimum of two values.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    86
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    87
 * This function returns the smaller value of two given values.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    88
 * If they are equal the value of b is returned.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    89
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    90
 * @param a The first value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    91
 * @param b The second value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    92
 * @return The smaller value or b if equals
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    93
 */
8315
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
    94
template<typename T> static inline T min(const T a, const T b)
7453
67ba4a6fc014 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 6987
diff changeset
    95
{
67ba4a6fc014 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 6987
diff changeset
    96
	return a < b ? a : b;
67ba4a6fc014 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 6987
diff changeset
    97
}
67ba4a6fc014 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 6987
diff changeset
    98
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    99
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   100
 * Returns the minimum of two integer.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   101
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   102
 * This function returns the smaller value of two given integers.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   103
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   104
 * @param a The first integer
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   105
 * @param b The second integer
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   106
 * @return The smaller value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   107
 */
8315
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
   108
static inline int min(const int a, const int b)
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
   109
{
8323
42d835c0867c (svn r11377) -Codechange: some more strictness in macros.h (skidd13)
truelight
parents: 8315
diff changeset
   110
	return a <= b ? a : b;
8315
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
   111
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   113
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   114
 * Returns the minimum of two unsigned integers.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   115
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   116
 * This function returns the smaller value of two given unsigned integers.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   117
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   118
 * @param a The first unsigned integer
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   119
 * @param b The second unsigned integer
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   120
 * @return The smaller value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   121
 */
8315
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
   122
static inline uint minu(const uint a, const uint b)
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
   123
{
8323
42d835c0867c (svn r11377) -Codechange: some more strictness in macros.h (skidd13)
truelight
parents: 8315
diff changeset
   124
	return a <= b ? a : b;
8315
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
   125
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   127
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   128
 * Clamp an integer between an interval.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   129
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   130
 * This function returns a value which is between the given interval of
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   131
 * min and max. If the given value is in this interval the value itself
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   132
 * is returned otherwise the border of the interval is returned, according
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   133
 * which side of the interval was 'left'.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   134
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   135
 * @note The min value must be less or equal of max or you get some
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   136
 *       unexpected results.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   137
 * @param a The value to clamp/truncate.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   138
 * @param min The minimum of the interval.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   139
 * @param max the maximum of the interval.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   140
 * @returns A value between min and max which is closest to a.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   141
 * @see clampu(uint, uint, uint)
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   142
 */
8315
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
   143
static inline int clamp(const int a, const int min, const int max)
1400
b8709b34afce (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
   144
{
b8709b34afce (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
   145
	if (a <= min) return min;
b8709b34afce (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
   146
	if (a >= max) return max;
b8709b34afce (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
   147
	return a;
b8709b34afce (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
   148
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   150
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   151
 * Clamp an unsigned integer between an interval.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   152
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   153
 * This function returns a value which is between the given interval of
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   154
 * min and max. If the given value is in this interval the value itself
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   155
 * is returned otherwise the border of the interval is returned, according
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   156
 * which side of the interval was 'left'.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   157
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   158
 * @note The min value must be less or equal of max or you get some
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   159
 *       unexpected results.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   160
 * @param a The value to clamp/truncate.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   161
 * @param min The minimum of the interval.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   162
 * @param max the maximum of the interval.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   163
 * @returns A value between min and max which is closest to a.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   164
 * @see clamp(int, int, int)
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   165
 */
8315
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
   166
static inline uint clampu(const uint a, const uint min, const uint max)
3352
49d580a11385 (svn r4142) - Fix [FS#74]: Incorrectly loaded settings from the config file when the signed uint32 variable would be negative.
Darkvater
parents: 3326
diff changeset
   167
{
49d580a11385 (svn r4142) - Fix [FS#74]: Incorrectly loaded settings from the config file when the signed uint32 variable would be negative.
Darkvater
parents: 3326
diff changeset
   168
	if (a <= min) return min;
49d580a11385 (svn r4142) - Fix [FS#74]: Incorrectly loaded settings from the config file when the signed uint32 variable would be negative.
Darkvater
parents: 3326
diff changeset
   169
	if (a >= max) return max;
49d580a11385 (svn r4142) - Fix [FS#74]: Incorrectly loaded settings from the config file when the signed uint32 variable would be negative.
Darkvater
parents: 3326
diff changeset
   170
	return a;
49d580a11385 (svn r4142) - Fix [FS#74]: Incorrectly loaded settings from the config file when the signed uint32 variable would be negative.
Darkvater
parents: 3326
diff changeset
   171
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   172
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   173
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   174
 * Reduce a signed 64-bit int to a signed 32-bit one
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   175
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   176
 * This function clamps a 64-bit integer to a 32-bit integer.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   177
 * If the 64-bit value is smaller than the smallest 32-bit integer
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   178
 * value 0x80000000 this value is returned (the left one bit is the sign bit).
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   179
 * If the 64-bit value is greater than the greatest 32-bit integer value 0x7FFFFFFF
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   180
 * this value is returned. In all other cases the 64-bit value 'fits' in a
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   181
 * 32-bits integer field and so the value is casted to int32 and returned.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   182
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   183
 * @param a The 64-bit value to clamps
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   184
 * @return The 64-bit value reduced to a 32-bit value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   185
 * @see clamp(int, int, int)
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   186
 */
8315
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
   187
static inline int32 ClampToI32(const int64 a)
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   188
{
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   189
	if (a <= (int32)0x80000000) return 0x80000000;
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   190
	if (a >= (int32)0x7FFFFFFF) return 0x7FFFFFFF;
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   191
	return (int32)a;
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   192
}
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   193
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   194
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   195
 * Multiply two integer values and shift the results to right.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   196
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   197
 * This function multiplies two integer values. The result is
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   198
 * shifted by the amount of shift to right.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   199
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   200
 * @param a The first integer
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   201
 * @param b The second integer
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   202
 * @param shift The amount to shift the value to right.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   203
 * @return The shifted result
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   204
 */
8323
42d835c0867c (svn r11377) -Codechange: some more strictness in macros.h (skidd13)
truelight
parents: 8315
diff changeset
   205
static inline int32 BIGMULSS(const int32 a, const int32 b, const uint8 shift)
6436
43c201d8ad95 (svn r8846) -Fix
tron
parents: 6435
diff changeset
   206
{
43c201d8ad95 (svn r8846) -Fix
tron
parents: 6435
diff changeset
   207
	return (int32)((int64)a * (int64)b >> shift);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   208
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   209
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   210
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   211
 * Multiply two unsigned integers and shift the results to right.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   212
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   213
 * This function multiplies two unsigned integers. The result is
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   214
 * shifted by the amount of shift to right.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   215
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   216
 * @param a The first unsigned integer
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   217
 * @param b The second unsigned integer
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   218
 * @param shift The amount to shift the value to right.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   219
 * @return The shifted result
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   220
 */
8323
42d835c0867c (svn r11377) -Codechange: some more strictness in macros.h (skidd13)
truelight
parents: 8315
diff changeset
   221
static inline uint32 BIGMULUS(const uint32 a, const uint32 b, const uint8 shift)
6436
43c201d8ad95 (svn r8846) -Fix
tron
parents: 6435
diff changeset
   222
{
43c201d8ad95 (svn r8846) -Fix
tron
parents: 6435
diff changeset
   223
	return (uint32)((uint64)a * (uint64)b >> shift);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   224
}
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
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   227
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   228
 * Checks if a value is between a window started at some base point.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   229
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   230
 * This macro checks if the value x is between the value of base
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   231
 * and base+size. If x equals base this returns true. If x equals
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   232
 * base+size this returns false.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   233
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   234
 * @param x The value to check
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   235
 * @param base The base value of the interval
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   236
 * @param size The size of the interval
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   237
 * @return True if the value is in the interval, false else.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   238
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   239
/* OPT: optimized into an unsigned comparison */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   240
//#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   241
#define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) )
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   242
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   243
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   244
 * Checks if a bit in a value is set.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   245
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   246
 * This function checks if a bit inside a value is set or not.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   247
 * The y value specific the position of the bit, started at the
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   248
 * LSB and count from 0.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   249
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   250
 * @param x The value to check
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   251
 * @param y The position of the bit to check, started from the LSB
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   252
 * @return True if the bit is set, false else.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   253
 */
8323
42d835c0867c (svn r11377) -Codechange: some more strictness in macros.h (skidd13)
truelight
parents: 8315
diff changeset
   254
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
   255
{
8315
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
   256
	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
   257
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   258
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   259
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   260
 * Set a bit in a variable.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   261
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   262
 * This function sets a bit in a variable. The variable is changed
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   263
 * and the value is also returned. Parameter y defines the bit and
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   264
 * starts at the LSB with 0.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   265
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   266
 * @param x The variable to set a bit
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   267
 * @param y The bit position to set
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   268
 * @return The new value of the old value with the bit set
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   269
 */
8323
42d835c0867c (svn r11377) -Codechange: some more strictness in macros.h (skidd13)
truelight
parents: 8315
diff changeset
   270
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
   271
{
8315
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
   272
	return 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
   273
}
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
   274
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   275
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   276
 * Clears a bit in a variable.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   277
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   278
 * This function clears a bit in a variable. The variable is
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   279
 * changed and the value is also returned. Parameter y defines the bit
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   280
 * to clear and starts at the LSB with 0.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   281
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   282
 * @param x The variable to clear the bit
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   283
 * @param y The bit position to clear
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   284
 * @return The new value of the old value with the bit cleared
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   285
 */
8323
42d835c0867c (svn r11377) -Codechange: some more strictness in macros.h (skidd13)
truelight
parents: 8315
diff changeset
   286
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
   287
{
8315
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
   288
	return 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
   289
}
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
   290
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   291
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   292
 * Toggles a bit in a variable.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   293
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   294
 * This function toggles a bit in a variable. The variable is
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   295
 * changed and the value is also returned. Parameter y defines the bit
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   296
 * to toggle and starts at the LSB with 0.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   297
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   298
 * @param x The varliable to toggle the bit
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   299
 * @param y The bit position to toggle
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   300
 * @return The new value of the old value with the bit toggled
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   301
 */
8323
42d835c0867c (svn r11377) -Codechange: some more strictness in macros.h (skidd13)
truelight
parents: 8315
diff changeset
   302
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
   303
{
8315
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
   304
	return 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
   305
}
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
   306
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   307
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
   308
/* checking more bits. Maybe unneccessary, but easy to use */
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   309
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   310
 * Check several bits in a value.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   311
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   312
 * This macro checks if a value contains at least one bit of an other
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   313
 * value.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   314
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   315
 * @param x The first value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   316
 * @param y The second value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   317
 * @return True if at least one bit is set in both values, false else.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   318
 */
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6527
diff changeset
   319
#define HASBITS(x, y) ((x) & (y))
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   320
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   321
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   322
 * Sets several bits in a variable.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   323
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   324
 * This macro sets several bits in a variable. The bits to set are provided
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   325
 * by a value. The new value is also returned.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   326
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   327
 * @param x The variable to set some bits
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   328
 * @param y The value with set bits for setting them in the variable
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   329
 * @return The new value of x
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   330
 */
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6527
diff changeset
   331
#define SETBITS(x, y) ((x) |= (y))
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   332
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   333
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   334
 * Clears several bits in a variable.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   335
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   336
 * This macro clears several bits in a variable. The bits to clear are
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   337
 * provided by a value. The new value is also returned.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   338
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   339
 * @param x The variable to clear some bits
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   340
 * @param y The value with set bits for clearing them in the variable
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   341
 * @return The new value of x
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   342
 */
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6527
diff changeset
   343
#define CLRBITS(x, y) ((x) &= ~(y))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   344
5919
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5856
diff changeset
   345
#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
   346
#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
   347
8115
95deea94d1f5 (svn r11149) -Fix [FS#1225]: Draw building stages for new house ground sprites.
maedhros
parents: 8012
diff changeset
   348
/**
95deea94d1f5 (svn r11149) -Fix [FS#1225]: Draw building stages for new house ground sprites.
maedhros
parents: 8012
diff changeset
   349
 * Whether a sprite comes from the original graphics files or a new grf file
95deea94d1f5 (svn r11149) -Fix [FS#1225]: Draw building stages for new house ground sprites.
maedhros
parents: 8012
diff changeset
   350
 * (either supplied by OpenTTD or supplied by the user).
95deea94d1f5 (svn r11149) -Fix [FS#1225]: Draw building stages for new house ground sprites.
maedhros
parents: 8012
diff changeset
   351
 *
95deea94d1f5 (svn r11149) -Fix [FS#1225]: Draw building stages for new house ground sprites.
maedhros
parents: 8012
diff changeset
   352
 * @param sprite The sprite to check
95deea94d1f5 (svn r11149) -Fix [FS#1225]: Draw building stages for new house ground sprites.
maedhros
parents: 8012
diff changeset
   353
 * @return True if it is a new sprite, or false if it is original.
95deea94d1f5 (svn r11149) -Fix [FS#1225]: Draw building stages for new house ground sprites.
maedhros
parents: 8012
diff changeset
   354
 */
95deea94d1f5 (svn r11149) -Fix [FS#1225]: Draw building stages for new house ground sprites.
maedhros
parents: 8012
diff changeset
   355
#define IS_CUSTOM_SPRITE(sprite) ((sprite) >= SPR_SIGNALS_BASE)
95deea94d1f5 (svn r11149) -Fix [FS#1225]: Draw building stages for new house ground sprites.
maedhros
parents: 8012
diff changeset
   356
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
extern const byte _ffb_64[128];
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   358
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   359
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   360
 * Returns the first occure of a bit in a 6-bit value (from right).
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   361
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   362
 * Returns the position of the first bit that is not zero, counted from the
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   363
 * 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
   364
 * 0.
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   365
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   366
 * @param x The 6-bit value to check the first zero-bit
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   367
 * @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
   368
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
#define FIND_FIRST_BIT(x) _ffb_64[(x)]
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   370
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   371
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   372
 * Returns a value with the first occured of a bit set to zero.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   373
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   374
 * Returns x with the first bit from LSB that is not zero set
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   375
 * to zero. So, 110100 returns 110000, 000001 returns 000000, etc.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   376
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   377
 * @param x The value to returned a new value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   378
 * @return The value which the first bit is set to zero
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
   379
 */
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6527
diff changeset
   380
#define KILL_FIRST_BIT(x) _ffb_64[(x) + 64]
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   382
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   383
 * Finds the position of the first bit in an integer.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   384
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   385
 * This function returns the position of the first bit set in the
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   386
 * integer. It does only check the bits of the bitmask
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   387
 * 0x3F3F (0011111100111111) and checks only the
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   388
 * bits of the bitmask 0x3F00 if and only if the
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   389
 * lower part 0x00FF is 0. This results the bits at 0x00C0 must
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   390
 * be also zero to check the bits at 0x3F00.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   391
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   392
 * @param value The value to check the first bits
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   393
 * @return The position of the first bit which is set
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   394
 * @see FIND_FIRST_BIT
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   395
 */
500
ef288590e096 (svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents: 239
diff changeset
   396
static inline int FindFirstBit2x64(int value)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   397
{
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   398
/*
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
	int i = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
	if ( (byte) value == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
		i += 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   402
		value >>= 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   404
	return i + FIND_FIRST_BIT(value & 0x3F);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   405
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   406
Faster ( or at least cleaner ) implementation below?
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   407
*/
2663
f3e7d6d3e3a1 (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
   408
	if (GB(value, 0, 8) == 0) {
f3e7d6d3e3a1 (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
   409
		return FIND_FIRST_BIT(GB(value, 8, 6)) + 8;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   410
	} else {
2663
f3e7d6d3e3a1 (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
   411
		return FIND_FIRST_BIT(GB(value, 0, 6));
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   412
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   415
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   416
 * Clear the first bit in an integer.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   417
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   418
 * This function returns a value where the first bit (from LSB)
8324
ef3f806cfae5 (svn r11378) -Codechange: optimize KillFirstBit2x64 (skidd13)
truelight
parents: 8323
diff changeset
   419
 * is cleared. This function checks only the bits of 0x3F3F!
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   420
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   421
 * @param value The value to clear the first bit
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   422
 * @return The new value with the first bit cleared
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   423
 */
8325
1552eb5e5430 (svn r11379) -Fix r11378: KillFirstBit2x64 accepts and returns unsigned variables, not signed, so enforce that a bit
truelight
parents: 8324
diff changeset
   424
static inline uint KillFirstBit2x64(uint value)
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   425
{
8325
1552eb5e5430 (svn r11379) -Fix r11378: KillFirstBit2x64 accepts and returns unsigned variables, not signed, so enforce that a bit
truelight
parents: 8324
diff changeset
   426
	return value &= (uint)(value - 1) | 0x3FFFC0C0;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   427
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   429
/**
8012
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   430
 * Counts the number of set bits in a variable.
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   431
 *
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   432
 * @param value the value to count the number of bits in.
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   433
 * @return the number of bits.
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   434
 */
8328
6909973c8359 (svn r11382) -Codechange: renamed COUNTBITS to CountBits, as it is no longer a macro (skidd13)
truelight
parents: 8325
diff changeset
   435
template<typename T> static inline uint CountBits(T value)
8012
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   436
{
8315
2d66a977506a (svn r11369) -Codechange [FS#1390]: changes some int to int8 in macros.h, as they describe a bit-position, which fits perfectly in an int8 (max 64) (skidd13)
truelight
parents: 8115
diff changeset
   437
	register uint num;
8012
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   438
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   439
	/* This loop is only called once for every bit set by clearing the lowest
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   440
	 * bit in each loop. The number of bits is therefore equal to the number of
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   441
	 * times the loop was called. It was found at the following website:
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   442
	 * http://graphics.stanford.edu/~seander/bithacks.html */
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   443
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   444
	for (num = 0; value != 0; num++) {
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   445
		value &= (T)(value - 1);
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   446
	}
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   447
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   448
	return num;
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   449
}
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   450
51288087bccd (svn r11031) -Codechange: reduce the amount of duplication of bit counting functions. Based on patches by skidd13, SmatZ and Belugas.
rubidium
parents: 7822
diff changeset
   451
/**
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   452
 * Checks if a byte is in an interval.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   453
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   454
 * This macro returns true if a byte value is in the interval of [min, max).
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   455
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   456
 * @param a The byte value to check
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   457
 * @param min The minimum of the interval
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   458
 * @param max The maximum of the interval
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   459
 * @see IS_INSIDE_1D
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   460
 */
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6527
diff changeset
   461
#define IS_BYTE_INSIDE(a, min, max) ((byte)((a) - (min)) < (byte)((max) - (min)))
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   462
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   463
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   464
 * Checks if an int is in an interval.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   465
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   466
 * This macro returns true if a integer value is in the interval of [min, max).
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   467
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   468
 * @param a The integer value to check
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   469
 * @param min The minimum of the interval
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   470
 * @param max The maximum of the interval
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   471
 * @see IS_INSIDE_1D
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   472
 */
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6527
diff changeset
   473
#define IS_INT_INSIDE(a, min, max) ((uint)((a) - (min)) < (uint)((max) - (min)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   475
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   476
 * Flips a coin with a given probability.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   477
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   478
 * This macro can be used to get true or false randomized according to a
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   479
 * given probability. The parameter a and b create a percent value with
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   480
 * (a/b). The macro returns true in (a/b) percent.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   481
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   482
 * @param a The numerator of the fraction
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   483
 * @param b The denominator of the fraction, must of course not be null
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   484
 * @return True in (a/b) percent
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   485
 */
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   486
#define CHANCE16(a, b) ((uint16)Random() <= (uint16)((65536 * (a)) / (b)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   487
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   488
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   489
 * Flips a coin with a given probability and saves the randomize-number in a variable.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   490
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   491
 * This macro uses the same parameters as the CHANCE16 marco. The third parameter
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   492
 * must be a variable the randomize-number from Random() is saved in.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   493
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   494
 * @param a The numerator of the fraction, see CHANCE16
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   495
 * @param b The denominator of the fraction, see CHANCE16
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   496
 * @param r The variable to save the randomize-number from Random()
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   497
 * @return True in (a/b) percent
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   498
 */
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6527
diff changeset
   499
#define CHANCE16R(a, b, r) ((uint16)(r = Random()) <= (uint16)((65536 * (a)) / (b)))
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   500
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   501
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   502
 * Checks if a given randomize-number is below a given probability.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   503
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   504
 * This macro is used to check if the given probability by the fraction of (a/b)
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   505
 * is greater than the given randomize-number v.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   506
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   507
 * @param a The numerator of the fraction, see CHANCE16
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   508
 * @param b The denominator of the fraction, see CHANCE16
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   509
 * @param v The given randomize-number
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   510
 * @return True if v is less or equals (a/b)
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   511
 */
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6527
diff changeset
   512
#define CHANCE16I(a, b, v) ((uint16)(v) <= (uint16)((65536 * (a)) / (b)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   513
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   514
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2775
diff changeset
   515
#define for_each_bit(_i, _b)            \
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2775
diff changeset
   516
	for (_i = 0; _b != 0; _i++, _b >>= 1) \
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2775
diff changeset
   517
		if (_b & 1)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   518
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   519
#define abs myabs
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   520
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   521
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
   522
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
   523
{
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   524
	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
   525
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   526
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
   527
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
   528
{
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   529
#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
   530
	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
   531
#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
   532
	return FROM_LE16(*(const uint16*)x);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   533
#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
   534
}
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   535
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   536
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
   537
/**
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
   538
 * ROtate x Left/Right by n (must be >= 0)
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
   539
 * @note Assumes a byte has 8 bits
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
   540
 */
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
   541
#define ROL(x, n) ((x) << (n) | (x) >> (sizeof(x) * 8 - (n)))
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
   542
#define ROR(x, n) ((x) >> (n) | (x) << (sizeof(x) * 8 - (n)))
1852
9cfa8bf9d39f (svn r2358) Add macros for getting (GB) and setting (SB) a range of bits
tron
parents: 1556
diff changeset
   543
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
   544
/**
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
   545
 * Return the smallest multiple of n equal or greater than x
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
   546
 * @note n must be a power of 2
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
   547
 */
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
   548
#define ALIGN(x, n) (((x) + (n) - 1) & ~((n) - 1))
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
   549
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4274
diff changeset
   550
/** 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
   551
 */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4274
diff changeset
   552
#define MAX_UVALUE(type) ((type)~(type)0)
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4274
diff changeset
   553
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
#endif /* MACROS_H */