Skip to content

Commit

Permalink
add eagerly NaN break for extrema
Browse files Browse the repository at this point in the history
  • Loading branch information
N5N3 committed Dec 31, 2021
1 parent c5fa06a commit 66d2a81
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1764,17 +1764,25 @@ function mapreduce_impl(f, ::typeof(_extrema_op), A::AbstractArrayOrBroadcasted,
Eltype = _return_type(elf, Tuple{Int})
Eltype <: NTuple{2,AbstractFloat} ||
return invoke(mapreduce_impl,Tuple{Any,Any,AbstractArrayOrBroadcasted,Int,Int},f,_extrema_op,A,fi,la)
v1 = v2 = v3 = v4 = Eltype((Inf,-Inf))
iter = fi:4:la-4
for i in iter
v1 = _extrema_op(v1, elf(i))
v2 = _extrema_op(v2, elf(i+1))
v3 = _extrema_op(v3, elf(i+2))
v4 = _extrema_op(v4, elf(i+3))
v1 = v2 = v3 = v4 = elf(fi)
len = (la - fi) >> 2
i = fi
for I in Iterators.partition(1:len, 64)
for _ in I
v1 = _extrema_op(v1, elf(i+=1))
v2 = _extrema_op(v2, elf(i+=1))
v3 = _extrema_op(v3, elf(i+=1))
v4 = _extrema_op(v4, elf(i+=1))
end
# short circuit in case of NaN
isnan(v1[1]) && return v1
isnan(v2[1]) && return v2
isnan(v3[1]) && return v3
isnan(v4[1]) && return v4
end
v = _extrema_op(_extrema_op(v1, v2), _extrema_op(v3, v4))
for i in last(iter)+4:la
v = _extrema_op(v, elf(i))
while i < la
v = _extrema_op(v, elf(i+=1))
end
v
end
Expand Down

0 comments on commit 66d2a81

Please sign in to comment.