1 // SPDX-License-Identifier: GPL-2.0
3 * A fast, small, non-recursive O(nlog n) sort for the Linux kernel
5 * Jan 23 2005 Matt Mackall <mpm@selenic.com>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/types.h>
11 #include <linux/export.h>
12 #include <linux/sort.h>
15 * is_aligned - is this pointer & size okay for word-wide copying?
16 * @base: pointer to data
17 * @size: size of each element
18 * @align: required aignment (typically 4 or 8)
20 * Returns true if elements can be copied using word loads and stores.
21 * The size must be a multiple of the alignment, and the base address must
22 * be if we do not have CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS.
24 * For some reason, gcc doesn't know to optimize "if (a & mask || b & mask)"
25 * to "if ((a | b) & mask)", so we do that by hand.
27 __attribute_const__ __always_inline
28 static bool is_aligned(const void *base, size_t size, unsigned char align)
30 unsigned char lsbits = (unsigned char)size;
33 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
34 lsbits |= (unsigned char)(uintptr_t)base;
36 return (lsbits & (align - 1)) == 0;
40 * swap_words_32 - swap two elements in 32-bit chunks
41 * @a, @b: pointers to the elements
42 * @size: element size (must be a multiple of 4)
44 * Exchange the two objects in memory. This exploits base+index addressing,
45 * which basically all CPUs have, to minimize loop overhead computations.
47 * For some reason, on x86 gcc 7.3.0 adds a redundant test of n at the
48 * bottom of the loop, even though the zero flag is stil valid from the
49 * subtract (since the intervening mov instructions don't alter the flags).
50 * Gcc 8.1.0 doesn't have that problem.
52 static void swap_words_32(void *a, void *b, int size)
54 size_t n = (unsigned int)size;
57 u32 t = *(u32 *)(a + (n -= 4));
58 *(u32 *)(a + n) = *(u32 *)(b + n);
64 * swap_words_64 - swap two elements in 64-bit chunks
65 * @a, @b: pointers to the elements
66 * @size: element size (must be a multiple of 8)
68 * Exchange the two objects in memory. This exploits base+index
69 * addressing, which basically all CPUs have, to minimize loop overhead
72 * We'd like to use 64-bit loads if possible. If they're not, emulating
73 * one requires base+index+4 addressing which x86 has but most other
74 * processors do not. If CONFIG_64BIT, we definitely have 64-bit loads,
75 * but it's possible to have 64-bit loads without 64-bit pointers (e.g.
76 * x32 ABI). Are there any cases the kernel needs to worry about?
78 static void swap_words_64(void *a, void *b, int size)
80 size_t n = (unsigned int)size;
84 u64 t = *(u64 *)(a + (n -= 8));
85 *(u64 *)(a + n) = *(u64 *)(b + n);
88 /* Use two 32-bit transfers to avoid base+index+4 addressing */
89 u32 t = *(u32 *)(a + (n -= 4));
90 *(u32 *)(a + n) = *(u32 *)(b + n);
93 t = *(u32 *)(a + (n -= 4));
94 *(u32 *)(a + n) = *(u32 *)(b + n);
101 * swap_bytes - swap two elements a byte at a time
102 * @a, @b: pointers to the elements
103 * @size: element size
105 * This is the fallback if alignment doesn't allow using larger chunks.
107 static void swap_bytes(void *a, void *b, int size)
109 size_t n = (unsigned int)size;
112 char t = ((char *)a)[--n];
113 ((char *)a)[n] = ((char *)b)[n];
119 * sort - sort an array of elements
120 * @base: pointer to data to sort
121 * @num: number of elements
122 * @size: size of each element
123 * @cmp_func: pointer to comparison function
124 * @swap_func: pointer to swap function or NULL
126 * This function does a heapsort on the given array. You may provide
127 * a swap_func function if you need to do something more than a memory
128 * copy (e.g. fix up pointers or auxiliary data), but the built-in swap
129 * isn't usually a bottleneck.
131 * Sorting time is O(n log n) both on average and worst-case. While
132 * qsort is about 20% faster on average, it suffers from exploitable
133 * O(n*n) worst-case behavior and extra memory requirements that make
134 * it less suitable for kernel use.
137 void sort(void *base, size_t num, size_t size,
138 int (*cmp_func)(const void *, const void *),
139 void (*swap_func)(void *, void *, int size))
141 /* pre-scale counters for performance */
142 int i = (num/2 - 1) * size, n = num * size, c, r;
145 if (is_aligned(base, size, 8))
146 swap_func = swap_words_64;
147 else if (is_aligned(base, size, 4))
148 swap_func = swap_words_32;
150 swap_func = swap_bytes;
154 for ( ; i >= 0; i -= size) {
155 for (r = i; r * 2 + size < n; r = c) {
158 cmp_func(base + c, base + c + size) < 0)
160 if (cmp_func(base + r, base + c) >= 0)
162 swap_func(base + r, base + c, size);
167 for (i = n - size; i > 0; i -= size) {
168 swap_func(base, base + i, size);
169 for (r = 0; r * 2 + size < i; r = c) {
172 cmp_func(base + c, base + c + size) < 0)
174 if (cmp_func(base + r, base + c) >= 0)
176 swap_func(base + r, base + c, size);