Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * 3 : * Permission is hereby granted, free of charge, to any person obtaining 4 : * a copy of this software and associated documentation files (the 5 : * "Software"), to deal in the Software without restriction, including 6 : * without limitation the rights to use, copy, modify, merge, publish, 7 : * distribute, sublicense, and/or sell copies of the Software, and to 8 : * permit persons to whom the Software is furnished to do so, subject to 9 : * the following conditions: 10 : * 11 : * The above copyright notice and this permission notice shall be included 12 : * in all copies or substantial portions of the Software. 13 : * 14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 : */ 22 : 23 : /* 24 : * bit-twiddling. 25 : */ 26 : 27 : #ifndef INCLUDED_BITS 28 : #define INCLUDED_BITS 29 : 30 : #include "lib/code_annotation.h" 31 : #include "lib/debug.h" 32 : #include "lib/status.h" 33 : 34 : #include <bit> 35 : #include <climits> 36 : #include <cstddef> 37 : #include <limits> 38 : #include <type_traits> 39 : 40 : /** 41 : * value of bit number \<n\>. 42 : * 43 : * @param n bit index. 44 : * 45 : * requirements: 46 : * - T should be an unsigned type 47 : * - n must be in [0, CHAR_BIT*sizeof(T)), else the result is undefined! 48 : **/ 49 : template<typename T> 50 6 : inline T Bit(size_t n) 51 : { 52 7 : const T one = T(1); 53 6 : return (T)(one << n); 54 : } 55 : 56 : /** 57 : * pretty much the same as Bit\<unsigned\>. 58 : * this is intended for the initialization of enum values, where a 59 : * compile-time constant is required. 60 : **/ 61 : #define BIT(n) (1u << (n)) 62 : 63 : template<typename T> 64 0 : inline bool IsBitSet(T value, size_t index) 65 : { 66 1 : const T bit = Bit<T>(index); 67 0 : return (value & bit) != 0; 68 : } 69 : 70 : 71 : // these are declared in the header and inlined to aid compiler optimizations 72 : // (they can easily end up being time-critical). 73 : // note: GCC can't inline extern functions, while VC's "Whole Program 74 : // Optimization" can. 75 : 76 : /** 77 : * a mask that includes the lowest N bits 78 : * 79 : * @param numBits Number of bits in mask. 80 : **/ 81 : template<typename T> 82 : inline T bit_mask(size_t numBits) 83 : { 84 13498 : const T bitsInT = sizeof(T)*CHAR_BIT; 85 12434 : const T allBits = (T)~T(0); 86 : // (shifts of at least bitsInT are undefined) 87 : if(numBits >= bitsInT) 88 : return allBits; 89 : // (note: the previous allBits >> (bitsInT-numBits) is not safe 90 : // because right-shifts of negative numbers are undefined.) 91 : const T mask = (T)((T(1) << numBits)-1); 92 : return mask; 93 : } 94 : 95 : 96 : /** 97 : * extract the value of bits hi_idx:lo_idx within num 98 : * 99 : * example: bits(0x69, 2, 5) == 0x0A 100 : * 101 : * @param num number whose bits are to be extracted 102 : * @param lo_idx bit index of lowest bit to include 103 : * @param hi_idx bit index of highest bit to include 104 : * @return value of extracted bits. 105 : **/ 106 : template<typename T> 107 1043 : inline T bits(T num, size_t lo_idx, size_t hi_idx) 108 : { 109 1056 : const size_t numBits = (hi_idx - lo_idx)+1; // # bits to return 110 1056 : T result = T(num >> lo_idx); 111 1044 : result = T(result & bit_mask<T>(numBits)); 112 : return result; 113 : } 114 : 115 : /** 116 : * round number up/down to the next given multiple. 117 : * 118 : * @param n Number to round. 119 : * @param multiple Must be a power of two. 120 : **/ 121 : template<typename T> 122 16455 : inline T round_up(T n, T multiple) 123 : { 124 : ASSERT(multiple > 0); 125 : ASSERT(std::has_single_bit(static_cast<std::make_unsigned_t<T>>(multiple))); 126 16454 : const T result = (n + multiple-1) & ~(multiple-1); 127 : ASSERT(n <= result && result < n+multiple); 128 : return result; 129 : } 130 : 131 : template<typename T> 132 0 : inline T round_down(T n, T multiple) 133 : { 134 : ASSERT(multiple > 0); 135 : ASSERT(std::has_single_bit(static_cast<std::make_unsigned_t<T>>(multiple))); 136 0 : const T result = n & ~(multiple-1); 137 : ASSERT(result <= n && n < result+multiple); 138 : return result; 139 : } 140 : 141 : // evaluates to an expression suitable as an initializer 142 : // for constant static data members. 143 : #define ROUND_UP(n, multiple) (((n) + (multiple)-1) & ~((multiple)-1)) 144 : 145 : #endif // #ifndef INCLUDED_BITS