20 #ifndef LUNCHBOX_BITOPERATION_H 21 #define LUNCHBOX_BITOPERATION_H 23 #include <lunchbox/compiler.h> 25 #include <servus/uint128_t.h> 29 #pragma warning(disable : 4985) // inconsistent decl of ceil 35 #elif defined(LB_GCC_4_3_OR_OLDER) && !defined(__clang__) && !defined(__APPLE__) 37 #define LB_GCC_BSWAP_FUNCTION 63 inline int32_t getIndexOfLastBit<uint32_t>(uint32_t value)
66 return ::fls(value) - 1;
67 #elif defined __GNUC__ 68 return value ? (31 - __builtin_clz(value)) : -1;
69 #elif defined _MSC_VER 71 return _BitScanReverse(&i, value) ? i : -1;
84 inline int32_t getIndexOfLastBit<uint64_t>(uint64_t value)
87 return value ? (63 - __builtin_clzll(value)) : -1;
90 return _BitScanReverse64(&i, value) ? i : -1;
102 #if defined(__linux__) && defined(_LP64) 104 inline int32_t getIndexOfLastBit<unsigned long long>(
unsigned long long value)
112 inline int32_t getIndexOfLastBit<unsigned long>(
unsigned long value)
118 inline int32_t getIndexOfLastBit<unsigned long>(
unsigned long value)
142 inline void byteswap(
unsigned char&)
151 inline void byteswap(uint32_t& value)
154 value = _byteswap_ulong(value);
155 #elif defined __xlC__ 156 __store4r(value, &value);
157 #elif defined LB_GCC_BSWAP_FUNCTION 158 value = bswap_32(value);
160 value = __builtin_bswap32(value);
165 inline void byteswap(int32_t& value)
167 byteswap(reinterpret_cast<uint32_t&>(value));
173 byteswap(reinterpret_cast<uint32_t&>(value));
177 inline void byteswap(uint16_t& value)
180 value = _byteswap_ushort(value);
181 #elif defined __xlC__ 182 __store2r(value, &value);
184 value = (uint16_t)(value >> 8) | (uint16_t)(value << 8);
189 inline void byteswap(int16_t& value)
191 byteswap(reinterpret_cast<uint16_t&>(value));
196 inline void byteswap(
unsigned long& value)
198 byteswap(reinterpret_cast<unsigned long&>(value));
203 inline void byteswap(uint64_t& value)
206 value = _byteswap_uint64(value);
207 #elif defined __xlC__ 208 value = __bswap_constant_64(value);
209 #elif defined LB_GCC_BSWAP_FUNCTION 210 value = bswap_64(value);
212 value = __builtin_bswap64(value);
217 inline void byteswap(int64_t& value)
219 byteswap(reinterpret_cast<uint64_t&>(value));
225 byteswap(reinterpret_cast<uint64_t&>(value));
229 inline void byteswap(servus::uint128_t& value)
235 template <
typename T>
236 inline void byteswap(
typename std::vector<T>& value)
238 for (
size_t i = 0; i < value.size(); ++i)
243 #endif // LUNCHBOX_BITOPERATION_H Basic type definitions not provided by the operating system.
void byteswap(T &value)
Swap the byte order of the given value.
int32_t getIndexOfLastBit(T value)
Abstraction layer and common utilities for multi-threaded programming.