-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc][math] Add Generic Comparison Operations for floating point types #144983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8b4ef34
f0997ab
2a4dc5f
e44e43e
8c30aa2
d1e54ed
80fd69d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,122 @@ | ||||||
//===-- Comparison operations on floating point numbers --------*- C++ -*-===// | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: formatting.
Suggested change
|
||||||
// | ||||||
// 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_SRC___SUPPORT_FPUTIL_COMPARISONOPERATIONS_H | ||||||
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_COMPARISONOPERATIONS_H | ||||||
|
||||||
#include "FEnvImpl.h" // raise_except_if_required | ||||||
#include "FPBits.h" // FPBits<T> | ||||||
#include "src/__support/CPP/type_traits.h" // enable_if, is_floating_point | ||||||
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL | ||||||
|
||||||
namespace LIBC_NAMESPACE_DECL { | ||||||
namespace fputil { | ||||||
|
||||||
// IEEE Standard 754-2019. Section 5.11 | ||||||
// Rules for comparison within the same floating point type | ||||||
// 1. +0 = −0 | ||||||
// 2. (i) +inf = +inf | ||||||
// (ii) -inf = -inf | ||||||
// (iii) -inf != +inf | ||||||
// 3. Any comparison with NaN return false except (NaN != NaN => true) | ||||||
template <typename T> | ||||||
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, bool> equals(T x, | ||||||
T y) { | ||||||
using FPBits = FPBits<T>; | ||||||
FPBits x_bits(x); | ||||||
FPBits y_bits(y); | ||||||
|
||||||
if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan()) | ||||||
fputil::raise_except_if_required(FE_INVALID); | ||||||
|
||||||
// NaN == x returns false for every x | ||||||
if (x_bits.is_nan() || y_bits.is_nan()) | ||||||
return false; | ||||||
|
||||||
// +/- 0 == +/- 0 | ||||||
if (x_bits.is_zero() && y_bits.is_zero()) | ||||||
return true; | ||||||
|
||||||
// should also work for comparisons of different signs | ||||||
return x_bits.uintval() == y_bits.uintval(); | ||||||
} | ||||||
|
||||||
// !(x == y) => x != y | ||||||
template <typename T> | ||||||
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, bool> | ||||||
not_equals(T x, T y) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need |
||||||
return !equals(x, y); | ||||||
} | ||||||
|
||||||
// Rules: | ||||||
// 1. -inf < x (x != -inf) | ||||||
// 2. x < +inf (x != +inf) | ||||||
// 3. Any comparison with NaN return false | ||||||
template <typename T> | ||||||
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, bool> less_than(T x, | ||||||
T y) { | ||||||
using FPBits = FPBits<T>; | ||||||
FPBits x_bits(x); | ||||||
FPBits y_bits(y); | ||||||
|
||||||
if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan()) | ||||||
fputil::raise_except_if_required(FE_INVALID); | ||||||
Comment on lines
+67
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comparisons other than equal and not equal should also raise
|
||||||
|
||||||
// Any comparison with NaN returns false | ||||||
if (x_bits.is_nan() || y_bits.is_nan()) | ||||||
return false; | ||||||
|
||||||
if (x_bits.is_zero() && y_bits.is_zero()) | ||||||
return false; | ||||||
|
||||||
if (x_bits.is_neg() && y_bits.is_pos()) | ||||||
return true; | ||||||
|
||||||
if (x_bits.is_pos() && y_bits.is_neg()) | ||||||
return false; | ||||||
|
||||||
// since we store the float in the format: s | e | m | ||||||
// the comparisons should work if we directly compare the uintval's | ||||||
|
||||||
// TODO: verify if we should use FPBits.get_exponent and FPBits.get_mantissa | ||||||
// instead of directly comparing uintval's | ||||||
|
||||||
// both negative | ||||||
if (x_bits.is_neg()) | ||||||
return x_bits.uintval() > y_bits.uintval(); | ||||||
|
||||||
// both positive | ||||||
return x_bits.uintval() < y_bits.uintval(); | ||||||
} | ||||||
|
||||||
// x < y => y > x | ||||||
template <typename T> | ||||||
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, bool> | ||||||
greater_than(T x, T y) { | ||||||
return less_than(y, x); | ||||||
} | ||||||
|
||||||
// following is expression is correct, accounting for NaN case(s) as well | ||||||
// x <= y => (x < y) || (x == y) | ||||||
template <typename T> | ||||||
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, bool> | ||||||
less_than_or_equals(T x, T y) { | ||||||
return less_than(x, y) || equals(x, y); | ||||||
} | ||||||
|
||||||
// x >= y => (x > y) || (x == y) => (y < x) || (x == y) | ||||||
template <typename T> | ||||||
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, bool> | ||||||
greater_than_or_equals(T x, T y) { | ||||||
return less_than(y, x) || equals(x, y); | ||||||
} | ||||||
|
||||||
} // namespace fputil | ||||||
} // namespace LIBC_NAMESPACE_DECL | ||||||
|
||||||
#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_COMPARISONOPERATIONS_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,15 @@ add_fp_unittest( | |
DEPENDS | ||
libc.src.__support.FPUtil.rounding_mode | ||
) | ||
|
||
add_fp_unittest( | ||
comparison_operations_test | ||
SUITE | ||
libc-fputil-tests | ||
SRCS | ||
comparison_operations_test.cpp | ||
DEPENDS | ||
libc.src.__support.FPUtil.fp_bits | ||
# libc.src.__support.FPUtil.comparison_operations | ||
Comment on lines
+48
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dependencies should be updated. |
||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: extra line break. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: sorting.