(svn r27950) -Merge: Documentation updates from 1.7 branch
[openttd.git] / src / core / bitmath_func.hpp
blob31e679b0054363645bd5fb5efe8ddc2595c56757
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file bitmath_func.hpp Functions related to bit mathematics. */
12 #ifndef BITMATH_FUNC_HPP
13 #define BITMATH_FUNC_HPP
15 /**
16 * Fetch \a n bits from \a x, started at bit \a s.
18 * This function can be used to fetch \a n bits from the value \a x. The
19 * \a s value set the start position to read. The start position is
20 * count from the LSB and starts at \c 0. The result starts at a
21 * LSB, as this isn't just an and-bitmask but also some
22 * bit-shifting operations. GB(0xFF, 2, 1) will so
23 * return 0x01 (0000 0001) instead of
24 * 0x04 (0000 0100).
26 * @param x The value to read some bits.
27 * @param s The start position to read some bits.
28 * @param n The number of bits to read.
29 * @pre n < sizeof(T) * 8
30 * @pre s + n <= sizeof(T) * 8
31 * @return The selected bits, aligned to a LSB.
33 template <typename T>
34 static inline uint GB(const T x, const uint8 s, const uint8 n)
36 return (x >> s) & (((T)1U << n) - 1);
39 /**
40 * Set \a n bits in \a x starting at bit \a s to \a d
42 * This function sets \a n bits from \a x which started as bit \a s to the value of
43 * \a d. The parameters \a x, \a s and \a n works the same as the parameters of
44 * #GB. The result is saved in \a x again. Unused bits in the window
45 * provided by n are set to 0 if the value of \a d isn't "big" enough.
46 * This is not a bug, its a feature.
48 * @note Parameter \a x must be a variable as the result is saved there.
49 * @note To avoid unexpected results the value of \a d should not use more
50 * space as the provided space of \a n bits (log2)
51 * @param x The variable to change some bits
52 * @param s The start position for the new bits
53 * @param n The size/window for the new bits
54 * @param d The actually new bits to save in the defined position.
55 * @pre n < sizeof(T) * 8
56 * @pre s + n <= sizeof(T) * 8
57 * @return The new value of \a x
59 template <typename T, typename U>
60 static inline T SB(T &x, const uint8 s, const uint8 n, const U d)
62 x &= (T)(~((((T)1U << n) - 1) << s));
63 x |= (T)(d << s);
64 return x;
67 /**
68 * Add \a i to \a n bits of \a x starting at bit \a s.
70 * This adds the value of \a i on \a n bits of \a x starting at bit \a s. The parameters \a x,
71 * \a s, \a i are similar to #GB. Besides, \ a x must be a variable as the result are
72 * saved there. An overflow does not affect the following bits of the given
73 * bit window and is simply ignored.
75 * @note Parameter x must be a variable as the result is saved there.
76 * @param x The variable to add some bits at some position
77 * @param s The start position of the addition
78 * @param n The size/window for the addition
79 * @pre n < sizeof(T) * 8
80 * @pre s + n <= sizeof(T) * 8
81 * @param i The value to add at the given start position in the given window.
82 * @return The new value of \a x
84 template <typename T, typename U>
85 static inline T AB(T &x, const uint8 s, const uint8 n, const U i)
87 const T mask = ((((T)1U << n) - 1) << s);
88 x = (T)((x & ~mask) | ((x + (i << s)) & mask));
89 return x;
92 /**
93 * Checks if a bit in a value is set.
95 * This function checks if a bit inside a value is set or not.
96 * The \a y value specific the position of the bit, started at the
97 * LSB and count from \c 0.
99 * @param x The value to check
100 * @param y The position of the bit to check, started from the LSB
101 * @pre y < sizeof(T) * 8
102 * @return True if the bit is set, false else.
104 template <typename T>
105 static inline bool HasBit(const T x, const uint8 y)
107 return (x & ((T)1U << y)) != 0;
111 * Set a bit in a variable.
113 * This function sets a bit in a variable. The variable is changed
114 * and the value is also returned. Parameter y defines the bit and
115 * starts at the LSB with 0.
117 * @param x The variable to set a bit
118 * @param y The bit position to set
119 * @pre y < sizeof(T) * 8
120 * @return The new value of the old value with the bit set
122 template <typename T>
123 static inline T SetBit(T &x, const uint8 y)
125 return x = (T)(x | ((T)1U << y));
129 * Sets several bits in a variable.
131 * This macro sets several bits in a variable. The bits to set are provided
132 * by a value. The new value is also returned.
134 * @param x The variable to set some bits
135 * @param y The value with set bits for setting them in the variable
136 * @return The new value of x
138 #define SETBITS(x, y) ((x) |= (y))
141 * Clears a bit in a variable.
143 * This function clears a bit in a variable. The variable is
144 * changed and the value is also returned. Parameter y defines the bit
145 * to clear and starts at the LSB with 0.
147 * @param x The variable to clear the bit
148 * @param y The bit position to clear
149 * @pre y < sizeof(T) * 8
150 * @return The new value of the old value with the bit cleared
152 template <typename T>
153 static inline T ClrBit(T &x, const uint8 y)
155 return x = (T)(x & ~((T)1U << y));
159 * Clears several bits in a variable.
161 * This macro clears several bits in a variable. The bits to clear are
162 * provided by a value. The new value is also returned.
164 * @param x The variable to clear some bits
165 * @param y The value with set bits for clearing them in the variable
166 * @return The new value of x
168 #define CLRBITS(x, y) ((x) &= ~(y))
171 * Toggles a bit in a variable.
173 * This function toggles a bit in a variable. The variable is
174 * changed and the value is also returned. Parameter y defines the bit
175 * to toggle and starts at the LSB with 0.
177 * @param x The variable to toggle the bit
178 * @param y The bit position to toggle
179 * @pre y < sizeof(T) * 8
180 * @return The new value of the old value with the bit toggled
182 template <typename T>
183 static inline T ToggleBit(T &x, const uint8 y)
185 return x = (T)(x ^ ((T)1U << y));
189 /** Lookup table to check which bit is set in a 6 bit variable */
190 extern const uint8 _ffb_64[64];
193 * Returns the first non-zero bit in a 6-bit value (from right).
195 * Returns the position of the first bit that is not zero, counted from the
196 * LSB. Ie, 110100 returns 2, 000001 returns 0, etc. When x == 0 returns
197 * 0.
199 * @param x The 6-bit value to check the first zero-bit
200 * @return The first position of a bit started from the LSB or 0 if x is 0.
202 #define FIND_FIRST_BIT(x) _ffb_64[(x)]
205 * Finds the position of the first non-zero bit in an integer.
207 * This function returns the position of the first bit set in the
208 * integer. It does only check the bits of the bitmask
209 * 0x3F3F (0011111100111111) and checks only the
210 * bits of the bitmask 0x3F00 if and only if the
211 * lower part 0x00FF is 0. This results the bits at 0x00C0 must
212 * be also zero to check the bits at 0x3F00.
214 * @param value The value to check the first bits
215 * @return The position of the first bit which is set
216 * @see FIND_FIRST_BIT
218 static inline uint8 FindFirstBit2x64(const int value)
220 if ((value & 0xFF) == 0) {
221 return FIND_FIRST_BIT((value >> 8) & 0x3F) + 8;
222 } else {
223 return FIND_FIRST_BIT(value & 0x3F);
227 uint8 FindFirstBit(uint32 x);
228 uint8 FindLastBit(uint64 x);
231 * Clear the first bit in an integer.
233 * This function returns a value where the first bit (from LSB)
234 * is cleared.
235 * So, 110100 returns 110000, 000001 returns 000000, etc.
237 * @param value The value to clear the first bit
238 * @return The new value with the first bit cleared
240 template <typename T>
241 static inline T KillFirstBit(T value)
243 return value &= (T)(value - 1);
247 * Counts the number of set bits in a variable.
249 * @param value the value to count the number of bits in.
250 * @return the number of bits.
252 template <typename T>
253 static inline uint CountBits(T value)
255 uint num;
257 /* This loop is only called once for every bit set by clearing the lowest
258 * bit in each loop. The number of bits is therefore equal to the number of
259 * times the loop was called. It was found at the following website:
260 * http://graphics.stanford.edu/~seander/bithacks.html */
262 for (num = 0; value != 0; num++) {
263 value &= (T)(value - 1);
266 return num;
270 * Test whether \a value has exactly 1 bit set
272 * @param value the value to test.
273 * @return does \a value have exactly 1 bit set?
275 template <typename T>
276 static inline bool HasExactlyOneBit(T value)
278 return value != 0 && (value & (value - 1)) == 0;
282 * Test whether \a value has at most 1 bit set
284 * @param value the value to test.
285 * @return does \a value have at most 1 bit set?
287 template <typename T>
288 static inline bool HasAtMostOneBit(T value)
290 return (value & (value - 1)) == 0;
294 * ROtate \a x Left by \a n
296 * @note Assumes a byte has 8 bits
297 * @param x The value which we want to rotate
298 * @param n The number how many we want to rotate
299 * @pre n < sizeof(T) * 8
300 * @return A bit rotated number
302 template <typename T>
303 static inline T ROL(const T x, const uint8 n)
305 return (T)(x << n | x >> (sizeof(x) * 8 - n));
309 * ROtate \a x Right by \a n
311 * @note Assumes a byte has 8 bits
312 * @param x The value which we want to rotate
313 * @param n The number how many we want to rotate
314 * @pre n < sizeof(T) * 8
315 * @return A bit rotated number
317 template <typename T>
318 static inline T ROR(const T x, const uint8 n)
320 return (T)(x >> n | x << (sizeof(x) * 8 - n));
324 * Do an operation for each set bit in a value.
326 * This macros is used to do an operation for each set
327 * bit in a variable. The second parameter is a
328 * variable that is used as the bit position counter.
329 * The fourth parameter is an expression of the bits
330 * we need to iterate over. This expression will be
331 * evaluated once.
333 * @param Tbitpos_type Type of the position counter variable.
334 * @param bitpos_var The position counter variable.
335 * @param Tbitset_type Type of the bitset value.
336 * @param bitset_value The bitset value which we check for bits.
338 * @see FOR_EACH_SET_BIT
340 #define FOR_EACH_SET_BIT_EX(Tbitpos_type, bitpos_var, Tbitset_type, bitset_value) \
341 for ( \
342 Tbitset_type ___FESBE_bits = (bitpos_var = (Tbitpos_type)0, bitset_value); \
343 ___FESBE_bits != (Tbitset_type)0; \
344 ___FESBE_bits = (Tbitset_type)(___FESBE_bits >> 1), bitpos_var++ \
346 if ((___FESBE_bits & 1) != 0)
349 * Do an operation for each set set bit in a value.
351 * This macros is used to do an operation for each set
352 * bit in a variable. The first parameter is a variable
353 * that is used as the bit position counter.
354 * The second parameter is an expression of the bits
355 * we need to iterate over. This expression will be
356 * evaluated once.
358 * @param bitpos_var The position counter variable.
359 * @param bitset_value The value which we check for set bits.
361 #define FOR_EACH_SET_BIT(bitpos_var, bitset_value) FOR_EACH_SET_BIT_EX(uint, bitpos_var, uint, bitset_value)
363 #if defined(__APPLE__)
364 /* Make endian swapping use Apple's macros to increase speed
365 * (since it will use hardware swapping if available).
366 * Even though they should return uint16 and uint32, we get
367 * warnings if we don't cast those (why?) */
368 #define BSWAP32(x) ((uint32)CFSwapInt32(x))
369 #define BSWAP16(x) ((uint16)CFSwapInt16(x))
370 #elif defined(_MSC_VER)
371 /* MSVC has intrinsics for swapping, resulting in faster code */
372 #define BSWAP32(x) (_byteswap_ulong(x))
373 #define BSWAP16(x) (_byteswap_ushort(x))
374 #else
376 * Perform a 32 bits endianness bitswap on x.
377 * @param x the variable to bitswap
378 * @return the bitswapped value.
380 static inline uint32 BSWAP32(uint32 x)
382 #if !defined(__ICC) && defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ >= 3))
383 /* GCC >= 4.3 provides a builtin, resulting in faster code */
384 return (uint32)__builtin_bswap32((int32)x);
385 #else
386 return ((x >> 24) & 0xFF) | ((x >> 8) & 0xFF00) | ((x << 8) & 0xFF0000) | ((x << 24) & 0xFF000000);
387 #endif /* defined(__GNUC__) */
391 * Perform a 16 bits endianness bitswap on x.
392 * @param x the variable to bitswap
393 * @return the bitswapped value.
395 static inline uint16 BSWAP16(uint16 x)
397 return (x >> 8) | (x << 8);
399 #endif /* __APPLE__ */
401 #endif /* BITMATH_FUNC_HPP */