[libc-commits] [libc] [libc][math][c23] Add atanhf16 C23 math function. (PR #132612)

via libc-commits libc-commits at lists.llvm.org
Wed Mar 26 10:01:42 PDT 2025


================
@@ -0,0 +1,86 @@
+//===-- Implementation of atanh(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/atanhf16.h"
+#include "explogxf.h"
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/cast.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"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(float16, atanhf16, (float16 x)) {
+  using FPBits = typename fputil::FPBits<float16>;
+
+  FPBits xbits(x);
+  Sign sign = xbits.sign();
+  uint16_t x_abs = xbits.abs().uintval();
+
+  if (LIBC_UNLIKELY(x_abs >= 0x3c00U)) {
+    if (xbits.is_nan()) {
+      return x;
+    }
+    // |x| == 1.0
+    if (x_abs == 0x3c00U) {
+      fputil::set_errno_if_required(ERANGE);
+      fputil::raise_except_if_required(FE_DIVBYZERO);
+      return FPBits::inf(sign).get_val();
+    } else {
+      fputil::set_errno_if_required(EDOM);
+      fputil::raise_except_if_required(FE_INVALID);
+      return FPBits::quiet_nan().get_val();
+    }
----------------
overmighty wrote:

Nits:
- https://llvm.org/docs/CodingStandards.html#don-t-use-else-after-a-return
- https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements

```suggestion
    if (xbits.is_nan())
      return x;

    // |x| == 1.0
    if (x_abs == 0x3c00U) {
      fputil::set_errno_if_required(ERANGE);
      fputil::raise_except_if_required(FE_DIVBYZERO);
      return FPBits::inf(sign).get_val();
    }
    // |x| > 1.0
    fputil::set_errno_if_required(EDOM);
    fputil::raise_except_if_required(FE_INVALID);
    return FPBits::quiet_nan().get_val();
```

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


More information about the libc-commits mailing list