author | rubidium |
Thu, 01 Feb 2007 15:49:12 +0000 | |
changeset 5893 | 7e431a4abebb |
parent 5832 | 974703f615e5 |
child 6006 | 95b48e822a10 |
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 |
||
5605
f9ea8ede273f
(svn r8061) -Fix r8055: obj-c can't handle templates
bjarni
parents:
5603
diff
changeset
|
21 |
/* Objective C don't like templates */ |
f9ea8ede273f
(svn r8061) -Fix r8055: obj-c can't handle templates
bjarni
parents:
5603
diff
changeset
|
22 |
#ifdef __cplusplus |
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 |
} |
5605
f9ea8ede273f
(svn r8061) -Fix r8055: obj-c can't handle templates
bjarni
parents:
5603
diff
changeset
|
28 |
#endif |
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
|
29 |
|
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
30 |
static inline int min(int a, int b) { if (a <= b) return a; return b; } |
0 | 31 |
|
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
32 |
static inline uint minu(uint a, uint b) { if (a <= b) return a; return b; } |
0 | 33 |
|
34 |
||
1400 | 35 |
static inline int clamp(int a, int min, int max) |
36 |
{ |
|
37 |
if (a <= min) return min; |
|
38 |
if (a >= max) return max; |
|
39 |
return a; |
|
40 |
} |
|
0 | 41 |
|
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
|
42 |
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
|
43 |
{ |
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 <= 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
|
45 |
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
|
46 |
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
|
47 |
} |
0 | 48 |
|
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
49 |
static inline int32 BIGMULSS(int32 a, int32 b, int shift) { |
0 | 50 |
return (int32)(((int64)(a) * (int64)(b)) >> (shift)); |
51 |
} |
|
52 |
||
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
53 |
static inline int64 BIGMULSS64(int64 a, int64 b, int shift) { |
236
da0ae9d977e0
(svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents:
222
diff
changeset
|
54 |
return ((a) * (b)) >> (shift); |
da0ae9d977e0
(svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents:
222
diff
changeset
|
55 |
} |
da0ae9d977e0
(svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents:
222
diff
changeset
|
56 |
|
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
57 |
static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift) { |
0 | 58 |
return (uint32)(((uint64)(a) * (uint64)(b)) >> (shift)); |
59 |
} |
|
60 |
||
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
61 |
static inline int64 BIGMULS(int32 a, int32 b) { |
4274
af2ccbc3f643
(svn r5902) -Fix [FS#26]: BIGMULS return-type fix (Graphite)
Darkvater
parents:
3943
diff
changeset
|
62 |
return (int64)(a) * (int64)(b); |
0 | 63 |
} |
64 |
||
65 |
/* OPT: optimized into an unsigned comparison */ |
|
66 |
//#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size)) |
|
67 |
#define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) ) |
|
68 |
||
69 |
||
3132
4ecd7f40fc76
(svn r3747) Change HASBIT() to return 0/1 instead of 0/value of tested bit, because the name suggests it does the former and current behavior broke in some places in very subtle ways (for example HASBIT(x, 0) != HASBIT(y, 1) doesn't work, returning a bool after HASBIT(x, 9) neither)
tron
parents:
2966
diff
changeset
|
70 |
#define HASBIT(x,y) (((x) & (1 << (y))) != 0) |
954
66c89362a778
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
947
diff
changeset
|
71 |
#define SETBIT(x,y) ((x) |= (1 << (y))) |
66c89362a778
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
947
diff
changeset
|
72 |
#define CLRBIT(x,y) ((x) &= ~(1 << (y))) |
66c89362a778
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
947
diff
changeset
|
73 |
#define TOGGLEBIT(x,y) ((x) ^= (1 << (y))) |
0 | 74 |
|
75 |
// checking more bits. Maybe unneccessary, but easy to use |
|
76 |
#define HASBITS(x,y) ((x) & (y)) |
|
77 |
#define SETBITS(x,y) ((x) |= (y)) |
|
78 |
#define CLRBITS(x,y) ((x) &= ~(y)) |
|
79 |
||
5668
36b39f4a9032
(svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents:
5605
diff
changeset
|
80 |
#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
|
81 |
#define PLAYER_SPRITE_COLOR(owner) (GENERAL_SPRITE_COLOR(_player_colors[owner])) |
0 | 82 |
|
83 |
extern const byte _ffb_64[128]; |
|
22 | 84 |
/* Returns the position of the first bit that is not zero, counted from the |
85 |
* left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns |
|
86 |
* 0. |
|
87 |
*/ |
|
0 | 88 |
#define FIND_FIRST_BIT(x) _ffb_64[(x)] |
22 | 89 |
/* Returns x with the first bit that is not zero, counted from the left, set |
90 |
* to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc. |
|
91 |
*/ |
|
0 | 92 |
#define KILL_FIRST_BIT(x) _ffb_64[(x)+64] |
93 |
||
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
94 |
static inline int FindFirstBit2x64(int value) |
0 | 95 |
{ |
1247 | 96 |
/* |
0 | 97 |
int i = 0; |
98 |
if ( (byte) value == 0) { |
|
99 |
i += 8; |
|
100 |
value >>= 8; |
|
101 |
} |
|
102 |
return i + FIND_FIRST_BIT(value & 0x3F); |
|
1247 | 103 |
|
104 |
Faster ( or at least cleaner ) implementation below? |
|
105 |
*/ |
|
2663 | 106 |
if (GB(value, 0, 8) == 0) { |
107 |
return FIND_FIRST_BIT(GB(value, 8, 6)) + 8; |
|
1247 | 108 |
} else { |
2663 | 109 |
return FIND_FIRST_BIT(GB(value, 0, 6)); |
1247 | 110 |
} |
111 |
||
0 | 112 |
} |
113 |
||
1247 | 114 |
static inline int KillFirstBit2x64(int value) |
115 |
{ |
|
2663 | 116 |
if (GB(value, 0, 8) == 0) { |
117 |
return KILL_FIRST_BIT(GB(value, 8, 6)) << 8; |
|
1247 | 118 |
} else { |
2663 | 119 |
return value & (KILL_FIRST_BIT(GB(value, 0, 6)) | 0x3F00); |
1247 | 120 |
} |
121 |
} |
|
0 | 122 |
|
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
|
123 |
/** 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
|
124 |
#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
|
125 |
|
0 | 126 |
/* [min,max), strictly less than */ |
127 |
#define IS_BYTE_INSIDE(a,min,max) ((byte)((a)-(min)) < (byte)((max)-(min))) |
|
128 |
#define IS_INT_INSIDE(a,min,max) ((uint)((a)-(min)) < (uint)((max)-(min))) |
|
129 |
||
130 |
||
3941
55fa782a443c
(svn r5085) - Fix (FS#198): add parentheses to CHANCE16*() (and GENERAL_SPRITE_COLOR) macro parameters (thanks to ASM)
peter1138
parents:
3812
diff
changeset
|
131 |
#define CHANCE16(a,b) ((uint16)Random() <= (uint16)((65536 * (a)) / (b))) |
3943
d3d5f7b3d3d0
(svn r5088) - Add another set of parentheses, missed in r5085, somehow... (thanks ASM)
peter1138
parents:
3941
diff
changeset
|
132 |
#define CHANCE16R(a,b,r) ((uint16)(r=Random()) <= (uint16)((65536 * (a)) / (b))) |
3941
55fa782a443c
(svn r5085) - Fix (FS#198): add parentheses to CHANCE16*() (and GENERAL_SPRITE_COLOR) macro parameters (thanks to ASM)
peter1138
parents:
3812
diff
changeset
|
133 |
#define CHANCE16I(a,b,v) ((uint16)(v) <= (uint16)((65536 * (a)) / (b))) |
0 | 134 |
|
135 |
||
2952 | 136 |
#define for_each_bit(_i, _b) \ |
137 |
for (_i = 0; _b != 0; _i++, _b >>= 1) \ |
|
138 |
if (_b & 1) |
|
0 | 139 |
|
140 |
#define abs myabs |
|
141 |
||
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
142 |
static inline int intxchg_(int *a, int b) { int t = *a; *a = b; return t; } |
0 | 143 |
#define intswap(a,b) ((b) = intxchg_(&(a), (b))) |
1884
804ff5f94ed0
(svn r2390) - Codechange: Fix some warnings on GCC 4.0.0
hackykid
parents:
1852
diff
changeset
|
144 |
static inline int uintxchg_(uint *a, uint b) { uint t = *a; *a = b; return t; } |
804ff5f94ed0
(svn r2390) - Codechange: Fix some warnings on GCC 4.0.0
hackykid
parents:
1852
diff
changeset
|
145 |
#define uintswap(a,b) ((b) = uintxchg_(&(a), (b))) |
0 | 146 |
|
147 |
||
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
|
148 |
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
|
149 |
{ |
661554d683cd
(svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents:
2952
diff
changeset
|
150 |
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
|
151 |
} |
0 | 152 |
|
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
|
153 |
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
|
154 |
{ |
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 |
#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
|
156 |
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
|
157 |
#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
|
158 |
return FROM_LE16(*(const uint16*)x); |
0 | 159 |
#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
|
160 |
} |
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 |
|
0 | 162 |
|
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
|
163 |
/** |
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
|
164 |
* 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
|
165 |
* @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
|
166 |
*/ |
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 |
#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
|
168 |
#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
|
169 |
|
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
|
170 |
/** |
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
|
171 |
* 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
|
172 |
* @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
|
173 |
*/ |
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 |
#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
|
175 |
|
4300
c7e43c47a2b9
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
4274
diff
changeset
|
176 |
/** 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
|
177 |
* known to work for uint32. |
c7e43c47a2b9
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
4274
diff
changeset
|
178 |
* used by TGP to set the max value of the _patches.generation_seed in its definition |
c7e43c47a2b9
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
4274
diff
changeset
|
179 |
*/ |
c7e43c47a2b9
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
4274
diff
changeset
|
180 |
#define MAX_UVALUE(type) ((type)~(type)0) |
c7e43c47a2b9
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents:
4274
diff
changeset
|
181 |
|
0 | 182 |
#endif /* MACROS_H */ |