diff --git a/src/RadixSortLSD.chpl b/src/RadixSortLSD.chpl index 8c1156e200..08f294efcf 100644 --- a/src/RadixSortLSD.chpl +++ b/src/RadixSortLSD.chpl @@ -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); } diff --git a/tests/sort_test.py b/tests/sort_test.py index 000075ff2f..bf53ffea74 100644 --- a/tests/sort_test.py +++ b/tests/sort_test.py @@ -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