[libc-commits] [libc] [libc][math][c23] Add acoshf16 C23 math function. (PR #130588)
via libc-commits
libc-commits at lists.llvm.org
Sat Mar 22 08:39:39 PDT 2025
================
@@ -0,0 +1,107 @@
+//===-- Half-precision acosh(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/acoshf16.h"
+#include "explogxf.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/cast.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/sqrt.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+static constexpr size_t N_EXCEPTS = 2;
+static constexpr fputil::ExceptValues<float16, N_EXCEPTS> ACOSHF16_EXCEPTS{{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ // x = 0x1.6dcp+1, acoshf16(x) = 0x1.b6p+0 (RZ)
+ {0x41B7, 0x3ED8, 1, 0, 0},
+ // x = 0x1.39p+0, acoshf16(x) = 0x1.4f8p-1 (RZ)
+ {0x3CE4, 0x393E, 1, 0, 1},
+}};
+
+LLVM_LIBC_FUNCTION(float16, acoshf16, (float16 x)) {
+ using FPBits = fputil::FPBits<float16>;
+ FPBits xbits(x);
+ uint16_t x_u = xbits.uintval();
+
+ // Domain error for inputs less than 1.0.
+ if (LIBC_UNLIKELY(x <= 1.0f)) {
+ if (x == 1.0f)
+ return FPBits::zero().get_val();
+ fputil::set_errno_if_required(EDOM);
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // Check for NaN input first.
+ if (LIBC_UNLIKELY(xbits.is_inf_or_nan())) {
----------------
overmighty wrote:
Should move the `if (LIBC_UNLIKELY(xbits.is_inf_or_nan()))` above the `if (LIBC_UNLIKELY(x <= 1.0f))`. We should check for NaNs before doing any kind of floating-point operation.
Even just comparing a quiet NaN could raise `FE_INVALID`: https://godbolt.org/z/M1GfPnPGa, although in this case it didn't happen when I tested on my machine, seemingly because Clang generated a `vucomiss` instruction instead of a `comiss` one.
https://github.com/llvm/llvm-project/pull/130588
More information about the libc-commits
mailing list