OpenTTD Source 20241224-master-gf74b0cf984
bitmath_func.hpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10#ifndef BITMATH_FUNC_HPP
11#define BITMATH_FUNC_HPP
12
31template <typename T>
32debug_inline constexpr static uint GB(const T x, const uint8_t s, const uint8_t n)
33{
34 return (x >> s) & (((T)1U << n) - 1);
35}
36
57template <typename T, typename U>
58constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
59{
60 x &= (T)(~((((T)1U << n) - 1) << s));
61 x |= (T)(d << s);
62 return x;
63}
64
82template <typename T, typename U>
83constexpr T AB(T &x, const uint8_t s, const uint8_t n, const U i)
84{
85 const T mask = ((((T)1U << n) - 1) << s);
86 x = (T)((x & ~mask) | ((x + (i << s)) & mask));
87 return x;
88}
89
102template <typename T>
103debug_inline constexpr bool HasBit(const T x, const uint8_t y)
104{
105 return (x & ((T)1U << y)) != 0;
106}
107
120template <typename T>
121constexpr T SetBit(T &x, const uint8_t y)
122{
123 return x = (T)(x | ((T)1U << y));
124}
125
136#define SETBITS(x, y) ((x) |= (y))
137
150template <typename T>
151constexpr T ClrBit(T &x, const uint8_t y)
152{
153 return x = (T)(x & ~((T)1U << y));
154}
155
166#define CLRBITS(x, y) ((x) &= ~(y))
167
180template <typename T>
181constexpr T ToggleBit(T &x, const uint8_t y)
182{
183 return x = (T)(x ^ ((T)1U << y));
184}
185
199template <typename T>
200constexpr T AssignBit(T &x, const uint8_t y, bool value)
201{
202 return SB<T>(x, y, 1, value ? 1 : 0);
203}
204
212template <typename T>
213constexpr uint8_t FindFirstBit(T x)
214{
215 if (x == 0) return 0;
216
217 if constexpr (std::is_enum_v<T>) {
218 return std::countr_zero<std::underlying_type_t<T>>(x);
219 } else {
220 return std::countr_zero(x);
221 }
222}
223
231template <typename T>
232constexpr uint8_t FindLastBit(T x)
233{
234 if (x == 0) return 0;
235
236 return std::numeric_limits<T>::digits - std::countl_zero(x) - 1;
237}
238
249template <typename T>
250constexpr T KillFirstBit(T value)
251{
252 return value &= (T)(value - 1);
253}
254
261template <typename T>
262constexpr uint CountBits(T value)
263{
264 if constexpr (std::is_enum_v<T>) {
265 return std::popcount<std::underlying_type_t<T>>(value);
266 } else {
267 return std::popcount(value);
268 }
269}
270
277template <typename T>
278constexpr bool HasExactlyOneBit(T value)
279{
280 return value != 0 && (value & (value - 1)) == 0;
281}
282
289template <typename T>
290constexpr bool HasAtMostOneBit(T value)
291{
292 return (value & (value - 1)) == 0;
293}
294
300template <typename Tbitpos = uint, typename Tbitset = uint>
302 struct Iterator {
303 typedef Tbitpos value_type;
304 typedef value_type *pointer;
305 typedef value_type &reference;
306 typedef size_t difference_type;
307 typedef std::forward_iterator_tag iterator_category;
308
309 explicit Iterator(Tbitset bitset) : bitset(bitset), bitpos(static_cast<Tbitpos>(0))
310 {
311 this->Validate();
312 }
313
314 bool operator==(const Iterator &other) const
315 {
316 return this->bitset == other.bitset;
317 }
318 bool operator!=(const Iterator &other) const { return !(*this == other); }
319 Tbitpos operator*() const { return this->bitpos; }
320 Iterator & operator++() { this->Next(); this->Validate(); return *this; }
321
322 private:
323 Tbitset bitset;
324 Tbitpos bitpos;
325 void Validate()
326 {
327 if (this->bitset != 0) {
328 typename std::make_unsigned<Tbitset>::type unsigned_value = this->bitset;
329 this->bitpos = static_cast<Tbitpos>(FindFirstBit(unsigned_value));
330 }
331 }
332 void Next()
333 {
334 this->bitset = KillFirstBit(this->bitset);
335 }
336 };
337
338 SetBitIterator(Tbitset bitset) : bitset(bitset) {}
339 Iterator begin() { return Iterator(this->bitset); }
340 Iterator end() { return Iterator(static_cast<Tbitset>(0)); }
341 bool empty() { return this->begin() == this->end(); }
342
343private:
344 Tbitset bitset;
345};
346
347#if defined(__APPLE__)
348 /* Make endian swapping use Apple's macros to increase speed
349 * (since it will use hardware swapping if available).
350 * Even though they should return uint16_t and uint32_t, we get
351 * warnings if we don't cast those (why?) */
352# define BSWAP32(x) (static_cast<uint32_t>(CFSwapInt32(x)))
353# define BSWAP16(x) (static_cast<uint16_t>(CFSwapInt16(x)))
354#elif defined(_MSC_VER)
355 /* MSVC has intrinsics for swapping, resulting in faster code */
356# define BSWAP32(x) (_byteswap_ulong(x))
357# define BSWAP16(x) (_byteswap_ushort(x))
358#else
364 static inline uint32_t BSWAP32(uint32_t x)
365 {
366#if !defined(__ICC) && defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ >= 3))
367 /* GCC >= 4.3 provides a builtin, resulting in faster code */
368 return static_cast<uint32_t>(__builtin_bswap32(static_cast<int32_t>(x)));
369#else
370 return ((x >> 24) & 0xFF) | ((x >> 8) & 0xFF00) | ((x << 8) & 0xFF0000) | ((x << 24) & 0xFF000000);
371#endif /* defined(__GNUC__) */
372 }
373
379 static inline uint16_t BSWAP16(uint16_t x)
380 {
381 return (x >> 8) | (x << 8);
382 }
383#endif /* __APPLE__ */
384
385#endif /* BITMATH_FUNC_HPP */
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr bool HasExactlyOneBit(T value)
Test whether value has exactly 1 bit set.
constexpr T AssignBit(T &x, const uint8_t y, bool value)
Assigns a bit in a variable.
constexpr T SB(T &x, const uint8_t s, const uint8_t n, const U d)
Set n bits in x starting at bit s to d.
constexpr uint8_t FindLastBit(T x)
Search the last set bit in a value.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
static uint32_t BSWAP32(uint32_t x)
Perform a 32 bits endianness bitswap on x.
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
constexpr uint CountBits(T value)
Counts the number of set bits in a variable.
constexpr bool HasAtMostOneBit(T value)
Test whether value has at most 1 bit set.
constexpr T AB(T &x, const uint8_t s, const uint8_t n, const U i)
Add i to n bits of x starting at bit s.
static uint16_t BSWAP16(uint16_t x)
Perform a 16 bits endianness bitswap on x.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr T ToggleBit(T &x, const uint8_t y)
Toggles a bit in a variable.
constexpr T KillFirstBit(T value)
Clear the first bit in an integer.
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
Iterable ensemble of each set bit in a value.