[PATCH] D68257: [Support] Add mathematical constants

Evandro Menezes via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 1 11:22:32 PDT 2019


evandro updated this revision to Diff 222658.
evandro added a comment.

Split the changes to the AMDGPU target in a separate patch.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D68257/new/

https://reviews.llvm.org/D68257

Files:
  llvm/include/llvm/Support/MathExtras.h
  llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
  llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp


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,7 @@
            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);
+      Eul = ConstantFP::get(Log->getType(), numbers::e);
     else if (ArgLb == Exp2Lb || ArgID == Intrinsic::exp2)
       Eul = ConstantFP::get(Log->getType(), 2.0);
     else
Index: llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -4969,12 +4969,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::log2e, dl, MVT::f32));
     return getLimitedPrecisionExp2(t0, dl, DAG);
   }
 
@@ -4992,10 +4991,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::ln2, dl, MVT::f32));
 
     // Get the significand and build it into a floating-point number with
     // exponent of 1.
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,25 @@
   ZB_Width
 };
 
+/// Mathematical constants.
+namespace numbers {
+constexpr double e          = 2.7182818284590452353602874713526624977572470937000, // https://oeis.org/A001113
+                 egamma     = 0.57721566490153286060651209008240243104215933593992, // https://oeis.org/A001620
+                 ln2        = 0.69314718055994530941723212145817656807550013436026, // https://oeis.org/A002162
+                 ln10       = 2.3025850929940456840179914546843642076011014886288, // https://oeis.org/A002392
+                 log2e      = 1.4426950408889634073599246810018921374266459541530,
+                 log10e     = 0.43429448190325182765112891891660508229439700580366,
+                 pi         = 3.1415926535897932384626433832795028841971693993751, // https://oeis.org/A000796
+                 inv_pi     = 0.31830988618379067153776752674502872406891929148091, // https://oeis.org/A049541
+                 sqrtpi     = 1.7724538509055160272981674833411451827975494561224, // https://oeis.org/A002161
+                 inv_sqrtpi = 0.56418958354775628694807945156077258584405062932900, // https://oeis.org/A087197
+                 sqrt2      = 1.4142135623730950488016887242096980785696718753770, // https://oeis.org/A002193
+                 inv_sqrt2  = 0.70710678118654752440084436210484903928483593768844,
+                 sqrt3      = 1.7320508075688772935274463415058723669428052538104, // https://oeis.org/A002194
+                 inv_sqrt3  = 0.57735026918962576450914878050195745564760175127012,
+                 phi        = 1.6180339887498948482045868343656381177203091798058; // https://oeis.org/A001622
+} // namespace numbers
+
 namespace detail {
 template <typename T, std::size_t SizeOfT> struct TrailingZerosCounter {
   static unsigned count(T Val, ZeroBehavior) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D68257.222658.patch
Type: text/x-patch
Size: 4760 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191001/4e32f1f0/attachment.bin>


More information about the llvm-commits mailing list