[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 07:18:35 PDT 2024


================
@@ -0,0 +1,145 @@
+//===-- 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/cast.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 ||
----------------
lntue wrote:

The condition `-2^(-14) <= x <= -2^(-9)` implies `|x| <= 0x1.d2p-4`, so you only need the check `x_abs <= 0x2f48U` here.

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


More information about the llvm-branch-commits mailing list