[libc-commits] [libc] [llvm] [libc][math] Refactor logf16 to header-only shared math (PR #175408)

Muhammad Bassiouni via libc-commits libc-commits at lists.llvm.org
Sun Jan 11 15:16:18 PST 2026


================
@@ -0,0 +1,170 @@
+//===-- Implementation header for logf16 ----------------------*- 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_LOGF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_LOGF16_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"
+#include "src/__support/math/expxf16_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
+static constexpr size_t N_LOGF16_EXCEPTS = 5;
+#else
+static constexpr size_t N_LOGF16_EXCEPTS = 11;
+#endif
+
+static constexpr fputil::ExceptValues<float16, N_LOGF16_EXCEPTS>
+    LOGF16_EXCEPTS = {{
+// (input, RZ output, RU offset, RD offset, RN offset)
+#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
+        // x = 0x1.61cp-13, logf16(x) = -0x1.16p+3 (RZ)
+        {0x0987U, 0xc858U, 0U, 1U, 0U},
+        // x = 0x1.f2p-12, logf16(x) = -0x1.e98p+2 (RZ)
+        {0x0fc8U, 0xc7a6U, 0U, 1U, 1U},
+#endif
+        // x = 0x1.4d4p-9, logf16(x) = -0x1.7e4p+2 (RZ)
+        {0x1935U, 0xc5f9U, 0U, 1U, 0U},
+        // x = 0x1.5ep-8, logf16(x) = -0x1.4ecp+2 (RZ)
+        {0x1d78U, 0xc53bU, 0U, 1U, 0U},
+#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
+        // x = 0x1.fdp-1, logf16(x) = -0x1.81p-8 (RZ)
+        {0x3bf4U, 0x9e04U, 0U, 1U, 1U},
+        // x = 0x1.fep-1, logf16(x) = -0x1.008p-8 (RZ)
+        {0x3bf8U, 0x9c02U, 0U, 1U, 0U},
+#endif
+        // x = 0x1.ffp-1, logf16(x) = -0x1.004p-9 (RZ)
+        {0x3bfcU, 0x9801U, 0U, 1U, 0U},
+        // x = 0x1.ff8p-1, logf16(x) = -0x1p-10 (RZ)
+        {0x3bfeU, 0x9400U, 0U, 1U, 1U},
+#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
+        // x = 0x1.4c4p+1, logf16(x) = 0x1.e84p-1 (RZ)
+        {0x4131U, 0x3ba1U, 1U, 0U, 1U},
+#else
+        // x = 0x1.75p+2, logf16(x) = 0x1.c34p+0 (RZ)
+        {0x45d4U, 0x3f0dU, 1U, 0U, 0U},
+        // x = 0x1.75p+2, logf16(x) = 0x1.c34p+0 (RZ)
+        {0x45d4U, 0x3f0dU, 1U, 0U, 0U},
+        // x = 0x1.d5p+9, logf16(x) = 0x1.b5cp+2 (RZ)
+        {0x6354U, 0x46d7U, 1U, 0U, 1U},
+#endif
+    }};
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
----------------
bassiounix wrote:

```suggestion
namespace logf16_internal {

#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
static constexpr size_t N_LOGF16_EXCEPTS = 5;
#else
static constexpr size_t N_LOGF16_EXCEPTS = 11;
#endif

static constexpr fputil::ExceptValues<float16, N_LOGF16_EXCEPTS>
    LOGF16_EXCEPTS = {{
// (input, RZ output, RU offset, RD offset, RN offset)
#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
        // x = 0x1.61cp-13, logf16(x) = -0x1.16p+3 (RZ)
        {0x0987U, 0xc858U, 0U, 1U, 0U},
        // x = 0x1.f2p-12, logf16(x) = -0x1.e98p+2 (RZ)
        {0x0fc8U, 0xc7a6U, 0U, 1U, 1U},
#endif
        // x = 0x1.4d4p-9, logf16(x) = -0x1.7e4p+2 (RZ)
        {0x1935U, 0xc5f9U, 0U, 1U, 0U},
        // x = 0x1.5ep-8, logf16(x) = -0x1.4ecp+2 (RZ)
        {0x1d78U, 0xc53bU, 0U, 1U, 0U},
#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
        // x = 0x1.fdp-1, logf16(x) = -0x1.81p-8 (RZ)
        {0x3bf4U, 0x9e04U, 0U, 1U, 1U},
        // x = 0x1.fep-1, logf16(x) = -0x1.008p-8 (RZ)
        {0x3bf8U, 0x9c02U, 0U, 1U, 0U},
#endif
        // x = 0x1.ffp-1, logf16(x) = -0x1.004p-9 (RZ)
        {0x3bfcU, 0x9801U, 0U, 1U, 0U},
        // x = 0x1.ff8p-1, logf16(x) = -0x1p-10 (RZ)
        {0x3bfeU, 0x9400U, 0U, 1U, 1U},
#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
        // x = 0x1.4c4p+1, logf16(x) = 0x1.e84p-1 (RZ)
        {0x4131U, 0x3ba1U, 1U, 0U, 1U},
#else
        // x = 0x1.75p+2, logf16(x) = 0x1.c34p+0 (RZ)
        {0x45d4U, 0x3f0dU, 1U, 0U, 0U},
        // x = 0x1.75p+2, logf16(x) = 0x1.c34p+0 (RZ)
        {0x45d4U, 0x3f0dU, 1U, 0U, 0U},
        // x = 0x1.d5p+9, logf16(x) = 0x1.b5cp+2 (RZ)
        {0x6354U, 0x46d7U, 1U, 0U, 1U},
#endif
    }};
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS

} // namespace logf16_internal
```

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


More information about the libc-commits mailing list