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