src/macros.h
author skidd13
Mon, 19 Nov 2007 20:40:14 +0000
changeset 8423 8453e9a0f0b5
parent 8422 cab41009c86e
child 8424 4a488a90ccab
permissions -rw-r--r--
(svn r11480) -Codechange: Rename the function ALIGN fitting to the naming style
This fixes also FS#1450
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
 *
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    11
 * This function can be used to fetch n bits from the value x. The
7822
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
 */
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    24
template<typename T> static inline uint GB(const T x, const uint8 s, const uint8 n)
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    25
{
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    26
	return (x >> s) & ((1U << n) - 1);
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    27
}
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    28
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    29
/** 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
    30
 *
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    31
 * This function sets n bits from x which started as bit s to the value of
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    32
 * 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
    33
 * #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
    34
 * 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
    35
 * 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
    36
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    37
 * @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
    38
 * @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
    39
 *       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
    40
 * @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
    41
 * @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
    42
 * @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
    43
 * @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
    44
 * @return The new value of x
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    45
 */
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    46
template<typename T, typename U> static inline T SB(T& x, const uint8 s, const uint8 n, const U d)
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    47
{
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    48
	x &= (T)(~(((1U << n) - 1) << s));
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    49
	x |= (T)(d << s);
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    50
	return x;
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    51
}
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    52
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    53
/** 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
    54
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    55
 * 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
    56
 * 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
    57
 * 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
    58
 * bit window and is simply ignored.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    59
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    60
 * @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
    61
 * @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
    62
 * @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
    63
 * @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
    64
 * @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
    65
 * @return The new value of x
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    66
 */
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    67
template<typename T, typename U> static inline T AB(T& x, const uint8 s, const uint8 n, const U i)
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    68
{
8336
a771088f1d1e (svn r11390) -Fix r11387: AB() was wrong (spotted by Rafal Rzepecki, patch by skidd13)
truelight
parents: 8333
diff changeset
    69
	const T mask = (T)(((1U << n) - 1) << s);
a771088f1d1e (svn r11390) -Fix r11387: AB() was wrong (spotted by Rafal Rzepecki, patch by skidd13)
truelight
parents: 8333
diff changeset
    70
	x = (T)((x & ~mask) | ((x + (i << s)) & mask));
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    71
	return x;
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
    72
}
2663
f3e7d6d3e3a1 (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    73
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 22
diff changeset
    74
#ifdef min
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
#undef min
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
#ifdef max
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
#undef max
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
8419
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
    82
#ifdef abs
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
    83
#undef abs
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
    84
#endif
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
    85
7822
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
 * Returns the maximum of two values.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    88
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    89
 * 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
    90
 * 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
    91
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    92
 * @param a The first value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    93
 * @param b The second value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
    94
 * @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
    95
 */
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
    96
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
    97
{
da4502a497d3 (svn r8057) -Codechange: Declare the "new" max template as static line.
celestar
parents: 5853
diff changeset
    98
	return a >= b ? a : b;
da4502a497d3 (svn r8057) -Codechange: Declare the "new" max template as static line.
celestar
parents: 5853
diff changeset
    99
}
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
   100
7822
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
 * Returns the minimum of two values.
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
 * 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
   105
 * 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
   106
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   107
 * @param a The first value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   108
 * @param b The second value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   109
 * @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
   110
 */
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
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
   112
{
67ba4a6fc014 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 6987
diff changeset
   113
	return a < b ? a : b;
67ba4a6fc014 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 6987
diff changeset
   114
}
67ba4a6fc014 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 6987
diff changeset
   115
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   116
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   117
 * Returns the minimum of two integer.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   118
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   119
 * 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
   120
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   121
 * @param a The first integer
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   122
 * @param b The second integer
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   123
 * @return The smaller value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   124
 */
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
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
   126
{
8347
af12cf4882ed (svn r11401) -Fix [FS#1391]: make all min functions do exactly the same instead of branching on either < or <=.
rubidium
parents: 8336
diff changeset
   127
	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
   128
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   130
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   131
 * 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
   132
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   133
 * 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
   134
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   135
 * @param a The first unsigned integer
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   136
 * @param b The second unsigned integer
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   137
 * @return The smaller value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   138
 */
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
   139
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
   140
{
8347
af12cf4882ed (svn r11401) -Fix [FS#1391]: make all min functions do exactly the same instead of branching on either < or <=.
rubidium
parents: 8336
diff changeset
   141
	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
   142
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   143
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   144
/**
8419
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   145
 * Returns the absolute value of (scalar) variable.
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   146
 *
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   147
 * @note assumes variable to be signed
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   148
 * @param a The value we want to unsign
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   149
 * @return The unsigned value
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   150
 */
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   151
template <typename T> static inline T abs(T a)
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   152
{
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   153
	return (a < (T)0) ? -a : a;
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   154
}
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   155
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   156
/**
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   157
 * Clamp an integer between an interval.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   158
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   159
 * 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
   160
 * 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
   161
 * 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
   162
 * 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
   163
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   164
 * @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
   165
 *       unexpected results.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   166
 * @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
   167
 * @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
   168
 * @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
   169
 * @returns A value between min and max which is closest to a.
8418
b49fc6be1ab9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 8399
diff changeset
   170
 * @see ClampU(uint, uint, uint)
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   171
 */
8418
b49fc6be1ab9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 8399
diff changeset
   172
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
   173
{
b8709b34afce (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
   174
	if (a <= min) return min;
b8709b34afce (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
   175
	if (a >= max) return max;
b8709b34afce (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
   176
	return a;
b8709b34afce (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
   177
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   179
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   180
 * 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
   181
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   182
 * 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
   183
 * 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
   184
 * 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
   185
 * 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
   186
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   187
 * @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
   188
 *       unexpected results.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   189
 * @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
   190
 * @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
   191
 * @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
   192
 * @returns A value between min and max which is closest to a.
8418
b49fc6be1ab9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 8399
diff changeset
   193
 * @see Clamp(int, int, int)
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   194
 */
8418
b49fc6be1ab9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 8399
diff changeset
   195
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
   196
{
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
   197
	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
   198
	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
   199
	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
   200
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   202
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   203
 * 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
   204
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   205
 * 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
   206
 * 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
   207
 * 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
   208
 * 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
   209
 * 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
   210
 * 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
   211
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   212
 * @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
   213
 * @return The 64-bit value reduced to a 32-bit value
8418
b49fc6be1ab9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 8399
diff changeset
   214
 * @see Clamp(int, int, int)
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   215
 */
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
   216
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
   217
{
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   218
	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
   219
	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
   220
	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
   221
}
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7453
diff changeset
   222
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   223
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   224
 * 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
   225
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   226
 * 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
   227
 * 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
   228
 * LSB and count from 0.
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
 * @param x The value to check
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   231
 * @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
   232
 * @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
   233
 */
8323
42d835c0867c (svn r11377) -Codechange: some more strictness in macros.h (skidd13)
truelight
parents: 8315
diff changeset
   234
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
   235
{
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
   236
	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
   237
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   238
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   239
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   240
 * Set a bit in a variable.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   241
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   242
 * 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
   243
 * 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
   244
 * starts at the LSB with 0.
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
 * @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
   247
 * @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
   248
 * @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
   249
 */
8323
42d835c0867c (svn r11377) -Codechange: some more strictness in macros.h (skidd13)
truelight
parents: 8315
diff changeset
   250
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
   251
{
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
   252
	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
   253
}
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
   254
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   255
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   256
 * Clears a bit in a variable.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   257
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   258
 * 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
   259
 * 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
   260
 * 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
   261
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   262
 * @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
   263
 * @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
   264
 * @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
   265
 */
8323
42d835c0867c (svn r11377) -Codechange: some more strictness in macros.h (skidd13)
truelight
parents: 8315
diff changeset
   266
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
   267
{
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
   268
	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
   269
}
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
   270
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   271
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   272
 * Toggles a bit in a variable.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   273
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   274
 * 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
   275
 * 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
   276
 * 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
   277
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   278
 * @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
   279
 * @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
   280
 * @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
   281
 */
8323
42d835c0867c (svn r11377) -Codechange: some more strictness in macros.h (skidd13)
truelight
parents: 8315
diff changeset
   282
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
   283
{
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
   284
	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
   285
}
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
   286
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   287
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
   288
/* 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
   289
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   290
 * Check several bits in a value.
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
 * 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
   293
 * value.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   294
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   295
 * @param x The first value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   296
 * @param y The second value
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   297
 * @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
   298
 */
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6527
diff changeset
   299
#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
   300
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   301
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   302
 * Sets several bits in a variable.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   303
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   304
 * 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
   305
 * 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
   306
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   307
 * @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
   308
 * @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
   309
 * @return The new value of x
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   310
 */
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6527
diff changeset
   311
#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
   312
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   313
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   314
 * Clears several bits in a variable.
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   315
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   316
 * 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
   317
 * 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
   318
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   319
 * @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
   320
 * @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
   321
 * @return The new value of x
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   322
 */
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6527
diff changeset
   323
#define CLRBITS(x, y) ((x) &= ~(y))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   324
5919
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5856
diff changeset
   325
#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
   326
#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
   327
8115
95deea94d1f5 (svn r11149) -Fix [FS#1225]: Draw building stages for new house ground sprites.
maedhros
parents: 8012
diff changeset
   328
/**
95deea94d1f5 (svn r11149) -Fix [FS#1225]: Draw building stages for new house ground sprites.
maedhros
parents: 8012
diff changeset
   329
 * 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
   330
 * (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
   331
 *
95deea94d1f5 (svn r11149) -Fix [FS#1225]: Draw building stages for new house ground sprites.
maedhros
parents: 8012
diff changeset
   332
 * @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
   333
 * @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
   334
 */
95deea94d1f5 (svn r11149) -Fix [FS#1225]: Draw building stages for new house ground sprites.
maedhros
parents: 8012
diff changeset
   335
#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
   336
8329
77e50bd6ad67 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 8328
diff changeset
   337
extern const byte _ffb_64[64];
7822
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
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   340
 * 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
   341
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   342
 * 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
   343
 * 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
   344
 * 0.
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   345
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   346
 * @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
   347
 * @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
   348
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   349
#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
   350
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   351
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   352
 * 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
   353
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   354
 * 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
   355
 * 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
   356
 * 0x3F3F (0011111100111111) and checks only the
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   357
 * 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
   358
 * 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
   359
 * 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
   360
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   361
 * @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
   362
 * @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
   363
 * @see FIND_FIRST_BIT
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   364
 */
500
ef288590e096 (svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents: 239
diff changeset
   365
static inline int FindFirstBit2x64(int value)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   366
{
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   367
	if ((value & 0xFF) == 0) {
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   368
		return FIND_FIRST_BIT((value >> 8) & 0x3F) + 8;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   369
	} else {
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   370
		return FIND_FIRST_BIT(value & 0x3F);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   371
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   372
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   373
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   374
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   375
 * 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
   376
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   377
 * This function returns a value where the first bit (from LSB)
8329
77e50bd6ad67 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 8328
diff changeset
   378
 * is cleared.
77e50bd6ad67 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 8328
diff changeset
   379
 * So, 110100 returns 110000, 000001 returns 000000, etc.
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   380
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   381
 * @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
   382
 * @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
   383
 */
8329
77e50bd6ad67 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 8328
diff changeset
   384
template<typename T> static inline T KillFirstBit(T value)
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   385
{
8329
77e50bd6ad67 (svn r11383) -Codechange: fixed all the mess around KillFirstBit (tnx to Rubidium and skidd13)
truelight
parents: 8328
diff changeset
   386
	return value &= (T)(value - 1);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   387
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   389
/**
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
   390
 * 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
   391
 *
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
   392
 * @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
   393
 * @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
   394
 */
8328
6909973c8359 (svn r11382) -Codechange: renamed COUNTBITS to CountBits, as it is no longer a macro (skidd13)
truelight
parents: 8325
diff changeset
   395
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
   396
{
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   397
	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
   398
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
   399
	/* 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
   400
	 * 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
   401
	 * 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
   402
	 * 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
   403
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
   404
	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
   405
		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
   406
	}
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
   407
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
   408
	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
   409
}
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
   410
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
   411
/**
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   412
 * Checks if a value is between a window started at some base point.
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   413
 *
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   414
 * This function checks if the value x is between the value of base
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   415
 * and base+size. If x equals base this returns true. If x equals
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   416
 * base+size this returns false.
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   417
 *
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   418
 * @param x The value to check
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   419
 * @param base The base value of the interval
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   420
 * @param size The size of the interval
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   421
 * @return True if the value is in the interval, false else.
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   422
 */
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   423
template<typename T> static inline bool IS_INSIDE_1D(const T x, const int base, const uint size)
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   424
{
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   425
	return (uint)(x - base) < size;
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   426
}
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   427
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   428
/**
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   429
 * 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
   430
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   431
 * 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
   432
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   433
 * @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
   434
 * @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
   435
 * @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
   436
 * @see IS_INSIDE_1D
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   437
 */
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6527
diff changeset
   438
#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
   439
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   440
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   441
 * 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
   442
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   443
 * 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
   444
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   445
 * @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
   446
 * @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
   447
 * @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
   448
 * @see IS_INSIDE_1D
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   449
 */
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6527
diff changeset
   450
#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
   451
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   452
/**
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   453
 * 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
   454
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   455
 * 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
   456
 * 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
   457
 * (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
   458
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   459
 * @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
   460
 * @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
   461
 * @return True in (a/b) percent
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   462
 */
8399
1f301df8328a (svn r11454) -Fix: the CHANCE16 functions were biased; a 32768 in 65536 chance was really a 32769 in 65536 chance.
rubidium
parents: 8347
diff changeset
   463
#define CHANCE16(a, b) CHANCE16I(a, b, Random())
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
7822
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
 * 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
   467
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   468
 * 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
   469
 * 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
   470
 *
8399
1f301df8328a (svn r11454) -Fix: the CHANCE16 functions were biased; a 32768 in 65536 chance was really a 32769 in 65536 chance.
rubidium
parents: 8347
diff changeset
   471
 * The low 16 bits of r will already be used and can therefor not be passed to
1f301df8328a (svn r11454) -Fix: the CHANCE16 functions were biased; a 32768 in 65536 chance was really a 32769 in 65536 chance.
rubidium
parents: 8347
diff changeset
   472
 * CHANCE16I. One can only send the high 16 bits to CHANCE16I.
1f301df8328a (svn r11454) -Fix: the CHANCE16 functions were biased; a 32768 in 65536 chance was really a 32769 in 65536 chance.
rubidium
parents: 8347
diff changeset
   473
 *
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   474
 * @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
   475
 * @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
   476
 * @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
   477
 * @return True in (a/b) percent
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   478
 */
8399
1f301df8328a (svn r11454) -Fix: the CHANCE16 functions were biased; a 32768 in 65536 chance was really a 32769 in 65536 chance.
rubidium
parents: 8347
diff changeset
   479
#define CHANCE16R(a, b, r) CHANCE16I(a, b, r = Random())
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   480
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
 * 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
   483
 *
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   484
 * This macro is used to check if the given probability by the fraction of (a/b)
8399
1f301df8328a (svn r11454) -Fix: the CHANCE16 functions were biased; a 32768 in 65536 chance was really a 32769 in 65536 chance.
rubidium
parents: 8347
diff changeset
   485
 * is greater than low 16 bits of the given randomize-number v.
1f301df8328a (svn r11454) -Fix: the CHANCE16 functions were biased; a 32768 in 65536 chance was really a 32769 in 65536 chance.
rubidium
parents: 8347
diff changeset
   486
 *
1f301df8328a (svn r11454) -Fix: the CHANCE16 functions were biased; a 32768 in 65536 chance was really a 32769 in 65536 chance.
rubidium
parents: 8347
diff changeset
   487
 * Do not use this function twice on the same random 16 bits as it will yield
1f301df8328a (svn r11454) -Fix: the CHANCE16 functions were biased; a 32768 in 65536 chance was really a 32769 in 65536 chance.
rubidium
parents: 8347
diff changeset
   488
 * the same result. One can use a random number for two calls to CHANCE16I,
1f301df8328a (svn r11454) -Fix: the CHANCE16 functions were biased; a 32768 in 65536 chance was really a 32769 in 65536 chance.
rubidium
parents: 8347
diff changeset
   489
 * where one call sends the low 16 bits and the other the high 16 bits.
7822
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
 * @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
   492
 * @param b The denominator of the fraction, see CHANCE16
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   493
 * @param r The given randomize-number
7822
28f55acee92d (svn r10689) -Documentation [FS#1057]: lots of function documentation written by Progman.
rubidium
parents: 7486
diff changeset
   494
 * @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
   495
 */
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   496
static inline bool CHANCE16I(const uint a, const uint b, const uint32 r)
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   497
{
8399
1f301df8328a (svn r11454) -Fix: the CHANCE16 functions were biased; a 32768 in 65536 chance was really a 32769 in 65536 chance.
rubidium
parents: 8347
diff changeset
   498
	return (uint16)r < (uint16)((65536 * a) / b);
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   499
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   500
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   501
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2775
diff changeset
   502
#define for_each_bit(_i, _b)            \
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2775
diff changeset
   503
	for (_i = 0; _b != 0; _i++, _b >>= 1) \
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2775
diff changeset
   504
		if (_b & 1)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   505
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
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
   507
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
   508
{
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   509
	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
   510
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   511
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
   512
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
   513
{
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   514
#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
   515
	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
   516
#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
   517
	return FROM_LE16(*(const uint16*)x);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   518
#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
   519
}
7f382cfeb93d (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   520
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   521
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
   522
/**
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   523
 * ROtate x Left by n
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   524
 *
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
   525
 * @note Assumes a byte has 8 bits
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   526
 * @param x The value which we want to rotate
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   527
 * @param n The number how many we waht to rotate
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   528
 * @return A bit rotated number
2086
dbe5faa270e0 (svn r2596) Add macros ROL and ROR to ROtate values Left/Right. Also shorten the parameter names for GB and SB to increase readability
tron
parents: 1981
diff changeset
   529
 */
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   530
template<typename T> static inline T ROL(const T x, const uint8 n)
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   531
{
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   532
	return (T)(x << n | x >> (sizeof(x) * 8 - n));
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   533
}
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   534
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   535
/**
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   536
 * ROtate x Right by n
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   537
 *
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   538
 * @note Assumes a byte has 8 bits
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   539
 * @param x The value which we want to rotate
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   540
 * @param n The number how many we waht to rotate
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   541
 * @return A bit rotated number
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   542
 */
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   543
template<typename T> static inline T ROR(const T x, const uint8 n)
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   544
{
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   545
	return (T)(x >> n | x << (sizeof(x) * 8 - n));
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   546
}
1852
9cfa8bf9d39f (svn r2358) Add macros for getting (GB) and setting (SB) a range of bits
tron
parents: 1556
diff changeset
   547
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
   548
/**
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
 * Return the smallest multiple of n equal or greater than x
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   550
 *
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
   551
 * @note n must be a power of 2
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   552
 * @param x The min value
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   553
 * @param n The base of the number we are searching
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   554
 * @return The smallest multiple of n equal or greater than x
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
   555
 */
8423
8453e9a0f0b5 (svn r11480) -Codechange: Rename the function ALIGN fitting to the naming style
skidd13
parents: 8422
diff changeset
   556
template<typename T> static inline T Align(const T x, uint n) {
8333
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   557
	n--;
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   558
	return (T)((x + n) & ~(n));
017a513267b0 (svn r11387) -Codechange: changed many macros.h #defines to static inline functions (patch by skidd13, with a big tnx to SmatZ for profiling!)
truelight
parents: 8329
diff changeset
   559
}
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
   560
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4274
diff changeset
   561
/** 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
   562
 */
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4274
diff changeset
   563
#define MAX_UVALUE(type) ((type)~(type)0)
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4274
diff changeset
   564
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
#endif /* MACROS_H */