-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[libc][math][c23] Add {totalorder,totalordermag}f16 C23 math functions #95014
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6b214b5
[libc][math][c23] Add totalorderf16 C23 math function
overmighty 5c725f5
[libc][math][c23] Add totalordermagf16 C23 math function
overmighty e295094
[libc][math][c23] Fix NaN negation in TotalOrderTest.h
overmighty 1ac6c81
[libc][math][c23] Refactor fputil::{totalorder,totalordermag}
overmighty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -240,6 +240,57 @@ LIBC_INLINE int canonicalize(T &cx, const T &x) { | |
return 0; | ||
} | ||
|
||
template <typename T> | ||
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, bool> | ||
totalorder(const T *x, const T *y) { | ||
using FPBits = FPBits<T>; | ||
FPBits xbits(*x); | ||
FPBits ybits(*y); | ||
|
||
if (LIBC_UNLIKELY(xbits.is_zero() && ybits.is_zero() || xbits.is_nan() || | ||
overmighty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ybits.is_nan())) { | ||
// totalOrder(−0, +0) is true. totalOrder(+0, -0) is false. | ||
if (xbits.is_zero() && ybits.is_zero()) { | ||
Sign xsign = xbits.sign(); | ||
Sign ysign = ybits.sign(); | ||
return (xsign == Sign::NEG && ysign == Sign::POS) || xsign == ysign; | ||
} | ||
|
||
// totalOrder(−NaN, y) is true. totalOrder(+NaN, y) is false. | ||
if (!ybits.is_nan()) | ||
return xbits.is_neg(); | ||
// totalOrder(x, +NaN) is true. totalOrder(x, -NaN) is false. | ||
if (!xbits.is_nan()) | ||
return ybits.is_pos(); | ||
|
||
// Negative sign orders below positive sign. | ||
if (xbits.is_neg() && ybits.is_pos()) | ||
return true; | ||
if (xbits.is_pos() && ybits.is_neg()) | ||
return false; | ||
|
||
// Signaling orders below quiet for +NaN, reverse for −NaN. | ||
if (xbits.is_quiet_nan() && ybits.is_signaling_nan()) | ||
return xbits.is_neg(); | ||
if (xbits.is_signaling_nan() && ybits.is_quiet_nan()) | ||
return ybits.is_pos(); | ||
|
||
// Otherwise, the order of NaNs is implementation-defined (IEEE 754-2019). | ||
// We order by payload. | ||
return xbits.get_nan_payload() <= ybits.get_nan_payload(); | ||
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 guess this should be documented in https://libc.llvm.org/dev/undefined_behavior.html. |
||
} | ||
|
||
return xbits.get_val() <= ybits.get_val(); | ||
} | ||
|
||
template <typename T> | ||
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, bool> | ||
totalordermag(const T *x, const T *y) { | ||
overmighty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
T abs_x = abs(*x); | ||
T abs_y = abs(*y); | ||
return totalorder(&abs_x, &abs_y); | ||
overmighty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
} // namespace fputil | ||
} // namespace LIBC_NAMESPACE | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//===-- Implementation of totalorderf16 function --------------------------===// | ||
// | ||
// 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/math/totalorderf16.h" | ||
#include "src/__support/FPUtil/BasicOperations.h" | ||
#include "src/__support/common.h" | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
LLVM_LIBC_FUNCTION(int, totalorderf16, (const float16 *x, const float16 *y)) { | ||
return static_cast<int>(fputil::totalorder(x, y)); | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation of totalordermagf16 function -----------------------===// | ||
// | ||
// 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/math/totalordermagf16.h" | ||
#include "src/__support/FPUtil/BasicOperations.h" | ||
#include "src/__support/common.h" | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
LLVM_LIBC_FUNCTION(int, totalordermagf16, | ||
(const float16 *x, const float16 *y)) { | ||
return static_cast<int>(fputil::totalordermag(x, y)); | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation header for totalorderf16 -----------------*- 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_SRC_MATH_TOTALORDERF16_H | ||
#define LLVM_LIBC_SRC_MATH_TOTALORDERF16_H | ||
|
||
#include "src/__support/macros/properties/types.h" | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
int totalorderf16(const float16 *x, const float16 *y); | ||
|
||
} // namespace LIBC_NAMESPACE | ||
|
||
#endif // LLVM_LIBC_SRC_MATH_TOTALORDERF16_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation header for totalordermagf16 --------------*- 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_SRC_MATH_TOTALORDERMAGF16_H | ||
#define LLVM_LIBC_SRC_MATH_TOTALORDERMAGF16_H | ||
|
||
#include "src/__support/macros/properties/types.h" | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
int totalordermagf16(const float16 *x, const float16 *y); | ||
|
||
} // namespace LIBC_NAMESPACE | ||
|
||
#endif // LLVM_LIBC_SRC_MATH_TOTALORDERMAGF16_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.