-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc][math][c23] add c23 floating point fmaximum and fminimum functions. #86016
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
Changes from all commits
1c6e19c
1f82128
855925a
1ae6510
6805958
4de63fb
5b1339d
7396dfb
767035e
bdea55b
d3d2b8c
e6119bf
417c41d
d5d01c6
dde2428
329449b
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
#include "FPBits.h" | ||
|
||
#include "FEnvImpl.h" | ||
#include "src/__support/CPP/type_traits.h" | ||
#include "src/__support/common.h" | ||
|
||
|
@@ -58,6 +59,110 @@ LIBC_INLINE T fmax(T x, T y) { | |
} | ||
} | ||
|
||
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0> | ||
LIBC_INLINE T fmaximum(T x, T y) { | ||
FPBits<T> bitx(x), bity(y); | ||
|
||
if (bitx.is_nan()) | ||
return x; | ||
if (bity.is_nan()) | ||
return y; | ||
if (bitx.sign() != bity.sign()) | ||
return (bitx.is_neg() ? y : x); | ||
return x > y ? x : y; | ||
} | ||
|
||
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0> | ||
LIBC_INLINE T fminimum(T x, T y) { | ||
const FPBits<T> bitx(x), bity(y); | ||
|
||
if (bitx.is_nan()) | ||
return x; | ||
if (bity.is_nan()) | ||
return y; | ||
if (bitx.sign() != bity.sign()) | ||
return (bitx.is_neg()) ? x : y; | ||
return x < y ? x : y; | ||
} | ||
|
||
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0> | ||
LIBC_INLINE T fmaximum_num(T x, T y) { | ||
FPBits<T> bitx(x), bity(y); | ||
if (bitx.is_signaling_nan() || bity.is_signaling_nan()) { | ||
fputil::raise_except_if_required(FE_INVALID); | ||
if (bitx.is_nan() && bity.is_nan()) | ||
return FPBits<T>::quiet_nan().get_val(); | ||
} | ||
if (bitx.is_nan()) | ||
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. I'm sorry for a late suggestion, but I just noticed that in section F.10.9.5 in C23 standard, the exceptional handling of
Do you mind updating Thanks, 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. Yea I’ll get it done tomorrow. 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. The others look correct to me. 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. Here’s the definition I was basing it on
I took this from here https://www.gnu.org/software/libc/manual/html_node/Misc-FP-Arithmetic.html 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. @lntue hello, I changed the code as you suggested. can you please give an idea of how to test the exception? In the codebase I have found examples of 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. Something like 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. Is my handling of the case associated with raising the exception correct? Because I used you example and I’m getting 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. I figured it out. i found the bug |
||
return y; | ||
if (bity.is_nan()) | ||
return x; | ||
if (bitx.sign() != bity.sign()) | ||
return (bitx.is_neg() ? y : x); | ||
return x > y ? x : y; | ||
} | ||
|
||
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0> | ||
LIBC_INLINE T fminimum_num(T x, T y) { | ||
FPBits<T> bitx(x), bity(y); | ||
if (bitx.is_signaling_nan() || bity.is_signaling_nan()) { | ||
fputil::raise_except_if_required(FE_INVALID); | ||
if (bitx.is_nan() && bity.is_nan()) | ||
return FPBits<T>::quiet_nan().get_val(); | ||
} | ||
if (bitx.is_nan()) | ||
return y; | ||
if (bity.is_nan()) | ||
return x; | ||
if (bitx.sign() != bity.sign()) | ||
return (bitx.is_neg() ? x : y); | ||
return x < y ? x : y; | ||
} | ||
|
||
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0> | ||
LIBC_INLINE T fmaximum_mag(T x, T y) { | ||
FPBits<T> bitx(x), bity(y); | ||
|
||
if (abs(x) > abs(y)) | ||
return x; | ||
if (abs(y) > abs(x)) | ||
return y; | ||
return fmaximum(x, y); | ||
} | ||
|
||
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0> | ||
LIBC_INLINE T fminimum_mag(T x, T y) { | ||
FPBits<T> bitx(x), bity(y); | ||
|
||
if (abs(x) < abs(y)) | ||
return x; | ||
if (abs(y) < abs(x)) | ||
return y; | ||
return fminimum(x, y); | ||
} | ||
|
||
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0> | ||
LIBC_INLINE T fmaximum_mag_num(T x, T y) { | ||
FPBits<T> bitx(x), bity(y); | ||
|
||
if (abs(x) > abs(y)) | ||
return x; | ||
if (abs(y) > abs(x)) | ||
return y; | ||
return fmaximum_num(x, y); | ||
} | ||
|
||
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0> | ||
LIBC_INLINE T fminimum_mag_num(T x, T y) { | ||
FPBits<T> bitx(x), bity(y); | ||
|
||
if (abs(x) < abs(y)) | ||
return x; | ||
if (abs(y) < abs(x)) | ||
return y; | ||
return fminimum_num(x, y); | ||
} | ||
|
||
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0> | ||
LIBC_INLINE T fdim(T x, T y) { | ||
FPBits<T> bitx(x), bity(y); | ||
|
Uh oh!
There was an error while loading. Please reload this page.