src/macros.h
author truelight
Mon, 23 Jul 2007 16:39:27 +0000
changeset 7310 eed5036fee1f
parent 6990 136a08baf0ed
child 7326 ce9dac18c218
permissions -rw-r--r--
(svn r10662) -Add: added 'V' as new shortcut for new viewport (bilbo)
-Add: added 'M' as new shortcut for smallmap (bilbo)
-Add: added '+' and '-' as shortcuts to zoom in the mainview (bilbo)
-Add: added support for other keycodes to be possible used in the future (bilbo)
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     2
6201
bee01dc45e39 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6111
diff changeset
     3
/** @file macros.h */
bee01dc45e39 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6111
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
6201
bee01dc45e39 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6111
diff changeset
     8
/* Fetch n bits starting at bit s from x */
2663
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
     9
#define GB(x, s, n) (((x) >> (s)) & ((1U << (n)) - 1))
6201
bee01dc45e39 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6111
diff changeset
    10
/* Set n bits starting at bit s in x to d */
2663
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    11
#define SB(x, s, n, d) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | ((d) << (s)))
6201
bee01dc45e39 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6111
diff changeset
    12
/* Add i to the n bits starting at bit s in x */
2663
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    13
#define AB(x, s, n, i) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | (((x) + ((i) << (s))) & (((1U << (n)) - 1) << (s))))
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    14
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 22
diff changeset
    15
#ifdef min
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#undef min
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
#ifdef max
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
#undef max
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
5603
adebdc9b413c (svn r8057) -Codechange: Declare the "new" max template as static line.
celestar
parents: 5602
diff changeset
    23
template <typename T>
adebdc9b413c (svn r8057) -Codechange: Declare the "new" max template as static line.
celestar
parents: 5602
diff changeset
    24
