Support: Clean up getRounded() tests
[oota-llvm.git] / include / llvm / Support / ScaledNumber.h
1 //===- llvm/Support/ScaledNumber.h - Support for scaled numbers -*- C++ -*-===//
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 // This file contains functions (and a class) useful for working with scaled
11 // numbers -- in particular, pairs of integers where one represents digits and
12 // another represents a scale.  The functions are helpers and live in the
13 // namespace ScaledNumbers.  The class ScaledNumber is useful for modelling
14 // certain cost metrics that need simple, integer-like semantics that are easy
15 // to reason about.
16 //
17 // These might remind you of soft-floats.  If you want one of those, you're in
18 // the wrong place.  Look at include/llvm/ADT/APFloat.h instead.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_SUPPORT_SCALEDNUMBER_H
23 #define LLVM_SUPPORT_SCALEDNUMBER_H
24
25 #include "llvm/Support/MathExtras.h"
26
27 #include <cstdint>
28 #include <limits>
29 #include <utility>
30
31 namespace llvm {
32 namespace ScaledNumbers {
33
34 /// \brief Get the width of a number.
35 template <class DigitsT> inline int getWidth() { return sizeof(DigitsT) * 8; }
36
37 /// \brief Conditionally round up a scaled number.
38 ///
39 /// Given \c Digits and \c Scale, round up iff \c ShouldRound is \c true.
40 /// Always returns \c Scale unless there's an overflow, in which case it
41 /// returns \c 1+Scale.
42 ///
43 /// \pre adding 1 to \c Scale will not overflow INT16_MAX.
44 template <class DigitsT>
45 inline std::pair<DigitsT, int16_t> getRounded(DigitsT Digits, int16_t Scale,
46                                               bool ShouldRound) {
47   static_assert(!std::numeric_limits<DigitsT>::is_signed, "expected unsigned");
48
49   if (ShouldRound)
50     if (!++Digits)
51       // Overflow.
52       return std::make_pair(DigitsT(1) << (getWidth<DigitsT>() - 1), Scale + 1);
53   return std::make_pair(Digits, Scale);
54 }
55
56 /// \brief Convenience helper for 32-bit rounding.
57 inline std::pair<uint32_t, int16_t> getRounded32(uint32_t Digits, int16_t Scale,
58                                                  bool ShouldRound) {
59   return getRounded(Digits, Scale, ShouldRound);
60 }
61
62 /// \brief Convenience helper for 64-bit rounding.
63 inline std::pair<uint64_t, int16_t> getRounded64(uint64_t Digits, int16_t Scale,
64                                                  bool ShouldRound) {
65   return getRounded(Digits, Scale, ShouldRound);
66 }
67
68 /// \brief Adjust a 64-bit scaled number down to the appropriate width.
69 ///
70 /// Adjust a soft float with 64-bits of digits down, keeping as much
71 /// information as possible, and rounding up on half.
72 ///
73 /// \pre Adding 1 to \c Scale will not overflow INT16_MAX.
74 template <class DigitsT>
75 inline std::pair<DigitsT, int16_t> getAdjusted(uint64_t Digits,
76                                                int16_t Scale = 0) {
77   static_assert(!std::numeric_limits<DigitsT>::is_signed, "expected unsigned");
78
79   const int Width = getWidth<DigitsT>();
80   if (Width == 64 || Digits <= std::numeric_limits<DigitsT>::max())
81     return std::make_pair(Digits, Scale);
82
83   // Shift right and round.
84   int Shift = 64 - Width - countLeadingZeros(Digits);
85   return getRounded<DigitsT>(Digits >> Shift, Scale + Shift,
86                              Digits & (UINT64_C(1) << (Shift - 1)));
87 }
88
89 /// \brief Convenience helper for adjusting to 32 bits.
90 inline std::pair<uint32_t, int16_t> getAdjusted32(uint64_t Digits,
91                                                   int16_t Scale = 0) {
92   return getAdjusted<uint32_t>(Digits, Scale);
93 }
94
95 /// \brief Convenience helper for adjusting to 64 bits.
96 inline std::pair<uint64_t, int16_t> getAdjusted64(uint64_t Digits,
97                                                   int16_t Scale = 0) {
98   return getAdjusted<uint64_t>(Digits, Scale);
99 }
100 }
101 }
102
103 #endif
104