[llvm] [APFloat] Add `APFloat::getConstant` for C++20 <numbers> constants (PR #227916)

Eric Ross via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 2 19:51:47 PDT 2026


================
@@ -6061,6 +6061,87 @@ APFloat APFloat::getAllOnesValue(const fltSemantics &Semantics) {
   return APFloat(Semantics, APInt::getAllOnes(Semantics.sizeInBits));
 }
 
+// Stores mathematical constants as APInt * 2^exponent. Each constant is rounded
+// via round-to-odd (jamming), so the LSB should be set. These constants are of
+// sufficient precision for f128 and ppcf128. More precision will be needed if
+// we add support for f256 in the future.
+struct MathConstantValue {
+  uint64_t Significand[2];
+  int Exponent;
+  // Precision of the mathematical constants, currently 128 bits.
+  static constexpr unsigned rawPrecision() {
+    return sizeof(Significand) * CHAR_BIT;
+  }
+  // The maximum precision that we can safely round to. We need at least two
+  // additional bits of precision to avoid double-rounding.
+  static constexpr unsigned roundedPrecision() { return rawPrecision() - 2; }
+};
+
+// IEEEQuad is the highest precision type we currently support.
+static constexpr unsigned PrecisionOfIEEEQuad = 113;
+
+static_assert(MathConstantValue::roundedPrecision() >= PrecisionOfIEEEQuad,
+              "Stored constants are too narrow for the supported semantics");
+
+// Internal semantics used to store mathematical constants at a high precision.
+// Uses the exponent range of IEEEquad. sizeInBits is unused, since nothing
+// bitcasts this format.
+static constexpr fltSemantics semMathConstant = {
+    /*maxExponent=*/16383, /*minExponent=*/-16382,
+    /*precision=*/MathConstantValue::rawPrecision(),
+    /*sizeInBits=*/0};
----------------
ZERICO2005 wrote:

What size should I use here for `semMathConstant`?

https://github.com/llvm/llvm-project/pull/227916


More information about the llvm-commits mailing list