[libc-commits] [libc] [llvm] [libc][math][c23] Add atan2f16 function (PR #183531)

Xinlong Chen via libc-commits libc-commits at lists.llvm.org
Fri Feb 27 22:57:00 PST 2026


================
@@ -0,0 +1,59 @@
+//===-- Exhaustive test for atan2f16 --------------------------------------===//
+//
+// 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 "exhaustive_test.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/atan2f16.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+struct Atan2f16Checker : public virtual LIBC_NAMESPACE::testing::Test {
+  using FloatType = float16;
+  using FPBits = LIBC_NAMESPACE::fputil::FPBits<float16>;
+  using StorageType = uint16_t;
+
+  uint64_t check(uint16_t x_start, uint16_t x_stop, uint16_t y_start,
+                 uint16_t y_stop, mpfr::RoundingMode rounding) {
+    mpfr::ForceRoundingMode r(rounding);
+    if (!r.success)
+      return (x_stop > x_start) || (y_stop > y_start);
+    uint16_t xbits = x_start;
+    uint64_t failed = 0;
+    do {
+      float16 x = FPBits(xbits).get_val();
+      uint16_t ybits = y_start;
+      do {
+        float16 y = FPBits(ybits).get_val();
+        mpfr::BinaryInput<float16> input{y, x}; // atan2(y, x)
+        // Allow 1 ULP: atan2f16 is implemented with ~1 ULP accuracy.
+        bool correct = TEST_MPFR_MATCH_ROUNDING_SILENTLY(
+            mpfr::Operation::Atan2, input, LIBC_NAMESPACE::atan2f16(y, x), 1.5,
+            rounding);
+        failed += (!correct);
+        if (!correct) {
+          EXPECT_MPFR_MATCH_ROUNDING(mpfr::Operation::Atan2, input,
+                                     LIBC_NAMESPACE::atan2f16(y, x), 1.5,
+                                     rounding);
----------------
Xinlong-Chen wrote:

@hulxv Thanks for the suggestions.

When the test tolerance is set to `0.5 ULP`, the current implementation still yields errors up to `1 ULP` in many cases.

I was wondering whether it would be acceptable to (1) perform the computation in double precision and (2) reuse atan_eval from inv_trigf_utils_internal (as in `atan2f`), in order to achieve `0.5 ULP` and pass the exhaustive test.

I’d be glad to adjust the approach if you have a different preference.

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


More information about the libc-commits mailing list