Skip to content

Commit

Permalink
Correctly rounded variant of the hypot code (#32345)
Browse files Browse the repository at this point in the history
* Implements the correctly rounded variant of the hypot code only in the case where there is a native FMA

* Adds comments explaining the functioning of the two branches in the computation.
  • Loading branch information
cfborges authored and simonbyrne committed Jul 1, 2019
1 parent 6341e3e commit 710e43d
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -579,12 +579,20 @@ function hypot(x::T,y::T) where T<:AbstractFloat
scale = one(scale)
end
h = sqrt(muladd(ax,ax,ay*ay))
if h <= 2*ay
delta = h-ay
h -= muladd(delta,delta-2*(ax-ay),ax*(2*delta - ax))/(2*h)
# This branch is correctly rounded but requires a native hardware fma.
if Base.Math.FMA_NATIVE
hsquared = h*h
axsquared = ax*ax
h -= (fma(-ay,ay,hsquared-axsquared) + fma(h,h,-hsquared) - fma(ax,ax,-axsquared))/(2*h)
# This branch is within one ulp of correctly rounded.
else
delta = h-ax
h -= muladd(delta,delta,muladd(ay,(4*delta-ay),2*delta*(ax-2*ay)))/(2*h)
if h <= 2*ay
delta = h-ay
h -= muladd(delta,delta-2*(ax-ay),ax*(2*delta - ax))/(2*h)
else
delta = h-ax
h -= muladd(delta,delta,muladd(ay,(4*delta-ay),2*delta*(ax-2*ay)))/(2*h)
end
end
h*scale
end
Expand Down

2 comments on commit 710e43d

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.