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

Fix rem2pi for NaN #36420

Merged
merged 1 commit into from
Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,8 @@ julia> rem2pi(7pi/4, RoundDown)
"""
function rem2pi end
function rem2pi(x::Float64, ::RoundingMode{:Nearest})
isnan(x) && return NaN

abs(x) < pi && return x
Comment on lines +1242 to 1244
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this can be optimized:

Suggested change
isnan(x) && return NaN
abs(x) < pi && return x
abs(x) >= pi || return x

Copy link
Contributor Author

@mgautam98 mgautam98 Jun 25, 2020

Choose a reason for hiding this comment

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

@kimikage Yes. but won't it make it hard to understand and debug later?

Copy link
Contributor

Choose a reason for hiding this comment

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

That's right. However, for such low-level functions, I prefer the speed. Is it not enough to just add comments into the source?

Copy link
Member

Choose a reason for hiding this comment

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

Worth checking if LLVM figures that out and eliminates the isnan check.

Copy link
Member

Choose a reason for hiding this comment

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

It seems like LLVM with optimize=true can't eliminate the fcmp ord double %0, 0.000000e+00 instruction and extra branch from isnan here. So I think it makes sense to use >= here to replace the isnan check and just add an explanatory comment about the NaN case.


n,y = rem_pio2_kernel(x)
Expand All @@ -1262,6 +1264,8 @@ function rem2pi(x::Float64, ::RoundingMode{:Nearest})
end
end
function rem2pi(x::Float64, ::RoundingMode{:ToZero})
isnan(x) && return NaN

ax = abs(x)
ax <= 2*Float64(pi,RoundDown) && return x

Expand All @@ -1287,6 +1291,8 @@ function rem2pi(x::Float64, ::RoundingMode{:ToZero})
copysign(z,x)
end
function rem2pi(x::Float64, ::RoundingMode{:Down})
isnan(x) && return NaN

if x < pi4o2_h
if x >= 0
return x
Expand Down Expand Up @@ -1316,6 +1322,8 @@ function rem2pi(x::Float64, ::RoundingMode{:Down})
end
end
function rem2pi(x::Float64, ::RoundingMode{:Up})
isnan(x) && return NaN

if x > -pi4o2_h
if x <= 0
return x
Expand Down
7 changes: 7 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2675,6 +2675,13 @@ end
@test rem2pi(T(-8), RoundUp) ≈ -8+2pi
end

@testset "PR #36420 $T" for T in (Float16, Float32, Float64)
@test rem2pi(T(NaN), RoundToZero) === T(NaN)
@test rem2pi(T(NaN), RoundNearest) === T(NaN)
@test rem2pi(T(NaN), RoundDown) === T(NaN)
@test rem2pi(T(NaN), RoundUp) === T(NaN)
end

import Base.^
struct PR20530; end
struct PR20889; x; end
Expand Down