[libc-commits] [libc] [libc][math] Implement fast pass for double precision atan2 with 1 ULP errors. (PR #100648)

via libc-commits libc-commits at lists.llvm.org
Thu Jul 25 14:25:13 PDT 2024


================
@@ -0,0 +1,131 @@
+//===-- Unittests for atan2 -----------------------------------------------===//
+//
+// 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 "hdr/math_macros.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/math/atan2.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcAtan2Test = LIBC_NAMESPACE::testing::FPTest<double>;
+using LIBC_NAMESPACE::testing::tlog;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+TEST_F(LlvmLibcAtan2Test, TrickyInputs) {
+  mpfr::BinaryInput<double> INPUTS[] = {
+      {0x1.0853408534085p-2, 0x1.e7b54166c6126p-2},
+      {FPBits::inf().get_val(), 0x0.0000000000001p-1022},
+  };
+  constexpr int N = sizeof(INPUTS) / sizeof(INPUTS[0]);
+
+  for (int i = 0; i < N; ++i) {
+    double x = INPUTS[i].x;
+    double y = INPUTS[i].y;
+    mpfr::RoundingMode rm = mpfr::RoundingMode::Downward;
+    mpfr::ForceRoundingMode rr(rm);
+    ASSERT_MPFR_MATCH(mpfr::Operation::Atan2, INPUTS[i],
+                      LIBC_NAMESPACE::atan2(x, y), 0.5, rm);
+    INPUTS[i].x = -INPUTS[i].x;
+    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, INPUTS[i],
+                                   LIBC_NAMESPACE::atan2(-x, y), 0.5);
+    INPUTS[i].y = -INPUTS[i].y;
+    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, INPUTS[i],
+                                   LIBC_NAMESPACE::atan2(-x, -y), 0.5);
+    INPUTS[i].x = -INPUTS[i].x;
+    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, INPUTS[i],
+                                   LIBC_NAMESPACE::atan2(x, -y), 0.5);
+  }
----------------
overmighty wrote:

You can use a range-based `for` loop even if you use a C-style array instead of a `cpp::array`:

```suggestion
  for (mpfr::BinaryInput<double> &input : INPUTS) {
    double x = input.x;
    double y = input.y;
    mpfr::RoundingMode rm = mpfr::RoundingMode::Downward;
    mpfr::ForceRoundingMode rr(rm);
    ASSERT_MPFR_MATCH(mpfr::Operation::Atan2, input,
                      LIBC_NAMESPACE::atan2(x, y), 0.5, rm);
    input.x = -input.x;
    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, input,
                                   LIBC_NAMESPACE::atan2(-x, y), 0.5);
    input.y = -input.y;
    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, input,
                                   LIBC_NAMESPACE::atan2(-x, -y), 0.5);
    input.x = -input.x;
    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, input,
                                   LIBC_NAMESPACE::atan2(x, -y), 0.5);
  }
```

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


More information about the libc-commits mailing list