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 
13 using namespace llvm;
14 
15 namespace {
16 
TEST(MathExtras,countTrailingZeros)17 TEST(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 
TEST(MathExtras,countLeadingZeros)37 TEST(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 
TEST(MathExtras,findFirstSet)69 TEST(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 
TEST(MathExtras,findLastSet)89 TEST(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 
TEST(MathExtras,reverseBits)109 TEST(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 
TEST(MathExtras,isPowerOf2_32)120 TEST(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 
TEST(MathExtras,isPowerOf2_64)127 TEST(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 
TEST(MathExtras,ByteSwap_32)134 TEST(MathExtras, ByteSwap_32) {
135   EXPECT_EQ(0x44332211u, ByteSwap_32(0x11223344));
136   EXPECT_EQ(0xDDCCBBAAu, ByteSwap_32(0xAABBCCDD));
137 }
138 
TEST(MathExtras,ByteSwap_64)139 TEST(MathExtras, ByteSwap_64) {
140   EXPECT_EQ(0x8877665544332211ULL, ByteSwap_64(0x1122334455667788LL));
141   EXPECT_EQ(0x1100FFEEDDCCBBAAULL, ByteSwap_64(0xAABBCCDDEEFF0011LL));
142 }
143 
TEST(MathExtras,countLeadingOnes)144 TEST(MathExtras, countLeadingOnes) {
145   for (int i = 30; i >= 0; --i) {
146     // Start with all ones and unset some bit.
147     EXPECT_EQ(31u - i, countLeadingOnes(0xFFFFFFFF ^ (1 << i)));
148   }
149   for (int i = 62; i >= 0; --i) {
150     // Start with all ones and unset some bit.
151     EXPECT_EQ(63u - i, countLeadingOnes(0xFFFFFFFFFFFFFFFFULL ^ (1LL << i)));
152   }
153   for (int i = 30; i >= 0; --i) {
154     // Start with all ones and unset some bit.
155     EXPECT_EQ(31u - i, countLeadingOnes(0xFFFFFFFF ^ (1 << i)));
156   }
157 }
158 
TEST(MathExtras,FloatBits)159 TEST(MathExtras, FloatBits) {
160   static const float kValue = 5632.34f;
161   EXPECT_FLOAT_EQ(kValue, BitsToFloat(FloatToBits(kValue)));
162 }
163 
TEST(MathExtras,DoubleBits)164 TEST(MathExtras, DoubleBits) {
165   static const double kValue = 87987234.983498;
166   EXPECT_FLOAT_EQ(kValue, BitsToDouble(DoubleToBits(kValue)));
167 }
168 
TEST(MathExtras,MinAlign)169 TEST(MathExtras, MinAlign) {
170   EXPECT_EQ(1u, MinAlign(2, 3));
171   EXPECT_EQ(2u, MinAlign(2, 4));
172   EXPECT_EQ(1u, MinAlign(17, 64));
173   EXPECT_EQ(256u, MinAlign(256, 512));
174 }
175 
TEST(MathExtras,NextPowerOf2)176 TEST(MathExtras, NextPowerOf2) {
177   EXPECT_EQ(4u, NextPowerOf2(3));
178   EXPECT_EQ(16u, NextPowerOf2(15));
179   EXPECT_EQ(256u, NextPowerOf2(128));
180 }
181 
TEST(MathExtras,RoundUpToAlignment)182 TEST(MathExtras, RoundUpToAlignment) {
183   EXPECT_EQ(8u, RoundUpToAlignment(5, 8));
184   EXPECT_EQ(24u, RoundUpToAlignment(17, 8));
185   EXPECT_EQ(0u, RoundUpToAlignment(~0LL, 8));
186 }
187 
188 }
189