[libc-commits] [libc] [libc][math] Adds entrypoint and tests for nearbyintf128, scalbnf128 (PR #88443)

via libc-commits libc-commits at lists.llvm.org
Fri Apr 12 13:58:32 PDT 2024


================
@@ -0,0 +1,105 @@
+//===-- Utility class to test different flavors of nearbyint ----*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TEST_SRC_MATH_NEARBYINTTEST_H
+#define LLVM_LIBC_TEST_SRC_MATH_NEARBYINTTEST_H
+
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+static constexpr int ROUNDING_MODES[4] = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO,
+                                          FE_TONEAREST};
+
+template <typename T>
+class NearbyIntTestTemplate : public LIBC_NAMESPACE::testing::Test {
+
+  DECLARE_SPECIAL_CONSTANTS(T)
+
+public:
+  typedef T (*NearbyIntFunc)(T);
+
+  void testNaN(NearbyIntFunc func) { ASSERT_FP_EQ(func(aNaN), aNaN); }
+
+  void testInfinities(NearbyIntFunc func) {
+    ASSERT_FP_EQ(func(inf), inf);
+    ASSERT_FP_EQ(func(neg_inf), neg_inf);
+  }
+
+  void testZeroes(NearbyIntFunc func) {
+    ASSERT_FP_EQ(func(zero), zero);
+    ASSERT_FP_EQ(func(neg_zero), neg_zero);
+  }
+
+  void testIntegers(NearbyIntFunc func) {
+    for (int mode : ROUNDING_MODES) {
+      LIBC_NAMESPACE::fputil::set_round(mode);
----------------
lntue wrote:

So the proper way to test with different rounding modes is to use `LIBC_NAMESPACE::fputil::testing::ForceRoundingMode` similar to https://github.com/lntue/llvm-project/blob/main/libc/test/UnitTest/FPMatcher.h#L204

Since you already included `FPMatcher.h`, you can follow that pattern in this test file.

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


More information about the libc-commits mailing list