/src/shaderc/third_party/glslang/SPIRV/bitutils.h
Line | Count | Source |
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> |
25 | 14.3k | Dest BitwiseCast(Src source) { |
26 | 14.3k | Dest dest; |
27 | 14.3k | static_assert(sizeof(source) == sizeof(dest), |
28 | 14.3k | "BitwiseCast: Source and destination must have the same size"); |
29 | 14.3k | std::memcpy(static_cast<void*>(&dest), &source, sizeof(dest)); |
30 | 14.3k | return dest; |
31 | 14.3k | } unsigned int spvutils::BitwiseCast<unsigned int, float>(float) Line | Count | Source | 25 | 1.31k | Dest BitwiseCast(Src source) { | 26 | 1.31k | Dest dest; | 27 | 1.31k | static_assert(sizeof(source) == sizeof(dest), | 28 | 1.31k | "BitwiseCast: Source and destination must have the same size"); | 29 | 1.31k | std::memcpy(static_cast<void*>(&dest), &source, sizeof(dest)); | 30 | 1.31k | return dest; | 31 | 1.31k | } |
unsigned int spvutils::BitwiseCast<unsigned int, spvutils::FloatProxy<float> >(spvutils::FloatProxy<float>) Line | Count | Source | 25 | 10.6k | Dest BitwiseCast(Src source) { | 26 | 10.6k | Dest dest; | 27 | 10.6k | static_assert(sizeof(source) == sizeof(dest), | 28 | 10.6k | "BitwiseCast: Source and destination must have the same size"); | 29 | 10.6k | std::memcpy(static_cast<void*>(&dest), &source, sizeof(dest)); | 30 | 10.6k | return dest; | 31 | 10.6k | } |
Unexecuted instantiation: unsigned short spvutils::BitwiseCast<unsigned short, unsigned short>(unsigned short) spvutils::FloatProxy<spvutils::Float16> spvutils::BitwiseCast<spvutils::FloatProxy<spvutils::Float16>, unsigned short>(unsigned short) Line | Count | Source | 25 | 1.11k | Dest BitwiseCast(Src source) { | 26 | 1.11k | Dest dest; | 27 | 1.11k | static_assert(sizeof(source) == sizeof(dest), | 28 | 1.11k | "BitwiseCast: Source and destination must have the same size"); | 29 | 1.11k | std::memcpy(static_cast<void*>(&dest), &source, sizeof(dest)); | 30 | 1.11k | return dest; | 31 | 1.11k | } |
spvutils::Float16 spvutils::BitwiseCast<spvutils::Float16, unsigned short>(unsigned short) Line | Count | Source | 25 | 1.31k | Dest BitwiseCast(Src source) { | 26 | 1.31k | Dest dest; | 27 | 1.31k | static_assert(sizeof(source) == sizeof(dest), | 28 | 1.31k | "BitwiseCast: Source and destination must have the same size"); | 29 | 1.31k | std::memcpy(static_cast<void*>(&dest), &source, sizeof(dest)); | 30 | 1.31k | return dest; | 31 | 1.31k | } |
Unexecuted instantiation: unsigned char spvutils::BitwiseCast<unsigned char, unsigned char>(unsigned char) Unexecuted instantiation: spvutils::FloatProxy<spvutils::FloatE5M2> spvutils::BitwiseCast<spvutils::FloatProxy<spvutils::FloatE5M2>, unsigned char>(unsigned char) Unexecuted instantiation: spvutils::FloatE5M2 spvutils::BitwiseCast<spvutils::FloatE5M2, unsigned char>(unsigned char) Unexecuted instantiation: spvutils::FloatProxy<spvutils::FloatE4M3> spvutils::BitwiseCast<spvutils::FloatProxy<spvutils::FloatE4M3>, unsigned char>(unsigned char) Unexecuted instantiation: spvutils::FloatE4M3 spvutils::BitwiseCast<spvutils::FloatE4M3, unsigned char>(unsigned char) Unexecuted instantiation: float spvutils::BitwiseCast<float, unsigned int>(unsigned int) Unexecuted instantiation: unsigned short spvutils::BitwiseCast<unsigned short, spvutils::FloatProxy<spvutils::Float16> >(spvutils::FloatProxy<spvutils::Float16>) Unexecuted instantiation: unsigned short spvutils::BitwiseCast<unsigned short, spvutils::Float16>(spvutils::Float16) |
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_ |