[libc-commits] [libc] [libc][math] Add Generic Comparison Operations for floating point types (PR #144983)

via libc-commits libc-commits at lists.llvm.org
Fri Jul 4 08:52:13 PDT 2025


================
@@ -0,0 +1,331 @@
+//===-- Unittests for Comparison Operations for FPBits class -------------===//
+//
+// 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/ComparisonOperations.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/macros/properties/types.h"
+#include "src/__support/sign.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::fputil::equals;
+using LIBC_NAMESPACE::fputil::greater_than;
+using LIBC_NAMESPACE::fputil::greater_than_or_equals;
+using LIBC_NAMESPACE::fputil::less_than;
+using LIBC_NAMESPACE::fputil::less_than_or_equals;
+
+#define TEST_EQUALS(Name, Type)                                                \
+  TEST(LlvmLibc##Name##ComparisionOperationsTest, Equals) {                    \
+    using Bits = LIBC_NAMESPACE::fputil::FPBits<Type>;                         \
+    Type pos_zero = Bits::zero().get_val();                                    \
+    Type neg_zero = -pos_zero;                                                 \
+    Type pos_inf = Bits::inf().get_val();                                      \
+    Type neg_inf = Bits::inf(Sign::NEG).get_val();                             \
+    Type qnan = Bits::quiet_nan().get_val();                                   \
+    Type snan = Bits::signaling_nan().get_val();                               \
+    Type pos_normal = Type(3.14);                                              \
+    Type neg_normal = Type(-2.71);                                             \
+    Type pos_large = Type(1000000.0);                                          \
+    Type neg_large = Type(-1000000.0);                                         \
----------------
overmighty wrote:

This is copy-pasted 5 times and it overlaps with the constants already defined by `DECLARE_SPECIAL_CONSTANTS` and `LIBC_NAMESPACE::testing::FPTest`.

I suggest you make a test class like this:

```cpp
template <typename T>
class ComparisonOperationsTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {

    DECLARE_SPECIAL_CONSTANTS(T)
```

or maybe:

```cpp
template <typename T>
struct ComparisonOperationsTest : public LIBC_NAMESPACE::testing::FPTest<T> {
```

See https://github.com/llvm/llvm-project/blob/main/libc/test/src/math/smoke/NextUpTest.h and https://github.com/llvm/llvm-project/blob/main/libc/test/src/math/smoke/AddTest.h for example.

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


More information about the libc-commits mailing list