summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>2014-06-20 01:30:43 +0000
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>2014-06-20 01:30:43 +0000
commit16e592a6fe237900a7fa347c732f29395013fc34 (patch)
tree66a4eaed0c841721e093500d237d9fef612352d5 /unittests
parentc577e71bf580d344970406915e050a301ae8a28d (diff)
downloadllvm-16e592a6fe237900a7fa347c732f29395013fc34.tar.gz
llvm-16e592a6fe237900a7fa347c732f29395013fc34.tar.bz2
llvm-16e592a6fe237900a7fa347c732f29395013fc34.tar.xz
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211333 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r--unittests/Support/CMakeLists.txt1
-rw-r--r--unittests/Support/ScaledNumberTest.cpp60
2 files changed, 61 insertions, 0 deletions
diff --git a/unittests/Support/CMakeLists.txt b/unittests/Support/CMakeLists.txt
index c50acdfb8e..08096a4a17 100644
--- a/unittests/Support/CMakeLists.txt
+++ b/unittests/Support/CMakeLists.txt
@@ -29,6 +29,7 @@ add_llvm_unittest(SupportTests
ProcessTest.cpp
ProgramTest.cpp
RegexTest.cpp
+ ScaledNumberTest.cpp
SourceMgrTest.cpp
StringPool.cpp
SwapByteOrderTest.cpp
diff --git a/unittests/Support/ScaledNumberTest.cpp b/unittests/Support/ScaledNumberTest.cpp
new file mode 100644
index 0000000000..1fa54ff81d
--- /dev/null
+++ b/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 <class UIntT> struct ScaledPair {
+ UIntT D;
+ int S;
+ ScaledPair(const std::pair<UIntT, int16_t> &F) : D(F.first), S(F.second) {}
+ ScaledPair(UIntT D, int S) : D(D), S(S) {}
+
+ bool operator==(const ScaledPair<UIntT> &X) const {
+ return D == X.D && S == X.S;
+ }
+};
+template <class UIntT>
+bool operator==(const std::pair<UIntT, int16_t> &L,
+ const ScaledPair<UIntT> &R) {
+ return ScaledPair<UIntT>(L) == R;
+}
+template <class UIntT>
+void PrintTo(const ScaledPair<UIntT> &F, ::std::ostream *os) {
+ *os << F.D << "*2^" << F.S;
+}
+
+typedef ScaledPair<uint32_t> SP32;
+typedef ScaledPair<uint64_t> SP64;
+
+TEST(ScaledNumberHelpersTest, getRounded) {
+ EXPECT_EQ(getRounded<uint32_t>(0, 0, false), SP32(0, 0));
+ EXPECT_EQ(getRounded<uint32_t>(0, 0, true), SP32(1, 0));
+ EXPECT_EQ(getRounded<uint32_t>(20, 21, true), SP32(21, 21));
+ EXPECT_EQ(getRounded<uint32_t>(UINT32_MAX, 0, false), SP32(UINT32_MAX, 0));
+ EXPECT_EQ(getRounded<uint32_t>(UINT32_MAX, 0, true), SP32(1 << 31, 1));
+
+ EXPECT_EQ(getRounded<uint64_t>(0, 0, false), SP64(0, 0));
+ EXPECT_EQ(getRounded<uint64_t>(0, 0, true), SP64(1, 0));
+ EXPECT_EQ(getRounded<uint64_t>(20, 21, true), SP64(21, 21));
+ EXPECT_EQ(getRounded<uint64_t>(UINT32_MAX, 0, false), SP64(UINT32_MAX, 0));
+ EXPECT_EQ(getRounded<uint64_t>(UINT32_MAX, 0, true),
+ SP64(UINT64_C(1) << 32, 0));
+ EXPECT_EQ(getRounded<uint64_t>(UINT64_MAX, 0, false), SP64(UINT64_MAX, 0));
+ EXPECT_EQ(getRounded<uint64_t>(UINT64_MAX, 0, true),
+ SP64(UINT64_C(1) << 63, 1));
+}
+}