1 //===- llvm/Support/ScaledNumber.h - Support for scaled numbers -*- C++ -*-===//
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 // 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
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.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_SUPPORT_SCALEDNUMBER_H
23 #define LLVM_SUPPORT_SCALEDNUMBER_H
25 #include "llvm/Support/MathExtras.h"
32 namespace ScaledNumbers {
34 /// \brief Get the width of a number.
35 template <class DigitsT> inline int getWidth() { return sizeof(DigitsT) * 8; }
37 /// \brief Conditionally round up a scaled number.
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.
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,
47 static_assert(!std::numeric_limits<DigitsT>::is_signed, "expected unsigned");
52 return std::make_pair(DigitsT(1) << (getWidth<DigitsT>() - 1), Scale + 1);
53 return std::make_pair(Digits, Scale);
56 /// \brief Convenience helper for 32-bit rounding.
57 inline std::pair<uint32_t, int16_t> getRounded32(uint32_t Digits, int16_t Scale,
59 return getRounded(Digits, Scale, ShouldRound);
62 /// \brief Convenience helper for 64-bit rounding.
63 inline std::pair<uint64_t, int16_t> getRounded64(uint64_t Digits, int16_t Scale,
65 return getRounded(Digits, Scale, ShouldRound);
68 /// \brief Adjust a 64-bit scaled number down to the appropriate width.
70 /// \pre Adding 64 to \c Scale will not overflow INT16_MAX.
71 template <class DigitsT>
72 inline std::pair<DigitsT, int16_t> getAdjusted(uint64_t Digits,
74 static_assert(!std::numeric_limits<DigitsT>::is_signed, "expected unsigned");
76 const int Width = getWidth<DigitsT>();
77 if (Width == 64 || Digits <= std::numeric_limits<DigitsT>::max())
78 return std::make_pair(Digits, Scale);
80 // Shift right and round.
81 int Shift = 64 - Width - countLeadingZeros(Digits);
82 return getRounded<DigitsT>(Digits >> Shift, Scale + Shift,
83 Digits & (UINT64_C(1) << (Shift - 1)));
86 /// \brief Convenience helper for adjusting to 32 bits.
87 inline std::pair<uint32_t, int16_t> getAdjusted32(uint64_t Digits,
89 return getAdjusted<uint32_t>(Digits, Scale);
92 /// \brief Convenience helper for adjusting to 64 bits.
93 inline std::pair<uint64_t, int16_t> getAdjusted64(uint64_t Digits,
95 return getAdjusted<uint64_t>(Digits, Scale);
98 /// \brief Multiply two 64-bit integers to create a 64-bit scaled number.
100 /// Implemented with four 64-bit integer multiplies.
101 std::pair<uint64_t, int16_t> multiply64(uint64_t LHS, uint64_t RHS);
103 /// \brief Multiply two 32-bit integers to create a 32-bit scaled number.
105 /// Implemented with one 64-bit integer multiply.
106 template <class DigitsT>
107 inline std::pair<DigitsT, int16_t> getProduct(DigitsT LHS, DigitsT RHS) {
108 static_assert(!std::numeric_limits<DigitsT>::is_signed, "expected unsigned");
110 if (getWidth<DigitsT>() <= 32 || (LHS <= UINT32_MAX && RHS <= UINT32_MAX))
111 return getAdjusted<DigitsT>(uint64_t(LHS) * RHS);
113 return multiply64(LHS, RHS);
116 /// \brief Convenience helper for 32-bit product.
117 inline std::pair<uint32_t, int16_t> getProduct32(uint32_t LHS, uint32_t RHS) {
118 return getProduct(LHS, RHS);
121 /// \brief Convenience helper for 64-bit product.
122 inline std::pair<uint64_t, int16_t> getProduct64(uint64_t LHS, uint64_t RHS) {
123 return getProduct(LHS, RHS);
126 /// \brief Divide two 64-bit integers to create a 64-bit scaled number.
128 /// Implemented with long division.
130 /// \pre \c Dividend and \c Divisor are non-zero.
131 std::pair<uint64_t, int16_t> divide64(uint64_t Dividend, uint64_t Divisor);
133 /// \brief Divide two 32-bit integers to create a 32-bit scaled number.
135 /// Implemented with one 64-bit integer divide/remainder pair.
137 /// \pre \c Dividend and \c Divisor are non-zero.
138 std::pair<uint32_t, int16_t> divide32(uint32_t Dividend, uint32_t Divisor);
140 /// \brief Divide two 32-bit numbers to create a 32-bit scaled number.
142 /// Implemented with one 64-bit integer divide/remainder pair.
144 /// Returns \c (DigitsT_MAX, INT16_MAX) for divide-by-zero (0 for 0/0).
145 template <class DigitsT>
146 std::pair<DigitsT, int16_t> getQuotient(DigitsT Dividend, DigitsT Divisor) {
147 static_assert(!std::numeric_limits<DigitsT>::is_signed, "expected unsigned");
148 static_assert(sizeof(DigitsT) == 4 || sizeof(DigitsT) == 8,
149 "expected 32-bit or 64-bit digits");
153 return std::make_pair(0, 0);
155 return std::make_pair(std::numeric_limits<DigitsT>::max(), INT16_MAX);
157 if (getWidth<DigitsT>() == 64)
158 return divide64(Dividend, Divisor);
159 return divide32(Dividend, Divisor);
162 /// \brief Convenience helper for 32-bit quotient.
163 inline std::pair<uint32_t, int16_t> getQuotient32(uint32_t Dividend,
165 return getQuotient(Dividend, Divisor);
168 /// \brief Convenience helper for 64-bit quotient.
169 inline std::pair<uint64_t, int16_t> getQuotient64(uint64_t Dividend,
171 return getQuotient(Dividend, Divisor);
174 /// \brief Implementation of getLg() and friends.
176 /// Returns the rounded lg of \c Digits*2^Scale and an int specifying whether
177 /// this was rounded up (1), down (-1), or exact (0).
179 /// Returns \c INT32_MIN when \c Digits is zero.
180 template <class DigitsT>
181 inline std::pair<int32_t, int> getLgImpl(DigitsT Digits, int16_t Scale) {
182 static_assert(!std::numeric_limits<DigitsT>::is_signed, "expected unsigned");
185 return std::make_pair(INT32_MIN, 0);
187 // Get the floor of the lg of Digits.
188 int32_t LocalFloor = sizeof(Digits) * 8 - countLeadingZeros(Digits) - 1;
190 // Get the actual floor.
191 int32_t Floor = Scale + LocalFloor;
192 if (Digits == UINT64_C(1) << LocalFloor)
193 return std::make_pair(Floor, 0);
195 // Round based on the next digit.
196 assert(LocalFloor >= 1);
197 bool Round = Digits & UINT64_C(1) << (LocalFloor - 1);
198 return std::make_pair(Floor + Round, Round ? 1 : -1);
201 /// \brief Get the lg (rounded) of a scaled number.
203 /// Get the lg of \c Digits*2^Scale.
205 /// Returns \c INT32_MIN when \c Digits is zero.
206 template <class DigitsT> int32_t getLg(DigitsT Digits, int16_t Scale) {
207 return getLgImpl(Digits, Scale).first;
210 /// \brief Get the lg floor of a scaled number.
212 /// Get the floor of the lg of \c Digits*2^Scale.
214 /// Returns \c INT32_MIN when \c Digits is zero.
215 template <class DigitsT> int32_t getLgFloor(DigitsT Digits, int16_t Scale) {
216 auto Lg = getLgImpl(Digits, Scale);
217 return Lg.first - (Lg.second > 0);
220 /// \brief Get the lg ceiling of a scaled number.
222 /// Get the ceiling of the lg of \c Digits*2^Scale.
224 /// Returns \c INT32_MIN when \c Digits is zero.
225 template <class DigitsT> int32_t getLgCeiling(DigitsT Digits, int16_t Scale) {
226 auto Lg = getLgImpl(Digits, Scale);
227 return Lg.first + (Lg.second < 0);
230 /// \brief Implementation for comparing scaled numbers.
232 /// Compare two 64-bit numbers with different scales. Given that the scale of
233 /// \c L is higher than that of \c R by \c ScaleDiff, compare them. Return -1,
234 /// 1, and 0 for less than, greater than, and equal, respectively.
236 /// \pre 0 <= ScaleDiff < 64.
237 int compareImpl(uint64_t L, uint64_t R, int ScaleDiff);
239 /// \brief Compare two scaled numbers.
241 /// Compare two scaled numbers. Returns 0 for equal, -1 for less than, and 1
242 /// for greater than.
243 template <class DigitsT>
244 int compare(DigitsT LDigits, int16_t LScale, DigitsT RDigits, int16_t RScale) {
245 static_assert(!std::numeric_limits<DigitsT>::is_signed, "expected unsigned");
249 return RDigits ? -1 : 0;
253 // Check for the scale. Use getLgFloor to be sure that the scale difference
254 // is always lower than 64.
255 int32_t lgL = getLgFloor(LDigits, LScale), lgR = getLgFloor(RDigits, RScale);
257 return lgL < lgR ? -1 : 1;
261 return compareImpl(LDigits, RDigits, RScale - LScale);
263 return -compareImpl(RDigits, LDigits, LScale - RScale);
266 } // end namespace ScaledNumbers
267 } // end namespace llvm