FixedPoint.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //===- FixedPoint.cpp - 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 implementation for the fixed point number interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Basic/FixedPoint.h"
  14. namespace clang {
  15. APFixedPoint APFixedPoint::convert(const FixedPointSemantics &DstSema,
  16. bool *Overflow) const {
  17. llvm::APSInt NewVal = Val;
  18. unsigned DstWidth = DstSema.getWidth();
  19. unsigned DstScale = DstSema.getScale();
  20. bool Upscaling = DstScale > getScale();
  21. if (Overflow)
  22. *Overflow = false;
  23. if (Upscaling) {
  24. NewVal = NewVal.extend(NewVal.getBitWidth() + DstScale - getScale());
  25. NewVal <<= (DstScale - getScale());
  26. } else {
  27. NewVal >>= (getScale() - DstScale);
  28. }
  29. auto Mask = llvm::APInt::getBitsSetFrom(
  30. NewVal.getBitWidth(),
  31. std::min(DstScale + DstSema.getIntegralBits(), NewVal.getBitWidth()));
  32. llvm::APInt Masked(NewVal & Mask);
  33. // Change in the bits above the sign
  34. if (!(Masked == Mask || Masked == 0)) {
  35. // Found overflow in the bits above the sign
  36. if (DstSema.isSaturated())
  37. NewVal = NewVal.isNegative() ? Mask : ~Mask;
  38. else if (Overflow)
  39. *Overflow = true;
  40. }
  41. // If the dst semantics are unsigned, but our value is signed and negative, we
  42. // clamp to zero.
  43. if (!DstSema.isSigned() && NewVal.isSigned() && NewVal.isNegative()) {
  44. // Found negative overflow for unsigned result
  45. if (DstSema.isSaturated())
  46. NewVal = 0;
  47. else if (Overflow)
  48. *Overflow = true;
  49. }
  50. NewVal = NewVal.extOrTrunc(DstWidth);
  51. NewVal.setIsSigned(DstSema.isSigned());
  52. return APFixedPoint(NewVal, DstSema);
  53. }
  54. int APFixedPoint::compare(const APFixedPoint &Other) const {
  55. llvm::APSInt ThisVal = getValue();
  56. llvm::APSInt OtherVal = Other.getValue();
  57. bool ThisSigned = Val.isSigned();
  58. bool OtherSigned = OtherVal.isSigned();
  59. unsigned OtherScale = Other.getScale();
  60. unsigned OtherWidth = OtherVal.getBitWidth();
  61. unsigned CommonWidth = std::max(Val.getBitWidth(), OtherWidth);
  62. // Prevent overflow in the event the widths are the same but the scales differ
  63. CommonWidth += getScale() >= OtherScale ? getScale() - OtherScale
  64. : OtherScale - getScale();
  65. ThisVal = ThisVal.extOrTrunc(CommonWidth);
  66. OtherVal = OtherVal.extOrTrunc(CommonWidth);
  67. unsigned CommonScale = std::max(getScale(), OtherScale);
  68. ThisVal = ThisVal.shl(CommonScale - getScale());
  69. OtherVal = OtherVal.shl(CommonScale - OtherScale);
  70. if (ThisSigned && OtherSigned) {
  71. if (ThisVal.sgt(OtherVal))
  72. return 1;
  73. else if (ThisVal.slt(OtherVal))
  74. return -1;
  75. } else if (!ThisSigned && !OtherSigned) {
  76. if (ThisVal.ugt(OtherVal))
  77. return 1;
  78. else if (ThisVal.ult(OtherVal))
  79. return -1;
  80. } else if (ThisSigned && !OtherSigned) {
  81. if (ThisVal.isSignBitSet())
  82. return -1;
  83. else if (ThisVal.ugt(OtherVal))
  84. return 1;
  85. else if (ThisVal.ult(OtherVal))
  86. return -1;
  87. } else {
  88. // !ThisSigned && OtherSigned
  89. if (OtherVal.isSignBitSet())
  90. return 1;
  91. else if (ThisVal.ugt(OtherVal))
  92. return 1;
  93. else if (ThisVal.ult(OtherVal))
  94. return -1;
  95. }
  96. return 0;
  97. }
  98. APFixedPoint APFixedPoint::getMax(const FixedPointSemantics &Sema) {
  99. bool IsUnsigned = !Sema.isSigned();
  100. auto Val = llvm::APSInt::getMaxValue(Sema.getWidth(), IsUnsigned);
  101. if (IsUnsigned && Sema.hasUnsignedPadding())
  102. Val = Val.lshr(1);
  103. return APFixedPoint(Val, Sema);
  104. }
  105. APFixedPoint APFixedPoint::getMin(const FixedPointSemantics &Sema) {
  106. auto Val = llvm::APSInt::getMinValue(Sema.getWidth(), !Sema.isSigned());
  107. return APFixedPoint(Val, Sema);
  108. }
  109. FixedPointSemantics FixedPointSemantics::getCommonSemantics(
  110. const FixedPointSemantics &Other) const {
  111. unsigned CommonScale = std::max(getScale(), Other.getScale());
  112. unsigned CommonWidth =
  113. std::max(getIntegralBits(), Other.getIntegralBits()) + CommonScale;
  114. bool ResultIsSigned = isSigned() || Other.isSigned();
  115. bool ResultIsSaturated = isSaturated() || Other.isSaturated();
  116. bool ResultHasUnsignedPadding = false;
  117. if (!ResultIsSigned) {
  118. // Both are unsigned.
  119. ResultHasUnsignedPadding = hasUnsignedPadding() &&
  120. Other.hasUnsignedPadding() && !ResultIsSaturated;
  121. }
  122. // If the result is signed, add an extra bit for the sign. Otherwise, if it is
  123. // unsigned and has unsigned padding, we only need to add the extra padding
  124. // bit back if we are not saturating.
  125. if (ResultIsSigned || ResultHasUnsignedPadding)
  126. CommonWidth++;
  127. return FixedPointSemantics(CommonWidth, CommonScale, ResultIsSigned,
  128. ResultIsSaturated, ResultHasUnsignedPadding);
  129. }
  130. APFixedPoint APFixedPoint::add(const APFixedPoint &Other,
  131. bool *Overflow) const {
  132. auto CommonFXSema = Sema.getCommonSemantics(Other.getSemantics());
  133. APFixedPoint ConvertedThis = convert(CommonFXSema);
  134. APFixedPoint ConvertedOther = Other.convert(CommonFXSema);
  135. llvm::APSInt ThisVal = ConvertedThis.getValue();
  136. llvm::APSInt OtherVal = ConvertedOther.getValue();
  137. bool Overflowed = false;
  138. llvm::APSInt Result;
  139. if (CommonFXSema.isSaturated()) {
  140. Result = CommonFXSema.isSigned() ? ThisVal.sadd_sat(OtherVal)
  141. : ThisVal.uadd_sat(OtherVal);
  142. } else {
  143. Result = ThisVal.isSigned() ? ThisVal.sadd_ov(OtherVal, Overflowed)
  144. : ThisVal.uadd_ov(OtherVal, Overflowed);
  145. }
  146. if (Overflow)
  147. *Overflow = Overflowed;
  148. return APFixedPoint(Result, CommonFXSema);
  149. }
  150. void APFixedPoint::toString(llvm::SmallVectorImpl<char> &Str) const {
  151. llvm::APSInt Val = getValue();
  152. unsigned Scale = getScale();
  153. if (Val.isSigned() && Val.isNegative() && Val != -Val) {
  154. Val = -Val;
  155. Str.push_back('-');
  156. }
  157. llvm::APSInt IntPart = Val >> Scale;
  158. // Add 4 digits to hold the value after multiplying 10 (the radix)
  159. unsigned Width = Val.getBitWidth() + 4;
  160. llvm::APInt FractPart = Val.zextOrTrunc(Scale).zext(Width);
  161. llvm::APInt FractPartMask = llvm::APInt::getAllOnesValue(Scale).zext(Width);
  162. llvm::APInt RadixInt = llvm::APInt(Width, 10);
  163. IntPart.toString(Str, /*Radix=*/10);
  164. Str.push_back('.');
  165. do {
  166. (FractPart * RadixInt)
  167. .lshr(Scale)
  168. .toString(Str, /*Radix=*/10, Val.isSigned());
  169. FractPart = (FractPart * RadixInt) & FractPartMask;
  170. } while (FractPart != 0);
  171. }
  172. APFixedPoint APFixedPoint::negate(bool *Overflow) const {
  173. if (!isSaturated()) {
  174. if (Overflow)
  175. *Overflow =
  176. (!isSigned() && Val != 0) || (isSigned() && Val.isMinSignedValue());
  177. return APFixedPoint(-Val, Sema);
  178. }
  179. // We never overflow for saturation
  180. if (Overflow)
  181. *Overflow = false;
  182. if (isSigned())
  183. return Val.isMinSignedValue() ? getMax(Sema) : APFixedPoint(-Val, Sema);
  184. else
  185. return APFixedPoint(Sema);
  186. }
  187. llvm::APSInt APFixedPoint::convertToInt(unsigned DstWidth, bool DstSign,
  188. bool *Overflow) const {
  189. llvm::APSInt Result = getIntPart();
  190. unsigned SrcWidth = getWidth();
  191. llvm::APSInt DstMin = llvm::APSInt::getMinValue(DstWidth, !DstSign);
  192. llvm::APSInt DstMax = llvm::APSInt::getMaxValue(DstWidth, !DstSign);
  193. if (SrcWidth < DstWidth) {
  194. Result = Result.extend(DstWidth);
  195. } else if (SrcWidth > DstWidth) {
  196. DstMin = DstMin.extend(SrcWidth);
  197. DstMax = DstMax.extend(SrcWidth);
  198. }
  199. if (Overflow) {
  200. if (Result.isSigned() && !DstSign) {
  201. *Overflow = Result.isNegative() || Result.ugt(DstMax);
  202. } else if (Result.isUnsigned() && DstSign) {
  203. *Overflow = Result.ugt(DstMax);
  204. } else {
  205. *Overflow = Result < DstMin || Result > DstMax;
  206. }
  207. }
  208. Result.setIsSigned(DstSign);
  209. return Result.extOrTrunc(DstWidth);
  210. }
  211. APFixedPoint APFixedPoint::getFromIntValue(const llvm::APSInt &Value,
  212. const FixedPointSemantics &DstFXSema,
  213. bool *Overflow) {
  214. FixedPointSemantics IntFXSema = FixedPointSemantics::GetIntegerSemantics(
  215. Value.getBitWidth(), Value.isSigned());
  216. return APFixedPoint(Value, IntFXSema).convert(DstFXSema, Overflow);
  217. }
  218. } // namespace clang