| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #ifndef LIBSPIRV_UTIL_BITUTILS_H_ |
| #define LIBSPIRV_UTIL_BITUTILS_H_ |
|
|
| #include <cstdint> |
| #include <cstring> |
|
|
| namespace spvutils { |
|
|
| |
| template <typename Dest, typename Src> |
| Dest BitwiseCast(Src source) { |
| Dest dest; |
| static_assert(sizeof(source) == sizeof(dest), |
| "BitwiseCast: Source and destination must have the same size"); |
| std::memcpy(static_cast<void*>(&dest), &source, sizeof(dest)); |
| return dest; |
| } |
|
|
| |
| |
| |
| |
| |
| template <typename T, size_t First = 0, size_t Num = 0> |
| struct SetBits { |
| static_assert(First < sizeof(T) * 8, |
| "Tried to set a bit that is shifted too far."); |
| const static T get = (T(1) << First) | SetBits<T, First + 1, Num - 1>::get; |
| }; |
|
|
| template <typename T, size_t Last> |
| struct SetBits<T, Last, 0> { |
| const static T get = T(0); |
| }; |
|
|
| |
| static_assert(SetBits<uint32_t, 0, 0>::get == uint32_t(0x00000000), |
| "SetBits failed"); |
| static_assert(SetBits<uint32_t, 0, 1>::get == uint32_t(0x00000001), |
| "SetBits failed"); |
| static_assert(SetBits<uint32_t, 31, 1>::get == uint32_t(0x80000000), |
| "SetBits failed"); |
| static_assert(SetBits<uint32_t, 1, 2>::get == uint32_t(0x00000006), |
| "SetBits failed"); |
| static_assert(SetBits<uint32_t, 30, 2>::get == uint32_t(0xc0000000), |
| "SetBits failed"); |
| static_assert(SetBits<uint32_t, 0, 31>::get == uint32_t(0x7FFFFFFF), |
| "SetBits failed"); |
| static_assert(SetBits<uint32_t, 0, 32>::get == uint32_t(0xFFFFFFFF), |
| "SetBits failed"); |
| static_assert(SetBits<uint32_t, 16, 16>::get == uint32_t(0xFFFF0000), |
| "SetBits failed"); |
|
|
| static_assert(SetBits<uint64_t, 0, 1>::get == uint64_t(0x0000000000000001LL), |
| "SetBits failed"); |
| static_assert(SetBits<uint64_t, 63, 1>::get == uint64_t(0x8000000000000000LL), |
| "SetBits failed"); |
| static_assert(SetBits<uint64_t, 62, 2>::get == uint64_t(0xc000000000000000LL), |
| "SetBits failed"); |
| static_assert(SetBits<uint64_t, 31, 1>::get == uint64_t(0x0000000080000000LL), |
| "SetBits failed"); |
| static_assert(SetBits<uint64_t, 16, 16>::get == uint64_t(0x00000000FFFF0000LL), |
| "SetBits failed"); |
|
|
| } |
|
|
| #endif |
|
|