Skip to content

Commit

Permalink
Merge pull request #941 from ronawho/fix-sort-bitsPerDigit-boundary
Browse files Browse the repository at this point in the history
Fix sorting arrays with values on bitsPerDigit boundaries
  • Loading branch information
reuster986 authored Oct 4, 2021
2 parents 436a4fc + f19cf58 commit 1fe718e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/RadixSortLSD.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ module RadixSortLSD
var aMin = min reduce a;
var aMax = max reduce a;
var wPos = if aMax >= 0 then numBits(int) - clz(aMax) else 0;
var wNeg = if aMin < 0 then numBits(int) - clz((-aMin)-1) + 1 else 0;
const bitWidth = max(wPos, wNeg);
var wNeg = if aMin < 0 then numBits(int) - clz((-aMin)-1) else 0;
const signBit = if aMin < 0 then 1 else 0;
const bitWidth = max(wPos, wNeg) + signBit;
const negs = aMin < 0;
return (bitWidth, negs);
}
Expand Down
28 changes: 27 additions & 1 deletion tests/sort_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,33 @@ def testSort(self):
spda = ak.sort(pda)
maxIndex = spda.argmax()
self.assertTrue(maxIndex > 0)


def testBitBoundaryHardcode(self):

# test hardcoded 16/17-bit boundaries with and without negative values
a = ak.array([1, -1, 32767]) # 16 bit
b = ak.array([1, 0, 32768]) # 16 bit
c = ak.array([1, -1, 32768]) # 17 bit
assert ak.is_sorted(ak.sort(a))
assert ak.is_sorted(ak.sort(b))
assert ak.is_sorted(ak.sort(c))

# test hardcoded 64-bit boundaries with and without negative values
d = ak.array([1, -1, 2**63-1])
e = ak.array([1, 0, 2**63-1])
f = ak.array([1, -2**63, 2**63-1])
assert ak.is_sorted(ak.sort(d))
assert ak.is_sorted(ak.sort(e))
assert ak.is_sorted(ak.sort(f))

def testBitBoundary(self):

# test 17-bit sort
L = -2**15
U = 2**16
a = ak.randint(L, U, 100)
assert ak.is_sorted(ak.sort(a))

def testErrorHandling(self):

# Test RuntimeError from bool NotImplementedError
Expand Down

0 comments on commit 1fe718e

Please sign in to comment.