bitutils.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef LIBSPIRV_UTIL_BITUTILS_H_
  15. #define LIBSPIRV_UTIL_BITUTILS_H_
  16. #include <cstdint>
  17. #include <cstring>
  18. namespace spvutils {
  19. // Performs a bitwise copy of source to the destination type Dest.
  20. template <typename Dest, typename Src>
  21. Dest BitwiseCast(Src source) {
  22. Dest dest;
  23. static_assert(sizeof(source) == sizeof(dest),
  24. "BitwiseCast: Source and destination must have the same size");
  25. std::memcpy(static_cast<void*>(&dest), &source, sizeof(dest));
  26. return dest;
  27. }
  28. // SetBits<T, First, Num> returns an integer of type <T> with bits set
  29. // for position <First> through <First + Num - 1>, counting from the least
  30. // significant bit. In particular when Num == 0, no positions are set to 1.
  31. // A static assert will be triggered if First + Num > sizeof(T) * 8, that is,
  32. // a bit that will not fit in the underlying type is set.
  33. template <typename T, size_t First = 0, size_t Num = 0>
  34. struct SetBits {
  35. static_assert(First < sizeof(T) * 8,
  36. "Tried to set a bit that is shifted too far.");
  37. const static T get = (T(1) << First) | SetBits<T, First + 1, Num - 1>::get;
  38. };
  39. template <typename T, size_t Last>
  40. struct SetBits<T, Last, 0> {
  41. const static T get = T(0);
  42. };
  43. // This is all compile-time so we can put our tests right here.
  44. static_assert(SetBits<uint32_t, 0, 0>::get == uint32_t(0x00000000),
  45. "SetBits failed");
  46. static_assert(SetBits<uint32_t, 0, 1>::get == uint32_t(0x00000001),
  47. "SetBits failed");
  48. static_assert(SetBits<uint32_t, 31, 1>::get == uint32_t(0x80000000),
  49. "SetBits failed");
  50. static_assert(SetBits<uint32_t, 1, 2>::get == uint32_t(0x00000006),
  51. "SetBits failed");
  52. static_assert(SetBits<uint32_t, 30, 2>::get == uint32_t(0xc0000000),
  53. "SetBits failed");
  54. static_assert(SetBits<uint32_t, 0, 31>::get == uint32_t(0x7FFFFFFF),
  55. "SetBits failed");
  56. static_assert(SetBits<uint32_t, 0, 32>::get == uint32_t(0xFFFFFFFF),
  57. "SetBits failed");
  58. static_assert(SetBits<uint32_t, 16, 16>::get == uint32_t(0xFFFF0000),
  59. "SetBits failed");
  60. static_assert(SetBits<uint64_t, 0, 1>::get == uint64_t(0x0000000000000001LL),
  61. "SetBits failed");
  62. static_assert(SetBits<uint64_t, 63, 1>::get == uint64_t(0x8000000000000000LL),
  63. "SetBits failed");
  64. static_assert(SetBits<uint64_t, 62, 2>::get == uint64_t(0xc000000000000000LL),
  65. "SetBits failed");
  66. static_assert(SetBits<uint64_t, 31, 1>::get == uint64_t(0x0000000080000000LL),
  67. "SetBits failed");
  68. static_assert(SetBits<uint64_t, 16, 16>::get == uint64_t(0x00000000FFFF0000LL),
  69. "SetBits failed");
  70. } // namespace spvutils
  71. #endif // LIBSPIRV_UTIL_BITUTILS_H_