Skip to content

MAINT: Translate ieee754.c.src to C++ using templates. #21367

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 5 commits into from
Apr 29, 2022

Conversation

serge-sans-paille
Copy link
Contributor

No description provided.

@serge-sans-paille serge-sans-paille force-pushed the feature/road-to-cxx-ieee754 branch from de349a3 to 7085883 Compare April 20, 2022 14:12
@charris charris changed the title Feature/road to cxx ieee754 MAINT: Translate ieee754.c.src to C++ using templates. Apr 20, 2022
@charris charris added the C++ Related to introduction and use of C++ in the NumPy code base label Apr 20, 2022
@serge-sans-paille serge-sans-paille force-pushed the feature/road-to-cxx-ieee754 branch 7 times, most recently from e222bed to 00766c3 Compare April 21, 2022 16:59
@serge-sans-paille
Copy link
Contributor Author

@charris should be good for review now!

Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

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

Had a look, and it seems good to me modulo that L that I think is missing. I am a bit surprised about swapping npy_longdouble with long double in some but not all places?

npy_uint64 lx;
npy_longdouble u;
constexpr npy_longdouble eps = 2.465190328815662e-32; // 0x1.0000000000000p-105L
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
constexpr npy_longdouble eps = 2.465190328815662e-32; // 0x1.0000000000000p-105L
constexpr npy_longdouble eps = 2.465190328815662e-32L; // 0x1.0000000000000p-105L

Seems like this requires the L, or the value gets cast to double (and is probably slightly off).

Copy link
Member

@seberg seberg Apr 22, 2022

Choose a reason for hiding this comment

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

I wonder if a test for his makes sense? But seems fine to skip. I don't think this is normally used.

I am not sure I trust this. This is double-double only, but that should have a lot more digits I think? Maybe we can calculate it as a power of two instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I checked and the internal representation of the two values is the same as I commited them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But to avoid confusion, I updated the patch with the 'L'-suffixed value :-)

Copy link
Member

Choose a reason for hiding this comment

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

How did you validate it?

In [1]: np.longdouble("2.465190328815662e-32") - 2.465190328815662e-32
Out[1]: 1.07979694287724704104e-48

And this is 80bit extended precision longdouble. IBM double-double has a much bigger mantissa. Probably this function isn't even needed, but I am not 100% sure (IIRC aarch64 or so may not provide nextafter even though it is C99).

Maybe use exp2l to calculate it? That is how we do it for np.finfo as well (in Python).

Copy link
Contributor Author

@serge-sans-paille serge-sans-paille Apr 22, 2022

Choose a reason for hiding this comment

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

(oopsie, code just updated) https://godbolt.org/z/EbaqdfPsP

Copy link
Member

Choose a reason for hiding this comment

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

I am worried that this is aarch64 (or similar): https://godbolt.org/z/53f8j4jY7

Copy link
Member

Choose a reason for hiding this comment

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

ohhh, I got it the wrong way. It actually must be without the L, because than it is the correct double precision value (i.e. exact in binary). But with the L it is not an exact in binary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

indeed. Let's do a math call then.

@serge-sans-paille
Copy link
Contributor Author

Had a look, and it seems good to me modulo that L that I think is missing. I am a bit surprised about swapping npy_longdouble with long double in some but not all places?

I only did that where it mattered for overload resolution.

@serge-sans-paille serge-sans-paille force-pushed the feature/road-to-cxx-ieee754 branch 3 times, most recently from 240dbac to 4e353a1 Compare April 23, 2022 20:20
@charris
Copy link
Member

charris commented Apr 23, 2022

The the failure of the simd test is new and unrelated. Something upstream has changed (again).

Replace them by regular C++ templates.
Clang-format it, and update build system accordingly.
- hex float literal are not supported in C++
- avoid overload issues when numpy_longdouble ultimately is equal to double
- _statusfp2 exepcts unsigned arguments
@serge-sans-paille serge-sans-paille force-pushed the feature/road-to-cxx-ieee754 branch from 4e353a1 to c99d908 Compare April 23, 2022 20:38
@serge-sans-paille
Copy link
Contributor Author

The the failure of the simd test is new and unrelated. Something upstream has changed (again).

ok! I restarted the test just in case it automagically got fixed :-)

@charris
Copy link
Member

charris commented Apr 25, 2022

close/reopen

@charris charris closed this Apr 25, 2022
@charris charris reopened this Apr 25, 2022
@serge-sans-paille
Copy link
Contributor Author

@charris / @seberg any other request on that one?

@seberg
Copy link
Member

seberg commented Apr 27, 2022

If Chuck is happy, I think we can go ahead and clean up anything that comes up later.

@seberg
Copy link
Member

seberg commented Apr 29, 2022

OK, let me just put this in, we can always fix it up and it seemed like a pretty straight forward conversion.
Thanks @serge-sans-paille!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
03 - Maintenance C++ Related to introduction and use of C++ in the NumPy code base
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants