[libc-commits] [libc] [libc][math][c23] Add exp2m1f C23 math function (PR #86996)

via libc-commits libc-commits at lists.llvm.org
Mon Apr 1 19:59:44 PDT 2024


================
@@ -223,6 +229,30 @@ class MPFRNumber {
     return result;
   }
 
+  MPFRNumber
+  exp2m1([[maybe_unused]] const MPFRNumber &underflow_rndz_fallback) const {
+    // TODO: Only use mpfr_exp2m1 once CI and buildbots get MPFR >= 4.2.0.
+#if MPFR_VERSION_MAJOR > 4 ||                                                  \
+    (MPFR_VERSION_MAJOR == 4 && MPFR_VERSION_MINOR >= 2)
+    MPFRNumber result(*this);
+    mpfr_exp2m1(result.value, value, mpfr_rounding);
+    return result;
+#else
+    MPFRNumber result(*this, mpfr_precision * 8);
----------------
lntue wrote:

You could use `mpfr_expm1` to compute `2^x - 1 = e^(x * ln2) - 1` instead:
```
    auto prec = mpfr_precision * 3;
    MPFRNumber result(*this, prec);
    MPFRNumber ln2(2.0f, prec);
    mpfr_log(ln2.value, ln2.value, mpfr_rounding); // log(2)
    mpfr_mul(result.value, value, mpfr_rounding); // x * log(2)
    mpfr_expm1(result.value, value, mpfr_rounding); // e^(x * log(2)) - 1
    return result;
```

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


More information about the libc-commits mailing list