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 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)); 22 } 23 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)); 29 } 30 31 TEST(MathExtras, ByteSwap_32) { 32 EXPECT_EQ(0x44332211u, ByteSwap_32(0x11223344)); 33 EXPECT_EQ(0xDDCCBBAAu, ByteSwap_32(0xAABBCCDD)); 34 } 35 36 TEST(MathExtras, ByteSwap_64) { 37 EXPECT_EQ(0x8877665544332211ULL, ByteSwap_64(0x1122334455667788LL)); 38 EXPECT_EQ(0x1100FFEEDDCCBBAAULL, ByteSwap_64(0xAABBCCDDEEFF0011LL)); 39 } 40 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)); 46 } 47 } 48 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)); 54 } 55 } 56 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))); 61 } 62 } 63 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))); 68 } 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))); 72 } 73 } 74 75 TEST(MathExtras, FloatBits) { 76 static const float kValue = 5632.34f; 77 EXPECT_FLOAT_EQ(kValue, BitsToFloat(FloatToBits(kValue))); 78 } 79 80 TEST(MathExtras, DoubleBits) { 81 static const double kValue = 87987234.983498; 82 EXPECT_FLOAT_EQ(kValue, BitsToDouble(DoubleToBits(kValue))); 83 } 84 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)); 90 } 91 92 TEST(MathExtras, NextPowerOf2) { 93 EXPECT_EQ(4u, NextPowerOf2(3)); 94 EXPECT_EQ(16u, NextPowerOf2(15)); 95 EXPECT_EQ(256u, NextPowerOf2(128)); 96 } 97 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)); 102 } 103 104 } 105