author | tron |
Tue, 22 Feb 2005 12:48:03 +0000 | |
changeset 1394 | 449e84bdc04e |
parent 1305 | ee656cd36baa |
child 1400 | b8709b34afce |
permissions | -rw-r--r-- |
0 | 1 |
#ifndef MACROS_H |
2 |
#define MACROS_H |
|
3 |
||
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
|
4 |
#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
|
5 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
22
diff
changeset
|
6 |
#ifdef min |
0 | 7 |
#undef min |
8 |
#endif |
|
9 |
||
10 |
#ifdef max |
|
11 |
#undef max |
|
12 |
#endif |
|
13 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
14 |
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
|
15 |
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
|
16 |
static inline int64 max64(int64 a, int64 b) { if (a >= b) return a; return b; } |
0 | 17 |
|
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
18 |
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
|
19 |
static inline uint maxu(uint a, uint b) { if (a >= b) return a; return b; } |
0 | 20 |
|
21 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
22 |
static inline int clamp(int a, int min, int max) { if (a <= min) return min; if (a >= max) return max; return a; } |
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
23 |
static inline int clamp2(int a, int min, int max) { if (a <= min) a=min; if (a >= max) a=max; return a; } |
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
24 |
static inline bool int32_add_overflow(int32 a, int32 b) { return (int32)(a^b)>=0 && (int32)(a^(a+b))<0; } |
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
25 |
static inline bool int32_sub_overflow(int32 a, int32 b) { return (int32)(a^b)<0 && (int32)(a^(a-b))<0; } |
0 | 26 |
|
27 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
28 |
static inline int32 BIGMULSS(int32 a, int32 b, int shift) { |
0 | 29 |
return (int32)(((int64)(a) * (int64)(b)) >> (shift)); |
30 |
} |
|
31 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
32 |
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
|
33 |
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
|
34 |
} |
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
|
35 |
|
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
36 |
static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift) { |
0 | 37 |
return (uint32)(((uint64)(a) * (uint64)(b)) >> (shift)); |
38 |
} |
|
39 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
40 |
static inline int64 BIGMULS(int32 a, int32 b) { |
0 | 41 |
return (int32)(((int64)(a) * (int64)(b))); |
42 |
} |
|
43 |
||
44 |
/* OPT: optimized into an unsigned comparison */ |
|
45 |
//#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size)) |
|
46 |
#define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) ) |
|
47 |
||
48 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
947
diff
changeset
|
49 |
#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
|
50 |
#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
|
51 |
#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
|
52 |
#define TOGGLEBIT(x,y) ((x) ^= (1 << (y))) |
0 | 53 |
|
54 |
// checking more bits. Maybe unneccessary, but easy to use |
|
55 |
#define HASBITS(x,y) ((x) & (y)) |
|
56 |
#define SETBITS(x,y) ((x) |= (y)) |
|
57 |
#define CLRBITS(x,y) ((x) &= ~(y)) |
|
58 |
||
59 |
#define PLAYER_SPRITE_COLOR(owner) ((_player_colors[owner] << 16) + 0x3070000) |
|
60 |
#define SPRITE_PALETTE(x) ((x) + 0x8000) |
|
61 |
||
62 |
extern const byte _ffb_64[128]; |
|
22 | 63 |
/* Returns the position of the first bit that is not zero, counted from the |
64 |
* left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns |
|
65 |
* 0. |
|
66 |
*/ |
|
0 | 67 |
#define FIND_FIRST_BIT(x) _ffb_64[(x)] |
22 | 68 |
/* Returns x with the first bit that is not zero, counted from the left, set |
69 |
* to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc. |
|
70 |
*/ |
|
0 | 71 |
#define KILL_FIRST_BIT(x) _ffb_64[(x)+64] |
72 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
73 |
static inline int FindFirstBit2x64(int value) |
0 | 74 |
{ |
1247 | 75 |
/* |
0 | 76 |
int i = 0; |
77 |
if ( (byte) value == 0) { |
|
78 |
i += 8; |
|
79 |
value >>= 8; |
|
80 |
} |
|
81 |
return i + FIND_FIRST_BIT(value & 0x3F); |
|
1247 | 82 |
|
83 |
Faster ( or at least cleaner ) implementation below? |
|
84 |
*/ |
|
85 |
if ( (byte) value == 0) { |
|
86 |
return FIND_FIRST_BIT((value >> 8) & 0x3F) + 8; |
|
87 |
} else { |
|
88 |
return FIND_FIRST_BIT(value & 0x3F); |
|
89 |
} |
|
90 |
||
0 | 91 |
} |
92 |
||
1247 | 93 |
static inline int KillFirstBit2x64(int value) |
94 |
{ |
|
95 |
if ( (byte) value == 0) { |
|
96 |
return KILL_FIRST_BIT((value >> 8) & 0x3F) << 8; |
|
97 |
} else { |
|
98 |
return value & (KILL_FIRST_BIT(value & 0x3F)|0x3F00); |
|
99 |
} |
|
100 |
} |
|
0 | 101 |
|
102 |
/* [min,max), strictly less than */ |
|
103 |
#define IS_BYTE_INSIDE(a,min,max) ((byte)((a)-(min)) < (byte)((max)-(min))) |
|
104 |
#define IS_INT_INSIDE(a,min,max) ((uint)((a)-(min)) < (uint)((max)-(min))) |
|
105 |
||
106 |
||
107 |
#define CHANCE16(a,b) ((uint16)Random() <= (uint16)((65536 * a) / b)) |
|
108 |
#define CHANCE16R(a,b,r) ((uint16)(r=Random()) <= (uint16)((65536 * a) / b)) |
|
109 |
#define CHANCE16I(a,b,v) ((uint16)(v) <= (uint16)((65536 * a) / b)) |
|
110 |
||
111 |
#define BEGIN_TILE_LOOP(var,w,h,tile) \ |
|
112 |
{int h_cur = h; \ |
|
113 |
uint var = tile; \ |
|
114 |
do { \ |
|
115 |
int w_cur = w; \ |
|
116 |
do { |
|
117 |
||
118 |
#define END_TILE_LOOP(var,w,h,tile) \ |
|
119 |
} while (++var, --w_cur != 0); \ |
|
120 |
} while (var += TILE_XY(0,1) - (w), --h_cur != 0);} |
|
121 |
||
122 |
||
123 |
#define for_each_bit(_i,_b) \ |
|
124 |
for(_i=0; _b!=0; _i++,_b>>=1) \ |
|
125 |
if (_b&1) |
|
126 |
||
127 |
#define assert_array(i,j) assert(i < lengthof(j)) |
|
128 |
||
129 |
#define abs myabs |
|
130 |
||
131 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
132 |
static inline int intxchg_(int *a, int b) { int t = *a; *a = b; return t; } |
0 | 133 |
#define intxchg(a,b) intxchg_(&(a), (b)) |
134 |
#define intswap(a,b) ((b) = intxchg_(&(a), (b))) |
|
135 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
136 |
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
|
137 |
static inline int64 myabs64(int64 a) { if (a<0) a = -a; return a; } |
0 | 138 |
|
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
139 |
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
|
140 |
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
|
141 |
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
|
142 |
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
|
143 |
static inline void swap_tile(TileIndex *a, TileIndex *b) { TileIndex t = *a; *a = *b; *b = t; } |
0 | 144 |
|
145 |
||
146 |
||
147 |
#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
|
148 |
# define READ_LE_UINT16(b) (*(const uint16*)(b)) |
0 | 149 |
# define ADD_WORD(x) (x)&0xFF, ((x) >> 8)&0xFF |
150 |
# define ADD_DWORD(x) (x)&0xFF, ((x) >> 8)&0xFF, ((x) >> 16)&0xFF, ((x) >> 24)&0xFF |
|
151 |
#elif defined(TTD_BIG_ENDIAN) |
|
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
152 |
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
|
153 |
return ((const byte*)b)[0] + (((const byte*)b)[1] << 8); |
0 | 154 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
22
diff
changeset
|
155 |
# define ADD_WORD(x) ((x) >> 8)&0xFF, (x)&0xFF |
0 | 156 |
# define ADD_DWORD(x) ((x) >> 24)&0xFF, ((x) >> 16)&0xFF, ((x) >> 8)&0xFF, (x)&0xFF |
157 |
#endif |
|
158 |
||
500
ef288590e096
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
159 |
static inline void WRITE_LE_UINT16(void *b, uint16 x) { |
0 | 160 |
((byte*)b)[0] = (byte)x; |
161 |
((byte*)b)[1] = (byte)(x >> 8); |
|
162 |
} |
|
163 |
||
164 |
#endif /* MACROS_H */ |