• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 #ifndef SOURCE_UTIL_BITUTILS_H_
16 #define SOURCE_UTIL_BITUTILS_H_
17 
18 #include <cstdint>
19 #include <cstring>
20 
21 namespace spvtools {
22 namespace utils {
23 
24 // Performs a bitwise copy of source to the destination type Dest.
25 template <typename Dest, typename Src>
BitwiseCast(Src source)26 Dest BitwiseCast(Src source) {
27   Dest dest;
28   static_assert(sizeof(source) == sizeof(dest),
29                 "BitwiseCast: Source and destination must have the same size");
30   std::memcpy(&dest, &source, sizeof(dest));
31   return dest;
32 }
33 
34 // SetBits<T, First, Num> returns an integer of type <T> with bits set
35 // for position <First> through <First + Num - 1>, counting from the least
36 // significant bit. In particular when Num == 0, no positions are set to 1.
37 // A static assert will be triggered if First + Num > sizeof(T) * 8, that is,
38 // a bit that will not fit in the underlying type is set.
39 template <typename T, size_t First = 0, size_t Num = 0>
40 struct SetBits {
41   static_assert(First < sizeof(T) * 8,
42                 "Tried to set a bit that is shifted too far.");
43   const static T get = (T(1) << First) | SetBits<T, First + 1, Num - 1>::get;
44 };
45 
46 template <typename T, size_t Last>
47 struct SetBits<T, Last, 0> {
48   const static T get = T(0);
49 };
50 
51 // This is all compile-time so we can put our tests right here.
52 static_assert(SetBits<uint32_t, 0, 0>::get == uint32_t(0x00000000),
53               "SetBits failed");
54 static_assert(SetBits<uint32_t, 0, 1>::get == uint32_t(0x00000001),
55               "SetBits failed");
56 static_assert(SetBits<uint32_t, 31, 1>::get == uint32_t(0x80000000),
57               "SetBits failed");
58 static_assert(SetBits<uint32_t, 1, 2>::get == uint32_t(0x00000006),
59               "SetBits failed");
60 static_assert(SetBits<uint32_t, 30, 2>::get == uint32_t(0xc0000000),
61               "SetBits failed");
62 static_assert(SetBits<uint32_t, 0, 31>::get == uint32_t(0x7FFFFFFF),
63               "SetBits failed");
64 static_assert(SetBits<uint32_t, 0, 32>::get == uint32_t(0xFFFFFFFF),
65               "SetBits failed");
66 static_assert(SetBits<uint32_t, 16, 16>::get == uint32_t(0xFFFF0000),
67               "SetBits failed");
68 
69 static_assert(SetBits<uint64_t, 0, 1>::get == uint64_t(0x0000000000000001LL),
70               "SetBits failed");
71 static_assert(SetBits<uint64_t, 63, 1>::get == uint64_t(0x8000000000000000LL),
72               "SetBits failed");
73 static_assert(SetBits<uint64_t, 62, 2>::get == uint64_t(0xc000000000000000LL),
74               "SetBits failed");
75 static_assert(SetBits<uint64_t, 31, 1>::get == uint64_t(0x0000000080000000LL),
76               "SetBits failed");
77 static_assert(SetBits<uint64_t, 16, 16>::get == uint64_t(0x00000000FFFF0000LL),
78               "SetBits failed");
79 
80 // Returns number of '1' bits in a word.
81 template <typename T>
82 size_t CountSetBits(T word) {
83   static_assert(std::is_integral<T>::value,
84                 "CountSetBits requires integer type");
85   size_t count = 0;
86   while (word) {
87     word &= word - 1;
88     ++count;
89   }
90   return count;
91 }
92 
93 }  // namespace utils
94 }  // namespace spvtools
95 
96 #endif  // SOURCE_UTIL_BITUTILS_H_
97