[libc-commits] [libc] [libc][math][c23] Add acoshf16 C23 math function. (PR #130588)

Harrison Hao via libc-commits libc-commits at lists.llvm.org
Thu Mar 13 05:55:41 PDT 2025


================
@@ -0,0 +1,82 @@
+//===-- Unittests for acoshf16 ----------------------------------------------===//
+//
+// 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/__support/FPUtil/cast.h"
+#include "src/errno/libc_errno.h"
+#include "src/math/acoshf16.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+#include <stdint.h>
+
+using LlvmLibcAcoshf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+TEST_F(LlvmLibcAcoshf16Test, SpecialNumbers) {
+  LIBC_NAMESPACE::libc_errno = 0;
+
+  EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acoshf16(aNaN));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acoshf16(sNaN), FE_INVALID);
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(float16(0.0f),
+                            LIBC_NAMESPACE::acoshf16(float16(1.0f)));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acoshf16(float16(0.5f)));
+  EXPECT_MATH_ERRNO(EDOM);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(inf, LIBC_NAMESPACE::acoshf16(inf));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acoshf16(neg_inf));
+  EXPECT_MATH_ERRNO(EDOM);
+}
+
+TEST_F(LlvmLibcAcoshf16Test, InFloat16Range) {
+  constexpr uint32_t COUNT = 100'000;
+  constexpr uint32_t STEP = UINT32_MAX / COUNT;
+
+  for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
+    LIBC_NAMESPACE::fputil::FPBits<float> bits(v);
+    float xf32 = bits.get_val();
+    if (bits.is_nan() || bits.is_inf())
+      continue;
+    if (xf32 < 1.0f)
+      continue;
+    float16 xh = LIBC_NAMESPACE::fputil::cast<float16>(xf32);
+
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acosh, xh,
+                                   LIBC_NAMESPACE::acoshf16(xh), 3.0);
----------------
harrisonGPU wrote:

Now I have already changed to 0.5 ULP.

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


More information about the libc-commits mailing list