1 //===- llvm/unittest/Support/ScaledNumberTest.cpp - ScaledPair 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 "llvm/Support/ScaledNumber.h"
12 #include "llvm/Support/DataTypes.h"
13 #include "gtest/gtest.h"
16 using namespace llvm::ScaledNumbers;
20 template <class UIntT> struct ScaledPair {
23 ScaledPair(const std::pair<UIntT, int16_t> &F) : D(F.first), S(F.second) {}
24 ScaledPair(UIntT D, int S) : D(D), S(S) {}
26 bool operator==(const ScaledPair<UIntT> &X) const {
27 return D == X.D && S == X.S;
30 template <class UIntT>
31 bool operator==(const std::pair<UIntT, int16_t> &L,
32 const ScaledPair<UIntT> &R) {
33 return ScaledPair<UIntT>(L) == R;
35 template <class UIntT>
36 void PrintTo(const ScaledPair<UIntT> &F, ::std::ostream *os) {
37 *os << F.D << "*2^" << F.S;
40 typedef ScaledPair<uint32_t> SP32;
41 typedef ScaledPair<uint64_t> SP64;
43 TEST(ScaledNumberHelpersTest, getRounded) {
44 EXPECT_EQ(getRounded<uint32_t>(0, 0, false), SP32(0, 0));
45 EXPECT_EQ(getRounded<uint32_t>(0, 0, true), SP32(1, 0));
46 EXPECT_EQ(getRounded<uint32_t>(20, 21, true), SP32(21, 21));
47 EXPECT_EQ(getRounded<uint32_t>(UINT32_MAX, 0, false), SP32(UINT32_MAX, 0));
48 EXPECT_EQ(getRounded<uint32_t>(UINT32_MAX, 0, true), SP32(1 << 31, 1));
50 EXPECT_EQ(getRounded<uint64_t>(0, 0, false), SP64(0, 0));
51 EXPECT_EQ(getRounded<uint64_t>(0, 0, true), SP64(1, 0));
52 EXPECT_EQ(getRounded<uint64_t>(20, 21, true), SP64(21, 21));
53 EXPECT_EQ(getRounded<uint64_t>(UINT32_MAX, 0, false), SP64(UINT32_MAX, 0));
54 EXPECT_EQ(getRounded<uint64_t>(UINT32_MAX, 0, true),
55 SP64(UINT64_C(1) << 32, 0));
56 EXPECT_EQ(getRounded<uint64_t>(UINT64_MAX, 0, false), SP64(UINT64_MAX, 0));
57 EXPECT_EQ(getRounded<uint64_t>(UINT64_MAX, 0, true),
58 SP64(UINT64_C(1) << 63, 1));
61 TEST(FloatsTest, getAdjusted) {
62 const uint64_t Max32In64 = UINT32_MAX;
63 EXPECT_EQ(getAdjusted32(0), SP32(0, 0));
64 EXPECT_EQ(getAdjusted32(0, 5), SP32(0, 5));
65 EXPECT_EQ(getAdjusted32(UINT32_MAX), SP32(UINT32_MAX, 0));
66 EXPECT_EQ(getAdjusted32(Max32In64 << 1), SP32(UINT32_MAX, 1));
67 EXPECT_EQ(getAdjusted32(Max32In64 << 1, 1), SP32(UINT32_MAX, 2));
68 EXPECT_EQ(getAdjusted32(Max32In64 << 31), SP32(UINT32_MAX, 31));
69 EXPECT_EQ(getAdjusted32(Max32In64 << 32), SP32(UINT32_MAX, 32));
70 EXPECT_EQ(getAdjusted32(Max32In64 + 1), SP32(1u << 31, 1));
71 EXPECT_EQ(getAdjusted32(UINT64_MAX), SP32(1u << 31, 33));
73 EXPECT_EQ(getAdjusted64(0), SP64(0, 0));
74 EXPECT_EQ(getAdjusted64(0, 5), SP64(0, 5));
75 EXPECT_EQ(getAdjusted64(UINT32_MAX), SP64(UINT32_MAX, 0));
76 EXPECT_EQ(getAdjusted64(Max32In64 << 1), SP64(Max32In64 << 1, 0));
77 EXPECT_EQ(getAdjusted64(Max32In64 << 1, 1), SP64(Max32In64 << 1, 1));
78 EXPECT_EQ(getAdjusted64(Max32In64 << 31), SP64(Max32In64 << 31, 0));
79 EXPECT_EQ(getAdjusted64(Max32In64 << 32), SP64(Max32In64 << 32, 0));
80 EXPECT_EQ(getAdjusted64(Max32In64 + 1), SP64(Max32In64 + 1, 0));
81 EXPECT_EQ(getAdjusted64(UINT64_MAX), SP64(UINT64_MAX, 0));