author | peter1138 |
Fri, 09 Jun 2006 16:35:07 +0000 | |
changeset 3997 | 29c77eab14a4 |
parent 3943 | c48d912ad44c |
child 4274 | bd088a325923 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
0 | 3 |
#ifndef MACROS_H |
4 |
#define MACROS_H |
|
5 |
||
926
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
900
diff
changeset
|
6 |
#include "map.h" |
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
900
diff
changeset
|
7 |
|
2663 | 8 |
/// Fetch n bits starting at bit s from x |
9 |
#define GB(x, s, n) (((x) >> (s)) & ((1U << (n)) - 1)) |
|
10 |
/// Set n bits starting at bit s in x to d |
|
11 |
#define SB(x, s, n, d) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | ((d) << (s))) |
|
12 |
/// Add i to the n bits starting at bit s in x |
|
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 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
23 |
static inline int min(int a, int b) { if (a <= b) return a; return b; } |
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
24 |
static inline int max(int a, int b) { if (a >= b) return a; return b; } |
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
25 |
static inline int64 max64(int64 a, int64 b) { if (a >= b) return a; return b; } |
0 | 26 |
|
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
27 |
static inline uint minu(uint a, uint b) { if (a <= b) return a; return b; } |
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
28 |
static inline uint maxu(uint a, uint b) { if (a >= b) return a; return b; } |
0 | 29 |
|
30 |
||
1400 | 31 |
static inline int clamp(int a, int min, int max) |
32 |
{ |
|
33 |
if (a <= min) return min; |
|
34 |
if (a >= max) return max; |
|
35 |
return a; |
|
36 |
} |
|
0 | 37 |
|
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
|
38 |
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
|
39 |
{ |
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 |
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
|
41 |
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
|
42 |
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
|
43 |
} |
0 | 44 |
|
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
45 |
static inline int32 BIGMULSS(int32 a, int32 b, int shift) { |
0 | 46 |
return (int32)(((int64)(a) * (int64)(b)) >> (shift)); |
47 |
} |
|
48 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
49 |
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
|
50 |
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
|
51 |
} |
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
|
52 |
|
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
53 |
static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift) { |
0 | 54 |
return (uint32)(((uint64)(a) * (uint64)(b)) >> (shift)); |
55 |
} |
|
56 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
57 |
static inline int64 BIGMULS(int32 a, int32 b) { |
0 | 58 |
return (int32)(((int64)(a) * (int64)(b))); |
59 |
} |
|
60 |
||
61 |
/* OPT: optimized into an unsigned comparison */ |
|
62 |
//#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size)) |
|
63 |
#define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) ) |
|
64 |
||
65 |
||
3132
724ede39bda9
(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
|
66 |
#define HASBIT(x,y) (((x) & (1 << (y))) != 0) |
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
947
diff
changeset
|
67 |
#define SETBIT(x,y) ((x) |= (1 << (y))) |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
947
diff
changeset
|
68 |
#define CLRBIT(x,y) ((x) &= ~(1 << (y))) |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
947
diff
changeset
|
69 |
#define TOGGLEBIT(x,y) ((x) ^= (1 << (y))) |
0 | 70 |
|
71 |
// checking more bits. Maybe unneccessary, but easy to use |
|
72 |
#define HASBITS(x,y) ((x) & (y)) |
|
73 |
#define SETBITS(x,y) ((x) |= (y)) |
|
74 |
#define CLRBITS(x,y) ((x) &= ~(y)) |
|
75 |
||
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
|
76 |
#define GENERAL_SPRITE_COLOR(color) ( ((color) + PALETTE_RECOLOR_START) << PALETTE_SPRITE_START) |
3326
b5ce3e805425
(svn r4092) CodeChange : Named sprites instead of magic numbers plus create/use helper macro/enum for recoloring scheme
belugas
parents:
3132
diff
changeset
|
77 |
#define PLAYER_SPRITE_COLOR(owner) ( GENERAL_SPRITE_COLOR(_player_colors[owner])) |
2187
2a51f8925eeb
(svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents:
2186
diff
changeset
|
78 |
#define SPRITE_PALETTE(x) ((x) | PALETTE_MODIFIER_COLOR) |
0 | 79 |
|
80 |
extern const byte _ffb_64[128]; |
|
22 | 81 |
/* Returns the position of the first bit that is not zero, counted from the |
82 |
* left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns |
|
83 |
* 0. |
|
84 |
*/ |
|
0 | 85 |
#define FIND_FIRST_BIT(x) _ffb_64[(x)] |
22 | 86 |
/* Returns x with the first bit that is not zero, counted from the left, set |
87 |
* to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc. |
|
88 |
*/ |
|
0 | 89 |
#define KILL_FIRST_BIT(x) _ffb_64[(x)+64] |
90 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
91 |
static inline int FindFirstBit2x64(int value) |
0 | 92 |
{ |
1247 | 93 |
/* |
0 | 94 |
int i = 0; |
95 |
if ( (byte) value == 0) { |
|
96 |
i += 8; |
|
97 |
value >>= 8; |
|
98 |
} |
|
99 |
return i + FIND_FIRST_BIT(value & 0x3F); |
|
1247 | 100 |
|
101 |
Faster ( or at least cleaner ) implementation below? |
|
102 |
*/ |
|
2663 | 103 |
if (GB(value, 0, 8) == 0) { |
104 |
return FIND_FIRST_BIT(GB(value, 8, 6)) + 8; |
|
1247 | 105 |
} else { |
2663 | 106 |
return FIND_FIRST_BIT(GB(value, 0, 6)); |
1247 | 107 |
} |
108 |
||
0 | 109 |
} |
110 |
||
1247 | 111 |
static inline int KillFirstBit2x64(int value) |
112 |
{ |
|
2663 | 113 |
if (GB(value, 0, 8) == 0) { |
114 |
return KILL_FIRST_BIT(GB(value, 8, 6)) << 8; |
|
1247 | 115 |
} else { |
2663 | 116 |
return value & (KILL_FIRST_BIT(GB(value, 0, 6)) | 0x3F00); |
1247 | 117 |
} |
118 |
} |
|
0 | 119 |
|
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
|
120 |
/** 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
|
121 |
#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
|
122 |
|
0 | 123 |
/* [min,max), strictly less than */ |
124 |
#define IS_BYTE_INSIDE(a,min,max) ((byte)((a)-(min)) < (byte)((max)-(min))) |
|
125 |
#define IS_INT_INSIDE(a,min,max) ((uint)((a)-(min)) < (uint)((max)-(min))) |
|
126 |
||
127 |
||
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
|
128 |
#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
|
129 |
#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
|
130 |
#define CHANCE16I(a,b,v) ((uint16)(v) <= (uint16)((65536 * (a)) / (b))) |
0 | 131 |
|
132 |
||
2952 | 133 |
#define for_each_bit(_i, _b) \ |
134 |
for (_i = 0; _b != 0; _i++, _b >>= 1) \ |
|
135 |
if (_b & 1) |
|
0 | 136 |
|
137 |
#define abs myabs |
|
138 |
||
139 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
140 |
static inline int intxchg_(int *a, int b) { int t = *a; *a = b; return t; } |
0 | 141 |
#define intswap(a,b) ((b) = intxchg_(&(a), (b))) |
1884
ae1d6213c6dd
(svn r2390) - Codechange: Fix some warnings on GCC 4.0.0
hackykid
parents:
1852
diff
changeset
|
142 |
static inline int uintxchg_(uint *a, uint b) { uint t = *a; *a = b; return t; } |
ae1d6213c6dd
(svn r2390) - Codechange: Fix some warnings on GCC 4.0.0
hackykid
parents:
1852
diff
changeset
|
143 |
#define uintswap(a,b) ((b) = uintxchg_(&(a), (b))) |
0 | 144 |
|
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
145 |
static inline int myabs(int a) { if (a<0) a = -a; return a; } |
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
146 |
static inline int64 myabs64(int64 a) { if (a<0) a = -a; return a; } |
0 | 147 |
|
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
148 |
static inline void swap_byte(byte *a, byte *b) { byte t = *a; *a = *b; *b = t; } |
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
149 |
static inline void swap_uint16(uint16 *a, uint16 *b) { uint16 t = *a; *a = *b; *b = t; } |
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
150 |
static inline void swap_int16(int16 *a, int16 *b) { int16 t = *a; *a = *b; *b = t; } |
1174
27e386195965
(svn r1676) Increase the size of TileIndex and TileIndexDiff to 32bits and adapt the save/load data and some other parts of the code to that change
tron
parents:
1035
diff
changeset
|
151 |
static inline void swap_int32(int32 *a, int32 *b) { int32 t = *a; *a = *b; *b = t; } |
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
152 |
static inline void swap_tile(TileIndex *a, TileIndex *b) { TileIndex t = *a; *a = *b; *b = t; } |
0 | 153 |
|
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 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
|
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 |
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
|
158 |
} |
0 | 159 |
|
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
|
160 |
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
|
161 |
{ |
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 |
#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
|
163 |
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
|
164 |
#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
|
165 |
return FROM_LE16(*(const uint16*)x); |
0 | 166 |
#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
|
167 |
} |
7f382cfeb93d
(svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents:
2952
diff
changeset
|
168 |
|
0 | 169 |
|
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
|
170 |
/** |
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
|
171 |
* 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
|
172 |
* @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
|
173 |
*/ |
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
|
174 |
#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
|
175 |
#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
|
176 |
|
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
|
177 |
/** |
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
|
178 |
* 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
|
179 |
* @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
|
180 |
*/ |
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
|
181 |
#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
|
182 |
|
0 | 183 |
#endif /* MACROS_H */ |