author | KUDr |
Fri, 20 Apr 2007 19:43:06 +0000 | |
changeset 6513 | 454347ca3dfb |
parent 6491 | 00dc414c909d |
child 6957 | a9af2426bf77 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
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 | 5 |
#ifndef MACROS_H |
6 |
#define MACROS_H |
|
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 | 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 | 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 | 13 |
#define AB(x, s, n, i) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | (((x) + ((i) << (s))) & (((1U << (n)) - 1) << (s)))) |
14 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
22
diff
changeset
|
15 |
#ifdef min |
0 | 16 |
#undef min |
17 |
#endif |
|
18 |
||
19 |
#ifdef max |
|
20 |
#undef max |
|
21 |
#endif |
|
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 |
|
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
29 |
static inline int min(int a, int b) { if (a <= b) return a; return b; } |
0 | 30 |
|
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
31 |
static inline uint minu(uint a, uint b) { if (a <= b) return a; return b; } |
0 | 32 |
|
33 |
||
1400 | 34 |
static inline int clamp(int a, int min, int max) |
35 |
{ |
|
36 |
if (a <= min) return min; |
|
37 |
if (a >= max) return max; |
|
38 |
return a; |
|
39 |
} |
|
0 | 40 |
|
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
|
41 |
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
|
42 |
{ |
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
|
43 |
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
|
44 |
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
|
45 |
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
|
46 |
} |
0 | 47 |
|
6110 | 48 |
static inline int32 BIGMULSS(int32 a, int32 b, int shift) |
49 |
{ |
|
50 |
return (int32)((int64)a * (int64)b >> shift); |
|
0 | 51 |
} |
52 |
||
6110 | 53 |
static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift) |
54 |
{ |
|
55 |
return (uint32)((uint64)a * (uint64)b >> shift); |
|
0 | 56 |
} |
57 |
||
58 |
||
59 |
/* OPT: optimized into an unsigned comparison */ |
|
60 |
//#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size)) |
|
61 |
#define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) ) |
|
62 |
||
6110 | 63 |
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
|
64 |
{ |
6110 | 65 |
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
|
66 |
} |
0 | 67 |
|
6110 | 68 |
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
|
69 |
{ |
6110 | 70 |
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
|
71 |
} |
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
|
72 |
|
6110 | 73 |
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
|
74 |
{ |
6110 | 75 |
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
|
76 |
} |
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
|
77 |
|
6110 | 78 |
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
|
79 |
{ |
6110 | 80 |
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
|
81 |
} |
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
|
82 |
|
0 | 83 |
|
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
|
84 |
/* 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
|
85 |
#define HASBITS(x, y) ((x) & (y)) |
00dc414c909d
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6201
diff
changeset
|
86 |
#define SETBITS(x, y) ((x) |= (y)) |
00dc414c909d
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6201
diff
changeset
|
87 |
#define CLRBITS(x, y) ((x) &= ~(y)) |
0 | 88 |
|
5668
36b39f4a9032
(svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents:
5605
diff
changeset
|
89 |
#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
|
90 |
#define PLAYER_SPRITE_COLOR(owner) (GENERAL_SPRITE_COLOR(_player_colors[owner])) |
0 | 91 |
|
92 |
extern const byte _ffb_64[128]; |
|
22 | 93 |
/* Returns the position of the first bit that is not zero, counted from the |
94 |
* left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns |
|
95 |
* 0. |
|
96 |
*/ |
|
0 | 97 |
#define FIND_FIRST_BIT(x) _ffb_64[(x)] |
22 | 98 |
/* Returns x with the first bit that is not zero, counted from the left, set |
99 |
* to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc. |
|
100 |
*/ |
|
6491
00dc414c909d
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6201
diff
changeset
|
101 |
#define KILL_FIRST_BIT(x) _ffb_64[(x) + 64] |
0 | 102 |
|
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
103 |
static inline int FindFirstBit2x64(int value) |
0 | 104 |
{ |
1247 | 105 |
/* |
0 | 106 |
int i = 0; |
107 |
if ( (byte) value == 0) { |
|
108 |
i += 8; |
|
109 |
value >>= 8; |
|
110 |
} |
|
111 |
return i + FIND_FIRST_BIT(value & 0x3F); |
|
1247 | 112 |
|
113 |
Faster ( or at least cleaner ) implementation below? |
|
114 |
*/ |
|
2663 | 115 |
if (GB(value, 0, 8) == 0) { |
116 |
return FIND_FIRST_BIT(GB(value, 8, 6)) + 8; |
|
1247 | 117 |
} else { |
2663 | 118 |
return FIND_FIRST_BIT(GB(value, 0, 6)); |
1247 | 119 |
} |
120 |
||
0 | 121 |
} |
122 |
||
1247 | 123 |
static inline int KillFirstBit2x64(int value) |
124 |
{ |
|
2663 | 125 |
if (GB(value, 0, 8) == 0) { |
126 |
return KILL_FIRST_BIT(GB(value, 8, 6)) << 8; |
|
1247 | 127 |
} else { |
2663 | 128 |
return value & (KILL_FIRST_BIT(GB(value, 0, 6)) | 0x3F00); |
1247 | 129 |
} |
130 |
} |
|
0 | 131 |
|
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
|
132 |
/** 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
|
133 |
#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
|
134 |
|
0 | 135 |
/* [min,max), strictly less than */ |
6491
00dc414c909d
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6201
diff
changeset
|
136 |
#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
|
137 |
#define IS_INT_INSIDE(a, min, max) ((uint)((a) - (min)) < (uint)((max) - (min))) |
0 | 138 |
|
139 |
||
6491
00dc414c909d
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6201
diff
changeset
|
140 |
#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
|
141 |
#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
|
142 |
#define CHANCE16I(a, b, v) ((uint16)(v) <= (uint16)((65536 * (a)) / (b))) |
0 | 143 |
|
144 |
||
2952 | 145 |
#define for_each_bit(_i, _b) \ |
146 |
for (_i = 0; _b != 0; _i++, _b >>= 1) \ |
|
147 |
if (_b & 1) |
|
0 | 148 |
|
149 |
#define abs myabs |
|
150 |
||
151 |
||
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
|
152 |
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
|
153 |
{ |
661554d683cd
(svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents:
2952
diff
changeset
|
154 |
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
|
155 |
} |
0 | 156 |
|
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
|
157 |
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
|
158 |
{ |
661554d683cd
(svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents:
2952
diff
changeset
|
159 |
#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
|
160 |
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
|
161 |
#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
|
162 |
return FROM_LE16(*(const uint16*)x); |
0 | 163 |
#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
|
164 |
} |
661554d683cd
(svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents:
2952
diff
changeset
|
165 |
|
0 | 166 |
|
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
|
167 |
/** |
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
|
168 |
* 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
|
169 |
* @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
|
170 |
*/ |
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
|
171 |
#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
|
172 |
#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
|
173 |
|
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
|
174 |
/** |
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
|
175 |
* 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
|
176 |
* @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
|
177 |
*/ |
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
|
178 |
#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
|
179 |
|
4300
c7e43c47a2b9
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
4274
diff
changeset
|
180 |
/** 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
|
181 |
*/ |
c7e43c47a2b9
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
4274
diff
changeset
|
182 |
#define MAX_UVALUE(type) ((type)~(type)0) |
c7e43c47a2b9
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
4274
diff
changeset
|
183 |
|
0 | 184 |
#endif /* MACROS_H */ |