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