| 1 |
package qsort |
| 2 |
|
| 3 |
import ( |
| 4 |
"sort" |
| 5 |
|
| 6 |
"github.com/segmentio/asm/bswap" |
| 7 |
"github.com/segmentio/asm/cpu" |
| 8 |
"github.com/segmentio/asm/cpu/x86" |
| 9 |
"github.com/segmentio/asm/internal" |
| 10 |
) |
| 11 |
|
| 12 |
// Sort sorts contiguous big-endian chunks of bytes of a fixed size. |
| 13 |
// Sorting specializations are available for sizes of 8, 16, 24 and 32 bytes. |
| 14 |
func Sort(data []byte, size int, swap func(int, int)) { |
| 15 |
if len(data) <= size { |
| 16 |
return |
| 17 |
} |
| 18 |
if size <= 0 || !internal.MultipleOf(size, len(data)) { |
| 19 |
panic("input length is not a multiple of element size") |
| 20 |
} |
| 21 |
|
| 22 |
// No specialization available. Use the slower generic sorting routine. |
| 23 |
if size%8 != 0 || size > 32 { |
| 24 |
sort.Sort(newGeneric(data, size, swap)) |
| 25 |
return |
| 26 |
} |
| 27 |
|
| 28 |
// Byte swap each qword prior to sorting. Doing a single pass here, and |
| 29 |
// again after the sort, is faster than byte swapping during each |
| 30 |
// comparison. The sorting routines have been written to assume that high |
| 31 |
// qwords come before low qwords, and so we're able to use the same |
| 32 |
// Swap64() routine rather than needing separate byte swapping routines |
| 33 |
// for 8, 16, 24, or 32 bytes. |
| 34 |
bswap.Swap64(data) |
| 35 |
defer bswap.Swap64(data) |
| 36 |
|
| 37 |
// If no indirect swapping is required, try to use the hybrid partitioning scheme from |
| 38 |
// https://blog.reverberate.org/2020/05/29/hoares-rebuttal-bubble-sorts-comeback.html |
| 39 |
switch { |
| 40 |
case swap == nil && !purego && size == 8 && cpu.X86.Has(x86.CMOV): |
| 41 |
hybridQuicksort64(unsafeBytesTo64(data)) |
| 42 |
case swap == nil && !purego && size == 16 && cpu.X86.Has(x86.AVX): |
| 43 |
hybridQuicksort128(unsafeBytesTo128(data)) |
| 44 |
case swap == nil && !purego && size == 32 && cpu.X86.Has(x86.AVX2): |
| 45 |
hybridQuicksort256(unsafeBytesTo256(data)) |
| 46 |
case size == 8: |
| 47 |
quicksort64(unsafeBytesTo64(data), 0, smallCutoff, insertionsort64, hoarePartition64, swap) |
| 48 |
case size == 16: |
| 49 |
quicksort128(unsafeBytesTo128(data), 0, smallCutoff, insertionsort128, hoarePartition128, swap) |
| 50 |
case size == 24: |
| 51 |
quicksort192(unsafeBytesTo192(data), 0, smallCutoff, insertionsort192, hoarePartition192, swap) |
| 52 |
case size == 32: |
| 53 |
quicksort256(unsafeBytesTo256(data), 0, smallCutoff, insertionsort256, hoarePartition256, swap) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func hybridQuicksort64(data []uint64) { |
| 58 |
// The hybrid Lomuto/Hoare partition scheme requires scratch space. We |
| 59 |
// allocate some stack space for the task here in this trampoline function, |
| 60 |
// so that we don't pay the stack cost unless necessary. |
| 61 |
var buf [scratchSize]byte |
| 62 |
scratch := unsafeBytesTo64(buf[:]) |
| 63 |
partition := func(data []uint64, base int, swap func(int, int)) int { |
| 64 |
return hybridPartition64(data, scratch) |
| 65 |
} |
| 66 |
quicksort64(data, 0, smallCutoff/2, bubblesort64NoSwap2, partition, nil) |
| 67 |
} |
| 68 |
|
| 69 |
func hybridQuicksort128(data []uint128) { |
| 70 |
var buf [scratchSize]byte |
| 71 |
scratch := unsafeBytesTo128(buf[:]) |
| 72 |
partition := func(data []uint128, base int, swap func(int, int)) int { |
| 73 |
return hybridPartition128(data, scratch) |
| 74 |
} |
| 75 |
quicksort128(data, 0, smallCutoff*2, insertionsort128NoSwap, partition, nil) |
| 76 |
} |
| 77 |
|
| 78 |
func hybridQuicksort256(data []uint256) { |
| 79 |
var buf [scratchSize]byte |
| 80 |
scratch := unsafeBytesTo256(buf[:]) |
| 81 |
partition := func(data []uint256, base int, swap func(int, int)) int { |
| 82 |
return hybridPartition256(data, scratch) |
| 83 |
} |
| 84 |
quicksort256(data, 0, smallCutoff*2, insertionsort256NoSwap, partition, nil) |
| 85 |
} |
| 86 |
|
| 87 |
// The threshold at which log-linear sorting methods switch to |
| 88 |
// a quadratic (but cache-friendly) method such as insertionsort. |
| 89 |
const smallCutoff = 256 |
| 90 |
|
| 91 |
// The amount of stack space to allocate as scratch space when |
| 92 |
// using the hybrid Lomuto/Hoare partition scheme. |
| 93 |
const scratchSize = 1024 |
| 94 |
|
| 95 |
func callswap(base int, swap func(int, int), i, j int) { |
| 96 |
if swap != nil { |
| 97 |
swap(base+i, base+j) |
| 98 |
} |
| 99 |
} |