Skip to content
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

constexpr as much of half as possible #87

Merged
merged 1 commit into from
Feb 5, 2021

Conversation

lgritz
Copy link
Contributor

@lgritz lgritz commented Feb 4, 2021

This is the remaining section of half that can be easily constexpr.

The way they are currently implemented, the basic half-from-float ctr
and half-to-float cast are not able to be constexpr, and that in turn
prevents almost all the arithmetic operations (which convert to float,
do the math, then convert back) from being constexpr.

This is the closest we're going to get with the current implementations.
The most offensive parts to constexpr are the use of table lookups, and
the strange overflow() trying to raise a hardware overflow exception.
It's not hard to imagine alternate implementations that don't work the
same way at all, but we must leave that for another day (and maybe for
somebody who is more willing to chase down ever corner case of IEEE754
logic).

I added private mantissa() and exponent() helper methods, using those
helped me make some other public methods be constexpr in C++11 by
making their body be a single return statement (if you spread over
multiple statements, you become constexpr in C++14 only on some
compilers). We could make those public if people think there are other
uses for these helpers.

Signed-off-by: Larry Gritz [email protected]

This is the remaining section of `half` that can be easily constexpr.

The way they are currently implemented, the basic half-from-float ctr
and half-to-float cast are not able to be constexpr, and that in turn
prevents almost all the arithmetic operations (which convert to float,
do the math, then convert back) from being constexpr.

This is the closest we're going to get with the current implementations.
The most offensive parts to constexpr are the use of table lookups, and
the strange overflow() trying to raise a hardware overflow exception.
It's not hard to imagine alternate implementations that don't work the
same way at all, but we must leave that for another day (and maybe for
somebody who is more willing to chase down ever corner case of IEEE754
logic).

I added private mantissa() and exponent() helper methods, using those
helped me make some other public methods be constexpr in C++11 by
making their body be a single return statement (if you spread over
multiple statements, you become constexpr in C++14 only on some
compilers). We could make those public if people think there are other
uses for these helpers.

Signed-off-by: Larry Gritz <[email protected]>
Copy link
Member

@cary-ilm cary-ilm left a comment

Choose a reason for hiding this comment

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

LGTM

@lgritz lgritz merged commit 1e7bb58 into AcademySoftwareFoundation:master Feb 5, 2021
@lgritz lgritz deleted the lg-halfconstexpr branch February 8, 2021 02:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants