Files
@ r4158:e34ff2424478
Branch filter:
Location: cpp/openttd-patchpack/source/macros.h - annotation
r4158:e34ff2424478
5.4 KiB
text/x-c
(svn r5582) Add and use AxisToTrack{Bits,}()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | r2186:5ee653b1b5e1 r2186:5ee653b1b5e1 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r926:fcf36609eb94 r926:fcf36609eb94 r2663:a401e6cb6233 r2663:a401e6cb6233 r2663:a401e6cb6233 r2663:a401e6cb6233 r2663:a401e6cb6233 r2663:a401e6cb6233 r2663:a401e6cb6233 r193:6aa65dc5a4b4 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r500:64773fd93d93 r500:64773fd93d93 r500:64773fd93d93 r0:d63b455452f6 r500:64773fd93d93 r500:64773fd93d93 r0:d63b455452f6 r0:d63b455452f6 r1400:c43509d32c7f r1400:c43509d32c7f r1400:c43509d32c7f r1400:c43509d32c7f r1400:c43509d32c7f r1400:c43509d32c7f r0:d63b455452f6 r3352:6f8f11986666 r3352:6f8f11986666 r3352:6f8f11986666 r3352:6f8f11986666 r3352:6f8f11986666 r3352:6f8f11986666 r0:d63b455452f6 r500:64773fd93d93 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r500:64773fd93d93 r236:fa8d11d132e4 r236:fa8d11d132e4 r236:fa8d11d132e4 r500:64773fd93d93 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r500:64773fd93d93 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r3132:8b6180fa2446 r954:3b88c3fb6a01 r954:3b88c3fb6a01 r954:3b88c3fb6a01 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r3941:ca89350e55ac r3326:d3b33ffce47d r2187:60cea7c48bf4 r0:d63b455452f6 r0:d63b455452f6 r22:d13b1ad89539 r22:d13b1ad89539 r22:d13b1ad89539 r22:d13b1ad89539 r0:d63b455452f6 r22:d13b1ad89539 r22:d13b1ad89539 r22:d13b1ad89539 r0:d63b455452f6 r0:d63b455452f6 r500:64773fd93d93 r0:d63b455452f6 r1247:d01094dbcdcc r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r1247:d01094dbcdcc r1247:d01094dbcdcc r1247:d01094dbcdcc r2663:a401e6cb6233 r2663:a401e6cb6233 r1247:d01094dbcdcc r2663:a401e6cb6233 r1247:d01094dbcdcc r1247:d01094dbcdcc r0:d63b455452f6 r0:d63b455452f6 r1247:d01094dbcdcc r1247:d01094dbcdcc r2663:a401e6cb6233 r2663:a401e6cb6233 r1247:d01094dbcdcc r2663:a401e6cb6233 r1247:d01094dbcdcc r1247:d01094dbcdcc r0:d63b455452f6 r3812:1a25bd693268 r3812:1a25bd693268 r3812:1a25bd693268 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r3941:ca89350e55ac r3943:a0d88767ed03 r3941:ca89350e55ac r0:d63b455452f6 r0:d63b455452f6 r2952:e97f823d2dbe r2952:e97f823d2dbe r2952:e97f823d2dbe r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r0:d63b455452f6 r500:64773fd93d93 r0:d63b455452f6 r1884:a82518e64689 r1884:a82518e64689 r0:d63b455452f6 r500:64773fd93d93 r500:64773fd93d93 r0:d63b455452f6 r500:64773fd93d93 r500:64773fd93d93 r500:64773fd93d93 r1174:4ffa62f5467c r500:64773fd93d93 r0:d63b455452f6 r0:d63b455452f6 r2966:4803bfe02c27 r2966:4803bfe02c27 r2966:4803bfe02c27 r2966:4803bfe02c27 r0:d63b455452f6 r2966:4803bfe02c27 r2966:4803bfe02c27 r2966:4803bfe02c27 r2966:4803bfe02c27 r2966:4803bfe02c27 r2966:4803bfe02c27 r0:d63b455452f6 r2966:4803bfe02c27 r2966:4803bfe02c27 r0:d63b455452f6 r2086:0041fefc1b12 r2086:0041fefc1b12 r2086:0041fefc1b12 r2086:0041fefc1b12 r2086:0041fefc1b12 r2086:0041fefc1b12 r1852:c5d43d1bd1e7 r2398:60efe212582c r2398:60efe212582c r2398:60efe212582c r2398:60efe212582c r2398:60efe212582c r2398:60efe212582c r0:d63b455452f6 | /* $Id$ */
#ifndef MACROS_H
#define MACROS_H
#include "map.h"
/// Fetch n bits starting at bit s from x
#define GB(x, s, n) (((x) >> (s)) & ((1U << (n)) - 1))
/// Set n bits starting at bit s in x to d
#define SB(x, s, n, d) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | ((d) << (s)))
/// Add i to the n bits starting at bit s in x
#define AB(x, s, n, i) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | (((x) + ((i) << (s))) & (((1U << (n)) - 1) << (s))))
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
static inline int min(int a, int b) { if (a <= b) return a; return b; }
static inline int max(int a, int b) { if (a >= b) return a; return b; }
static inline int64 max64(int64 a, int64 b) { if (a >= b) return a; return b; }
static inline uint minu(uint a, uint b) { if (a <= b) return a; return b; }
static inline uint maxu(uint a, uint b) { if (a >= b) return a; return b; }
static inline int clamp(int a, int min, int max)
{
if (a <= min) return min;
if (a >= max) return max;
return a;
}
static inline uint clampu(uint a, uint min, uint max)
{
if (a <= min) return min;
if (a >= max) return max;
return a;
}
static inline int32 BIGMULSS(int32 a, int32 b, int shift) {
return (int32)(((int64)(a) * (int64)(b)) >> (shift));
}
static inline int64 BIGMULSS64(int64 a, int64 b, int shift) {
return ((a) * (b)) >> (shift);
}
static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift) {
return (uint32)(((uint64)(a) * (uint64)(b)) >> (shift));
}
static inline int64 BIGMULS(int32 a, int32 b) {
return (int32)(((int64)(a) * (int64)(b)));
}
/* OPT: optimized into an unsigned comparison */
//#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size))
#define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) )
#define HASBIT(x,y) (((x) & (1 << (y))) != 0)
#define SETBIT(x,y) ((x) |= (1 << (y)))
#define CLRBIT(x,y) ((x) &= ~(1 << (y)))
#define TOGGLEBIT(x,y) ((x) ^= (1 << (y)))
// checking more bits. Maybe unneccessary, but easy to use
#define HASBITS(x,y) ((x) & (y))
#define SETBITS(x,y) ((x) |= (y))
#define CLRBITS(x,y) ((x) &= ~(y))
#define GENERAL_SPRITE_COLOR(color) ( ((color) + PALETTE_RECOLOR_START) << PALETTE_SPRITE_START)
#define PLAYER_SPRITE_COLOR(owner) ( GENERAL_SPRITE_COLOR(_player_colors[owner]))
#define SPRITE_PALETTE(x) ((x) | PALETTE_MODIFIER_COLOR)
extern const byte _ffb_64[128];
/* Returns the position of the first bit that is not zero, counted from the
* left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns
* 0.
*/
#define FIND_FIRST_BIT(x) _ffb_64[(x)]
/* Returns x with the first bit that is not zero, counted from the left, set
* to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc.
*/
#define KILL_FIRST_BIT(x) _ffb_64[(x)+64]
static inline int FindFirstBit2x64(int value)
{
/*
int i = 0;
if ( (byte) value == 0) {
i += 8;
value >>= 8;
}
return i + FIND_FIRST_BIT(value & 0x3F);
Faster ( or at least cleaner ) implementation below?
*/
if (GB(value, 0, 8) == 0) {
return FIND_FIRST_BIT(GB(value, 8, 6)) + 8;
} else {
return FIND_FIRST_BIT(GB(value, 0, 6));
}
}
static inline int KillFirstBit2x64(int value)
{
if (GB(value, 0, 8) == 0) {
return KILL_FIRST_BIT(GB(value, 8, 6)) << 8;
} else {
return value & (KILL_FIRST_BIT(GB(value, 0, 6)) | 0x3F00);
}
}
/** returns true if value a has only one bit set to 1 */
#define HAS_SINGLE_BIT(a) ( ((a) & ((a) - 1)) == 0)
/* [min,max), strictly less than */
#define IS_BYTE_INSIDE(a,min,max) ((byte)((a)-(min)) < (byte)((max)-(min)))
#define IS_INT_INSIDE(a,min,max) ((uint)((a)-(min)) < (uint)((max)-(min)))
#define CHANCE16(a,b) ((uint16)Random() <= (uint16)((65536 * (a)) / (b)))
#define CHANCE16R(a,b,r) ((uint16)(r=Random()) <= (uint16)((65536 * (a)) / (b)))
#define CHANCE16I(a,b,v) ((uint16)(v) <= (uint16)((65536 * (a)) / (b)))
#define for_each_bit(_i, _b) \
for (_i = 0; _b != 0; _i++, _b >>= 1) \
if (_b & 1)
#define abs myabs
static inline int intxchg_(int *a, int b) { int t = *a; *a = b; return t; }
#define intswap(a,b) ((b) = intxchg_(&(a), (b)))
static inline int uintxchg_(uint *a, uint b) { uint t = *a; *a = b; return t; }
#define uintswap(a,b) ((b) = uintxchg_(&(a), (b)))
static inline int myabs(int a) { if (a<0) a = -a; return a; }
static inline int64 myabs64(int64 a) { if (a<0) a = -a; return a; }
static inline void swap_byte(byte *a, byte *b) { byte t = *a; *a = *b; *b = t; }
static inline void swap_uint16(uint16 *a, uint16 *b) { uint16 t = *a; *a = *b; *b = t; }
static inline void swap_int16(int16 *a, int16 *b) { int16 t = *a; *a = *b; *b = t; }
static inline void swap_int32(int32 *a, int32 *b) { int32 t = *a; *a = *b; *b = t; }
static inline void swap_tile(TileIndex *a, TileIndex *b) { TileIndex t = *a; *a = *b; *b = t; }
static inline uint16 ReadLE16Aligned(const void* x)
{
return FROM_LE16(*(const uint16*)x);
}
static inline uint16 ReadLE16Unaligned(const void* x)
{
#ifdef OTTD_ALIGNMENT
return ((const byte*)x)[0] | ((const byte*)x)[1] << 8;
#else
return FROM_LE16(*(const uint16*)x);
#endif
}
/**
* ROtate x Left/Right by n (must be >= 0)
* @note Assumes a byte has 8 bits
*/
#define ROL(x, n) ((x) << (n) | (x) >> (sizeof(x) * 8 - (n)))
#define ROR(x, n) ((x) >> (n) | (x) << (sizeof(x) * 8 - (n)))
/**
* Return the smallest multiple of n equal or greater than x
* @note n must be a power of 2
*/
#define ALIGN(x, n) (((x) + (n) - 1) & ~((n) - 1))
#endif /* MACROS_H */
|