Skip to content

[libc][math][c23] Implement canonicalize functions #85940

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 39 commits into from
Mar 26, 2024
Merged

Conversation

Sh0g0-1758
Copy link
Member

Fixes: #85286

@Sh0g0-1758
Copy link
Member Author

@lntue, creating this to confirm whether or not this is moving in the right direction. I am little confused in the implementation of the long double type. I will add tests once you confirm about the implementation of others. As you said :

float, double, and float128: these are IEEE 754 standard formats, most of the inputs are already canonical, with signaling NaNs are the only exception.

So I figured that they are already in canonical format and so I just need to assign *x to *cx.

@lntue lntue self-requested a review March 20, 2024 14:53
@jcranmer-intel
Copy link
Contributor

Some drive-by comments:

  • canonicalize(SNAN) needs to raise the INVALID floating point exception
  • it's a should, not a must requirement, but the corresponding quiet NaN should have the same payload as the signaling NaN (i.e., only the bit indicating whether or not the NaN is an sNaN should change)
  • x86 and PPC long double types have non-canonical values that need to be handled. I don't know enough about the PPC long double type to give guidance here, but the x86 long double type considers all non-canonical values to be invalid, and will generally refuse to operate on them in the first place--glibc in this case returns 1 and does nothing.

@lntue
Copy link
Contributor

lntue commented Mar 20, 2024

Some drive-by comments:

  • canonicalize(SNAN) needs to raise the INVALID floating point exception
  • it's a should, not a must requirement, but the corresponding quiet NaN should have the same payload as the signaling NaN (i.e., only the bit indicating whether or not the NaN is an sNaN should change)
  • x86 and PPC long double types have non-canonical values that need to be handled. I don't know enough about the PPC long double type to give guidance here, but the x86 long double type considers all non-canonical values to be invalid, and will generally refuse to operate on them in the first place--glibc in this case returns 1 and does nothing.

Thanks @jcranmer-intel ! I totally forgot to include error handling section F.10.8.7 (N3096) of canonicalize functions to the issue's description.

About the PPC long double, we will skip it for now and focusing on x86-64, arm, and riscv, which we have the hardware to test directly. Hopefully, someone with PPC knowledge and hardware could help us to fill it in later.

Copy link

github-actions bot commented Mar 21, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@Sh0g0-1758 Sh0g0-1758 force-pushed the 85286 branch 5 times, most recently from a18474a to 3c9a012 Compare March 25, 2024 09:06
Bug fixes

Ran formatter

Updated tests

Ran formatter

bug fix

Bug fixes

Minor bug fixes

Bug Fixes

Add namespace

Bug Fix

Changed namespace

Minor changes

Experimenting

revert

changed type

Update tests

Added types

Update BasicOperations.h

Bug Fix

Formatter

minor changes

pushing all tests

Formatting

revert

Bug fix

Minor changes

changes

Formatter

Testing

changes

Added a test

Ran formatter

Minor push

formatter

fix

Typo

Removed bit 61

revamp testing

TEst 4

changes

added type

Formatter

Test

Bug fix

Bug fix

Fixed bug

Formatter

nits

nits

info

RE

s

s

s

s

TEst

TEst

Added test 5

F

minor changes

nits

nits

bit62 fix

testing

revert

nots

fix

nits

Format

fix

F

test

test

Buf Fix
@Sh0g0-1758
Copy link
Member Author

Ah I got what was causing the error in the tests. Fixed them.

@Sh0g0-1758 Sh0g0-1758 requested a review from rupprecht as a code owner March 26, 2024 04:58
Copy link
Contributor

@lntue lntue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch!

@lntue lntue merged commit 19ca79e into llvm:main Mar 26, 2024
@nickdesaulniers
Copy link
Member

This breaks the arm32 build in postsubmit, PTAL.
https://lab.llvm.org/buildbot/#/builders/229/builds/24394

In file included from /llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc/src/math/generic/sinf.cpp:11:
/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc/src/__support/FPUtil/BasicOperations.h:202:10: error: no viable conversion from '__llvm_libc_19_0_0_git::BigInt<128, false>' to 'bool'
    bool bit62 = ((mantissa & (1ULL << 62)) >> 62);
         ^       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc/src/__support/UInt.h:186:56: note: explicit conversion function is not a candidate
  template <typename T> LIBC_INLINE constexpr explicit operator T() const {
                                                       ^
/llvm/libc_worker/worker/libc-arm32-debian/libc-arm32-debian-dbg/llvm-project/libc/src/__support/UInt.h:216:34: note: explicit conversion function is not a candidate
  LIBC_INLINE constexpr explicit operator bool() const { return !is_zero(); }
                                 ^

@nickdesaulniers
Copy link
Member

I wonder why UInt::operator bool is marked explicit? Implicit conversions to bool sure are nice.

@Sh0g0-1758
Copy link
Member Author

I can provide a patch but I don't have context. Can start working on it once you provide some pointers.

michaelrj-google added a commit to michaelrj-google/llvm-project that referenced this pull request Mar 26, 2024
The canonicalize test added in llvm#85940 defined the LIBC_NAMESPACE macro.
this macro is intended to be set only by the build system and never in
the code.
@michaelrj-google
Copy link
Contributor

Since the issue is that the implicit conversion to bool is failing, the easiest solution is likely to add a static_cast<bool> to the statement.

michaelrj-google added a commit that referenced this pull request Mar 26, 2024
The canonicalize test added in #85940 defined the LIBC_NAMESPACE macro.
this macro is intended to be set only by the build system and never in
the code.
nickdesaulniers added a commit to nickdesaulniers/llvm-project that referenced this pull request Mar 26, 2024
The arm32 buildbot reports an error because UInt::operator bool() is explicit,
thus an explicit cast is necessary.

Link: llvm#85940
@nickdesaulniers
Copy link
Member

Since the issue is that the implicit conversion to bool is failing, the easiest solution is likely to add a static_cast<bool> to the statement.

Cant leave the buildbots red for long: #86736

@Sh0g0-1758
Copy link
Member Author

Ah sorry for the delay. My OS crashed and I saw the dreaded white death screen. Had to set up my system from scratch. Thanks for the patch @nickdesaulniers.

nickdesaulniers added a commit that referenced this pull request Mar 26, 2024
The arm32 buildbot reports an error because UInt::operator bool() is explicit,
thus an explicit cast is necessary.

Link: #85940
lntue pushed a commit that referenced this pull request Mar 28, 2024
…ctions. (#86924)

Updates the special case of pseudo infinty as pointed in #85940
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc][math][c23] Implement canonicalize functions.
5 participants