1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ART_LIBARTBASE_BASE_BIT_STRUCT_DETAIL_H_
18 #define ART_LIBARTBASE_BASE_BIT_STRUCT_DETAIL_H_
19
20 #include "bit_utils.h"
21 #include "globals.h"
22
23 #include <type_traits>
24
25 // Implementation details for bit_struct.h
26 // Not intended to be used stand-alone.
27
28 namespace art {
29
30 template <typename T>
31 static constexpr size_t BitStructSizeOf();
32
33 namespace detail {
34 // Select the smallest uintX_t that will fit kBitSize bits.
35 template <size_t kBitSize>
36 struct MinimumTypeUnsignedHelper {
37 using type =
38 std::conditional_t<kBitSize == 0, void, // NOLINT [whitespace/operators] [3]
39 std::conditional_t<kBitSize <= 8, uint8_t, // NOLINT [whitespace/operators] [3]
40 std::conditional_t<kBitSize <= 16, uint16_t, // NOLINT [whitespace/operators] [3]
41 std::conditional_t<kBitSize <= 32, uint32_t,
42 std::conditional_t<kBitSize <= 64, uint64_t,
43 std::conditional_t<kBitSize <= BitSizeOf<uintmax_t>(), uintmax_t, void>>>>>>;
44 };
45
46 // Select the smallest [u]intX_t that will fit kBitSize bits.
47 // Automatically picks intX_t or uintX_t based on the sign-ness of T.
48 template <typename T, size_t kBitSize>
49 struct MinimumTypeHelper {
50 using type_unsigned = typename MinimumTypeUnsignedHelper<kBitSize>::type;
51
52 using type =
53 std::conditional_t</* if */ std::is_signed_v<T>,
54 /* then */ std::make_signed_t<type_unsigned>,
55 /* else */ type_unsigned>;
56 };
57
58 // Helper for converting to and from T to an integral type.
59 template <typename T>
60 union ValueConverter {
61 using StorageType = typename MinimumTypeHelper<T, sizeof(T) * kBitsPerByte>::type;
62
ToUnderlyingStorage(T value)63 static constexpr StorageType ToUnderlyingStorage(T value) {
64 ValueConverter converter;
65 converter.value_.val_ = value;
66 return converter.storage_.val_;
67 }
68
FromUnderlyingStorage(StorageType storage)69 static constexpr T FromUnderlyingStorage(StorageType storage) {
70 ValueConverter converter;
71 converter.storage_.val_ = storage;
72 return converter.value_.val_;
73 }
74
75 // Underlying values must be wrapped in separate standard-layout structs.
76 // See below for more details.
77 struct StorageWrapper {
78 StorageType val_;
79 };
80 struct ValueWrapper {
81 T val_;
82 };
83
84 // Safely alias storage_ and value_ together.
85 //
86 // See C++ 9.5.1 [class.union]:
87 // If a standard-layout union contains several standard-layout structs that share a common
88 // initial sequence ... it is permitted to inspect the common initial sequence of any of
89 // standard-layout struct members.
90 StorageWrapper storage_;
91 ValueWrapper value_;
92 #if __cplusplus >= 202000L
93 #error "When upgrading to C++20, remove this error and check that this is OK for all use cases."
94 static_assert(std::is_layout_compatible_v<StorageWrapper, ValueWrapper>);
95 #endif
96
97 // Future work: In theory almost non-standard layout can be supported here,
98 // assuming they don't rely on the address of (this).
99 // We just have to use memcpy since the union-aliasing would not work.
100 };
101
102 // Denotes the beginning of a bit struct.
103 //
104 // This marker is required by the C++ standard in order to
105 // have a "common initial sequence".
106 //
107 // See C++ 9.5.1 [class.union]:
108 // If a standard-layout union contains several standard-layout structs that share a common
109 // initial sequence ... it is permitted to inspect the common initial sequence of any of
110 // standard-layout struct members.
111 template <size_t kSize>
112 struct DefineBitStructSize {
113 private:
114 typename MinimumTypeUnsignedHelper<kSize>::type _;
115 };
116
117 // Check if type "T" has a member called _ in it.
118 template <typename T>
119 struct HasUnderscoreField {
120 private:
121 using TrueT = std::bool_constant<true>::type;
122 using FalseT = std::bool_constant<false>::type;
123
124 template <typename C>
125 static constexpr auto Test(void*) -> decltype(std::declval<C>()._, TrueT{});
126
127 template <typename>
128 static constexpr FalseT Test(...);
129
130 public:
131 static constexpr bool value = decltype(Test<T>(nullptr))::value;
132 };
133
134 // Infer the type of the member of &T::M.
135 template <typename T, typename M>
136 M GetMemberType(M T:: *);
137
138 // Ensure the minimal type storage for 'T' matches its declared BitStructSizeOf.
139 // Nominally used by the BITSTRUCT_DEFINE_END macro.
140 template <typename T>
ValidateBitStructSize()141 static constexpr bool ValidateBitStructSize() {
142 static_assert(std::is_union_v<T>, "T must be union");
143 static_assert(std::is_standard_layout_v<T>, "T must be standard-layout");
144 static_assert(HasUnderscoreField<T>::value, "T must have the _ DefineBitStructSize");
145
146 const size_t kBitStructSizeOf = BitStructSizeOf<T>();
147 static_assert(std::is_same_v<decltype(GetMemberType(&T::_)),
148 DefineBitStructSize<kBitStructSizeOf>>,
149 "T::_ must be a DefineBitStructSize of the same size");
150
151 const size_t kExpectedSize = (BitStructSizeOf<T>() < kBitsPerByte)
152 ? kBitsPerByte
153 : RoundUpToPowerOfTwo(kBitStructSizeOf);
154
155 // Ensure no extra fields were added in between START/END.
156 const size_t kActualSize = sizeof(T) * kBitsPerByte;
157 return kExpectedSize == kActualSize;
158 }
159 } // namespace detail
160 } // namespace art
161
162 #endif // ART_LIBARTBASE_BASE_BIT_STRUCT_DETAIL_H_
163