afdc7dc1888a291f404284ca6dd368958f83b767
[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 <cstdint>
26 #include <limits>
27 #include <utility>
28
29 namespace llvm {
30 namespace ScaledNumbers {
31
32 /// \brief Get the width of a number.
33 template <class DigitsT> inline int getWidth() { return sizeof(DigitsT) * 8; }
34
35 /// \brief Conditionally round up a scaled number.
36 ///
37 /// Given \c Digits and \c Scale, round up iff \c ShouldRound is \c true.
38 /// Always returns \c Scale unless there's an overflow, in which case it
39 /// returns \c 1+Scale.
40 ///
41 /// \pre adding 1 to \c Scale will not overflow INT16_MAX.
42 template <class DigitsT>
43 inline std::pair<DigitsT, int16_t> getRounded(DigitsT Digits, int16_t Scale,
44                                               bool ShouldRound) {
45   static_assert(!std::numeric_limits<DigitsT>::is_signed, "expected unsigned");
46
47   if (ShouldRound)
48     if (!++Digits)
49       // Overflow.
50       return std::make_pair(DigitsT(1) << (getWidth<DigitsT>() - 1), Scale + 1);
51   return std::make_pair(Digits, Scale);
52 }
53 }
54 }
55
56 #endif
57