1 //===- llvm/unittest/Support/MathExtrasTest.cpp - math utils tests --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
11 #include "llvm/Support/MathExtras.h"
17 TEST(MathExtras
, isPowerOf2_32
) {
18 EXPECT_TRUE(isPowerOf2_32(1 << 6));
19 EXPECT_TRUE(isPowerOf2_32(1 << 12));
20 EXPECT_FALSE(isPowerOf2_32((1 << 19) + 3));
21 EXPECT_FALSE(isPowerOf2_32(0xABCDEF0));
24 TEST(MathExtras
, isPowerOf2_64
) {
25 EXPECT_TRUE(isPowerOf2_64(1LL << 46));
26 EXPECT_TRUE(isPowerOf2_64(1LL << 12));
27 EXPECT_FALSE(isPowerOf2_64((1LL << 53) + 3));
28 EXPECT_FALSE(isPowerOf2_64(0xABCDEF0ABCDEF0LL
));
31 TEST(MathExtras
, ByteSwap_32
) {
32 EXPECT_EQ(0x44332211u
, ByteSwap_32(0x11223344));
33 EXPECT_EQ(0xDDCCBBAAu
, ByteSwap_32(0xAABBCCDD));
36 TEST(MathExtras
, ByteSwap_64
) {
37 EXPECT_EQ(0x8877665544332211ULL
, ByteSwap_64(0x1122334455667788LL
));
38 EXPECT_EQ(0x1100FFEEDDCCBBAAULL
, ByteSwap_64(0xAABBCCDDEEFF0011LL
));
41 TEST(MathExtras
, CountLeadingZeros_32
) {
42 EXPECT_EQ(8u, CountLeadingZeros_32(0x00F000FF));
43 EXPECT_EQ(8u, CountLeadingZeros_32(0x00F12345));
44 for (unsigned i
= 0; i
<= 30; ++i
) {
45 EXPECT_EQ(31 - i
, CountLeadingZeros_32(1 << i
));
49 TEST(MathExtras
, CountLeadingZeros_64
) {
50 EXPECT_EQ(8u, CountLeadingZeros_64(0x00F1234500F12345LL
));
51 EXPECT_EQ(1u, CountLeadingZeros_64(1LL << 62));
52 for (unsigned i
= 0; i
<= 62; ++i
) {
53 EXPECT_EQ(63 - i
, CountLeadingZeros_64(1LL << i
));
57 TEST(MathExtras
, CountLeadingOnes_32
) {
58 for (int i
= 30; i
>= 0; --i
) {
59 // Start with all ones and unset some bit.
60 EXPECT_EQ(31u - i
, CountLeadingOnes_32(0xFFFFFFFF ^ (1 << i
)));
64 TEST(MathExtras
, CountLeadingOnes_64
) {
65 for (int i
= 62; i
>= 0; --i
) {
66 // Start with all ones and unset some bit.
67 EXPECT_EQ(63u - i
, CountLeadingOnes_64(0xFFFFFFFFFFFFFFFFLL
^ (1LL << i
)));
69 for (int i
= 30; i
>= 0; --i
) {
70 // Start with all ones and unset some bit.
71 EXPECT_EQ(31u - i
, CountLeadingOnes_32(0xFFFFFFFF ^ (1 << i
)));
75 TEST(MathExtras
, FloatBits
) {
76 static const float kValue
= 5632.34;
77 EXPECT_FLOAT_EQ(kValue
, BitsToFloat(FloatToBits(kValue
)));
80 TEST(MathExtras
, DoubleBits
) {
81 static const double kValue
= 87987234.983498;
82 EXPECT_FLOAT_EQ(kValue
, BitsToDouble(DoubleToBits(kValue
)));
85 TEST(MathExtras
, MinAlign
) {
86 EXPECT_EQ(1u, MinAlign(2, 3));
87 EXPECT_EQ(2u, MinAlign(2, 4));
88 EXPECT_EQ(1u, MinAlign(17, 64));
89 EXPECT_EQ(256u, MinAlign(256, 512));
92 TEST(MathExtras
, NextPowerOf2
) {
93 EXPECT_EQ(4u, NextPowerOf2(3));
94 EXPECT_EQ(16u, NextPowerOf2(15));
95 EXPECT_EQ(256u, NextPowerOf2(128));
98 TEST(MathExtras
, RoundUpToAlignment
) {
99 EXPECT_EQ(8u, RoundUpToAlignment(5, 8));
100 EXPECT_EQ(24u, RoundUpToAlignment(17, 8));
101 EXPECT_EQ(0u, RoundUpToAlignment(~0LL, 8));