Lines Matching +full:a +full:- +full:b
1 // SPDX-License-Identifier: GPL-2.0
3 * A fast, small, non-recursive O(n log n) sort for the Linux kernel
8 * Quicksort manages n*log2(n) - 1.26*n for random inputs (1.63*n
18 * is_aligned - is this pointer & size okay for word-wide copying?
24 * The size must be a multiple of the alignment, and the base address must
27 * For some reason, gcc doesn't know to optimize "if (a & mask || b & mask)"
28 * to "if ((a | b) & mask)", so we do that by hand.
39 return (lsbits & (align - 1)) == 0; in is_aligned()
43 * swap_words_32 - swap two elements in 32-bit chunks
44 * @a: pointer to the first element to swap
45 * @b: pointer to the second element to swap
46 * @n: element size (must be a multiple of 4)
51 * For some reason, on x86 gcc 7.3.0 adds a redundant test of n at the
56 static void swap_words_32(void *a, void *b, size_t n) in swap_words_32() argument
59 u32 t = *(u32 *)(a + (n -= 4)); in swap_words_32()
60 *(u32 *)(a + n) = *(u32 *)(b + n); in swap_words_32()
61 *(u32 *)(b + n) = t; in swap_words_32()
66 * swap_words_64 - swap two elements in 64-bit chunks
67 * @a: pointer to the first element to swap
68 * @b: pointer to the second element to swap
69 * @n: element size (must be a multiple of 8)
75 * We'd like to use 64-bit loads if possible. If they're not, emulating
77 * processors do not. If CONFIG_64BIT, we definitely have 64-bit loads,
78 * but it's possible to have 64-bit loads without 64-bit pointers (e.g.
81 static void swap_words_64(void *a, void *b, size_t n) in swap_words_64() argument
85 u64 t = *(u64 *)(a + (n -= 8)); in swap_words_64()
86 *(u64 *)(a + n) = *(u64 *)(b + n); in swap_words_64()
87 *(u64 *)(b + n) = t; in swap_words_64()
89 /* Use two 32-bit transfers to avoid base+index+4 addressing */ in swap_words_64()
90 u32 t = *(u32 *)(a + (n -= 4)); in swap_words_64()
91 *(u32 *)(a + n) = *(u32 *)(b + n); in swap_words_64()
92 *(u32 *)(b + n) = t; in swap_words_64()
94 t = *(u32 *)(a + (n -= 4)); in swap_words_64()
95 *(u32 *)(a + n) = *(u32 *)(b + n); in swap_words_64()
96 *(u32 *)(b + n) = t; in swap_words_64()
102 * swap_bytes - swap two elements a byte at a time
103 * @a: pointer to the first element to swap
104 * @b: pointer to the second element to swap
109 static void swap_bytes(void *a, void *b, size_t n) in swap_bytes() argument
112 char t = ((char *)a)[--n]; in swap_bytes()
113 ((char *)a)[n] = ((char *)b)[n]; in swap_bytes()
114 ((char *)b)[n] = t; in swap_bytes()
120 * a pointer, but small integers make for the smallest compare
137 static void do_swap(void *a, void *b, size_t size, swap_r_func_t swap_func, const void *priv) in do_swap() argument
140 ((const struct wrapper *)priv)->swap(a, b, (int)size); in do_swap()
145 swap_words_64(a, b, size); in do_swap()
147 swap_words_32(a, b, size); in do_swap()
149 swap_bytes(a, b, size); in do_swap()
151 swap_func(a, b, (int)size, priv); in do_swap()
156 static int do_cmp(const void *a, const void *b, cmp_r_func_t cmp, const void *priv) in do_cmp() argument
159 return ((const struct wrapper *)priv)->cmp(a, b); in do_cmp()
160 return cmp(a, b, priv); in do_cmp()
164 * parent - given the offset of the child, find the offset of the parent.
165 * @i: the offset of the heap element whose parent is sought. Non-zero.
166 * @lsbit: a precomputed 1-bit mask, equal to "size & -size"
170 * (j-1)/2. But when working in byte offsets, we can't use implicit
174 * @size has a least significant bit. That bit will be clear if @i is
177 * Logically, we're doing "if (i & lsbit) i -= size;", but since the
178 * branch is unpredictable, it's done with a bit of clever branch-free
184 i -= size; in parent()
185 i -= size & -(i & lsbit); in parent()
197 /* pre-scale counters for performance */ in __sort_r()
198 size_t n = num * size, a = (num/2) * size; in __sort_r() local
199 const unsigned int lsbit = size & -size; /* Used to find parent */ in __sort_r()
202 if (!a) /* num < 2 || size == 0 */ in __sort_r()
206 if (swap_func == SWAP_WRAPPER && !((struct wrapper *)priv)->swap) in __sort_r()
220 * 1. elements [a,n) satisfy the heap property (compare greater than in __sort_r()
223 * 3. a <= b <= c <= d <= n (whenever they are valid). in __sort_r()
226 size_t b, c, d; in __sort_r() local
228 if (a) /* Building heap: sift down a */ in __sort_r()
229 a -= size << shift; in __sort_r()
231 n -= size; in __sort_r()
234 a = size << shift; in __sort_r()
235 n -= size; in __sort_r()
236 do_swap(base + a, base + n, size, swap_func, priv); in __sort_r()
242 * Sift element at "a" down into heap. This is the in __sort_r()
243 * "bottom-up" variant, which significantly reduces in __sort_r()
244 * calls to cmp_func(): we find the sift-down path all in __sort_r()
250 * on the way down. (A bit more than half as many on in __sort_r()
251 * average, 3/4 worst-case.) in __sort_r()
253 for (b = a; c = 2*b + size, (d = c + size) < n;) in __sort_r()
254 b = do_cmp(base + c, base + d, cmp_func, priv) > 0 ? c : d; in __sort_r()
256 b = c; in __sort_r()
258 /* Now backtrack from "b" to the correct location for "a" */ in __sort_r()
259 while (b != a && do_cmp(base + a, base + b, cmp_func, priv) >= 0) in __sort_r()
260 b = parent(b, lsbit, size); in __sort_r()
261 c = b; /* Where "a" belongs */ in __sort_r()
262 while (b != a) { /* Shift it into place */ in __sort_r()
263 b = parent(b, lsbit, size); in __sort_r()
264 do_swap(base + b, base + c, size, swap_func, priv); in __sort_r()
271 n -= size; in __sort_r()
278 * sort_r - sort an array of elements
286 * This function does a heapsort on the given array. You may provide
287 * a swap_func function if you need to do something more than a memory
288 * copy (e.g. fix up pointers or auxiliary data), but the built-in swap
289 * avoids a slow retpoline and so is significantly faster.
293 * - Antisymmetry: cmp_func(a, b) must return the opposite sign of
294 * cmp_func(b, a).
295 * - Transitivity: if cmp_func(a, b) <= 0 and cmp_func(b, c) <= 0, then
296 * cmp_func(a, c) <= 0.
298 * Sorting time is O(n log n) both on average and worst-case. While
300 * O(n*n) worst-case behavior and extra memory requirements that make
313 * sort_r_nonatomic - sort an array of elements, with cond_resched
321 * Same as sort_r, but preferred for larger arrays as it does a periodic