[llvm-branch-commits] [libc] [libc][math][c23] Add tanhf16 C23 math function (PR #106006)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Oct 15 06:49:39 PDT 2024


https://github.com/overmighty updated https://github.com/llvm/llvm-project/pull/106006

>From 1a0ef1066e233c275133c6c75e3ba6631a2edadf Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Sun, 25 Aug 2024 22:02:09 +0200
Subject: [PATCH 1/2] [libc][math][c23] Add tanhf16 C23 math function

Part of #95250.
---
 libc/config/gpu/entrypoints.txt           |   1 +
 libc/config/linux/x86_64/entrypoints.txt  |   1 +
 libc/docs/math/index.rst                  |   2 +-
 libc/spec/stdc.td                         |   1 +
 libc/src/math/CMakeLists.txt              |   1 +
 libc/src/math/generic/CMakeLists.txt      |  22 ++++
 libc/src/math/generic/tanhf16.cpp         | 144 ++++++++++++++++++++++
 libc/src/math/tanhf16.h                   |  21 ++++
 libc/test/src/math/CMakeLists.txt         |  11 ++
 libc/test/src/math/smoke/CMakeLists.txt   |  12 ++
 libc/test/src/math/smoke/tanhf16_test.cpp | 131 ++++++++++++++++++++
 libc/test/src/math/tanhf16_test.cpp       |  40 ++++++
 12 files changed, 386 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/math/generic/tanhf16.cpp
 create mode 100644 libc/src/math/tanhf16.h
 create mode 100644 libc/test/src/math/smoke/tanhf16_test.cpp
 create mode 100644 libc/test/src/math/tanhf16_test.cpp

diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index 4bb81f5d3b2de1..d89093b2117c2d 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -587,6 +587,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.setpayloadf16
     libc.src.math.setpayloadsigf16
     libc.src.math.sinhf16
+    libc.src.math.tanhf16
     libc.src.math.totalorderf16
     libc.src.math.totalordermagf16
     libc.src.math.truncf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index a8988336cb44fc..1645ff917821b3 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -680,6 +680,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.setpayloadf16
     libc.src.math.setpayloadsigf16
     libc.src.math.sinhf16
+    libc.src.math.tanhf16
     libc.src.math.totalorderf16
     libc.src.math.totalordermagf16
     libc.src.math.truncf16
diff --git a/libc/docs/math/index.rst b/libc/docs/math/index.rst
index 29a0e6e29d8612..61c95e065b24da 100644
--- a/libc/docs/math/index.rst
+++ b/libc/docs/math/index.rst
@@ -348,7 +348,7 @@ Higher Math Functions
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | tan       | |check|          | |check|         |                        |                      |                        | 7.12.4.7               | F.10.1.7                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
-| tanh      | |check|          |                 |                        |                      |                        | 7.12.5.6               | F.10.2.6                   |
+| tanh      | |check|          |                 |                        | |check|              |                        | 7.12.5.6               | F.10.2.6                   |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
 | tanpi     |                  |                 |                        |                      |                        | 7.12.4.14              | F.10.1.14                  |
 +-----------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index e4e46e7e13a586..196dab9f81b38e 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -798,6 +798,7 @@ def StdC : StandardSpec<"stdc"> {
           GuardedFunctionSpec<"sinhf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
 
           FunctionSpec<"tanhf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
+          GuardedFunctionSpec<"tanhf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
 
           FunctionSpec<"acosf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
 
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index c35950d4b61823..8b9e7aeee591c7 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -495,6 +495,7 @@ add_math_entrypoint_object(tanf)
 
 add_math_entrypoint_object(tanh)
 add_math_entrypoint_object(tanhf)
+add_math_entrypoint_object(tanhf16)
 
 add_math_entrypoint_object(tgamma)
 add_math_entrypoint_object(tgammaf)
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index c29c5d0b007419..a5d10cf7f65cc1 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4269,6 +4269,28 @@ add_entrypoint_object(
     -O3
 )
 
+add_entrypoint_object(
+  tanhf16
+  SRCS
+    tanhf16.cpp
+  HDRS
+    ../tanhf16.h
+  DEPENDS
+    .expxf16
+    libc.hdr.fenv_macros
+    libc.src.__support.CPP.array
+    libc.src.__support.FPUtil.except_value_utils
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.FPUtil.nearest_integer
+    libc.src.__support.FPUtil.polyeval
+    libc.src.__support.FPUtil.rounding_mode
+    libc.src.__support.macros.optimization
+  COMPILE_OPTIONS
+    -O3
+)
+
 add_entrypoint_object(
   acoshf
   SRCS
diff --git a/libc/src/math/generic/tanhf16.cpp b/libc/src/math/generic/tanhf16.cpp
new file mode 100644
index 00000000000000..52caa3bc49409a
--- /dev/null
+++ b/libc/src/math/generic/tanhf16.cpp
@@ -0,0 +1,144 @@
+//===-- Half-precision tanh(x) function -----------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/tanhf16.h"
+#include "expxf16.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/CPP/array.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/nearest_integer.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+static constexpr fputil::ExceptValues<float16, 2> TANHF16_EXCEPTS = {{
+    // x = 0x1.f54p+0, tanhf16(x) = 0x1.ecp-1 (RZ)
+    {0x3fd5U, 0x3bb0U, 1U, 0U, 0U},
+    // x = -0x1.f54p+0, tanhf16(x) = -0x1.ecp-1 (RZ)
+    {0xbfd5U, 0xbbb0U, 0U, 1U, 0U},
+}};
+
+LLVM_LIBC_FUNCTION(float16, tanhf16, (float16 x)) {
+  using FPBits = fputil::FPBits<float16>;
+  FPBits x_bits(x);
+
+  uint16_t x_u = x_bits.uintval();
+  uint16_t x_abs = x_u & 0x7fffU;
+
+  // When -2^(-14) <= x <= -2^(-9), or |x| <= 0x1.d2p-4,
+  // or |x| >= atanh(1 - 2^(-11)), or x is NaN.
+  if (LIBC_UNLIKELY((x_u >= 0x8400U && x_u <= 0x9800U) || x_abs <= 0x2f48U ||
+                    x_abs >= 0x4429U)) {
+    // tanh(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;
+    }
+
+    // When -2^(-14) <= x <= -2^(-9).
+    if (x_u >= 0x8400U && x_u <= 0x9800U) {
+      switch (fputil::quick_get_round()) {
+      case FE_TONEAREST:
+      case FE_DOWNWARD:
+        return x;
+      default:
+        return FPBits(static_cast<uint16_t>(x_u - 1U)).get_val();
+      }
+    }
+
+    // When |x| <= 0x1.d2p-4.
+    if (x_abs <= 0x2f48U) {
+      float xf = x;
+      float xf_sq = xf * xf;
+      // Degree-7 Taylor expansion generated by Sollya with the following
+      // commands:
+      //   > taylor(tanh(x), 7, 0);
+      //   > display = hexadecimal;
+      //   > // For each coefficient:
+      //   > round(/* put coefficient here */, SG, RN);
+      return static_cast<float16>(
+          xf * fputil::polyeval(xf_sq, 0x1p+0f, -0x1.555556p-2f, 0x1.111112p-3f,
+                                -0x1.ba1ba2p-5f));
+    }
+
+    // tanh(+/-inf) = +/-1
+    if (x_bits.is_inf())
+      return FPBits::one(x_bits.sign()).get_val();
+
+    // When |x| >= atanh(1 - 2^(-11)).
+    fputil::raise_except_if_required(FE_INEXACT);
+
+    int rounding_mode = fputil::quick_get_round();
+    if ((rounding_mode == FE_TONEAREST && x_abs >= 0x4482U) ||
+        (rounding_mode == FE_UPWARD && x_bits.is_pos()) ||
+        (rounding_mode == FE_DOWNWARD && x_bits.is_neg())) {
+      return FPBits::one(x_bits.sign()).get_val();
+    }
+    if (x_bits.is_pos())
+      return static_cast<float16>(0x1.ffcp-1);
+    return static_cast<float16>(-0x1.ffcp-1);
+  }
+
+  if (auto r = TANHF16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
+    return r.value();
+
+  // For atanh(-1 + 2^(-11)) < x < atanh(1 - 2^(-11)), to compute tanh(x), we
+  // perform the following range reduction: find hi, mid, lo, such that:
+  //   x = (hi + mid) * log(2) * 0.5 + lo, in which
+  //     hi is an integer,
+  //     mid * 2^5 is an integer,
+  //     -2^(-5) <= lo < 2^(-5).
+  // In particular,
+  //   hi + mid = round(x * log2(e) * 2 * 2^5) * 2^(-5).
+  // Then,
+  //   tanh(x) = sinh(x)/cosh(x)
+  //           = (e^x - e^(-x)) / (e^x + e^(-x))
+  //           = (e^(2x) - 1) / (e^(2x) + 1)
+  //           = (2^(hi + mid) * e^(2*lo) - 1) / (2^(hi + mid) * e^(2*lo) + 1)
+  //           = (e^(2*lo) - 2^(-hi - mid)) / (e^(2*lo) + 2^(-hi - mid))
+  // We store 2^(-mid) in the lookup table EXP2_MID_5_BITS, and compute
+  // 2^(-hi - mid) by adding -hi to the exponent field of 2^(-mid).
+  // e^lo is computed using a degree-3 minimax polynomial generated by Sollya.
+
+  float xf = x;
+  float kf = fputil::nearest_integer(xf * (LOG2F_E * 2.0f * 0x1.0p+5f));
+  int x_hi_mid = -static_cast<int>(kf);
+  int x_hi = x_hi_mid >> 5;
+  int x_mid = x_hi_mid & 0x1f;
+  // lo = x - (hi + mid)
+  //    = round(x * log2(e) * 2 * 2^5) * log(2) * 0.5 * (-2^(-5)) + x
+  float lo = fputil::multiply_add(kf, LOGF_2 * 0.5f * -0x1.0p-5f, xf);
+
+  uint32_t exp2_hi_mid_bits =
+      EXP2_MID_5_BITS[x_mid] +
+      static_cast<uint32_t>(x_hi << fputil::FPBits<float>::FRACTION_LEN);
+  // exp2_hi_mid = 2^(-hi - mid)
+  float exp2_hi_mid = fputil::FPBits<float>(exp2_hi_mid_bits).get_val();
+  // Degree-3 minimax polynomial generated by Sollya with the following
+  // commands:
+  //   > display = hexadecimal;
+  //   > P = fpminimax(expm1(2*x)/x, 2, [|SG...|], [-2^-5, 2^-5]);
+  //   > 1 + x * P;
+  float exp_2lo =
+      fputil::polyeval(lo, 0x1p+0f, 0x1p+1f, 0x1.001p+1f, 0x1.555ddep+0f);
+  return static_cast<float16>((exp_2lo - exp2_hi_mid) /
+                              (exp_2lo + exp2_hi_mid));
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/tanhf16.h b/libc/src/math/tanhf16.h
new file mode 100644
index 00000000000000..67498708fc462e
--- /dev/null
+++ b/libc/src/math/tanhf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for tanhf16 -----------------------*- 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_MATH_TANHF16_H
+#define LLVM_LIBC_SRC_MATH_TANHF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+float16 tanhf16(float16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_TANHF16_H
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 254d736fd998a5..d2a75bd7c8e1f9 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -1955,6 +1955,17 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  tanhf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    tanhf16_test.cpp
+  DEPENDS
+    libc.src.math.tanhf16
+)
+
 add_fp_unittest(
   atanhf_test
   NEED_MPFR
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 71fc85c88ef607..46321c022cbad3 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3767,6 +3767,18 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  tanhf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    tanhf16_test.cpp
+  DEPENDS
+    libc.hdr.fenv_macros
+    libc.src.errno.errno
+    libc.src.math.tanhf16
+)
+
 add_fp_unittest(
   atanhf_test
   SUITE
diff --git a/libc/test/src/math/smoke/tanhf16_test.cpp b/libc/test/src/math/smoke/tanhf16_test.cpp
new file mode 100644
index 00000000000000..48bbeb677541e8
--- /dev/null
+++ b/libc/test/src/math/smoke/tanhf16_test.cpp
@@ -0,0 +1,131 @@
+//===-- Unittests for tanhf16 ---------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "hdr/fenv_macros.h"
+#include "src/errno/libc_errno.h"
+#include "src/math/tanhf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcTanhf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
+
+TEST_F(LlvmLibcTanhf16Test, SpecialNumbers) {
+  LIBC_NAMESPACE::libc_errno = 0;
+
+  EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::tanhf16(aNaN));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::tanhf16(sNaN), FE_INVALID);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(static_cast<float16>(1.0),
+                            LIBC_NAMESPACE::tanhf16(inf));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(static_cast<float16>(-1.0),
+                            LIBC_NAMESPACE::tanhf16(neg_inf));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::tanhf16(zero));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::tanhf16(neg_zero));
+  EXPECT_MATH_ERRNO(0);
+}
+
+TEST_F(LlvmLibcTanhf16Test, ResultNearBounds) {
+  LIBC_NAMESPACE::libc_errno = 0;
+
+  EXPECT_FP_EQ_WITH_EXCEPTION(static_cast<float16>(1.0),
+                              LIBC_NAMESPACE::tanhf16(max_normal), FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION(static_cast<float16>(-1.0),
+                              LIBC_NAMESPACE::tanhf16(neg_max_normal),
+                              FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  // round(atanh(1 - 2^-11), HP, RU);
+  float16 x = static_cast<float16>(0x1.0a4p+2);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      static_cast<float16>(0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      static_cast<float16>(1.0), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      static_cast<float16>(0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+      static_cast<float16>(0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  x = static_cast<float16>(0x1.208p+2);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      static_cast<float16>(1.0), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      static_cast<float16>(1.0), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      static_cast<float16>(0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+      static_cast<float16>(0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  // round(atanh(-1 + 2^-11), HP, RD);
+  x = static_cast<float16>(-0x1.0a4p+2);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      static_cast<float16>(-0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x),
+      FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(static_cast<float16>(-0x1.ffcp-1),
+                                              LIBC_NAMESPACE::tanhf16(x),
+                                              FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      static_cast<float16>(-1.0), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+      static_cast<float16>(-0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x),
+      FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  x = static_cast<float16>(-0x1.208p+2);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      static_cast<float16>(-1.0), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(static_cast<float16>(-0x1.ffcp-1),
+                                              LIBC_NAMESPACE::tanhf16(x),
+                                              FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      static_cast<float16>(-1.0), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+      static_cast<float16>(-0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x),
+      FE_INEXACT);
+  EXPECT_MATH_ERRNO(0);
+}
diff --git a/libc/test/src/math/tanhf16_test.cpp b/libc/test/src/math/tanhf16_test.cpp
new file mode 100644
index 00000000000000..7124a83f3d7bc8
--- /dev/null
+++ b/libc/test/src/math/tanhf16_test.cpp
@@ -0,0 +1,40 @@
+//===-- Exhaustive test for tanhf16 ---------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/math/tanhf16.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcTanhf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// Range: [0, Inf];
+static constexpr uint16_t POS_START = 0x0000U;
+static constexpr uint16_t POS_STOP = 0x7c00U;
+
+// Range: [-Inf, 0];
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xfc00U;
+
+TEST_F(LlvmLibcTanhf16Test, PositiveRange) {
+  for (uint16_t v = POS_START; v <= POS_STOP; ++v) {
+    float16 x = FPBits(v).get_val();
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanh, x,
+                                   LIBC_NAMESPACE::tanhf16(x), 0.5);
+  }
+}
+
+TEST_F(LlvmLibcTanhf16Test, NegativeRange) {
+  for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) {
+    float16 x = FPBits(v).get_val();
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanh, x,
+                                   LIBC_NAMESPACE::tanhf16(x), 0.5);
+  }
+}

>From c343f44962a84cf86d2132d2b2d4ca7ab6a92c0c Mon Sep 17 00:00:00 2001
From: OverMighty <its.overmighty at gmail.com>
Date: Tue, 15 Oct 2024 15:46:53 +0200
Subject: [PATCH 2/2] Migrate to fputil::cast

---
 libc/src/math/generic/CMakeLists.txt      |  1 +
 libc/src/math/generic/tanhf16.cpp         | 11 ++--
 libc/test/src/math/smoke/CMakeLists.txt   |  1 +
 libc/test/src/math/smoke/tanhf16_test.cpp | 74 +++++++++++++----------
 4 files changed, 51 insertions(+), 36 deletions(-)

diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index a5d10cf7f65cc1..477dfd2af29e73 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4279,6 +4279,7 @@ add_entrypoint_object(
     .expxf16
     libc.hdr.fenv_macros
     libc.src.__support.CPP.array
+    libc.src.__support.FPUtil.cast
     libc.src.__support.FPUtil.except_value_utils
     libc.src.__support.FPUtil.fenv_impl
     libc.src.__support.FPUtil.fp_bits
diff --git a/libc/src/math/generic/tanhf16.cpp b/libc/src/math/generic/tanhf16.cpp
index 52caa3bc49409a..3b555e3ccfaaa6 100644
--- a/libc/src/math/generic/tanhf16.cpp
+++ b/libc/src/math/generic/tanhf16.cpp
@@ -13,6 +13,7 @@
 #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/FPUtil/nearest_integer.h"
@@ -72,7 +73,7 @@ LLVM_LIBC_FUNCTION(float16, tanhf16, (float16 x)) {
       //   > display = hexadecimal;
       //   > // For each coefficient:
       //   > round(/* put coefficient here */, SG, RN);
-      return static_cast<float16>(
+      return fputil::cast<float16>(
           xf * fputil::polyeval(xf_sq, 0x1p+0f, -0x1.555556p-2f, 0x1.111112p-3f,
                                 -0x1.ba1ba2p-5f));
     }
@@ -91,8 +92,8 @@ LLVM_LIBC_FUNCTION(float16, tanhf16, (float16 x)) {
       return FPBits::one(x_bits.sign()).get_val();
     }
     if (x_bits.is_pos())
-      return static_cast<float16>(0x1.ffcp-1);
-    return static_cast<float16>(-0x1.ffcp-1);
+      return fputil::cast<float16>(0x1.ffcp-1);
+    return fputil::cast<float16>(-0x1.ffcp-1);
   }
 
   if (auto r = TANHF16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
@@ -137,8 +138,8 @@ LLVM_LIBC_FUNCTION(float16, tanhf16, (float16 x)) {
   //   > 1 + x * P;
   float exp_2lo =
       fputil::polyeval(lo, 0x1p+0f, 0x1p+1f, 0x1.001p+1f, 0x1.555ddep+0f);
-  return static_cast<float16>((exp_2lo - exp2_hi_mid) /
-                              (exp_2lo + exp2_hi_mid));
+  return fputil::cast<float16>((exp_2lo - exp2_hi_mid) /
+                               (exp_2lo + exp2_hi_mid));
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 46321c022cbad3..b307d6caa7a1b1 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -3777,6 +3777,7 @@ add_fp_unittest(
     libc.hdr.fenv_macros
     libc.src.errno.errno
     libc.src.math.tanhf16
+    libc.src.__support.FPUtil.cast
 )
 
 add_fp_unittest(
diff --git a/libc/test/src/math/smoke/tanhf16_test.cpp b/libc/test/src/math/smoke/tanhf16_test.cpp
index 48bbeb677541e8..fa6328e9ef0a60 100644
--- a/libc/test/src/math/smoke/tanhf16_test.cpp
+++ b/libc/test/src/math/smoke/tanhf16_test.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/cast.h"
 #include "src/errno/libc_errno.h"
 #include "src/math/tanhf16.h"
 #include "test/UnitTest/FPMatcher.h"
@@ -23,11 +24,11 @@ TEST_F(LlvmLibcTanhf16Test, SpecialNumbers) {
   EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::tanhf16(sNaN), FE_INVALID);
   EXPECT_MATH_ERRNO(0);
 
-  EXPECT_FP_EQ_ALL_ROUNDING(static_cast<float16>(1.0),
+  EXPECT_FP_EQ_ALL_ROUNDING(LIBC_NAMESPACE::fputil::cast<float16>(1.0),
                             LIBC_NAMESPACE::tanhf16(inf));
   EXPECT_MATH_ERRNO(0);
 
-  EXPECT_FP_EQ_ALL_ROUNDING(static_cast<float16>(-1.0),
+  EXPECT_FP_EQ_ALL_ROUNDING(LIBC_NAMESPACE::fputil::cast<float16>(-1.0),
                             LIBC_NAMESPACE::tanhf16(neg_inf));
   EXPECT_MATH_ERRNO(0);
 
@@ -41,91 +42,102 @@ TEST_F(LlvmLibcTanhf16Test, SpecialNumbers) {
 TEST_F(LlvmLibcTanhf16Test, ResultNearBounds) {
   LIBC_NAMESPACE::libc_errno = 0;
 
-  EXPECT_FP_EQ_WITH_EXCEPTION(static_cast<float16>(1.0),
+  EXPECT_FP_EQ_WITH_EXCEPTION(LIBC_NAMESPACE::fputil::cast<float16>(1.0),
                               LIBC_NAMESPACE::tanhf16(max_normal), FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
-  EXPECT_FP_EQ_WITH_EXCEPTION(static_cast<float16>(-1.0),
+  EXPECT_FP_EQ_WITH_EXCEPTION(LIBC_NAMESPACE::fputil::cast<float16>(-1.0),
                               LIBC_NAMESPACE::tanhf16(neg_max_normal),
                               FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
   // round(atanh(1 - 2^-11), HP, RU);
-  float16 x = static_cast<float16>(0x1.0a4p+2);
+  float16 x = LIBC_NAMESPACE::fputil::cast<float16>(0x1.0a4p+2);
 
   EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
-      static_cast<float16>(0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+      LIBC_NAMESPACE::fputil::cast<float16>(0x1.ffcp-1),
+      LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
   EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
-      static_cast<float16>(1.0), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+      LIBC_NAMESPACE::fputil::cast<float16>(1.0), LIBC_NAMESPACE::tanhf16(x),
+      FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
   EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
-      static_cast<float16>(0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+      LIBC_NAMESPACE::fputil::cast<float16>(0x1.ffcp-1),
+      LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
   EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
-      static_cast<float16>(0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+      LIBC_NAMESPACE::fputil::cast<float16>(0x1.ffcp-1),
+      LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
-  x = static_cast<float16>(0x1.208p+2);
+  x = LIBC_NAMESPACE::fputil::cast<float16>(0x1.208p+2);
 
   EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
-      static_cast<float16>(1.0), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+      LIBC_NAMESPACE::fputil::cast<float16>(1.0), LIBC_NAMESPACE::tanhf16(x),
+      FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
   EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
-      static_cast<float16>(1.0), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+      LIBC_NAMESPACE::fputil::cast<float16>(1.0), LIBC_NAMESPACE::tanhf16(x),
+      FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
   EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
-      static_cast<float16>(0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+      LIBC_NAMESPACE::fputil::cast<float16>(0x1.ffcp-1),
+      LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
   EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
-      static_cast<float16>(0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+      LIBC_NAMESPACE::fputil::cast<float16>(0x1.ffcp-1),
+      LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
   // round(atanh(-1 + 2^-11), HP, RD);
-  x = static_cast<float16>(-0x1.0a4p+2);
+  x = LIBC_NAMESPACE::fputil::cast<float16>(-0x1.0a4p+2);
 
   EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
-      static_cast<float16>(-0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x),
-      FE_INEXACT);
+      LIBC_NAMESPACE::fputil::cast<float16>(-0x1.ffcp-1),
+      LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
-  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(static_cast<float16>(-0x1.ffcp-1),
-                                              LIBC_NAMESPACE::tanhf16(x),
-                                              FE_INEXACT);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      LIBC_NAMESPACE::fputil::cast<float16>(-0x1.ffcp-1),
+      LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
   EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
-      static_cast<float16>(-1.0), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+      LIBC_NAMESPACE::fputil::cast<float16>(-1.0), LIBC_NAMESPACE::tanhf16(x),
+      FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
   EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
-      static_cast<float16>(-0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x),
-      FE_INEXACT);
+      LIBC_NAMESPACE::fputil::cast<float16>(-0x1.ffcp-1),
+      LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
-  x = static_cast<float16>(-0x1.208p+2);
+  x = LIBC_NAMESPACE::fputil::cast<float16>(-0x1.208p+2);
 
   EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
-      static_cast<float16>(-1.0), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+      LIBC_NAMESPACE::fputil::cast<float16>(-1.0), LIBC_NAMESPACE::tanhf16(x),
+      FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
-  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(static_cast<float16>(-0x1.ffcp-1),
-                                              LIBC_NAMESPACE::tanhf16(x),
-                                              FE_INEXACT);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      LIBC_NAMESPACE::fputil::cast<float16>(-0x1.ffcp-1),
+      LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
   EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
-      static_cast<float16>(-1.0), LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
+      LIBC_NAMESPACE::fputil::cast<float16>(-1.0), LIBC_NAMESPACE::tanhf16(x),
+      FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 
   EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
-      static_cast<float16>(-0x1.ffcp-1), LIBC_NAMESPACE::tanhf16(x),
-      FE_INEXACT);
+      LIBC_NAMESPACE::fputil::cast<float16>(-0x1.ffcp-1),
+      LIBC_NAMESPACE::tanhf16(x), FE_INEXACT);
   EXPECT_MATH_ERRNO(0);
 }



More information about the llvm-branch-commits mailing list