Repository navigation
Add Float64-free rem2pi for Float32 and Float16 - #40
Merged
Merged
Conversation
Base computes rem2pi(::Float32, r) and rem2pi(::Float16, r) in Float64, so mod2pi fails to compile on devices without double precision. Reduce in fixed point with integer Payne-Hanek instead, which needs no Float64 and stays accurate up to floatmax(Float32). Compared with Base over all 2^32 Float32 values on the CPU, results are within 0.51 ulp in all four rounding modes, and all Float16 results match.
The low 32 bits of the 96-bit product never affect the result: over all finite Float32 inputs, results are bit-identical without them, for both the fraction and its negation. Dropping them removes the separate negation and the extra word during normalization. Also correct the stated worst case (x / 2π comes within 2^-29.9 of an integer, not 2^-30) and test the next two closest inputs.
Member
|
Thanks! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
mod2piandrem2pionFloat32andFloat16don't compile on devices without double precision:Base reduces these types in Float64, on every supported Julia version including current master, and no back-end overrides them:
This adds overrides to
float64_overridesforRoundDown,RoundUp,RoundToZeroandRoundNearest. The Float32 path computes the fractional part ofx / 2πwith integer Payne-Hanek: the 24-bit mantissa times a 96-bit window of the bits of1/2π, using 32×32→64-bit products, without Float64,UInt128or a lookup table. The high 64 bits of that product are then scaled by 2π. The window needs 96 bits becausex / 2πcomes as close as 2^-29.9 to an integer (atx = 3.0916716f29); a 64-bit window gives wrong results. Float16 goes through the Float32 path.rem2pi(::Int32, r)is not covered, because Base returns Float64 for it.Verification, against Base over all 2^32 Float32 inputs, in each of the four modes:
RoundToZeroandRoundNearestdiffer from Base on 2 and 8 inputs, each by exactly 0.5 ulp. No signed zeros differ, and every Float16 result matches Base.