1//===- unittests/Support/MathExtrasTest.cpp - math utils tests ------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "gtest/gtest.h"
11#include "llvm/Support/MathExtras.h"
12
13using namespace llvm;
14
15namespace {
16
17TEST(MathExtras, countTrailingZeros) {
18  uint8_t Z8 = 0;
19  uint16_t Z16 = 0;
20  uint32_t Z32 = 0;
21  uint64_t Z64 = 0;
22  EXPECT_EQ(8u, countTrailingZeros(Z8));
23  EXPECT_EQ(16u, countTrailingZeros(Z16));
24  EXPECT_EQ(32u, countTrailingZeros(Z32));
25  EXPECT_EQ(64u, countTrailingZeros(Z64));
26
27  uint8_t NZ8 = 42;
28  uint16_t NZ16 = 42;
29  uint32_t NZ32 = 42;
30  uint64_t NZ64 = 42;
31  EXPECT_EQ(1u, countTrailingZeros(NZ8));
32  EXPECT_EQ(1u, countTrailingZeros(NZ16));
33  EXPECT_EQ(1u, countTrailingZeros(NZ32));
34  EXPECT_EQ(1u, countTrailingZeros(NZ64));
35}
36
37TEST(MathExtras, countLeadingZeros) {
38  uint8_t Z8 = 0;
39  uint16_t Z16 = 0;
40  uint32_t Z32 = 0;
41  uint64_t Z64 = 0;
42  EXPECT_EQ(8u, countLeadingZeros(Z8));
43  EXPECT_EQ(16u, countLeadingZeros(Z16));
44  EXPECT_EQ(32u, countLeadingZeros(Z32));
45  EXPECT_EQ(64u, countLeadingZeros(Z64));
46
47  uint8_t NZ8 = 42;
48  uint16_t NZ16 = 42;
49  uint32_t NZ32 = 42;
50  uint64_t NZ64 = 42;
51  EXPECT_EQ(2u, countLeadingZeros(NZ8));
52  EXPECT_EQ(10u, countLeadingZeros(NZ16));
53  EXPECT_EQ(26u, countLeadingZeros(NZ32));
54  EXPECT_EQ(58u, countLeadingZeros(NZ64));
55
56  EXPECT_EQ(8u, countLeadingZeros(0x00F000FFu));
57  EXPECT_EQ(8u, countLeadingZeros(0x00F12345u));
58  for (unsigned i = 0; i <= 30; ++i) {
59    EXPECT_EQ(31 - i, countLeadingZeros(1u << i));
60  }
61
62  EXPECT_EQ(8u, countLeadingZeros(0x00F1234500F12345ULL));
63  EXPECT_EQ(1u, countLeadingZeros(1ULL << 62));
64  for (unsigned i = 0; i <= 62; ++i) {
65    EXPECT_EQ(63 - i, countLeadingZeros(1ULL << i));
66  }
67}
68
69TEST(MathExtras, findFirstSet) {
70  uint8_t Z8 = 0;
71  uint16_t Z16 = 0;
72  uint32_t Z32 = 0;
73  uint64_t Z64 = 0;
74  EXPECT_EQ(0xFFULL, findFirstSet(Z8));
75  EXPECT_EQ(0xFFFFULL, findFirstSet(Z16));
76  EXPECT_EQ(0xFFFFFFFFULL, findFirstSet(Z32));
77  EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, findFirstSet(Z64));
78
79  uint8_t NZ8 = 42;
80  uint16_t NZ16 = 42;
81  uint32_t NZ32 = 42;
82  uint64_t NZ64 = 42;
83  EXPECT_EQ(1u, findFirstSet(NZ8));
84  EXPECT_EQ(1u, findFirstSet(NZ16));
85  EXPECT_EQ(1u, findFirstSet(NZ32));
86  EXPECT_EQ(1u, findFirstSet(NZ64));
87}
88
89TEST(MathExtras, findLastSet) {
90  uint8_t Z8 = 0;
91  uint16_t Z16 = 0;
92  uint32_t Z32 = 0;
93  uint64_t Z64 = 0;
94  EXPECT_EQ(0xFFULL, findLastSet(Z8));
95  EXPECT_EQ(0xFFFFULL, findLastSet(Z16));
96  EXPECT_EQ(0xFFFFFFFFULL, findLastSet(Z32));
97  EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, findLastSet(Z64));
98
99  uint8_t NZ8 = 42;
100  uint16_t NZ16 = 42;
101  uint32_t NZ32 = 42;
102  uint64_t NZ64 = 42;
103  EXPECT_EQ(5u, findLastSet(NZ8));
104  EXPECT_EQ(5u, findLastSet(NZ16));
105  EXPECT_EQ(5u, findLastSet(NZ32));
106  EXPECT_EQ(5u, findLastSet(NZ64));
107}
108
109TEST(MathExtras, reverseBits) {
110  uint8_t NZ8 = 42;
111  uint16_t NZ16 = 42;
112  uint32_t NZ32 = 42;
113  uint64_t NZ64 = 42;
114  EXPECT_EQ(0x54ULL, reverseBits(NZ8));
115  EXPECT_EQ(0x5400ULL, reverseBits(NZ16));
116  EXPECT_EQ(0x54000000ULL, reverseBits(NZ32));
117  EXPECT_EQ(0x5400000000000000ULL, reverseBits(NZ64));
118}
119
120TEST(MathExtras, isPowerOf2_32) {
121  EXPECT_TRUE(isPowerOf2_32(1 << 6));
122  EXPECT_TRUE(isPowerOf2_32(1 << 12));
123  EXPECT_FALSE(isPowerOf2_32((1 << 19) + 3));
124  EXPECT_FALSE(isPowerOf2_32(0xABCDEF0));
125}
126
127TEST(MathExtras, isPowerOf2_64) {
128  EXPECT_TRUE(isPowerOf2_64(1LL << 46));
129  EXPECT_TRUE(isPowerOf2_64(1LL << 12));
130  EXPECT_FALSE(isPowerOf2_64((1LL << 53) + 3));
131  EXPECT_FALSE(isPowerOf2_64(0xABCDEF0ABCDEF0LL));
132}
133
134TEST(MathExtras, ByteSwap_32) {
135  EXPECT_EQ(0x44332211u, ByteSwap_32(0x11223344));
136  EXPECT_EQ(0xDDCCBBAAu, ByteSwap_32(0xAABBCCDD));
137}
138
139TEST(MathExtras, ByteSwap_64) {
140  EXPECT_EQ(0x8877665544332211ULL, ByteSwap_64(0x1122334455667788LL));
141  EXPECT_EQ(0x1100FFEEDDCCBBAAULL, ByteSwap_64(0xAABBCCDDEEFF0011LL));
142}
143
144TEST(MathExtras, CountLeadingOnes_32) {
145  for (int i = 30; i >= 0; --i) {
146    // Start with all ones and unset some bit.
147    EXPECT_EQ(31u - i, CountLeadingOnes_32(0xFFFFFFFF ^ (1 << i)));
148  }
149}
150
151TEST(MathExtras, CountLeadingOnes_64) {
152  for (int i = 62; i >= 0; --i) {
153    // Start with all ones and unset some bit.
154    EXPECT_EQ(63u - i, CountLeadingOnes_64(0xFFFFFFFFFFFFFFFFLL ^ (1LL << i)));
155  }
156  for (int i = 30; i >= 0; --i) {
157    // Start with all ones and unset some bit.
158    EXPECT_EQ(31u - i, CountLeadingOnes_32(0xFFFFFFFF ^ (1 << i)));
159  }
160}
161
162TEST(MathExtras, FloatBits) {
163  static const float kValue = 5632.34f;
164  EXPECT_FLOAT_EQ(kValue, BitsToFloat(FloatToBits(kValue)));
165}
166
167TEST(MathExtras, DoubleBits) {
168  static const double kValue = 87987234.983498;
169  EXPECT_FLOAT_EQ(kValue, BitsToDouble(DoubleToBits(kValue)));
170}
171
172TEST(MathExtras, MinAlign) {
173  EXPECT_EQ(1u, MinAlign(2, 3));
174  EXPECT_EQ(2u, MinAlign(2, 4));
175  EXPECT_EQ(1u, MinAlign(17, 64));
176  EXPECT_EQ(256u, MinAlign(256, 512));
177}
178
179TEST(MathExtras, NextPowerOf2) {
180  EXPECT_EQ(4u, NextPowerOf2(3));
181  EXPECT_EQ(16u, NextPowerOf2(15));
182  EXPECT_EQ(256u, NextPowerOf2(128));
183}
184
185TEST(MathExtras, RoundUpToAlignment) {
186  EXPECT_EQ(8u, RoundUpToAlignment(5, 8));
187  EXPECT_EQ(24u, RoundUpToAlignment(17, 8));
188  EXPECT_EQ(0u, RoundUpToAlignment(~0LL, 8));
189}
190
191}
192