APFixedPoint.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //===- APFixedPoint.h - Fixed point constant handling -----------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. /// \file
  10. /// Defines the fixed point number interface.
  11. /// This is a class for abstracting various operations performed on fixed point
  12. /// types.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_ADT_APFIXEDPOINT_H
  16. #define LLVM_ADT_APFIXEDPOINT_H
  17. #include "llvm/ADT/APSInt.h"
  18. #include "llvm/ADT/SmallString.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. namespace llvm {
  21. class APFloat;
  22. struct fltSemantics;
  23. /// The fixed point semantics work similarly to fltSemantics. The width
  24. /// specifies the whole bit width of the underlying scaled integer (with padding
  25. /// if any). The scale represents the number of fractional bits in this type.
  26. /// When HasUnsignedPadding is true and this type is unsigned, the first bit
  27. /// in the value this represents is treated as padding.
  28. class FixedPointSemantics {
  29. public:
  30. FixedPointSemantics(unsigned Width, unsigned Scale, bool IsSigned,
  31. bool IsSaturated, bool HasUnsignedPadding)
  32. : Width(Width), Scale(Scale), IsSigned(IsSigned),
  33. IsSaturated(IsSaturated), HasUnsignedPadding(HasUnsignedPadding) {
  34. assert(Width >= Scale && "Not enough room for the scale");
  35. assert(!(IsSigned && HasUnsignedPadding) &&
  36. "Cannot have unsigned padding on a signed type.");
  37. }
  38. unsigned getWidth() const { return Width; }
  39. unsigned getScale() const { return Scale; }
  40. bool isSigned() const { return IsSigned; }
  41. bool isSaturated() const { return IsSaturated; }
  42. bool hasUnsignedPadding() const { return HasUnsignedPadding; }
  43. void setSaturated(bool Saturated) { IsSaturated = Saturated; }
  44. /// Return the number of integral bits represented by these semantics. These
  45. /// are separate from the fractional bits and do not include the sign or
  46. /// padding bit.
  47. unsigned getIntegralBits() const {
  48. if (IsSigned || (!IsSigned && HasUnsignedPadding))
  49. return Width - Scale - 1;
  50. else
  51. return Width - Scale;
  52. }
  53. /// Return the FixedPointSemantics that allows for calculating the full
  54. /// precision semantic that can precisely represent the precision and ranges
  55. /// of both input values. This does not compute the resulting semantics for a
  56. /// given binary operation.
  57. FixedPointSemantics
  58. getCommonSemantics(const FixedPointSemantics &Other) const;
  59. /// Returns true if this fixed-point semantic with its value bits interpreted
  60. /// as an integer can fit in the given floating point semantic without
  61. /// overflowing to infinity.
  62. /// For example, a signed 8-bit fixed-point semantic has a maximum and
  63. /// minimum integer representation of 127 and -128, respectively. If both of
  64. /// these values can be represented (possibly inexactly) in the floating
  65. /// point semantic without overflowing, this returns true.
  66. bool fitsInFloatSemantics(const fltSemantics &FloatSema) const;
  67. /// Return the FixedPointSemantics for an integer type.
  68. static FixedPointSemantics GetIntegerSemantics(unsigned Width,
  69. bool IsSigned) {
  70. return FixedPointSemantics(Width, /*Scale=*/0, IsSigned,
  71. /*IsSaturated=*/false,
  72. /*HasUnsignedPadding=*/false);
  73. }
  74. private:
  75. unsigned Width : 16;
  76. unsigned Scale : 13;
  77. unsigned IsSigned : 1;
  78. unsigned IsSaturated : 1;
  79. unsigned HasUnsignedPadding : 1;
  80. };
  81. /// The APFixedPoint class works similarly to APInt/APSInt in that it is a
  82. /// functional replacement for a scaled integer. It is meant to replicate the
  83. /// fixed point types proposed in ISO/IEC JTC1 SC22 WG14 N1169. The class carries
  84. /// info about the fixed point type's width, sign, scale, and saturation, and
  85. /// provides different operations that would normally be performed on fixed point
  86. /// types.
  87. class APFixedPoint {
  88. public:
  89. APFixedPoint(const APInt &Val, const FixedPointSemantics &Sema)
  90. : Val(Val, !Sema.isSigned()), Sema(Sema) {
  91. assert(Val.getBitWidth() == Sema.getWidth() &&
  92. "The value should have a bit width that matches the Sema width");
  93. }
  94. APFixedPoint(uint64_t Val, const FixedPointSemantics &Sema)
  95. : APFixedPoint(APInt(Sema.getWidth(), Val, Sema.isSigned()), Sema) {}
  96. // Zero initialization.
  97. APFixedPoint(const FixedPointSemantics &Sema) : APFixedPoint(0, Sema) {}
  98. APSInt getValue() const { return APSInt(Val, !Sema.isSigned()); }
  99. inline unsigned getWidth() const { return Sema.getWidth(); }
  100. inline unsigned getScale() const { return Sema.getScale(); }
  101. inline bool isSaturated() const { return Sema.isSaturated(); }
  102. inline bool isSigned() const { return Sema.isSigned(); }
  103. inline bool hasPadding() const { return Sema.hasUnsignedPadding(); }
  104. FixedPointSemantics getSemantics() const { return Sema; }
  105. bool getBoolValue() const { return Val.getBoolValue(); }
  106. // Convert this number to match the semantics provided. If the overflow
  107. // parameter is provided, set this value to true or false to indicate if this
  108. // operation results in an overflow.
  109. APFixedPoint convert(const FixedPointSemantics &DstSema,
  110. bool *Overflow = nullptr) const;
  111. // Perform binary operations on a fixed point type. The resulting fixed point
  112. // value will be in the common, full precision semantics that can represent
  113. // the precision and ranges of both input values. See convert() for an
  114. // explanation of the Overflow parameter.
  115. APFixedPoint add(const APFixedPoint &Other, bool *Overflow = nullptr) const;
  116. APFixedPoint sub(const APFixedPoint &Other, bool *Overflow = nullptr) const;
  117. APFixedPoint mul(const APFixedPoint &Other, bool *Overflow = nullptr) const;
  118. APFixedPoint div(const APFixedPoint &Other, bool *Overflow = nullptr) const;
  119. // Perform shift operations on a fixed point type. Unlike the other binary
  120. // operations, the resulting fixed point value will be in the original
  121. // semantic.
  122. APFixedPoint shl(unsigned Amt, bool *Overflow = nullptr) const;
  123. APFixedPoint shr(unsigned Amt, bool *Overflow = nullptr) const {
  124. // Right shift cannot overflow.
  125. if (Overflow)
  126. *Overflow = false;
  127. return APFixedPoint(Val >> Amt, Sema);
  128. }
  129. /// Perform a unary negation (-X) on this fixed point type, taking into
  130. /// account saturation if applicable.
  131. APFixedPoint negate(bool *Overflow = nullptr) const;
  132. /// Return the integral part of this fixed point number, rounded towards
  133. /// zero. (-2.5k -> -2)
  134. APSInt getIntPart() const {
  135. if (Val < 0 && Val != -Val) // Cover the case when we have the min val
  136. return -(-Val >> getScale());
  137. else
  138. return Val >> getScale();
  139. }
  140. /// Return the integral part of this fixed point number, rounded towards
  141. /// zero. The value is stored into an APSInt with the provided width and sign.
  142. /// If the overflow parameter is provided, and the integral value is not able
  143. /// to be fully stored in the provided width and sign, the overflow parameter
  144. /// is set to true.
  145. APSInt convertToInt(unsigned DstWidth, bool DstSign,
  146. bool *Overflow = nullptr) const;
  147. /// Convert this fixed point number to a floating point value with the
  148. /// provided semantics.
  149. APFloat convertToFloat(const fltSemantics &FloatSema) const;
  150. void toString(SmallVectorImpl<char> &Str) const;
  151. std::string toString() const {
  152. SmallString<40> S;
  153. toString(S);
  154. return std::string(S.str());
  155. }
  156. // If LHS > RHS, return 1. If LHS == RHS, return 0. If LHS < RHS, return -1.
  157. int compare(const APFixedPoint &Other) const;
  158. bool operator==(const APFixedPoint &Other) const {
  159. return compare(Other) == 0;
  160. }
  161. bool operator!=(const APFixedPoint &Other) const {
  162. return compare(Other) != 0;
  163. }
  164. bool operator>(const APFixedPoint &Other) const { return compare(Other) > 0; }
  165. bool operator<(const APFixedPoint &Other) const { return compare(Other) < 0; }
  166. bool operator>=(const APFixedPoint &Other) const {
  167. return compare(Other) >= 0;
  168. }
  169. bool operator<=(const APFixedPoint &Other) const {
  170. return compare(Other) <= 0;
  171. }
  172. static APFixedPoint getMax(const FixedPointSemantics &Sema);
  173. static APFixedPoint getMin(const FixedPointSemantics &Sema);
  174. /// Given a floating point semantic, return the next floating point semantic
  175. /// with a larger exponent and larger or equal mantissa.
  176. static const fltSemantics *promoteFloatSemantics(const fltSemantics *S);
  177. /// Create an APFixedPoint with a value equal to that of the provided integer,
  178. /// and in the same semantics as the provided target semantics. If the value
  179. /// is not able to fit in the specified fixed point semantics, and the
  180. /// overflow parameter is provided, it is set to true.
  181. static APFixedPoint getFromIntValue(const APSInt &Value,
  182. const FixedPointSemantics &DstFXSema,
  183. bool *Overflow = nullptr);
  184. /// Create an APFixedPoint with a value equal to that of the provided
  185. /// floating point value, in the provided target semantics. If the value is
  186. /// not able to fit in the specified fixed point semantics and the overflow
  187. /// parameter is specified, it is set to true.
  188. /// For NaN, the Overflow flag is always set. For +inf and -inf, if the
  189. /// semantic is saturating, the value saturates. Otherwise, the Overflow flag
  190. /// is set.
  191. static APFixedPoint getFromFloatValue(const APFloat &Value,
  192. const FixedPointSemantics &DstFXSema,
  193. bool *Overflow = nullptr);
  194. private:
  195. APSInt Val;
  196. FixedPointSemantics Sema;
  197. };
  198. inline raw_ostream &operator<<(raw_ostream &OS, const APFixedPoint &FX) {
  199. OS << FX.toString();
  200. return OS;
  201. }
  202. } // namespace llvm
  203. #endif