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

via libc-commits libc-commits at lists.llvm.org
Tue Apr 2 06:58:34 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:

The following allows you to remove `underflow_rndz_fallback`:
```
    auto prec = mpfr_precision * 3;
    MPFRNumber result(*this, prec);

    float f = mpfr_get_flt(abs().value, mpfr_rounding);
    if (f > 0.5f && f < 0x1.0p30f) {
      mpfr_exp2(result.value, value, mpfr_rounding);
      mpfr_sub_ui(result.value, result.value, 1, mpfr_rounding);
      return result;
    }

    MPFRNumber ln2(2.0f, prec);
    mpfr_log(ln2.value, ln2.value, mpfr_rounding); // log(2)
    mpfr_mul(result.value, value, ln2.value, mpfr_rounding); // x * log(2)
    int ex = mpfr_expm1(result.value, result.value, mpfr_rounding); // e^(x * log(2)) - 1
    mpfr_subnormalize(result.value, ex, mpfr_rounding);
    return result;
```

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


More information about the libc-commits mailing list