[libc-commits] [libc] [libc][math][c23] Add atanhf16 C23 math function. (PR #132612)

via libc-commits libc-commits at lists.llvm.org
Thu Apr 24 08:36:03 PDT 2025


================
@@ -297,6 +297,48 @@ LIBC_INLINE static double log2_eval(double x) {
   return result;
 }
 
+// x should be positive, normal finite value
+LIBC_INLINE static float log_eval_f(float x) {
+  // For x = 2^ex * (1 + mx), logf(x) = ex * logf(2) + logf(1 + mx).
+  using FPBits = fputil::FPBits<float>;
+  FPBits xbits(x);
+
+  float ex = static_cast<float>(xbits.get_exponent());
+  // p1 is the leading 7 bits of mx, i.e.
+  // p1 * 2^(-7) <= m_x < (p1 + 1) * 2^(-7).
+  int p1 = static_cast<int>(xbits.get_mantissa() >> (FPBits::FRACTION_LEN - 7));
+
+  // Set bs to (1 + (mx - p1*2^(-7))
+  xbits.set_uintval(xbits.uintval() & (FPBits::FRACTION_MASK >> 7));
+  xbits.set_biased_exponent(FPBits::EXP_BIAS);
+  // dx = (mx - p1*2^(-7)) / (1 + p1*2^(-7)).
+  float dx = (xbits.get_val() - 1.0f) * ONE_OVER_F_FLOAT[p1];
+
+  // Minimax polynomial for log(1 + dx), generated using Sollya:
+  //   > P = fpminimax(log(1 + x)/x, 6, [|SG...|], [0, 2^-7]);
+  //   > Q = (P - 1) / x;
+  //   > for i from 0 to degree(Q) do print(coeff(Q, i));
+  static constexpr float COEFFS[6] = {-0x1p-1f,        0x1.555556p-2f,
----------------
lntue wrote:

`static` is not needed.

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


More information about the libc-commits mailing list