[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 11:35:06 PDT 2025
================
@@ -0,0 +1,342 @@
+//===-- 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/FEnvSafeTest.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;
+
+template <typename T>
+class ComparisonOperationsTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+ DECLARE_SPECIAL_CONSTANTS(T)
+
+ static constexpr T normal1 = T(3.14);
+ static constexpr T normal2 = T(2.71);
+ static constexpr T small = T(0.1);
+ static constexpr T neg_small = T(-0.1);
+ static constexpr T large = T(10000.0);
+ static constexpr T neg_large = T(-10000.0);
+
+public:
+ void test_equals() {
+ EXPECT_TRUE(equals(neg_zero, neg_zero));
+ EXPECT_TRUE(equals(zero, neg_zero));
+ EXPECT_TRUE(equals(neg_zero, zero));
+
+ EXPECT_TRUE(equals(inf, inf));
+ EXPECT_TRUE(equals(neg_inf, neg_inf));
+ EXPECT_FALSE(equals(inf, neg_inf));
+ EXPECT_FALSE(equals(neg_inf, inf));
+
+ EXPECT_TRUE(equals(normal1, normal1));
+ EXPECT_TRUE(equals(normal2, normal2));
+ EXPECT_FALSE(equals(normal1, normal2));
+ EXPECT_FALSE(equals(normal1, -normal1));
+
+ LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
+ auto test_qnan = [&](T x, T y) {
+ EXPECT_FALSE(equals(x, y));
+ // NOTE: equals and not equals operations should raise FE_INVALID for
+ // quiet NaN operands
----------------
overmighty wrote:
```suggestion
// NOTE: equals and not equals operations should not raise FE_INVALID for
// quiet NaN operands
```
But I think what we should do instead is add comments at definition for `fputil::equals`, `fputil::not_equals`, etc, saying which IEEE 754 comparison predicate they implement. E.g., for `fputil::equals`:
```cpp
// Implements the compareQuietEqual predicate as per IEEE Std 754-2019.
template <typename T>
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, bool> equals(T x,
T y) {
```
https://github.com/llvm/llvm-project/pull/144983
More information about the libc-commits
mailing list