author | celestar |
Sun, 24 Jul 2005 15:56:31 +0000 | |
changeset 2187 | 2a51f8925eeb |
parent 2186 | 461a2aff3486 |
child 2238 | 14ec09c1a272 |
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 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
22
diff
changeset
|
8 |
#ifdef min |
0 | 9 |
#undef min |
10 |
#endif |
|
11 |
||
12 |
#ifdef max |
|
13 |
#undef max |
|
14 |
#endif |
|
15 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
16 |
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
|
17 |
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
|
18 |
static inline int64 max64(int64 a, int64 b) { if (a >= b) return a; return b; } |
0 | 19 |
|
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
20 |
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
|
21 |
static inline uint maxu(uint a, uint b) { if (a >= b) return a; return b; } |
0 | 22 |
|
23 |
||
1400 | 24 |
static inline int clamp(int a, int min, int max) |
25 |
{ |
|
26 |
if (a <= min) return min; |
|
27 |
if (a >= max) return max; |
|
28 |
return a; |
|
29 |
} |
|
0 | 30 |
|
31 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
32 |
static inline int32 BIGMULSS(int32 a, int32 b, int shift) { |
0 | 33 |
return (int32)(((int64)(a) * (int64)(b)) >> (shift)); |
34 |
} |
|
35 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
36 |
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
|
37 |
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
|
38 |
} |
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
|
39 |
|
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
40 |
static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift) { |
0 | 41 |
return (uint32)(((uint64)(a) * (uint64)(b)) >> (shift)); |
42 |
} |
|
43 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
44 |
static inline int64 BIGMULS(int32 a, int32 b) { |
0 | 45 |
return (int32)(((int64)(a) * (int64)(b))); |
46 |
} |
|
47 |
||
48 |
/* OPT: optimized into an unsigned comparison */ |
|
49 |
//#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size)) |
|
50 |
#define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) ) |
|
51 |
||
52 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
947
diff
changeset
|
53 |
#define HASBIT(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
|
54 |
#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
|
55 |
#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
|
56 |
#define TOGGLEBIT(x,y) ((x) ^= (1 << (y))) |
0 | 57 |
|
58 |
// checking more bits. Maybe unneccessary, but easy to use |
|
59 |
#define HASBITS(x,y) ((x) & (y)) |
|
60 |
#define SETBITS(x,y) ((x) |= (y)) |
|
61 |
#define CLRBITS(x,y) ((x) &= ~(y)) |
|
62 |
||
2187
2a51f8925eeb
(svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents:
2186
diff
changeset
|
63 |
#define PLAYER_SPRITE_COLOR(owner) ( (_player_colors[owner] + 0x307) << PALETTE_SPRITE_START) |
2a51f8925eeb
(svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents:
2186
diff
changeset
|
64 |
#define SPRITE_PALETTE(x) ((x) | PALETTE_MODIFIER_COLOR) |
0 | 65 |
|
66 |
extern const byte _ffb_64[128]; |
|
22 | 67 |
/* Returns the position of the first bit that is not zero, counted from the |
68 |
* left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns |
|
69 |
* 0. |
|
70 |
*/ |
|
0 | 71 |
#define FIND_FIRST_BIT(x) _ffb_64[(x)] |
22 | 72 |
/* Returns x with the first bit that is not zero, counted from the left, set |
73 |
* to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc. |
|
74 |
*/ |
|
0 | 75 |
#define KILL_FIRST_BIT(x) _ffb_64[(x)+64] |
76 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
77 |
static inline int FindFirstBit2x64(int value) |
0 | 78 |
{ |
1247 | 79 |
/* |
0 | 80 |
int i = 0; |
81 |
if ( (byte) value == 0) { |
|
82 |
i += 8; |
|
83 |
value >>= 8; |
|
84 |
} |
|
85 |
return i + FIND_FIRST_BIT(value & 0x3F); |
|
1247 | 86 |
|
87 |
Faster ( or at least cleaner ) implementation below? |
|
88 |
*/ |
|
89 |
if ( (byte) value == 0) { |
|
90 |
return FIND_FIRST_BIT((value >> 8) & 0x3F) + 8; |
|
91 |
} else { |
|
92 |
return FIND_FIRST_BIT(value & 0x3F); |
|
93 |
} |
|
94 |
||
0 | 95 |
} |
96 |
||
1247 | 97 |
static inline int KillFirstBit2x64(int value) |
98 |
{ |
|
99 |
if ( (byte) value == 0) { |
|
100 |
return KILL_FIRST_BIT((value >> 8) & 0x3F) << 8; |
|
101 |
} else { |
|
102 |
return value & (KILL_FIRST_BIT(value & 0x3F)|0x3F00); |
|
103 |
} |
|
104 |
} |
|
0 | 105 |
|
106 |
/* [min,max), strictly less than */ |
|
107 |
#define IS_BYTE_INSIDE(a,min,max) ((byte)((a)-(min)) < (byte)((max)-(min))) |
|
108 |
#define IS_INT_INSIDE(a,min,max) ((uint)((a)-(min)) < (uint)((max)-(min))) |
|
109 |
||
110 |
||
111 |
#define CHANCE16(a,b) ((uint16)Random() <= (uint16)((65536 * a) / b)) |
|
112 |
#define CHANCE16R(a,b,r) ((uint16)(r=Random()) <= (uint16)((65536 * a) / b)) |
|
113 |
#define CHANCE16I(a,b,v) ((uint16)(v) <= (uint16)((65536 * a) / b)) |
|
114 |
||
115 |
||
116 |
#define for_each_bit(_i,_b) \ |
|
117 |
for(_i=0; _b!=0; _i++,_b>>=1) \ |
|
118 |
if (_b&1) |
|
119 |
||
120 |
#define abs myabs |
|
121 |
||
122 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
123 |
static inline int intxchg_(int *a, int b) { int t = *a; *a = b; return t; } |
0 | 124 |
#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
|
125 |
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
|
126 |
#define uintswap(a,b) ((b) = uintxchg_(&(a), (b))) |
0 | 127 |
|
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
128 |
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
|
129 |
static inline int64 myabs64(int64 a) { if (a<0) a = -a; return a; } |
0 | 130 |
|
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
131 |
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
|
132 |
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
|
133 |
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
|
134 |
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
|
135 |
static inline void swap_tile(TileIndex *a, TileIndex *b) { TileIndex t = *a; *a = *b; *b = t; } |
0 | 136 |
|
137 |
||
138 |
||
139 |
#if defined(TTD_LITTLE_ENDIAN) |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
193
diff
changeset
|
140 |
# define READ_LE_UINT16(b) (*(const uint16*)(b)) |
0 | 141 |
#elif defined(TTD_BIG_ENDIAN) |
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
142 |
static inline uint16 READ_LE_UINT16(const void *b) { |
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
193
diff
changeset
|
143 |
return ((const byte*)b)[0] + (((const byte*)b)[1] << 8); |
0 | 144 |
} |
145 |
#endif |
|
146 |
||
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
|
147 |
/// Fetch n bits starting at bit s from x |
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
|
148 |
#define GB(x, s, n) (((x) >> (s)) & ((1 << (n)) - 1)) |
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
|
149 |
/// Set n bits in x starting at bit s to d |
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
|
150 |
#define SB(x, s, n, d) ((x) = ((x) & ~(((1 << (n)) - 1) << (s))) | ((d) << (s))) |
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
|
151 |
|
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
|
152 |
/** |
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
|
153 |
* 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
|
154 |
* @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
|
155 |
*/ |
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
|
156 |
#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
|
157 |
#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
|
158 |
|
2159
3b634157c3b2
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2086
diff
changeset
|
159 |
/* IS_INT_INSIDE = filter for ascii-function codes like BELL and so on [we need an special filter here later] */ |
3b634157c3b2
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2086
diff
changeset
|
160 |
static inline bool IsValidAsciiChar(byte key) |
3b634157c3b2
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2086
diff
changeset
|
161 |
{ |
3b634157c3b2
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2086
diff
changeset
|
162 |
return IS_INT_INSIDE(key, ' ', 256); |
3b634157c3b2
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2086
diff
changeset
|
163 |
} |
3b634157c3b2
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2086
diff
changeset
|
164 |
|
0 | 165 |
#endif /* MACROS_H */ |