author | tron |
Sun, 17 Jul 2005 20:14:58 +0000 | |
changeset 2116 | 23031555ff54 |
parent 2086 | 5c32d85b589a |
child 2159 | f6284cf5fab0 |
permissions | -rw-r--r-- |
0 | 1 |
#ifndef MACROS_H |
2 |
#define MACROS_H |
|
3 |
||
926
a6d140a6a4de
(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" |
a6d140a6a4de
(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
8e52f7797b48
(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; } |
8e52f7797b48
(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; } |
8e52f7797b48
(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
8e52f7797b48
(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; } |
8e52f7797b48
(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 |
||
1400 | 22 |
static inline int clamp(int a, int min, int max) |
23 |
{ |
|
24 |
if (a <= min) return min; |
|
25 |
if (a >= max) return max; |
|
26 |
return a; |
|
27 |
} |
|
0 | 28 |
|
29 |
||
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
30 |
static inline int32 BIGMULSS(int32 a, int32 b, int shift) { |
0 | 31 |
return (int32)(((int64)(a) * (int64)(b)) >> (shift)); |
32 |
} |
|
33 |
||
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
34 |
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
|
35 |
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
|
36 |
} |
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 |
|
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
38 |
static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift) { |
0 | 39 |
return (uint32)(((uint64)(a) * (uint64)(b)) >> (shift)); |
40 |
} |
|
41 |
||
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
42 |
static inline int64 BIGMULS(int32 a, int32 b) { |
0 | 43 |
return (int32)(((int64)(a) * (int64)(b))); |
44 |
} |
|
45 |
||
46 |
/* OPT: optimized into an unsigned comparison */ |
|
47 |
//#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size)) |
|
48 |
#define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) ) |
|
49 |
||
50 |
||
954
66c89362a778
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
947
diff
changeset
|
51 |
#define HASBIT(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
|
52 |
#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
|
53 |
#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
|
54 |
#define TOGGLEBIT(x,y) ((x) ^= (1 << (y))) |
0 | 55 |
|
56 |
// checking more bits. Maybe unneccessary, but easy to use |
|
57 |
#define HASBITS(x,y) ((x) & (y)) |
|
58 |
#define SETBITS(x,y) ((x) |= (y)) |
|
59 |
#define CLRBITS(x,y) ((x) &= ~(y)) |
|
60 |
||
61 |
#define PLAYER_SPRITE_COLOR(owner) ((_player_colors[owner] << 16) + 0x3070000) |
|
62 |
#define SPRITE_PALETTE(x) ((x) + 0x8000) |
|
63 |
||
64 |
extern const byte _ffb_64[128]; |
|
22 | 65 |
/* Returns the position of the first bit that is not zero, counted from the |
66 |
* left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns |
|
67 |
* 0. |
|
68 |
*/ |
|
0 | 69 |
#define FIND_FIRST_BIT(x) _ffb_64[(x)] |
22 | 70 |
/* Returns x with the first bit that is not zero, counted from the left, set |
71 |
* to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc. |
|
72 |
*/ |
|
0 | 73 |
#define KILL_FIRST_BIT(x) _ffb_64[(x)+64] |
74 |
||
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
75 |
static inline int FindFirstBit2x64(int value) |
0 | 76 |
{ |
1247 | 77 |
/* |
0 | 78 |
int i = 0; |
79 |
if ( (byte) value == 0) { |
|
80 |
i += 8; |
|
81 |
value >>= 8; |
|
82 |
} |
|
83 |
return i + FIND_FIRST_BIT(value & 0x3F); |
|
1247 | 84 |
|
85 |
Faster ( or at least cleaner ) implementation below? |
|
86 |
*/ |
|
87 |
if ( (byte) value == 0) { |
|
88 |
return FIND_FIRST_BIT((value >> 8) & 0x3F) + 8; |
|
89 |
} else { |
|
90 |
return FIND_FIRST_BIT(value & 0x3F); |
|
91 |
} |
|
92 |
||
0 | 93 |
} |
94 |
||
1247 | 95 |
static inline int KillFirstBit2x64(int value) |
96 |
{ |
|
97 |
if ( (byte) value == 0) { |
|
98 |
return KILL_FIRST_BIT((value >> 8) & 0x3F) << 8; |
|
99 |
} else { |
|
100 |
return value & (KILL_FIRST_BIT(value & 0x3F)|0x3F00); |
|
101 |
} |
|
102 |
} |
|
0 | 103 |
|
104 |
/* [min,max), strictly less than */ |
|
105 |
#define IS_BYTE_INSIDE(a,min,max) ((byte)((a)-(min)) < (byte)((max)-(min))) |
|
106 |
#define IS_INT_INSIDE(a,min,max) ((uint)((a)-(min)) < (uint)((max)-(min))) |
|
107 |
||
108 |
||
109 |
#define CHANCE16(a,b) ((uint16)Random() <= (uint16)((65536 * a) / b)) |
|
110 |
#define CHANCE16R(a,b,r) ((uint16)(r=Random()) <= (uint16)((65536 * a) / b)) |
|
111 |
#define CHANCE16I(a,b,v) ((uint16)(v) <= (uint16)((65536 * a) / b)) |
|
112 |
||
113 |
#define BEGIN_TILE_LOOP(var,w,h,tile) \ |
|
114 |
{int h_cur = h; \ |
|
115 |
uint var = tile; \ |
|
116 |
do { \ |
|
117 |
int w_cur = w; \ |
|
118 |
do { |
|
119 |
||
120 |
#define END_TILE_LOOP(var,w,h,tile) \ |
|
121 |
} while (++var, --w_cur != 0); \ |
|
1981 | 122 |
} while (var += TileDiffXY(0, 1) - (w), --h_cur != 0);} |
0 | 123 |
|
124 |
||
125 |
#define for_each_bit(_i,_b) \ |
|
126 |
for(_i=0; _b!=0; _i++,_b>>=1) \ |
|
127 |
if (_b&1) |
|
128 |
||
129 |
#define abs myabs |
|
130 |
||
131 |
||
500
8e52f7797b48
(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 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
|
134 |
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
|
135 |
#define uintswap(a,b) ((b) = uintxchg_(&(a), (b))) |
0 | 136 |
|
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
137 |
static inline int myabs(int a) { if (a<0) a = -a; return a; } |
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
138 |
static inline int64 myabs64(int64 a) { if (a<0) a = -a; return a; } |
0 | 139 |
|
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
140 |
static inline void swap_byte(byte *a, byte *b) { byte t = *a; *a = *b; *b = t; } |
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
141 |
static inline void swap_uint16(uint16 *a, uint16 *b) { uint16 t = *a; *a = *b; *b = t; } |
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
142 |
static inline void swap_int16(int16 *a, int16 *b) { int16 t = *a; *a = *b; *b = t; } |
1174
6a5e747f3ba6
(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
|
143 |
static inline void swap_int32(int32 *a, int32 *b) { int32 t = *a; *a = *b; *b = t; } |
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
144 |
static inline void swap_tile(TileIndex *a, TileIndex *b) { TileIndex t = *a; *a = *b; *b = t; } |
0 | 145 |
|
146 |
||
147 |
||
148 |
#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
|
149 |
# define READ_LE_UINT16(b) (*(const uint16*)(b)) |
0 | 150 |
#elif defined(TTD_BIG_ENDIAN) |
500
8e52f7797b48
(svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents:
239
diff
changeset
|
151 |
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
|
152 |
return ((const byte*)b)[0] + (((const byte*)b)[1] << 8); |
0 | 153 |
} |
154 |
#endif |
|
155 |
||
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
|
156 |
/// Fetch n bits starting at bit s from x |
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
|
157 |
#define GB(x, s, n) (((x) >> (s)) & ((1 << (n)) - 1)) |
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
|
158 |
/// Set n bits in x starting at bit s to d |
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
|
159 |
#define SB(x, s, n, d) ((x) = ((x) & ~(((1 << (n)) - 1) << (s))) | ((d) << (s))) |
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
|
160 |
|
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
|
161 |
/** |
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
|
162 |
* 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
|
163 |
* @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
|
164 |
*/ |
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 |
#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
|
166 |
#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
|
167 |
|
0 | 168 |
#endif /* MACROS_H */ |