Index: llvm/include/llvm/Support/MathExtras.h =================================================================== --- llvm/include/llvm/Support/MathExtras.h +++ llvm/include/llvm/Support/MathExtras.h @@ -39,6 +39,7 @@ #endif namespace llvm { + /// The behavior an operation has on an input of 0. enum ZeroBehavior { /// The returned value is undefined. @@ -49,6 +50,41 @@ ZB_Width }; +/// Mathematical constants. +namespace numbers { +/// TODO: Track C++20 std::numbners. +constexpr double e = 2.718281828459045, // https://oeis.org/A001113 + egamma = 0.5772156649015329, // https://oeis.org/A001620 + ln2 = 0.6931471805599453, // https://oeis.org/A002162 + ln10 = 2.302585092994046, // https://oeis.org/A002392 + log2e = 1.442695040888963, + log10e = 0.4342944819032518, + pi = 3.141592653589793, // https://oeis.org/A000796 + inv_pi = 0.3183098861837907, // https://oeis.org/A049541 + sqrtpi = 1.772453850905516, // https://oeis.org/A002161 + inv_sqrtpi = 0.5641895835477563, // https://oeis.org/A087197 + sqrt2 = 1.414213562373095, // https://oeis.org/A002193 + inv_sqrt2 = 0.7071067811865475, + sqrt3 = 1.732050807568877, // https://oeis.org/A002194 + inv_sqrt3 = 0.5773502691896258, + phi = 1.618033988749895; // https://oeis.org/A001622 +constexpr float ef = 2.718282, // https://oeis.org/A001113 + egammaf = 0.5772157, // https://oeis.org/A001620 + ln2f = 0.6931472, // https://oeis.org/A002162 + ln10f = 2.302585, // https://oeis.org/A002392 + log2ef = 1.442695, + log10ef = 0.4342945, + pif = 3.141593, // https://oeis.org/A000796 + inv_pif = 0.3183099, // https://oeis.org/A049541 + sqrtpif = 1.772454, // https://oeis.org/A002161 + inv_sqrtpif = 0.5641896, // https://oeis.org/A087197 + sqrt2f = 1.414214, // https://oeis.org/A002193 + inv_sqrt2f = 0.7071068, + sqrt3f = 1.732051, // https://oeis.org/A002194 + inv_sqrt3f = 0.5773503, + phif = 1.618034; // https://oeis.org/A001622 +} // namespace numbers + namespace detail { template struct TrailingZerosCounter { static unsigned count(T Val, ZeroBehavior) { Index: llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp =================================================================== --- llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -4974,12 +4974,11 @@ // Put the exponent in the right bit position for later addition to the // final result: // - // #define LOG2OFe 1.4426950f - // t0 = Op * LOG2OFe + // t0 = Op * log2(e) // TODO: What fast-math-flags should be set here? SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, - getF32Constant(DAG, 0x3fb8aa3b, dl)); + DAG.getConstantFP(numbers::log2ef, dl, MVT::f32)); return getLimitedPrecisionExp2(t0, dl, DAG); } @@ -4997,10 +4996,11 @@ LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); - // Scale the exponent by log(2) [0.69314718f]. + // Scale the exponent by log(2). SDValue Exp = GetExponent(DAG, Op1, TLI, dl); - SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, - getF32Constant(DAG, 0x3f317218, dl)); + SDValue LogOfExponent = + DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, + DAG.getConstantFP(numbers::ln2f, dl, MVT::f32)); // Get the significand and build it into a floating-point number with // exponent of 1. Index: llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp =================================================================== --- llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -48,7 +48,6 @@ cl::desc("Enable unsafe double to float " "shrinking for math lib calls")); - //===----------------------------------------------------------------------===// // Helper Functions //===----------------------------------------------------------------------===// @@ -1941,9 +1940,9 @@ ArgID == Intrinsic::exp || ArgID == Intrinsic::exp2) { Constant *Eul; if (ArgLb == ExpLb || ArgID == Intrinsic::exp) - // FIXME: The Euler number should be M_E, but it's place of definition - // is not quite standard. - Eul = ConstantFP::get(Log->getType(), 2.7182818284590452354); + // FIXME: The `long double` type is not fully supported by the classes + // `APFloat` and `Constant`. + Eul = ConstantFP::get(Log->getType(), numbers::e); else if (ArgLb == Exp2Lb || ArgID == Intrinsic::exp2) Eul = ConstantFP::get(Log->getType(), 2.0); else