1 /*
2  * Copyright (C) 2015 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 #include <vector>
18 
19 #include "bit_utils.h"
20 
21 #include "gtest/gtest.h"
22 
23 namespace art {
24 
25 // NOTE: CLZ(0u) is undefined.
26 static_assert(31 == CLZ<uint32_t>(1u), "TestCLZ32#1");
27 static_assert(30 == CLZ<uint32_t>(2u), "TestCLZ32#2");
28 static_assert(16 == CLZ<uint32_t>(0x00008765u), "TestCLZ32#3");
29 static_assert(15 == CLZ<uint32_t>(0x00012345u), "TestCLZ32#4");
30 static_assert(1 == CLZ<uint32_t>(0x43214321u), "TestCLZ32#5");
31 static_assert(0 == CLZ<uint32_t>(0x87654321u), "TestCLZ32#6");
32 
33 // NOTE: CLZ(0ull) is undefined.
34 static_assert(63 == CLZ<uint64_t>(UINT64_C(1)), "TestCLZ64#1");
35 static_assert(62 == CLZ<uint64_t>(UINT64_C(3)), "TestCLZ64#2");
36 static_assert(48 == CLZ<uint64_t>(UINT64_C(0x00008765)), "TestCLZ64#3");
37 static_assert(32 == CLZ<uint64_t>(UINT64_C(0x87654321)), "TestCLZ64#4");
38 static_assert(31 == CLZ<uint64_t>(UINT64_C(0x123456789)), "TestCLZ64#5");
39 static_assert(16 == CLZ<uint64_t>(UINT64_C(0x876543211234)), "TestCLZ64#6");
40 static_assert(1 == CLZ<uint64_t>(UINT64_C(0x4321432187654321)), "TestCLZ64#7");
41 static_assert(0 == CLZ<uint64_t>(UINT64_C(0x8765432187654321)), "TestCLZ64#8");
42 
43 // NOTE: CTZ(0u) is undefined.
44 static_assert(0 == CTZ<uint32_t>(1u), "TestCTZ32#1");
45 static_assert(1 == CTZ<uint32_t>(2u), "TestCTZ32#2");
46 static_assert(15 == CTZ<uint32_t>(0x45678000u), "TestCTZ32#3");
47 static_assert(16 == CTZ<uint32_t>(0x43210000u), "TestCTZ32#4");
48 static_assert(30 == CTZ<uint32_t>(0xc0000000u), "TestCTZ32#5");
49 static_assert(31 == CTZ<uint32_t>(0x80000000u), "TestCTZ32#6");
50 
51 // NOTE: CTZ(0ull) is undefined.
52 static_assert(0 == CTZ<uint64_t>(UINT64_C(1)), "TestCTZ64#1");
53 static_assert(1 == CTZ<uint64_t>(UINT64_C(2)), "TestCTZ64#2");
54 static_assert(16 == CTZ<uint64_t>(UINT64_C(0x43210000)), "TestCTZ64#3");
55 static_assert(31 == CTZ<uint64_t>(UINT64_C(0x80000000)), "TestCTZ64#4");
56 static_assert(32 == CTZ<uint64_t>(UINT64_C(0x8765432100000000)), "TestCTZ64#5");
57 static_assert(48 == CTZ<uint64_t>(UINT64_C(0x4321000000000000)), "TestCTZ64#6");
58 static_assert(62 == CTZ<uint64_t>(UINT64_C(0x4000000000000000)), "TestCTZ64#7");
59 static_assert(63 == CTZ<uint64_t>(UINT64_C(0x8000000000000000)), "TestCTZ64#8");
60 
61 static_assert(0 == POPCOUNT<uint32_t>(0u), "TestPOPCOUNT32#1");
62 static_assert(1 == POPCOUNT<uint32_t>(8u), "TestPOPCOUNT32#2");
63 static_assert(15 == POPCOUNT<uint32_t>(0x55555554u), "TestPOPCOUNT32#3");
64 static_assert(16 == POPCOUNT<uint32_t>(0xaaaaaaaau), "TestPOPCOUNT32#4");
65 static_assert(31 == POPCOUNT<uint32_t>(0xfffffffeu), "TestPOPCOUNT32#5");
66 static_assert(32 == POPCOUNT<uint32_t>(0xffffffffu), "TestPOPCOUNT32#6");
67 
68 static_assert(0 == POPCOUNT<uint64_t>(UINT64_C(0)), "TestPOPCOUNT64#1");
69 static_assert(1 == POPCOUNT<uint64_t>(UINT64_C(0x40000)), "TestPOPCOUNT64#2");
70 static_assert(16 == POPCOUNT<uint64_t>(UINT64_C(0x1414141482828282)), "TestPOPCOUNT64#3");
71 static_assert(31 == POPCOUNT<uint64_t>(UINT64_C(0x0000ffff00007fff)), "TestPOPCOUNT64#4");
72 static_assert(32 == POPCOUNT<uint64_t>(UINT64_C(0x5555555555555555)), "TestPOPCOUNT64#5");
73 static_assert(48 == POPCOUNT<uint64_t>(UINT64_C(0x7777bbbbddddeeee)), "TestPOPCOUNT64#6");
74 static_assert(63 == POPCOUNT<uint64_t>(UINT64_C(0x7fffffffffffffff)), "TestPOPCOUNT64#7");
75 static_assert(64 == POPCOUNT<uint64_t>(UINT64_C(0xffffffffffffffff)), "TestPOPCOUNT64#8");
76 
77 static_assert(-1 == MostSignificantBit<uint32_t>(0u), "TestMSB32#1");
78 static_assert(0 == MostSignificantBit<uint32_t>(1u), "TestMSB32#2");
79 static_assert(31 == MostSignificantBit<uint32_t>(~static_cast<uint32_t>(0u)), "TestMSB32#3");
80 static_assert(2 == MostSignificantBit<uint32_t>(0b110), "TestMSB32#4");
81 static_assert(2 == MostSignificantBit<uint32_t>(0b100), "TestMSB32#5");
82 
83 static_assert(-1 == MostSignificantBit<uint64_t>(UINT64_C(0)), "TestMSB64#1");
84 static_assert(0 == MostSignificantBit<uint64_t>(UINT64_C(1)), "TestMSB64#2");
85 static_assert(63 == MostSignificantBit<uint64_t>(~UINT64_C(0)), "TestMSB64#3");
86 static_assert(34 == MostSignificantBit<uint64_t>(UINT64_C(0x700000000)), "TestMSB64#4");
87 static_assert(34 == MostSignificantBit<uint64_t>(UINT64_C(0x777777777)), "TestMSB64#5");
88 
89 static_assert(-1 == LeastSignificantBit<uint32_t>(0u), "TestLSB32#1");
90 static_assert(0 == LeastSignificantBit<uint32_t>(1u), "TestLSB32#1");
91 static_assert(0 == LeastSignificantBit<uint32_t>(~static_cast<uint32_t>(0u)), "TestLSB32#1");
92 static_assert(1 == LeastSignificantBit<uint32_t>(0b110), "TestLSB32#1");
93 static_assert(2 == LeastSignificantBit<uint32_t>(0b100), "TestLSB32#1");
94 
95 static_assert(-1 == LeastSignificantBit<uint64_t>(UINT64_C(0)), "TestLSB64#1");
96 static_assert(0 == LeastSignificantBit<uint64_t>(UINT64_C(1)), "TestLSB64#2");
97 static_assert(0 == LeastSignificantBit<uint64_t>(~UINT64_C(0)), "TestLSB64#3");
98 static_assert(12 == LeastSignificantBit<uint64_t>(UINT64_C(0x5000)), "TestLSB64#4");
99 static_assert(48 == LeastSignificantBit<uint64_t>(UINT64_C(0x5555000000000000)), "TestLSB64#5");
100 
101 static_assert(0u == MinimumBitsToStore<uint32_t>(0u), "TestMinBits2Store32#1");
102 static_assert(1u == MinimumBitsToStore<uint32_t>(1u), "TestMinBits2Store32#2");
103 static_assert(2u == MinimumBitsToStore<uint32_t>(0b10u), "TestMinBits2Store32#3");
104 static_assert(2u == MinimumBitsToStore<uint32_t>(0b11u), "TestMinBits2Store32#4");
105 static_assert(3u == MinimumBitsToStore<uint32_t>(0b100u), "TestMinBits2Store32#5");
106 static_assert(3u == MinimumBitsToStore<uint32_t>(0b110u), "TestMinBits2Store32#6");
107 static_assert(3u == MinimumBitsToStore<uint32_t>(0b101u), "TestMinBits2Store32#7");
108 static_assert(8u == MinimumBitsToStore<uint32_t>(0xFFu), "TestMinBits2Store32#8");
109 static_assert(32u == MinimumBitsToStore<uint32_t>(~static_cast<uint32_t>(0u)),
110               "TestMinBits2Store32#9");
111 
112 static_assert(0u == MinimumBitsToStore<uint64_t>(UINT64_C(0)), "TestMinBits2Store64#1");
113 static_assert(1u == MinimumBitsToStore<uint64_t>(UINT64_C(1)), "TestMinBits2Store64#2");
114 static_assert(2u == MinimumBitsToStore<uint64_t>(UINT64_C(0b10)), "TestMinBits2Store64#3");
115 static_assert(2u == MinimumBitsToStore<uint64_t>(UINT64_C(0b11)), "TestMinBits2Store64#4");
116 static_assert(3u == MinimumBitsToStore<uint64_t>(UINT64_C(0b100)), "TestMinBits2Store64#5");
117 static_assert(3u == MinimumBitsToStore<uint64_t>(UINT64_C(0b110)), "TestMinBits2Store64#6");
118 static_assert(3u == MinimumBitsToStore<uint64_t>(UINT64_C(0b101)), "TestMinBits2Store64#7");
119 static_assert(8u == MinimumBitsToStore<uint64_t>(UINT64_C(0xFF)), "TestMinBits2Store64#8");
120 static_assert(32u == MinimumBitsToStore<uint64_t>(UINT64_C(0xFFFFFFFF)), "TestMinBits2Store64#9");
121 static_assert(33u == MinimumBitsToStore<uint64_t>(UINT64_C(0x1FFFFFFFF)), "TestMinBits2Store64#10");
122 static_assert(64u == MinimumBitsToStore<uint64_t>(~UINT64_C(0)), "TestMinBits2Store64#11");
123 
124 static_assert(0 == RoundUpToPowerOfTwo<uint32_t>(0u), "TestRoundUpPowerOfTwo32#1");
125 static_assert(1 == RoundUpToPowerOfTwo<uint32_t>(1u), "TestRoundUpPowerOfTwo32#2");
126 static_assert(2 == RoundUpToPowerOfTwo<uint32_t>(2u), "TestRoundUpPowerOfTwo32#3");
127 static_assert(4 == RoundUpToPowerOfTwo<uint32_t>(3u), "TestRoundUpPowerOfTwo32#4");
128 static_assert(8 == RoundUpToPowerOfTwo<uint32_t>(7u), "TestRoundUpPowerOfTwo32#5");
129 static_assert(0x40000u == RoundUpToPowerOfTwo<uint32_t>(0x2aaaau),
130               "TestRoundUpPowerOfTwo32#6");
131 static_assert(0x80000000u == RoundUpToPowerOfTwo<uint32_t>(0x40000001u),
132               "TestRoundUpPowerOfTwo32#7");
133 static_assert(0x80000000u == RoundUpToPowerOfTwo<uint32_t>(0x80000000u),
134               "TestRoundUpPowerOfTwo32#8");
135 
136 static_assert(0 == RoundUpToPowerOfTwo<uint64_t>(UINT64_C(0)), "TestRoundUpPowerOfTwo64#1");
137 static_assert(1 == RoundUpToPowerOfTwo<uint64_t>(UINT64_C(1)), "TestRoundUpPowerOfTwo64#2");
138 static_assert(2 == RoundUpToPowerOfTwo<uint64_t>(UINT64_C(2)), "TestRoundUpPowerOfTwo64#3");
139 static_assert(4 == RoundUpToPowerOfTwo<uint64_t>(UINT64_C(3)), "TestRoundUpPowerOfTwo64#4");
140 static_assert(8 == RoundUpToPowerOfTwo<uint64_t>(UINT64_C(7)), "TestRoundUpPowerOfTwo64#5");
141 static_assert(UINT64_C(0x40000) == RoundUpToPowerOfTwo<uint64_t>(UINT64_C(0x2aaaa)),
142               "TestRoundUpPowerOfTwo64#6");
143 static_assert(
144     UINT64_C(0x8000000000000000) == RoundUpToPowerOfTwo<uint64_t>(UINT64_C(0x4000000000000001)),
145     "TestRoundUpPowerOfTwo64#7");
146 static_assert(
147     UINT64_C(0x8000000000000000) == RoundUpToPowerOfTwo<uint64_t>(UINT64_C(0x8000000000000000)),
148     "TestRoundUpPowerOfTwo64#8");
149 
150 static constexpr int64_t kInt32MinMinus1 =
151     static_cast<int64_t>(std::numeric_limits<int32_t>::min()) - 1;
152 static constexpr int64_t kInt32MaxPlus1 =
153     static_cast<int64_t>(std::numeric_limits<int32_t>::max()) + 1;
154 static constexpr int64_t kUint32MaxPlus1 =
155     static_cast<int64_t>(std::numeric_limits<uint32_t>::max()) + 1;
156 
TEST(BitUtilsTest,TestIsInt32)157 TEST(BitUtilsTest, TestIsInt32) {
158   EXPECT_FALSE(IsInt<int32_t>(1, -2));
159   EXPECT_TRUE(IsInt<int32_t>(1, -1));
160   EXPECT_TRUE(IsInt<int32_t>(1, 0));
161   EXPECT_FALSE(IsInt<int32_t>(1, 1));
162   EXPECT_FALSE(IsInt<int32_t>(4, -9));
163   EXPECT_TRUE(IsInt<int32_t>(4, -8));
164   EXPECT_TRUE(IsInt<int32_t>(4, 7));
165   EXPECT_FALSE(IsInt<int32_t>(4, 8));
166   EXPECT_FALSE(IsInt<int32_t>(31, std::numeric_limits<int32_t>::min()));
167   EXPECT_FALSE(IsInt<int32_t>(31, std::numeric_limits<int32_t>::max()));
168   EXPECT_TRUE(IsInt<int32_t>(32, std::numeric_limits<int32_t>::min()));
169   EXPECT_TRUE(IsInt<int32_t>(32, std::numeric_limits<int32_t>::max()));
170 }
171 
TEST(BitUtilsTest,TestIsInt64)172 TEST(BitUtilsTest, TestIsInt64) {
173   EXPECT_FALSE(IsInt<int64_t>(1, -2));
174   EXPECT_TRUE(IsInt<int64_t>(1, -1));
175   EXPECT_TRUE(IsInt<int64_t>(1, 0));
176   EXPECT_FALSE(IsInt<int64_t>(1, 1));
177   EXPECT_FALSE(IsInt<int64_t>(4, -9));
178   EXPECT_TRUE(IsInt<int64_t>(4, -8));
179   EXPECT_TRUE(IsInt<int64_t>(4, 7));
180   EXPECT_FALSE(IsInt<int64_t>(4, 8));
181   EXPECT_FALSE(IsInt<int64_t>(31, std::numeric_limits<int32_t>::min()));
182   EXPECT_FALSE(IsInt<int64_t>(31, std::numeric_limits<int32_t>::max()));
183   EXPECT_TRUE(IsInt<int64_t>(32, std::numeric_limits<int32_t>::min()));
184   EXPECT_TRUE(IsInt<int64_t>(32, std::numeric_limits<int32_t>::max()));
185   EXPECT_FALSE(IsInt<int64_t>(32, kInt32MinMinus1));
186   EXPECT_FALSE(IsInt<int64_t>(32, kInt32MaxPlus1));
187   EXPECT_FALSE(IsInt<int64_t>(63, std::numeric_limits<int64_t>::min()));
188   EXPECT_FALSE(IsInt<int64_t>(63, std::numeric_limits<int64_t>::max()));
189   EXPECT_TRUE(IsInt<int64_t>(64, std::numeric_limits<int64_t>::min()));
190   EXPECT_TRUE(IsInt<int64_t>(64, std::numeric_limits<int64_t>::max()));
191 }
192 
193 static_assert(!IsInt<1, int32_t>(-2), "TestIsInt32#1");
194 static_assert(IsInt<1, int32_t>(-1), "TestIsInt32#2");
195 static_assert(IsInt<1, int32_t>(0), "TestIsInt32#3");
196 static_assert(!IsInt<1, int32_t>(1), "TestIsInt32#4");
197 static_assert(!IsInt<4, int32_t>(-9), "TestIsInt32#5");
198 static_assert(IsInt<4, int32_t>(-8), "TestIsInt32#6");
199 static_assert(IsInt<4, int32_t>(7), "TestIsInt32#7");
200 static_assert(!IsInt<4, int32_t>(8), "TestIsInt32#8");
201 static_assert(!IsInt<31, int32_t>(std::numeric_limits<int32_t>::min()), "TestIsInt32#9");
202 static_assert(!IsInt<31, int32_t>(std::numeric_limits<int32_t>::max()), "TestIsInt32#10");
203 static_assert(IsInt<32, int32_t>(std::numeric_limits<int32_t>::min()), "TestIsInt32#11");
204 static_assert(IsInt<32, int32_t>(std::numeric_limits<int32_t>::max()), "TestIsInt32#12");
205 
206 static_assert(!IsInt<1, int64_t>(-2), "TestIsInt64#1");
207 static_assert(IsInt<1, int64_t>(-1), "TestIsInt64#2");
208 static_assert(IsInt<1, int64_t>(0), "TestIsInt64#3");
209 static_assert(!IsInt<1, int64_t>(1), "TestIsInt64#4");
210 static_assert(!IsInt<4, int64_t>(-9), "TestIsInt64#5");
211 static_assert(IsInt<4, int64_t>(-8), "TestIsInt64#6");
212 static_assert(IsInt<4, int64_t>(7), "TestIsInt64#7");
213 static_assert(!IsInt<4, int64_t>(8), "TestIsInt64#8");
214 static_assert(!IsInt<31, int64_t>(std::numeric_limits<int32_t>::min()), "TestIsInt64#9");
215 static_assert(!IsInt<31, int64_t>(std::numeric_limits<int32_t>::max()), "TestIsInt64#10");
216 static_assert(IsInt<32, int64_t>(std::numeric_limits<int32_t>::min()), "TestIsInt64#11");
217 static_assert(IsInt<32, int64_t>(std::numeric_limits<int32_t>::max()), "TestIsInt64#12");
218 static_assert(!IsInt<32, int64_t>(kInt32MinMinus1), "TestIsInt64#13");
219 static_assert(!IsInt<32, int64_t>(kInt32MaxPlus1), "TestIsInt64#14");
220 static_assert(!IsInt<63, int64_t>(std::numeric_limits<int64_t>::min()), "TestIsInt64#15");
221 static_assert(!IsInt<63, int64_t>(std::numeric_limits<int64_t>::max()), "TestIsInt64#16");
222 static_assert(IsInt<64, int64_t>(std::numeric_limits<int64_t>::min()), "TestIsInt64#17");
223 static_assert(IsInt<64, int64_t>(std::numeric_limits<int64_t>::max()), "TestIsInt64#18");
224 
225 static_assert(!IsUint<1, int32_t>(-1), "TestIsUint32#1");
226 static_assert(IsUint<1, int32_t>(0), "TestIsUint32#2");
227 static_assert(IsUint<1, int32_t>(1), "TestIsUint32#3");
228 static_assert(!IsUint<1, int32_t>(2), "TestIsUint32#4");
229 static_assert(!IsUint<4, int32_t>(-1), "TestIsUint32#5");
230 static_assert(IsUint<4, int32_t>(0), "TestIsUint32#6");
231 static_assert(IsUint<4, int32_t>(15), "TestIsUint32#7");
232 static_assert(!IsUint<4, int32_t>(16), "TestIsUint32#8");
233 static_assert(!IsUint<30, int32_t>(std::numeric_limits<int32_t>::max()), "TestIsUint32#9");
234 static_assert(IsUint<31, int32_t>(std::numeric_limits<int32_t>::max()), "TestIsUint32#10");
235 static_assert(!IsUint<32, int32_t>(-1), "TestIsUint32#11");
236 static_assert(IsUint<32, int32_t>(0), "TestIsUint32#11");
237 static_assert(IsUint<32, uint32_t>(static_cast<uint32_t>(-1)), "TestIsUint32#12");
238 
239 static_assert(!IsUint<1, int64_t>(-1), "TestIsUint64#1");
240 static_assert(IsUint<1, int64_t>(0), "TestIsUint64#2");
241 static_assert(IsUint<1, int64_t>(1), "TestIsUint64#3");
242 static_assert(!IsUint<1, int64_t>(2), "TestIsUint64#4");
243 static_assert(!IsUint<4, int64_t>(-1), "TestIsUint64#5");
244 static_assert(IsUint<4, int64_t>(0), "TestIsUint64#6");
245 static_assert(IsUint<4, int64_t>(15), "TestIsUint64#7");
246 static_assert(!IsUint<4, int64_t>(16), "TestIsUint64#8");
247 static_assert(!IsUint<30, int64_t>(std::numeric_limits<int32_t>::max()), "TestIsUint64#9");
248 static_assert(IsUint<31, int64_t>(std::numeric_limits<int32_t>::max()), "TestIsUint64#10");
249 static_assert(!IsUint<62, int64_t>(std::numeric_limits<int64_t>::max()), "TestIsUint64#11");
250 static_assert(IsUint<63, int64_t>(std::numeric_limits<int64_t>::max()), "TestIsUint64#12");
251 static_assert(!IsUint<64, int64_t>(-1), "TestIsUint64#13");
252 static_assert(IsUint<64, int64_t>(0), "TestIsUint64#14");
253 static_assert(IsUint<64, uint64_t>(static_cast<uint32_t>(-1)), "TestIsUint64#15");
254 
255 static_assert(!IsAbsoluteUint<1, int32_t>(-2), "TestIsAbsoluteUint32#1");
256 static_assert(IsAbsoluteUint<1, int32_t>(-1), "TestIsAbsoluteUint32#2");
257 static_assert(IsAbsoluteUint<1, int32_t>(0), "TestIsAbsoluteUint32#3");
258 static_assert(IsAbsoluteUint<1, int32_t>(1), "TestIsAbsoluteUint32#4");
259 static_assert(!IsAbsoluteUint<1, int32_t>(2), "TestIsAbsoluteUint32#5");
260 static_assert(!IsAbsoluteUint<4, int32_t>(-16), "TestIsAbsoluteUint32#6");
261 static_assert(IsAbsoluteUint<4, int32_t>(-15), "TestIsAbsoluteUint32#7");
262 static_assert(IsAbsoluteUint<4, int32_t>(0), "TestIsAbsoluteUint32#8");
263 static_assert(IsAbsoluteUint<4, int32_t>(15), "TestIsAbsoluteUint32#9");
264 static_assert(!IsAbsoluteUint<4, int32_t>(16), "TestIsAbsoluteUint32#10");
265 static_assert(!IsAbsoluteUint<30, int32_t>(std::numeric_limits<int32_t>::max()),
266               "TestIsAbsoluteUint32#11");
267 static_assert(IsAbsoluteUint<31, int32_t>(std::numeric_limits<int32_t>::max()),
268               "TestIsAbsoluteUint32#12");
269 static_assert(!IsAbsoluteUint<31, int32_t>(std::numeric_limits<int32_t>::min()),
270               "TestIsAbsoluteUint32#13");
271 static_assert(IsAbsoluteUint<31, int32_t>(std::numeric_limits<int32_t>::min() + 1),
272               "TestIsAbsoluteUint32#14");
273 static_assert(IsAbsoluteUint<32, int32_t>(std::numeric_limits<int32_t>::max()),
274               "TestIsAbsoluteUint32#15");
275 static_assert(IsAbsoluteUint<32, int32_t>(std::numeric_limits<int32_t>::min()),
276               "TestIsAbsoluteUint32#16");
277 static_assert(IsAbsoluteUint<32, int32_t>(0), "TestIsAbsoluteUint32#17");
278 
279 static_assert(!IsAbsoluteUint<1, int64_t>(-2), "TestIsAbsoluteUint64#1");
280 static_assert(IsAbsoluteUint<1, int64_t>(-1), "TestIsAbsoluteUint64#2");
281 static_assert(IsAbsoluteUint<1, int64_t>(0), "TestIsAbsoluteUint64#3");
282 static_assert(IsAbsoluteUint<1, int64_t>(1), "TestIsAbsoluteUint64#4");
283 static_assert(!IsAbsoluteUint<1, int64_t>(2), "TestIsAbsoluteUint64#5");
284 static_assert(!IsAbsoluteUint<4, int64_t>(-16), "TestIsAbsoluteUint64#6");
285 static_assert(IsAbsoluteUint<4, int64_t>(-15), "TestIsAbsoluteUint64#7");
286 static_assert(IsAbsoluteUint<4, int64_t>(0), "TestIsAbsoluteUint64#8");
287 static_assert(IsAbsoluteUint<4, int64_t>(15), "TestIsAbsoluteUint64#9");
288 static_assert(!IsAbsoluteUint<4, int64_t>(16), "TestIsAbsoluteUint64#10");
289 static_assert(!IsAbsoluteUint<30, int64_t>(std::numeric_limits<int32_t>::max()),
290               "TestIsAbsoluteUint64#11");
291 static_assert(IsAbsoluteUint<31, int64_t>(std::numeric_limits<int32_t>::max()),
292               "TestIsAbsoluteUint64#12");
293 static_assert(!IsAbsoluteUint<31, int64_t>(std::numeric_limits<int32_t>::min()),
294               "TestIsAbsoluteUint64#13");
295 static_assert(IsAbsoluteUint<31, int64_t>(std::numeric_limits<int32_t>::min() + 1),
296               "TestIsAbsoluteUint64#14");
297 static_assert(IsAbsoluteUint<32, int64_t>(std::numeric_limits<int32_t>::max()),
298               "TestIsAbsoluteUint64#15");
299 static_assert(IsAbsoluteUint<32, int64_t>(std::numeric_limits<int32_t>::min()),
300               "TestIsAbsoluteUint64#16");
301 static_assert(!IsAbsoluteUint<62, int64_t>(std::numeric_limits<int64_t>::max()),
302               "TestIsAbsoluteUint64#17");
303 static_assert(IsAbsoluteUint<63, int64_t>(std::numeric_limits<int64_t>::max()),
304               "TestIsAbsoluteUint64#18");
305 static_assert(!IsAbsoluteUint<63, int64_t>(std::numeric_limits<int64_t>::min()),
306               "TestIsAbsoluteUint64#19");
307 static_assert(IsAbsoluteUint<63, int64_t>(std::numeric_limits<int64_t>::min() + 1),
308               "TestIsAbsoluteUint64#20");
309 static_assert(IsAbsoluteUint<64, int64_t>(std::numeric_limits<int64_t>::max()),
310               "TestIsAbsoluteUint64#21");
311 static_assert(IsAbsoluteUint<64, int64_t>(std::numeric_limits<int64_t>::min()),
312               "TestIsAbsoluteUint64#22");
313 static_assert(!IsAbsoluteUint<32, int64_t>(-kUint32MaxPlus1), "TestIsAbsoluteUint64#23");
314 static_assert(IsAbsoluteUint<32, int64_t>(-kUint32MaxPlus1 + 1), "TestIsAbsoluteUint64#24");
315 static_assert(IsAbsoluteUint<32, int64_t>(0), "TestIsAbsoluteUint64#25");
316 static_assert(IsAbsoluteUint<64, int64_t>(0), "TestIsAbsoluteUint64#26");
317 static_assert(IsAbsoluteUint<32, int64_t>(std::numeric_limits<uint32_t>::max()),
318               "TestIsAbsoluteUint64#27");
319 static_assert(!IsAbsoluteUint<32, int64_t>(kUint32MaxPlus1), "TestIsAbsoluteUint64#28");
320 
321 template <typename Container>
CheckElements(const std::initializer_list<uint32_t> & expected,const Container & elements)322 void CheckElements(const std::initializer_list<uint32_t>& expected, const Container& elements) {
323   auto expected_it = expected.begin();
324   auto element_it = elements.begin();
325   size_t idx = 0u;
326   while (expected_it != expected.end() && element_it != elements.end()) {
327     EXPECT_EQ(*expected_it, *element_it) << idx;
328     ++idx;
329     ++expected_it;
330     ++element_it;
331   }
332   ASSERT_TRUE(expected_it == expected.end() && element_it == elements.end())
333       << std::boolalpha << (expected_it == expected.end()) << " " << (element_it == elements.end());
334 }
335 
TEST(BitUtilsTest,TestLowToHighBits32)336 TEST(BitUtilsTest, TestLowToHighBits32) {
337   CheckElements({}, LowToHighBits<uint32_t>(0u));
338   CheckElements({0}, LowToHighBits<uint32_t>(1u));
339   CheckElements({15}, LowToHighBits<uint32_t>(0x8000u));
340   CheckElements({31}, LowToHighBits<uint32_t>(0x80000000u));
341   CheckElements({0, 31}, LowToHighBits<uint32_t>(0x80000001u));
342   CheckElements({0, 1, 2, 3, 4, 5, 6, 7, 31}, LowToHighBits<uint32_t>(0x800000ffu));
343   CheckElements({0, 8, 16, 24, 31}, LowToHighBits<uint32_t>(0x81010101u));
344   CheckElements({16, 17, 30, 31}, LowToHighBits<uint32_t>(0xc0030000u));
345   CheckElements({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
346                  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
347                 LowToHighBits<uint32_t>(0xffffffffu));
348 }
349 
TEST(BitUtilsTest,TestLowToHighBits64)350 TEST(BitUtilsTest, TestLowToHighBits64) {
351   CheckElements({}, LowToHighBits<uint64_t>(UINT64_C(0)));
352   CheckElements({0}, LowToHighBits<uint64_t>(UINT64_C(1)));
353   CheckElements({32}, LowToHighBits<uint64_t>(UINT64_C(0x100000000)));
354   CheckElements({63}, LowToHighBits<uint64_t>(UINT64_C(0x8000000000000000)));
355   CheckElements({0, 63}, LowToHighBits<uint64_t>(UINT64_C(0x8000000000000001)));
356   CheckElements({0, 1, 2, 3, 4, 5, 6, 7, 63},
357                 LowToHighBits<uint64_t>(UINT64_C(0x80000000000000ff)));
358   CheckElements({0, 8, 16, 24, 32, 40, 48, 56, 63},
359                 LowToHighBits<uint64_t>(UINT64_C(0x8101010101010101)));
360   CheckElements({16, 17, 62, 63}, LowToHighBits<uint64_t>(UINT64_C(0xc000000000030000)));
361   CheckElements({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
362                  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
363                  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
364                  48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63},
365                 LowToHighBits<uint64_t>(UINT64_C(0xffffffffffffffff)));
366 }
367 
TEST(BitUtilsTest,TestHighToLowBits32)368 TEST(BitUtilsTest, TestHighToLowBits32) {
369   CheckElements({}, HighToLowBits<uint32_t>(0u));
370   CheckElements({0}, HighToLowBits<uint32_t>(1u));
371   CheckElements({15}, HighToLowBits<uint32_t>(0x8000u));
372   CheckElements({31}, HighToLowBits<uint32_t>(0x80000000u));
373   CheckElements({31, 0}, HighToLowBits<uint32_t>(0x80000001u));
374   CheckElements({31, 7, 6, 5, 4, 3, 2, 1, 0}, HighToLowBits<uint32_t>(0x800000ffu));
375   CheckElements({31, 24, 16, 8, 0}, HighToLowBits<uint32_t>(0x81010101u));
376   CheckElements({31, 30, 17, 16}, HighToLowBits<uint32_t>(0xc0030000u));
377   CheckElements({31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
378                  15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
379                 HighToLowBits<uint32_t>(0xffffffffu));
380 }
381 
TEST(BitUtilsTest,TestHighToLowBits64)382 TEST(BitUtilsTest, TestHighToLowBits64) {
383   CheckElements({}, HighToLowBits<uint64_t>(UINT64_C(0)));
384   CheckElements({0}, HighToLowBits<uint64_t>(UINT64_C(1)));
385   CheckElements({32}, HighToLowBits<uint64_t>(UINT64_C(0x100000000)));
386   CheckElements({63}, HighToLowBits<uint64_t>(UINT64_C(0x8000000000000000)));
387   CheckElements({63, 0}, HighToLowBits<uint64_t>(UINT64_C(0x8000000000000001)));
388   CheckElements({63, 7, 6, 5, 4, 3, 2, 1, 0},
389                 HighToLowBits<uint64_t>(UINT64_C(0x80000000000000ff)));
390   CheckElements({63, 56, 48, 40, 32, 24, 16, 8, 0},
391                 HighToLowBits<uint64_t>(UINT64_C(0x8101010101010101)));
392   CheckElements({63, 62, 17, 16}, HighToLowBits<uint64_t>(UINT64_C(0xc000000000030000)));
393   CheckElements({63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48,
394                  47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32,
395                  31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
396                  15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
397                 HighToLowBits<uint64_t>(UINT64_C(0xffffffffffffffff)));
398 }
399 
400 }  // namespace art
401