[libc-commits] [libc] [libc][math][c23] Add log2p1f16 C23 math function (PR #186754)

Shikhar Soni via libc-commits libc-commits at lists.llvm.org
Sun Mar 22 03:18:01 PDT 2026


================
@@ -0,0 +1,205 @@
+//===-- Implementation header for log2p1f16 ---------------------*- 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_LOG2P1F16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LOG2P1F16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#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 log2p1f16(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, NaN, +/-inf, or |x| <= 2^-3.
+  if (LIBC_UNLIKELY(x_abs <= 0x3000U || x_abs >= 0x7c00U)) {
+    // log2p1(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;
+    }
+
+    // log2p1(+/-0) = +/-0
+    if (x_abs == 0U)
+      return x;
+
+    // log2p1(+inf) = +inf
+    if (x_u == 0x7c00U)
+      return FPBits::inf().get_val();
+
+    // log2p1(-inf) = NaN
+    if (x_abs >= 0x7c00U) {
+      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 degree-5 minimax polynomial on x.
+    // For y = 1+x near 1, the table-based range reduction suffers from
+    // catastrophic cancellation (m + log2(f) nearly cancel).  Computing
+    // log2(1+x) directly via polynomial avoids this.
+    //
+    // Generated by Sollya with:
+    //   > display = hexadecimal;
+    //   > Q = fpminimax(log2(1 + x), [|1, 2, 3, 4, 5|], [|SG...|],
+    //                   [-2^-3, 2^-3]);
+    //   > Q;
----------------
shikharish wrote:

The relative error bound is

  0x1.6ed1f4728dcb6f1d6a651a3c728937f7468f8eedfp-22
  ~= 3.4162787369e-7 ~= 2^-21.4811.

There are 11 worst cases all at 1 ULP:

  0x0FB1, 0x2701, 0x8309, 0x9505, 0x972E, 0x9E67,
  0xA33A, 0xA5CF, 0xAA1C, 0xAB52, 0xAF68


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


More information about the libc-commits mailing list