static inline T max(T a, T b)
adebdc9b413c (svn r8057) -Codechange: Declare the "new" max template as static line.
celestar
parents: 5602
diff changeset
    25
{
adebdc9b413c (svn r8057) -Codechange: Declare the "new" max template as static line.
celestar
parents: 5602
diff changeset
    26
	return a >= b ? a : b;
adebdc9b413c (svn r8057) -Codechange: Declare the "new" max template as static line.
celestar
parents: 5602
diff changeset
    27
}
5601
d58f82901b2f (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: 5587
diff changeset
    28
6957
a9af2426bf77 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 6491
diff changeset
    29
template <typename T>
a9af2426bf77 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 6491
diff changeset
    30
static inline T min(T a, T b)
a9af2426bf77 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 6491
diff changeset
    31
{
a9af2426bf77 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 6491
diff changeset
    32
	return a < b ? a : b;
a9af2426bf77 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 6491
diff changeset
    33
}
a9af2426bf77 (svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
rubidium
parents: 6491
diff changeset
    34
500
8e52f7797b48 (svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents: 239
diff changeset
    35
static inline int min(int a, int b) { if (a <= b) return a; return b; }
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
500
8e52f7797b48 (svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents: 239
diff changeset
    37
static inline uint minu(uint a, uint b) { if (a <= b) return a; return b; }
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
1400
09a471f04d17 (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
    40
static inline int clamp(int a, int min, int max)
09a471f04d17 (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
    41
{
09a471f04d17 (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
    42
	if (a <= min) return min;
09a471f04d17 (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
    43
	if (a >= max) return max;
09a471f04d17 (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
    44
	return a;
09a471f04d17 (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
    45
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
3352
99e5e3634e9d (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
    47
static inline uint clampu(uint a, uint min, uint max)
99e5e3634e9d (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
    48
{
99e5e3634e9d (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
    49
	if (a <= min) return min;
99e5e3634e9d (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
    50
	if (a >= max) return max;
99e5e3634e9d (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
    51
	return a;
99e5e3634e9d (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
    52
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
6990
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6957
diff changeset
    54
/* Gracefully reduce a signed 64-bit int to signed 32-bit -- no bogusly truncating the sign bit */
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6957
diff changeset
    55
static inline int32 ClampToI32(int64 a)
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6957
diff changeset
    56
{
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6957
diff changeset
    57
	if (a <= (int32)0x80000000) return 0x80000000;
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6957
diff changeset
    58
	if (a >= (int32)0x7FFFFFFF) return 0x7FFFFFFF;
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6957
diff changeset
    59
	return (int32)a;
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6957
diff changeset
    60
}
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6957
diff changeset
    61
6110
0656e15abc24 (svn r8846) -Fix
tron
parents: 6109
diff changeset
    62
static inline int32 BIGMULSS(int32 a, int32 b, int shift)
0656e15abc24 (svn r8846) -Fix
tron
parents: 6109
diff changeset
    63
{
0656e15abc24 (svn r8846) -Fix
tron
parents: 6109
diff changeset
    64
	return (int32)((int64)a * (int64)b >> shift);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    65
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    66
6110
0656e15abc24 (svn r8846) -Fix
tron
parents: 6109
diff changeset
    67
static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift)
0656e15abc24 (svn r8846) -Fix
tron
parents: 6109
diff changeset
    68
{
0656e15abc24 (svn r8846) -Fix
tron
parents: 6109
diff changeset
    69
	return (uint32)((uint64)a * (uint64)b >> shift);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    71
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
/* OPT: optimized into an unsigned comparison */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    74
//#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
#define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) )
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
6110
0656e15abc24 (svn r8846) -Fix
tron
parents: 6109
diff changeset
    77
template<typename T> static inline bool HASBIT(T x, int y)
6010
3dfeb08abf4a (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6006
diff changeset
    78
{
6110
0656e15abc24 (svn r8846) -Fix
tron
parents: 6109
diff changeset
    79
	return (x & ((T)1 << y)) != 0;
6010
3dfeb08abf4a (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6006
diff changeset
    80
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
6110
0656e15abc24 (svn r8846) -Fix
tron
parents: 6109
diff changeset
    82
template<typename T> static inline T SETBIT(T& x, int y)
6010
3dfeb08abf4a (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6006
diff changeset
    83
{
6110
0656e15abc24 (svn r8846) -Fix
tron
parents: 6109
diff changeset
    84
	return x |= (T)1 << y;
6010
3dfeb08abf4a (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6006
diff changeset
    85
}
3dfeb08abf4a (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6006
diff changeset
    86
6110
0656e15abc24 (svn r8846) -Fix
tron
parents: 6109
diff changeset
    87
template<typename T> static inline T CLRBIT(T& x, int y)
6010
3dfeb08abf4a (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6006
diff changeset
    88
{
6110
0656e15abc24 (svn r8846) -Fix
tron
parents: 6109
diff changeset
    89
	return x &= ~((T)1 << y);
6010
3dfeb08abf4a (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6006
diff changeset
    90
}
3dfeb08abf4a (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6006
diff changeset
    91
6110
0656e15abc24 (svn r8846) -Fix
tron
parents: 6109
diff changeset
    92
template<typename T> static inline T TOGGLEBIT(T& x, int y)
6010
3dfeb08abf4a (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6006
diff changeset
    93
{
6110
0656e15abc24 (svn r8846) -Fix
tron
parents: 6109
diff changeset
    94
	return x ^= (T)1 << y;
6010
3dfeb08abf4a (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6006
diff changeset
    95
}
3dfeb08abf4a (svn r8732) -Codechange/Fix(r8705): Turned the bit-handling macros into template functions. Fixes a problem with MSVC and 64-bit shifts.
celestar
parents: 6006
diff changeset
    96
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
6201
bee01dc45e39 (svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents: 6111
diff changeset
    98
/* checking more bits. Maybe unneccessary, but easy to use */
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6201
diff changeset
    99
#define HASBITS(x, y) ((x) & (y))
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6201
diff changeset
   100
#define SETBITS(x, y) ((x) |= (y))
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6201
diff changeset
   101
#define CLRBITS(x, y) ((x) &= ~(y))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   102
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5605
diff changeset
   103
#define GENERAL_SPRITE_COLOR(color) ((color) + PALETTE_RECOLOR_START)
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5605
diff changeset
   104
#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
   105
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
extern const byte _ffb_64[128];
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
   107
/* Returns the position of the first bit that is not zero, counted from the
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
   108
 * left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
   109
 * 0.
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
   110
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
#define FIND_FIRST_BIT(x) _ffb_64[(x)]
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
   112
/* Returns x with the first bit that is not zero, counted from the left, set
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
   113
 * to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc.
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
   114
 */
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6201
diff changeset
   115
#define KILL_FIRST_BIT(x) _ffb_64[(x) + 64]
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   116
500
8e52f7797b48 (svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents: 239
diff changeset
   117
static inline int FindFirstBit2x64(int value)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   118
{
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   119
/*
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
	int i = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
	if ( (byte) value == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
		i += 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
		value >>= 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
	return i + FIND_FIRST_BIT(value & 0x3F);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   126
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   127
Faster ( or at least cleaner ) implementation below?
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   128
*/
2663
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
   129
	if (GB(value, 0, 8) == 0) {
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
   130
		return FIND_FIRST_BIT(GB(value, 8, 6)) + 8;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   131
	} else {
2663
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
   132
		return FIND_FIRST_BIT(GB(value, 0, 6));
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   133
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   134
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   135
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   137
static inline int KillFirstBit2x64(int value)
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   138
{
2663
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
   139
	if (GB(value, 0, 8) == 0) {
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
   140
		return KILL_FIRST_BIT(GB(value, 8, 6)) << 8;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   141
	} else {
2663
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
   142
		return value & (KILL_FIRST_BIT(GB(value, 0, 6)) | 0x3F00);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   143
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   144
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   145
3812
e6fc916c17ab (svn r4822) -Feature: Station List View can now be sorted and filtered (by waiting cargo type and facilities)
celestar
parents: 3352
diff changeset
   146
/** returns true if value a has only one bit set to 1 */
e6fc916c17ab (svn r4822) -Feature: Station List View can now be sorted and filtered (by waiting cargo type and facilities)
celestar
parents: 3352
diff changeset
   147
#define HAS_SINGLE_BIT(a) ( ((a) & ((a) - 1)) == 0)
e6fc916c17ab (svn r4822) -Feature: Station List View can now be sorted and filtered (by waiting cargo type and facilities)
celestar
parents: 3352
diff changeset
   148
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
/* [min,max), strictly less than */
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6201
diff changeset
   150
#define IS_BYTE_INSIDE(a, min, max) ((byte)((a) - (min)) < (byte)((max) - (min)))
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6201
diff changeset
   151
#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
   152
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6201
diff changeset
   154
#define CHANCE16(a, b) ((uint16)Random() <= (uint16)((65536 * (a)) / (b)))
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6201
diff changeset
   155
#define CHANCE16R(a, b, r) ((uint16)(r = Random()) <= (uint16)((65536 * (a)) / (b)))
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6201
diff changeset
   156
#define CHANCE16I(a, b, v) ((uint16)(v) <= (uint16)((65536 * (a)) / (b)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   157
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   158
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2775
diff changeset
   159
#define for_each_bit(_i, _b)            \
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2775
diff changeset
   160
	for (_i = 0; _b != 0; _i++, _b >>= 1) \
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2775
diff changeset
   161
		if (_b & 1)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
#define abs myabs
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
2966
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   166
static inline uint16 ReadLE16Aligned(const void* x)
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   167
{
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   168
	return FROM_LE16(*(const uint16*)x);
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   169
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
2966
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   171
static inline uint16 ReadLE16Unaligned(const void* x)
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   172
{
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   173
#ifdef OTTD_ALIGNMENT
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   174
	return ((const byte*)x)[0] | ((const byte*)x)[1] << 8;
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   175
#else
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   176
	return FROM_LE16(*(const uint16*)x);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
#endif
2966
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   178
}
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   179
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
2086
5c32d85b589a (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
   181
/**
5c32d85b589a (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
   182
 * ROtate x Left/Right by n (must be >= 0)
5c32d85b589a (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
   183
 * @note Assumes a byte has 8 bits
5c32d85b589a (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
   184
 */
5c32d85b589a (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
   185
#define ROL(x, n) ((x) << (n) | (x) >> (sizeof(x) * 8 - (n)))
5c32d85b589a (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
   186
#define ROR(x, n) ((x) >> (n) | (x) << (sizeof(x) * 8 - (n)))
1852
a7f9f961c5e1 (svn r2358) Add macros for getting (GB) and setting (SB) a range of bits
tron
parents: 1556
diff changeset
   187
2398
912f16512ce2 (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
   188
/**
912f16512ce2 (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
   189
 * Return the smallest multiple of n equal or greater than x
912f16512ce2 (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
   190
 * @note n must be a power of 2
912f16512ce2 (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
   191
 */
912f16512ce2 (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
   192
#define ALIGN(x, n) (((x) + (n) - 1) & ~((n) - 1))
912f16512ce2 (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
   193
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4274
diff changeset
   194
/** return the largest value that can be entered in a variable.
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4274
diff changeset
   195
 */
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4274
diff changeset
   196
#define MAX_UVALUE(type) ((type)~(type)0)
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4274
diff changeset
   197
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   198
#endif /* MACROS_H */