constexpr as much of half as possible #87
Merged
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.
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]