macros.h
author Darkvater
Mon, 20 Feb 2006 19:42:39 +0000
changeset 3046 baa216f9911a
parent 2966 661554d683cd
child 3132 4ecd7f40fc76
permissions -rw-r--r--
(svn r3626) - Merge the SlGlobVarList (global variables) and SaveLoad (offset in struct, variable determined runtime) structs. The only difference between these two is the last element that either holds the address or the offset in the struct. Which one to take is determined by which function is called; SlObject or SlGlobList.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2159
diff changeset
     2
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#ifndef MACROS_H
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     4
#define MACROS_H
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
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
     6
#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
     7
2663
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
     8
/// Fetch n bits starting at bit s from x
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
     9
#define GB(x, s, n) (((x) >> (s)) & ((1U << (n)) - 1))
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    10
/// Set n bits starting at bit s in x to d
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    11
#define SB(x, s, n, d) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | ((d) << (s)))
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    12
/// Add i to the n bits starting at bit s in x
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    13
#define AB(x, s, n, i) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | (((x) + ((i) << (s))) & (((1U << (n)) - 1) << (s))))
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    14
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 22
diff changeset
    15
#ifdef min
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#undef min
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
#ifdef max
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
#undef max
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
500
8e52f7797b48 (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; }
8e52f7797b48 (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; }
8e52f7797b48 (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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    26
500
8e52f7797b48 (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; }
8e52f7797b48 (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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    29
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    30
1400
09a471f04d17 (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
    31
static inline int clamp(int a, int min, int max)
09a471f04d17 (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
    32
{
09a471f04d17 (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
    33
	if (a <= min) return min;
09a471f04d17 (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
    34
	if (a >= max) return max;
09a471f04d17 (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
    35
	return a;
09a471f04d17 (svn r1904) Remove some more unused stuff
tron
parents: 1394
diff changeset
    36
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
500
8e52f7797b48 (svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents: 239
diff changeset
    39
static inline int32 BIGMULSS(int32 a, int32 b, int shift) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
	return (int32)(((int64)(a) * (int64)(b)) >> (shift));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
500
8e52f7797b48 (svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents: 239
diff changeset
    43
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
    44
	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
    45
}
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
    46
500
8e52f7797b48 (svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents: 239
diff changeset
    47
static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
	return (uint32)(((uint64)(a) * (uint64)(b)) >> (shift));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
500
8e52f7797b48 (svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents: 239
diff changeset
    51
static inline int64 BIGMULS(int32 a, int32 b) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
	return (int32)(((int64)(a) * (int64)(b)));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
/* OPT: optimized into an unsigned comparison */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
//#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    57
#define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) )
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 947
diff changeset
    60
#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
    61
#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
    62
#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
    63
#define TOGGLEBIT(x,y) ((x) ^=  (1 << (y)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    64
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    65
// checking more bits. Maybe unneccessary, but easy to use
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    66
#define HASBITS(x,y) ((x) & (y))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
#define SETBITS(x,y) ((x) |= (y))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
#define CLRBITS(x,y) ((x) &= ~(y))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
2187
a0e206ce9fbf (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    70
#define PLAYER_SPRITE_COLOR(owner) ( (_player_colors[owner] + 0x307) << PALETTE_SPRITE_START)
a0e206ce9fbf (svn r2702) -Codechange: Cleaned up the sprite code and replaced many magic numbers
celestar
parents: 2186
diff changeset
    71
#define SPRITE_PALETTE(x) ((x) | PALETTE_MODIFIER_COLOR)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
extern const byte _ffb_64[128];
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
    74
/* Returns the position of the first bit that is not zero, counted from the
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
    75
 * left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
    76
 * 0.
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
    77
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
#define FIND_FIRST_BIT(x) _ffb_64[(x)]
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
    79
/* Returns x with the first bit that is not zero, counted from the left, set
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
    80
 * to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc.
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 0
diff changeset
    81
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    82
#define KILL_FIRST_BIT(x) _ffb_64[(x)+64]
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    83
500
8e52f7797b48 (svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents: 239
diff changeset
    84
static inline int FindFirstBit2x64(int value)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    85
{
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
    86
/*
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    87
	int i = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
	if ( (byte) value == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    89
		i += 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
		value >>= 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    92
	return i + FIND_FIRST_BIT(value & 0x3F);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
    93
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
    94
Faster ( or at least cleaner ) implementation below?
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
    95
*/
2663
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    96
	if (GB(value, 0, 8) == 0) {
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    97
		return FIND_FIRST_BIT(GB(value, 8, 6)) + 8;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
    98
	} else {
2663
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
    99
		return FIND_FIRST_BIT(GB(value, 0, 6));
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   100
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   101
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   102
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   103
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   104
static inline int KillFirstBit2x64(int value)
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   105
{
2663
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
   106
	if (GB(value, 0, 8) == 0) {
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
   107
		return KILL_FIRST_BIT(GB(value, 8, 6)) << 8;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   108
	} else {
2663
e5a3df7f225a (svn r3205) Some more uses for GB/SB
tron
parents: 2493
diff changeset
   109
		return value & (KILL_FIRST_BIT(GB(value, 0, 6)) | 0x3F00);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   110
	}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1202
diff changeset
   111
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
/* [min,max), strictly less than */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
#define IS_BYTE_INSIDE(a,min,max) ((byte)((a)-(min)) < (byte)((max)-(min)))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   115
#define IS_INT_INSIDE(a,min,max) ((uint)((a)-(min)) < (uint)((max)-(min)))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   116
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   118
#define CHANCE16(a,b) ((uint16)Random() <= (uint16)((65536 * a) / b))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
#define CHANCE16R(a,b,r) ((uint16)(r=Random()) <= (uint16)((65536 * a) / b))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
#define CHANCE16I(a,b,v) ((uint16)(v) <= (uint16)((65536 * a) / b))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2775
diff changeset
   123
#define for_each_bit(_i, _b)            \
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2775
diff changeset
   124
	for (_i = 0; _b != 0; _i++, _b >>= 1) \
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2775
diff changeset
   125
		if (_b & 1)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
#define abs myabs
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
500
8e52f7797b48 (svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents: 239
diff changeset
   130
static inline int intxchg_(int *a, int b) { int t = *a; *a = b; return t; }
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
#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
   132
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
   133
#define uintswap(a,b) ((b) = uintxchg_(&(a), (b)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
500
8e52f7797b48 (svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents: 239
diff changeset
   135
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
   136
static inline int64 myabs64(int64 a) { if (a<0) a = -a; return a; }
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
500
8e52f7797b48 (svn r793) Merge INLINE -> inline replacement (revision 376)
tron
parents: 239
diff changeset
   138
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
   139
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
   140
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
   141
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
   142
static inline void swap_tile(TileIndex *a, TileIndex *b) { TileIndex t = *a; *a = *b; *b = t; }
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   143
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   144
2966
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   145
static inline uint16 ReadLE16Aligned(const void* x)
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   146
{
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   147
	return FROM_LE16(*(const uint16*)x);
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   148
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
2966
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   150
static inline uint16 ReadLE16Unaligned(const void* x)
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   151
{
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   152
#ifdef OTTD_ALIGNMENT
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   153
	return ((const byte*)x)[0] | ((const byte*)x)[1] << 8;
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   154
#else
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   155
	return FROM_LE16(*(const uint16*)x);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   156
#endif
2966
661554d683cd (svn r3529) - Fix: [ 1415782 ] crash in string code with openbsd/zaurus; alignment issues (thanks Tron for the help)
Darkvater
parents: 2952
diff changeset
   157
}
661554d683cd (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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   159
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
   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
 * 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
   162
 * @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
   163
 */
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
#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
   165
#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
   166
2398
912f16512ce2 (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
   167
/**
912f16512ce2 (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
   168
 * Return the smallest multiple of n equal or greater than x
912f16512ce2 (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
   169
 * @note n must be a power of 2
912f16512ce2 (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
   170
 */
912f16512ce2 (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
   171
#define ALIGN(x, n) (((x) + (n) - 1) & ~((n) - 1))
912f16512ce2 (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
   172
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
#endif /* MACROS_H */