OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <limits> | 5 #include <limits> |
6 | 6 |
7 #include "src/base/bits.h" | 7 #include "src/base/bits.h" |
8 #include "src/base/macros.h" | 8 #include "src/base/macros.h" |
9 #include "testing/gtest-support.h" | 9 #include "testing/gtest-support.h" |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 TEST(Bits, CountPopulation32) { | 21 TEST(Bits, CountPopulation32) { |
22 EXPECT_EQ(0u, CountPopulation32(0)); | 22 EXPECT_EQ(0u, CountPopulation32(0)); |
23 EXPECT_EQ(1u, CountPopulation32(1)); | 23 EXPECT_EQ(1u, CountPopulation32(1)); |
24 EXPECT_EQ(8u, CountPopulation32(0x11111111)); | 24 EXPECT_EQ(8u, CountPopulation32(0x11111111)); |
25 EXPECT_EQ(16u, CountPopulation32(0xf0f0f0f0)); | 25 EXPECT_EQ(16u, CountPopulation32(0xf0f0f0f0)); |
26 EXPECT_EQ(24u, CountPopulation32(0xfff0f0ff)); | 26 EXPECT_EQ(24u, CountPopulation32(0xfff0f0ff)); |
27 EXPECT_EQ(32u, CountPopulation32(0xffffffff)); | 27 EXPECT_EQ(32u, CountPopulation32(0xffffffff)); |
28 } | 28 } |
29 | 29 |
30 | 30 |
| 31 TEST(Bits, CountPopulation64) { |
| 32 EXPECT_EQ(0u, CountPopulation64(0)); |
| 33 EXPECT_EQ(1u, CountPopulation64(1)); |
| 34 EXPECT_EQ(2u, CountPopulation64(0x8000000000000001)); |
| 35 EXPECT_EQ(8u, CountPopulation64(0x11111111)); |
| 36 EXPECT_EQ(16u, CountPopulation64(0xf0f0f0f0)); |
| 37 EXPECT_EQ(24u, CountPopulation64(0xfff0f0ff)); |
| 38 EXPECT_EQ(32u, CountPopulation64(0xffffffff)); |
| 39 EXPECT_EQ(16u, CountPopulation64(0x1111111111111111)); |
| 40 EXPECT_EQ(32u, CountPopulation64(0xf0f0f0f0f0f0f0f0)); |
| 41 EXPECT_EQ(48u, CountPopulation64(0xfff0f0fffff0f0ff)); |
| 42 EXPECT_EQ(64u, CountPopulation64(0xffffffffffffffff)); |
| 43 } |
| 44 |
| 45 |
31 TEST(Bits, CountLeadingZeros32) { | 46 TEST(Bits, CountLeadingZeros32) { |
32 EXPECT_EQ(32u, CountLeadingZeros32(0)); | 47 EXPECT_EQ(32u, CountLeadingZeros32(0)); |
33 EXPECT_EQ(31u, CountLeadingZeros32(1)); | 48 EXPECT_EQ(31u, CountLeadingZeros32(1)); |
34 TRACED_FORRANGE(uint32_t, shift, 0, 31) { | 49 TRACED_FORRANGE(uint32_t, shift, 0, 31) { |
35 EXPECT_EQ(31u - shift, CountLeadingZeros32(1u << shift)); | 50 EXPECT_EQ(31u - shift, CountLeadingZeros32(1u << shift)); |
36 } | 51 } |
37 EXPECT_EQ(4u, CountLeadingZeros32(0x0f0f0f0f)); | 52 EXPECT_EQ(4u, CountLeadingZeros32(0x0f0f0f0f)); |
38 } | 53 } |
39 | 54 |
40 | 55 |
| 56 TEST(Bits, CountLeadingZeros64) { |
| 57 EXPECT_EQ(64u, CountLeadingZeros64(0)); |
| 58 EXPECT_EQ(63u, CountLeadingZeros64(1)); |
| 59 TRACED_FORRANGE(uint32_t, shift, 0, 63) { |
| 60 EXPECT_EQ(63u - shift, CountLeadingZeros64(V8_UINT64_C(1) << shift)); |
| 61 } |
| 62 EXPECT_EQ(36u, CountLeadingZeros64(0x0f0f0f0f)); |
| 63 EXPECT_EQ(4u, CountLeadingZeros64(0x0f0f0f0f00000000)); |
| 64 } |
| 65 |
| 66 |
41 TEST(Bits, CountTrailingZeros32) { | 67 TEST(Bits, CountTrailingZeros32) { |
42 EXPECT_EQ(32u, CountTrailingZeros32(0)); | 68 EXPECT_EQ(32u, CountTrailingZeros32(0)); |
43 EXPECT_EQ(31u, CountTrailingZeros32(0x80000000)); | 69 EXPECT_EQ(31u, CountTrailingZeros32(0x80000000)); |
44 TRACED_FORRANGE(uint32_t, shift, 0, 31) { | 70 TRACED_FORRANGE(uint32_t, shift, 0, 31) { |
45 EXPECT_EQ(shift, CountTrailingZeros32(1u << shift)); | 71 EXPECT_EQ(shift, CountTrailingZeros32(1u << shift)); |
46 } | 72 } |
47 EXPECT_EQ(4u, CountTrailingZeros32(0xf0f0f0f0)); | 73 EXPECT_EQ(4u, CountTrailingZeros32(0xf0f0f0f0)); |
48 } | 74 } |
49 | 75 |
50 | 76 |
| 77 TEST(Bits, CountTrailingZeros64) { |
| 78 EXPECT_EQ(64u, CountTrailingZeros64(0)); |
| 79 EXPECT_EQ(63u, CountTrailingZeros64(0x8000000000000000)); |
| 80 TRACED_FORRANGE(uint32_t, shift, 0, 63) { |
| 81 EXPECT_EQ(shift, CountTrailingZeros64(V8_UINT64_C(1) << shift)); |
| 82 } |
| 83 EXPECT_EQ(4u, CountTrailingZeros64(0xf0f0f0f0)); |
| 84 EXPECT_EQ(36u, CountTrailingZeros64(0xf0f0f0f000000000)); |
| 85 } |
| 86 |
| 87 |
51 TEST(Bits, IsPowerOfTwo32) { | 88 TEST(Bits, IsPowerOfTwo32) { |
52 EXPECT_FALSE(IsPowerOfTwo32(0U)); | 89 EXPECT_FALSE(IsPowerOfTwo32(0U)); |
53 TRACED_FORRANGE(uint32_t, shift, 0, 31) { | 90 TRACED_FORRANGE(uint32_t, shift, 0, 31) { |
54 EXPECT_TRUE(IsPowerOfTwo32(1U << shift)); | 91 EXPECT_TRUE(IsPowerOfTwo32(1U << shift)); |
55 EXPECT_FALSE(IsPowerOfTwo32((1U << shift) + 5U)); | 92 EXPECT_FALSE(IsPowerOfTwo32((1U << shift) + 5U)); |
56 EXPECT_FALSE(IsPowerOfTwo32(~(1U << shift))); | 93 EXPECT_FALSE(IsPowerOfTwo32(~(1U << shift))); |
57 } | 94 } |
58 TRACED_FORRANGE(uint32_t, shift, 2, 31) { | 95 TRACED_FORRANGE(uint32_t, shift, 2, 31) { |
59 EXPECT_FALSE(IsPowerOfTwo32((1U << shift) - 1U)); | 96 EXPECT_FALSE(IsPowerOfTwo32((1U << shift) - 1U)); |
60 } | 97 } |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 TRACED_FORRANGE(int32_t, j, 1, i) { | 195 TRACED_FORRANGE(int32_t, j, 1, i) { |
159 EXPECT_FALSE(SignedSubOverflow32(i, j, &val)); | 196 EXPECT_FALSE(SignedSubOverflow32(i, j, &val)); |
160 EXPECT_EQ(i - j, val); | 197 EXPECT_EQ(i - j, val); |
161 } | 198 } |
162 } | 199 } |
163 } | 200 } |
164 | 201 |
165 } // namespace bits | 202 } // namespace bits |
166 } // namespace base | 203 } // namespace base |
167 } // namespace v8 | 204 } // namespace v8 |
OLD | NEW |