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