From 9c62dd583b0dc969dd7e87c58e7102685b9654ed Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Fri, 20 Jun 2014 01:30:43 +0000 Subject: Support: Write ScaledNumbers::getRounded() Start extracting helper functions out of -block-freq's `UnsignedFloat` into `Support/ScaledNumber.h` with the eventual goal of moving and renaming the class to `ScaledNumber`. The bike shed about names is still being painted, but I'm going with this for now. llvm-svn: 211333 --- llvm/unittests/Support/ScaledNumberTest.cpp | 60 +++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 llvm/unittests/Support/ScaledNumberTest.cpp (limited to 'llvm/unittests/Support/ScaledNumberTest.cpp') diff --git a/llvm/unittests/Support/ScaledNumberTest.cpp b/llvm/unittests/Support/ScaledNumberTest.cpp new file mode 100644 index 0000000..1fa54ff --- /dev/null +++ b/llvm/unittests/Support/ScaledNumberTest.cpp @@ -0,0 +1,60 @@ +//===- llvm/unittest/Support/ScaledNumberTest.cpp - ScaledPair tests -----==// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/ScaledNumber.h" + +#include "llvm/Support/DataTypes.h" +#include "gtest/gtest.h" + +using namespace llvm; +using namespace llvm::ScaledNumbers; + +namespace { + +template struct ScaledPair { + UIntT D; + int S; + ScaledPair(const std::pair &F) : D(F.first), S(F.second) {} + ScaledPair(UIntT D, int S) : D(D), S(S) {} + + bool operator==(const ScaledPair &X) const { + return D == X.D && S == X.S; + } +}; +template +bool operator==(const std::pair &L, + const ScaledPair &R) { + return ScaledPair(L) == R; +} +template +void PrintTo(const ScaledPair &F, ::std::ostream *os) { + *os << F.D << "*2^" << F.S; +} + +typedef ScaledPair SP32; +typedef ScaledPair SP64; + +TEST(ScaledNumberHelpersTest, getRounded) { + EXPECT_EQ(getRounded(0, 0, false), SP32(0, 0)); + EXPECT_EQ(getRounded(0, 0, true), SP32(1, 0)); + EXPECT_EQ(getRounded(20, 21, true), SP32(21, 21)); + EXPECT_EQ(getRounded(UINT32_MAX, 0, false), SP32(UINT32_MAX, 0)); + EXPECT_EQ(getRounded(UINT32_MAX, 0, true), SP32(1 << 31, 1)); + + EXPECT_EQ(getRounded(0, 0, false), SP64(0, 0)); + EXPECT_EQ(getRounded(0, 0, true), SP64(1, 0)); + EXPECT_EQ(getRounded(20, 21, true), SP64(21, 21)); + EXPECT_EQ(getRounded(UINT32_MAX, 0, false), SP64(UINT32_MAX, 0)); + EXPECT_EQ(getRounded(UINT32_MAX, 0, true), + SP64(UINT64_C(1) << 32, 0)); + EXPECT_EQ(getRounded(UINT64_MAX, 0, false), SP64(UINT64_MAX, 0)); + EXPECT_EQ(getRounded(UINT64_MAX, 0, true), + SP64(UINT64_C(1) << 63, 1)); +} +} -- cgit v1.1