[libc-commits] [libc] [libc][math][c23] Add log10p1f16 C23 math function (PR #184739)

Shikhar Soni via libc-commits libc-commits at lists.llvm.org
Wed Mar 11 23:01:58 PDT 2026


================
@@ -0,0 +1,192 @@
+//===-- Implementation header for log10p1f16 ---------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_LOG10P1F16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LOG10P1F16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "exp10_float16_constants.h"
+#include "expxf16_utils.h"
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+#include "src/__support/macros/properties/cpu_features.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE float16 log10p1f16(float16 x) {
+  using namespace math::expxf16_internal;
+  using FPBits = fputil::FPBits<float16>;
+  FPBits x_bits(x);
+
+  uint16_t x_u = x_bits.uintval();
+  uint16_t x_abs = x_u & 0x7fffU;
+
+  // If x is +-0, +inf, NaN, -1, x < -1, or |x| <= 2^-3.
+  if (LIBC_UNLIKELY(x_abs == 0U || x_abs <= 0x3000U || x_u == 0x7c00U ||
+                    x_u >= 0xbc00U || x_bits.is_nan())) {
+    // log10p1(NaN) = NaN
+    if (x_bits.is_nan()) {
+      if (x_bits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
+
+      return x;
+    }
+
+    // log10p1(+/-0) = +/-0
+    if (x_abs == 0U)
+      return x;
+
+    // log10p1(+inf) = +inf
+    if (x_u == 0x7c00U)
+      return FPBits::inf().get_val();
+
+    // log10p1(-1) = -inf
+    if (x_u == 0xbc00U) {
+      fputil::raise_except_if_required(FE_DIVBYZERO);
+      return FPBits::inf(Sign::NEG).get_val();
+    }
+
+    // log10p1(x) = NaN for x < -1
+    if (x_u > 0xbc00U) {
+      fputil::set_errno_if_required(EDOM);
+      fputil::raise_except_if_required(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
+
+    // When |x| <= 2^-3, use a polynomial approximation directly on x.
+    // For y = 1+x near 1, the table-based range reduction suffers from
+    // catastrophic cancellation (m*log10(2) + log10(f) nearly cancel).
+    // Computing log10(1+x) directly via polynomial avoids this.
+    //   log10(1+x) = x * (c0 + c1*x + c2*x^2 + c3*x^3 + c4*x^4 + c5*x^5 +
----------------
shikharish wrote:

@lntue updated. 

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


More information about the libc-commits mailing list