author | KUDr |
Sat, 03 Mar 2007 11:50:45 +0000 | |
branch | cpp_gui |
changeset 6281 | 2ae707873e23 |
parent 6268 | 4b5241e5dd10 |
child 6285 | 187e3ef04cc9 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
0 | 3 |
#ifndef MACROS_H |
4 |
#define MACROS_H |
|
5 |
||
2663 | 6 |
/// Fetch n bits starting at bit s from x |
7 |
#define GB(x, s, n) (((x) >> (s)) & ((1U << (n)) - 1)) |
|
8 |
/// Set n bits starting at bit s in x to d |
|
9 |
#define SB(x, s, n, d) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | ((d) << (s))) |
|
10 |
/// Add i to the n bits starting at bit s in x |
|
11 |
#define AB(x, s, n, i) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | (((x) + ((i) << (s))) & (((1U << (n)) - 1) << (s)))) |
|
12 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
22
diff
changeset
|
13 |
#ifdef min |
0 | 14 |
#undef min |
15 |
#endif |
|
16 |
||
17 |
#ifdef max |
|
18 |
#undef max |
|
19 |
#endif |
|
20 |
||
5854
da4502a497d3
(svn r8057) -Codechange: Declare the "new" max template as static line.
celestar
parents:
5853
diff
changeset
|
21 |
template <typename T> |
da4502a497d3
(svn r8057) -Codechange: Declare the "new" max template as static line.
celestar
parents:
5853
diff
changeset
|
22 |
static inline T max(T a, T b) |
da4502a497d3
(svn r8057) -Codechange: Declare the "new" max template as static line.
celestar
parents:
5853
diff
changeset
|
23 |
{ |
da4502a497d3
(svn r8057) -Codechange: Declare the "new" max template as static line.
celestar
parents:
5853
diff
changeset
|
24 |
return a >= b ? a : b; |
da4502a497d3
(svn r8057) -Codechange: Declare the "new" max template as static line.
celestar
parents:
5853
diff
changeset
|
25 |
} |
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
|
26 |
|
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
27 |
static inline int min(int a, int b) { if (a <= b) return a; return b; } |
0 | 28 |
|
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
29 |
static inline uint minu(uint a, uint b) { if (a <= b) return a; return b; } |
0 | 30 |
|
31 |
||
1400 | 32 |
static inline int clamp(int a, int min, int max) |
33 |
{ |
|
34 |
if (a <= min) return min; |
|
35 |
if (a >= max) return max; |
|
36 |
return a; |
|
37 |
} |
|
0 | 38 |
|
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
|
39 |
static inline uint clampu(uint a, uint min, uint 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
|
40 |
{ |
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
|
41 |
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
|
42 |
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
|
43 |
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
|
44 |
} |
0 | 45 |
|
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6254
diff
changeset
|
46 |
static inline int32 BIGMULSS(int32 a, int32 b, int shift) |
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6254
diff
changeset
|
47 |
{ |
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6254
diff
changeset
|
48 |
return (int32)((int64)a * (int64)b >> shift); |
0 | 49 |
} |
50 |
||
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6254
diff
changeset
|
51 |
static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift) |
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6254
diff
changeset
|
52 |
{ |
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6254
diff
changeset
|
53 |
return (uint32)((uint64)a * (uint64)b >> shift); |
0 | 54 |
} |
55 |
||
56 |
||
57 |
/* OPT: optimized into an unsigned comparison */ |
|
58 |
//#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size)) |
|
59 |
#define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) ) |
|
60 |
||
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6254
diff
changeset
|
61 |
template<typename T> static inline bool HASBIT(T x, int y) |
6254
abc6ad7c035c
(svn r8769) [cpp_gui] -Sync with trunk (r8730..r8768)
KUDr
parents:
6253
diff
changeset
|
62 |
{ |
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6254
diff
changeset
|
63 |
return (x & ((T)1 << y)) != 0; |
6254
abc6ad7c035c
(svn r8769) [cpp_gui] -Sync with trunk (r8730..r8768)
KUDr
parents:
6253
diff
changeset
|
64 |
} |
0 | 65 |
|
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6254
diff
changeset
|
66 |
template<typename T> static inline T SETBIT(T& x, int y) |
6254
abc6ad7c035c
(svn r8769) [cpp_gui] -Sync with trunk (r8730..r8768)
KUDr
parents:
6253
diff
changeset
|
67 |
{ |
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6254
diff
changeset
|
68 |
return x |= (T)1 << y; |
6254
abc6ad7c035c
(svn r8769) [cpp_gui] -Sync with trunk (r8730..r8768)
KUDr
parents:
6253
diff
changeset
|
69 |
} |
abc6ad7c035c
(svn r8769) [cpp_gui] -Sync with trunk (r8730..r8768)
KUDr
parents:
6253
diff
changeset
|
70 |
|
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6254
diff
changeset
|
71 |
template<typename T> static inline T CLRBIT(T& x, int y) |
6254
abc6ad7c035c
(svn r8769) [cpp_gui] -Sync with trunk (r8730..r8768)
KUDr
parents:
6253
diff
changeset
|
72 |
{ |
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6254
diff
changeset
|
73 |
return x &= ~((T)1 << y); |
6254
abc6ad7c035c
(svn r8769) [cpp_gui] -Sync with trunk (r8730..r8768)
KUDr
parents:
6253
diff
changeset
|
74 |
} |
abc6ad7c035c
(svn r8769) [cpp_gui] -Sync with trunk (r8730..r8768)
KUDr
parents:
6253
diff
changeset
|
75 |
|
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6254
diff
changeset
|
76 |
template<typename T> static inline T TOGGLEBIT(T& x, int y) |
6254
abc6ad7c035c
(svn r8769) [cpp_gui] -Sync with trunk (r8730..r8768)
KUDr
parents:
6253
diff
changeset
|
77 |
{ |
6268
4b5241e5dd10
(svn r8938) [cpp_gui] -Sync with trunk (r8772..r8900)
bjarni
parents:
6254
diff
changeset
|
78 |
return x ^= (T)1 << y; |
6254
abc6ad7c035c
(svn r8769) [cpp_gui] -Sync with trunk (r8730..r8768)
KUDr
parents:
6253
diff
changeset
|
79 |
} |
abc6ad7c035c
(svn r8769) [cpp_gui] -Sync with trunk (r8730..r8768)
KUDr
parents:
6253
diff
changeset
|
80 |
|
0 | 81 |
|
82 |
// checking more bits. Maybe unneccessary, but easy to use |
|
83 |
#define HASBITS(x,y) ((x) & (y)) |
|
84 |
#define SETBITS(x,y) ((x) |= (y)) |
|
85 |
#define CLRBITS(x,y) ((x) &= ~(y)) |
|
86 |
||
5919
2b58160d667d
(svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents:
5856
diff
changeset
|
87 |
#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
|
88 |
#define PLAYER_SPRITE_COLOR(owner) (GENERAL_SPRITE_COLOR(_player_colors[owner])) |
0 | 89 |
|
90 |
extern const byte _ffb_64[128]; |
|
22 | 91 |
/* Returns the position of the first bit that is not zero, counted from the |
92 |
* left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns |
|
93 |
* 0. |
|
94 |
*/ |
|
0 | 95 |
#define FIND_FIRST_BIT(x) _ffb_64[(x)] |
22 | 96 |
/* Returns x with the first bit that is not zero, counted from the left, set |
97 |
* to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc. |
|
98 |
*/ |
|
0 | 99 |
#define KILL_FIRST_BIT(x) _ffb_64[(x)+64] |
100 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
101 |
static inline int FindFirstBit2x64(int value) |
0 | 102 |
{ |
1247 | 103 |
/* |
0 | 104 |
int i = 0; |
105 |
if ( (byte) value == 0) { |
|
106 |
i += 8; |
|
107 |
value >>= 8; |
|
108 |
} |
|
109 |
return i + FIND_FIRST_BIT(value & 0x3F); |
|
1247 | 110 |
|
111 |
Faster ( or at least cleaner ) implementation below? |
|
112 |
*/ |
|
2663 | 113 |
if (GB(value, 0, 8) == 0) { |
114 |
return FIND_FIRST_BIT(GB(value, 8, 6)) + 8; |
|
1247 | 115 |
} else { |
2663 | 116 |
return FIND_FIRST_BIT(GB(value, 0, 6)); |
1247 | 117 |
} |
118 |
||
0 | 119 |
} |
120 |
||
1247 | 121 |
static inline int KillFirstBit2x64(int value) |
122 |
{ |
|
2663 | 123 |
if (GB(value, 0, 8) == 0) { |
124 |
return KILL_FIRST_BIT(GB(value, 8, 6)) << 8; |
|
1247 | 125 |
} else { |
2663 | 126 |
return value & (KILL_FIRST_BIT(GB(value, 0, 6)) | 0x3F00); |
1247 | 127 |
} |
128 |
} |
|
0 | 129 |
|
3812
d9b3041ee3d0
(svn r4822) -Feature: Station List View can now be sorted and filtered (by waiting cargo type and facilities)
celestar
parents:
3352
diff
changeset
|
130 |
/** returns true if value a has only one bit set to 1 */ |
d9b3041ee3d0
(svn r4822) -Feature: Station List View can now be sorted and filtered (by waiting cargo type and facilities)
celestar
parents:
3352
diff
changeset
|
131 |
#define HAS_SINGLE_BIT(a) ( ((a) & ((a) - 1)) == 0) |
d9b3041ee3d0
(svn r4822) -Feature: Station List View can now be sorted and filtered (by waiting cargo type and facilities)
celestar
parents:
3352
diff
changeset
|
132 |
|
0 | 133 |
/* [min,max), strictly less than */ |
134 |
#define IS_BYTE_INSIDE(a,min,max) ((byte)((a)-(min)) < (byte)((max)-(min))) |
|
135 |
#define IS_INT_INSIDE(a,min,max) ((uint)((a)-(min)) < (uint)((max)-(min))) |
|
136 |
||
137 |
||
3941
34116fc17887
(svn r5085) - Fix (FS#198): add parentheses to CHANCE16*() (and GENERAL_SPRITE_COLOR) macro parameters (thanks to ASM)
peter1138
parents:
3812
diff
changeset
|
138 |
#define CHANCE16(a,b) ((uint16)Random() <= (uint16)((65536 * (a)) / (b))) |
3943
c48d912ad44c
(svn r5088) - Add another set of parentheses, missed in r5085, somehow... (thanks ASM)
peter1138
parents:
3941
diff
changeset
|
139 |
#define CHANCE16R(a,b,r) ((uint16)(r=Random()) <= (uint16)((65536 * (a)) / (b))) |
3941
34116fc17887
(svn r5085) - Fix (FS#198): add parentheses to CHANCE16*() (and GENERAL_SPRITE_COLOR) macro parameters (thanks to ASM)
peter1138
parents:
3812
diff
changeset
|
140 |
#define CHANCE16I(a,b,v) ((uint16)(v) <= (uint16)((65536 * (a)) / (b))) |
0 | 141 |
|
142 |
||
2952 | 143 |
#define for_each_bit(_i, _b) \ |
144 |
for (_i = 0; _b != 0; _i++, _b >>= 1) \ |
|
145 |
if (_b & 1) |
|
0 | 146 |
|
147 |
#define abs myabs |
|
148 |
||
149 |
||
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
|
150 |
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
|
151 |
{ |
7f382cfeb93d
(svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents:
2952
diff
changeset
|
152 |
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
|
153 |
} |
0 | 154 |
|
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
|
155 |
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
|
156 |
{ |
7f382cfeb93d
(svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents:
2952
diff
changeset
|
157 |
#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
|
158 |
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
|
159 |
#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
|
160 |
return FROM_LE16(*(const uint16*)x); |
0 | 161 |
#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
|
162 |
} |
7f382cfeb93d
(svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents:
2952
diff
changeset
|
163 |
|
0 | 164 |
|
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
|
165 |
/** |
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
|
166 |
* ROtate x Left/Right by n (must be >= 0) |
dbe5faa270e0
(svn r2596) Add macros ROL and ROR to ROtate values Left/Right. Also shorten the parameter names for GB and SB to increase readability
tron
parents:
1981
diff
changeset
|
167 |
* @note Assumes a byte has 8 bits |
dbe5faa270e0
(svn r2596) Add macros ROL and ROR to ROtate values Left/Right. Also shorten the parameter names for GB and SB to increase readability
tron
parents:
1981
diff
changeset
|
168 |
*/ |
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
|
169 |
#define ROL(x, n) ((x) << (n) | (x) >> (sizeof(x) * 8 - (n))) |
dbe5faa270e0
(svn r2596) Add macros ROL and ROR to ROtate values Left/Right. Also shorten the parameter names for GB and SB to increase readability
tron
parents:
1981
diff
changeset
|
170 |
#define ROR(x, n) ((x) >> (n) | (x) << (sizeof(x) * 8 - (n))) |
1852
9cfa8bf9d39f
(svn r2358) Add macros for getting (GB) and setting (SB) a range of bits
tron
parents:
1556
diff
changeset
|
171 |
|
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
|
172 |
/** |
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
|
173 |
* Return the smallest multiple of n equal or greater than x |
70de6626d65f
(svn r2924) Introduce the ALIGN() macro which aligns values to multiples of a power of 2, for exact semantics see the commment in macros.h
tron
parents:
2238
diff
changeset
|
174 |
* @note n must be a power of 2 |
70de6626d65f
(svn r2924) Introduce the ALIGN() macro which aligns values to multiples of a power of 2, for exact semantics see the commment in macros.h
tron
parents:
2238
diff
changeset
|
175 |
*/ |
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
|
176 |
#define ALIGN(x, n) (((x) + (n) - 1) & ~((n) - 1)) |
70de6626d65f
(svn r2924) Introduce the ALIGN() macro which aligns values to multiples of a power of 2, for exact semantics see the commment in macros.h
tron
parents:
2238
diff
changeset
|
177 |
|
4300
687a17c9c557
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
4274
diff
changeset
|
178 |
/** 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
|
179 |
*/ |
687a17c9c557
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
4274
diff
changeset
|
180 |
#define MAX_UVALUE(type) ((type)~(type)0) |
687a17c9c557
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
4274
diff
changeset
|
181 |
|
0 | 182 |
#endif /* MACROS_H */ |