Skip to content

Commit d9fcaef

Browse files
committed
Fix midpoint calculation to always round down
to make sure the branch elseif lenB <= length(buf) in pagedMerge! is never taken.
1 parent ea4eb9a commit d9fcaef

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/SortingAlgorithms.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -999,11 +999,11 @@ end
999999
midpoint(lo::Integer, hi::Integer) = lo + ((hi - lo) >>> 0x01)
10001000

10011001
function pagedmergesort!(v::AbstractVector{T}, lo::Integer, hi::Integer, o::Ordering, buf::AbstractVector{T}, blockLocation) where T
1002-
len = hi + 1 -lo
1002+
len = hi + 1 - lo
10031003
if len <= Base.SMALL_THRESHOLD
10041004
return Base.Sort.sort!(v, lo, hi, Base.Sort.InsertionSortAlg(), o)
10051005
end
1006-
m = midpoint(lo, hi)
1006+
m = midpoint(lo, hi-1) # hi-1: ensure midpoint is rounded down. OK, because lo < hi is satisfied here
10071007
pagedmergesort!(v, lo, m, o, buf, blockLocation)
10081008
pagedmergesort!(v, m+1, hi, o, buf, blockLocation)
10091009
if len <= length(buf)
@@ -1032,7 +1032,7 @@ function threaded_pagedmergesort!(v::AbstractVector, lo::Integer, hi::Integer, o
10321032
if len <= Base.SMALL_THRESHOLD
10331033
return Base.Sort.sort!(v, lo, hi, Base.Sort.InsertionSortAlg(), o)
10341034
end
1035-
m = midpoint(lo, hi)
1035+
m = midpoint(lo, hi-1) # hi-1: ensure midpoint is rounded down. OK, because lo < hi is satisfied here
10361036
if len > threadingThreshold
10371037
thr = Threads.@spawn threaded_pagedmergesort!(v, lo, m, o, bufs, blockLocations, c, threadingThreshold)
10381038
threaded_pagedmergesort!(v, m+1, hi, o, bufs, blockLocations, c, threadingThreshold)

0 commit comments

Comments
 (0)