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