1ca1e02f7SKent Overstreet // SPDX-License-Identifier: GPL-2.0
2ca1e02f7SKent Overstreet
3ca1e02f7SKent Overstreet #include "eytzinger.h"
4ca1e02f7SKent Overstreet
5ca1e02f7SKent Overstreet /**
6ca1e02f7SKent Overstreet * is_aligned - is this pointer & size okay for word-wide copying?
7ca1e02f7SKent Overstreet * @base: pointer to data
8ca1e02f7SKent Overstreet * @size: size of each element
9ca1e02f7SKent Overstreet * @align: required alignment (typically 4 or 8)
10ca1e02f7SKent Overstreet *
11ca1e02f7SKent Overstreet * Returns true if elements can be copied using word loads and stores.
12ca1e02f7SKent Overstreet * The size must be a multiple of the alignment, and the base address must
13ca1e02f7SKent Overstreet * be if we do not have CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS.
14ca1e02f7SKent Overstreet *
15ca1e02f7SKent Overstreet * For some reason, gcc doesn't know to optimize "if (a & mask || b & mask)"
16ca1e02f7SKent Overstreet * to "if ((a | b) & mask)", so we do that by hand.
17ca1e02f7SKent Overstreet */
18ca1e02f7SKent Overstreet __attribute_const__ __always_inline
is_aligned(const void * base,size_t size,unsigned char align)19ca1e02f7SKent Overstreet static bool is_aligned(const void *base, size_t size, unsigned char align)
20ca1e02f7SKent Overstreet {
21ca1e02f7SKent Overstreet unsigned char lsbits = (unsigned char)size;
22ca1e02f7SKent Overstreet
23ca1e02f7SKent Overstreet (void)base;
24ca1e02f7SKent Overstreet #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
25ca1e02f7SKent Overstreet lsbits |= (unsigned char)(uintptr_t)base;
26ca1e02f7SKent Overstreet #endif
27ca1e02f7SKent Overstreet return (lsbits & (align - 1)) == 0;
28ca1e02f7SKent Overstreet }
29ca1e02f7SKent Overstreet
30ca1e02f7SKent Overstreet /**
31ca1e02f7SKent Overstreet * swap_words_32 - swap two elements in 32-bit chunks
32ca1e02f7SKent Overstreet * @a: pointer to the first element to swap
33ca1e02f7SKent Overstreet * @b: pointer to the second element to swap
34ca1e02f7SKent Overstreet * @n: element size (must be a multiple of 4)
35ca1e02f7SKent Overstreet *
36ca1e02f7SKent Overstreet * Exchange the two objects in memory. This exploits base+index addressing,
37ca1e02f7SKent Overstreet * which basically all CPUs have, to minimize loop overhead computations.
38ca1e02f7SKent Overstreet *
39ca1e02f7SKent Overstreet * For some reason, on x86 gcc 7.3.0 adds a redundant test of n at the
40ca1e02f7SKent Overstreet * bottom of the loop, even though the zero flag is still valid from the
41ca1e02f7SKent Overstreet * subtract (since the intervening mov instructions don't alter the flags).
42ca1e02f7SKent Overstreet * Gcc 8.1.0 doesn't have that problem.
43ca1e02f7SKent Overstreet */
swap_words_32(void * a,void * b,size_t n)44ca1e02f7SKent Overstreet static void swap_words_32(void *a, void *b, size_t n)
45ca1e02f7SKent Overstreet {
46ca1e02f7SKent Overstreet do {
47ca1e02f7SKent Overstreet u32 t = *(u32 *)(a + (n -= 4));
48ca1e02f7SKent Overstreet *(u32 *)(a + n) = *(u32 *)(b + n);
49ca1e02f7SKent Overstreet *(u32 *)(b + n) = t;
50ca1e02f7SKent Overstreet } while (n);
51ca1e02f7SKent Overstreet }
52ca1e02f7SKent Overstreet
53ca1e02f7SKent Overstreet /**
54ca1e02f7SKent Overstreet * swap_words_64 - swap two elements in 64-bit chunks
55ca1e02f7SKent Overstreet * @a: pointer to the first element to swap
56ca1e02f7SKent Overstreet * @b: pointer to the second element to swap
57ca1e02f7SKent Overstreet * @n: element size (must be a multiple of 8)
58ca1e02f7SKent Overstreet *
59ca1e02f7SKent Overstreet * Exchange the two objects in memory. This exploits base+index
60ca1e02f7SKent Overstreet * addressing, which basically all CPUs have, to minimize loop overhead
61ca1e02f7SKent Overstreet * computations.
62ca1e02f7SKent Overstreet *
63ca1e02f7SKent Overstreet * We'd like to use 64-bit loads if possible. If they're not, emulating
64ca1e02f7SKent Overstreet * one requires base+index+4 addressing which x86 has but most other
65ca1e02f7SKent Overstreet * processors do not. If CONFIG_64BIT, we definitely have 64-bit loads,
66ca1e02f7SKent Overstreet * but it's possible to have 64-bit loads without 64-bit pointers (e.g.
67ca1e02f7SKent Overstreet * x32 ABI). Are there any cases the kernel needs to worry about?
68ca1e02f7SKent Overstreet */
swap_words_64(void * a,void * b,size_t n)69ca1e02f7SKent Overstreet static void swap_words_64(void *a, void *b, size_t n)
70ca1e02f7SKent Overstreet {
71ca1e02f7SKent Overstreet do {
72ca1e02f7SKent Overstreet #ifdef CONFIG_64BIT
73ca1e02f7SKent Overstreet u64 t = *(u64 *)(a + (n -= 8));
74ca1e02f7SKent Overstreet *(u64 *)(a + n) = *(u64 *)(b + n);
75ca1e02f7SKent Overstreet *(u64 *)(b + n) = t;
76ca1e02f7SKent Overstreet #else
77ca1e02f7SKent Overstreet /* Use two 32-bit transfers to avoid base+index+4 addressing */
78ca1e02f7SKent Overstreet u32 t = *(u32 *)(a + (n -= 4));
79ca1e02f7SKent Overstreet *(u32 *)(a + n) = *(u32 *)(b + n);
80ca1e02f7SKent Overstreet *(u32 *)(b + n) = t;
81ca1e02f7SKent Overstreet
82ca1e02f7SKent Overstreet t = *(u32 *)(a + (n -= 4));
83ca1e02f7SKent Overstreet *(u32 *)(a + n) = *(u32 *)(b + n);
84ca1e02f7SKent Overstreet *(u32 *)(b + n) = t;
85ca1e02f7SKent Overstreet #endif
86ca1e02f7SKent Overstreet } while (n);
87ca1e02f7SKent Overstreet }
88ca1e02f7SKent Overstreet
89ca1e02f7SKent Overstreet /**
90ca1e02f7SKent Overstreet * swap_bytes - swap two elements a byte at a time
91ca1e02f7SKent Overstreet * @a: pointer to the first element to swap
92ca1e02f7SKent Overstreet * @b: pointer to the second element to swap
93ca1e02f7SKent Overstreet * @n: element size
94ca1e02f7SKent Overstreet *
95ca1e02f7SKent Overstreet * This is the fallback if alignment doesn't allow using larger chunks.
96ca1e02f7SKent Overstreet */
swap_bytes(void * a,void * b,size_t n)97ca1e02f7SKent Overstreet static void swap_bytes(void *a, void *b, size_t n)
98ca1e02f7SKent Overstreet {
99ca1e02f7SKent Overstreet do {
100ca1e02f7SKent Overstreet char t = ((char *)a)[--n];
101ca1e02f7SKent Overstreet ((char *)a)[n] = ((char *)b)[n];
102ca1e02f7SKent Overstreet ((char *)b)[n] = t;
103ca1e02f7SKent Overstreet } while (n);
104ca1e02f7SKent Overstreet }
105ca1e02f7SKent Overstreet
106ca1e02f7SKent Overstreet /*
107ca1e02f7SKent Overstreet * The values are arbitrary as long as they can't be confused with
108ca1e02f7SKent Overstreet * a pointer, but small integers make for the smallest compare
109ca1e02f7SKent Overstreet * instructions.
110ca1e02f7SKent Overstreet */
111ca1e02f7SKent Overstreet #define SWAP_WORDS_64 (swap_r_func_t)0
112ca1e02f7SKent Overstreet #define SWAP_WORDS_32 (swap_r_func_t)1
113ca1e02f7SKent Overstreet #define SWAP_BYTES (swap_r_func_t)2
114ca1e02f7SKent Overstreet #define SWAP_WRAPPER (swap_r_func_t)3
115ca1e02f7SKent Overstreet
116ca1e02f7SKent Overstreet struct wrapper {
117ca1e02f7SKent Overstreet cmp_func_t cmp;
1182d793e93SThorsten Blum swap_func_t swap_func;
119ca1e02f7SKent Overstreet };
120ca1e02f7SKent Overstreet
121ca1e02f7SKent Overstreet /*
122ca1e02f7SKent Overstreet * The function pointer is last to make tail calls most efficient if the
123ca1e02f7SKent Overstreet * compiler decides not to inline this function.
124ca1e02f7SKent Overstreet */
do_swap(void * a,void * b,size_t size,swap_r_func_t swap_func,const void * priv)125ca1e02f7SKent Overstreet static void do_swap(void *a, void *b, size_t size, swap_r_func_t swap_func, const void *priv)
126ca1e02f7SKent Overstreet {
127ca1e02f7SKent Overstreet if (swap_func == SWAP_WRAPPER) {
1282d793e93SThorsten Blum ((const struct wrapper *)priv)->swap_func(a, b, (int)size);
129ca1e02f7SKent Overstreet return;
130ca1e02f7SKent Overstreet }
131ca1e02f7SKent Overstreet
132ca1e02f7SKent Overstreet if (swap_func == SWAP_WORDS_64)
133ca1e02f7SKent Overstreet swap_words_64(a, b, size);
134ca1e02f7SKent Overstreet else if (swap_func == SWAP_WORDS_32)
135ca1e02f7SKent Overstreet swap_words_32(a, b, size);
136ca1e02f7SKent Overstreet else if (swap_func == SWAP_BYTES)
137ca1e02f7SKent Overstreet swap_bytes(a, b, size);
138ca1e02f7SKent Overstreet else
139ca1e02f7SKent Overstreet swap_func(a, b, (int)size, priv);
140ca1e02f7SKent Overstreet }
141ca1e02f7SKent Overstreet
142ca1e02f7SKent Overstreet #define _CMP_WRAPPER ((cmp_r_func_t)0L)
143ca1e02f7SKent Overstreet
do_cmp(const void * a,const void * b,cmp_r_func_t cmp,const void * priv)144ca1e02f7SKent Overstreet static int do_cmp(const void *a, const void *b, cmp_r_func_t cmp, const void *priv)
145ca1e02f7SKent Overstreet {
146ca1e02f7SKent Overstreet if (cmp == _CMP_WRAPPER)
147ca1e02f7SKent Overstreet return ((const struct wrapper *)priv)->cmp(a, b);
148ca1e02f7SKent Overstreet return cmp(a, b, priv);
149ca1e02f7SKent Overstreet }
150ca1e02f7SKent Overstreet
eytzinger0_do_cmp(void * base,size_t n,size_t size,cmp_r_func_t cmp_func,const void * priv,size_t l,size_t r)151ca1e02f7SKent Overstreet static inline int eytzinger0_do_cmp(void *base, size_t n, size_t size,
152ca1e02f7SKent Overstreet cmp_r_func_t cmp_func, const void *priv,
153ca1e02f7SKent Overstreet size_t l, size_t r)
154ca1e02f7SKent Overstreet {
155ca1e02f7SKent Overstreet return do_cmp(base + inorder_to_eytzinger0(l, n) * size,
156ca1e02f7SKent Overstreet base + inorder_to_eytzinger0(r, n) * size,
157ca1e02f7SKent Overstreet cmp_func, priv);
158ca1e02f7SKent Overstreet }
159ca1e02f7SKent Overstreet
eytzinger0_do_swap(void * base,size_t n,size_t size,swap_r_func_t swap_func,const void * priv,size_t l,size_t r)160ca1e02f7SKent Overstreet static inline void eytzinger0_do_swap(void *base, size_t n, size_t size,
161ca1e02f7SKent Overstreet swap_r_func_t swap_func, const void *priv,
162ca1e02f7SKent Overstreet size_t l, size_t r)
163ca1e02f7SKent Overstreet {
164ca1e02f7SKent Overstreet do_swap(base + inorder_to_eytzinger0(l, n) * size,
165ca1e02f7SKent Overstreet base + inorder_to_eytzinger0(r, n) * size,
166ca1e02f7SKent Overstreet size, swap_func, priv);
167ca1e02f7SKent Overstreet }
168ca1e02f7SKent Overstreet
eytzinger0_sort_r(void * base,size_t n,size_t size,cmp_r_func_t cmp_func,swap_r_func_t swap_func,const void * priv)169ca1e02f7SKent Overstreet void eytzinger0_sort_r(void *base, size_t n, size_t size,
170ca1e02f7SKent Overstreet cmp_r_func_t cmp_func,
171ca1e02f7SKent Overstreet swap_r_func_t swap_func,
172ca1e02f7SKent Overstreet const void *priv)
173ca1e02f7SKent Overstreet {
174*0ddb5f08SKuan-Wei Chiu int i, j, k;
175ca1e02f7SKent Overstreet
176ca1e02f7SKent Overstreet /* called from 'sort' without swap function, let's pick the default */
1772d793e93SThorsten Blum if (swap_func == SWAP_WRAPPER && !((struct wrapper *)priv)->swap_func)
178ca1e02f7SKent Overstreet swap_func = NULL;
179ca1e02f7SKent Overstreet
180ca1e02f7SKent Overstreet if (!swap_func) {
181ca1e02f7SKent Overstreet if (is_aligned(base, size, 8))
182ca1e02f7SKent Overstreet swap_func = SWAP_WORDS_64;
183ca1e02f7SKent Overstreet else if (is_aligned(base, size, 4))
184ca1e02f7SKent Overstreet swap_func = SWAP_WORDS_32;
185ca1e02f7SKent Overstreet else
186ca1e02f7SKent Overstreet swap_func = SWAP_BYTES;
187ca1e02f7SKent Overstreet }
188ca1e02f7SKent Overstreet
189ca1e02f7SKent Overstreet /* heapify */
190ca1e02f7SKent Overstreet for (i = n / 2 - 1; i >= 0; --i) {
191*0ddb5f08SKuan-Wei Chiu /* Find the sift-down path all the way to the leaves. */
192*0ddb5f08SKuan-Wei Chiu for (j = i; k = j * 2 + 1, k + 1 < n;)
193*0ddb5f08SKuan-Wei Chiu j = eytzinger0_do_cmp(base, n, size, cmp_func, priv, k, k + 1) > 0 ? k : k + 1;
194ca1e02f7SKent Overstreet
195*0ddb5f08SKuan-Wei Chiu /* Special case for the last leaf with no sibling. */
196*0ddb5f08SKuan-Wei Chiu if (j * 2 + 2 == n)
197*0ddb5f08SKuan-Wei Chiu j = j * 2 + 1;
198ca1e02f7SKent Overstreet
199*0ddb5f08SKuan-Wei Chiu /* Backtrack to the correct location. */
200*0ddb5f08SKuan-Wei Chiu while (j != i && eytzinger0_do_cmp(base, n, size, cmp_func, priv, i, j) >= 0)
201*0ddb5f08SKuan-Wei Chiu j = (j - 1) / 2;
202ca1e02f7SKent Overstreet
203*0ddb5f08SKuan-Wei Chiu /* Shift the element into its correct place. */
204*0ddb5f08SKuan-Wei Chiu for (k = j; j != i;) {
205*0ddb5f08SKuan-Wei Chiu j = (j - 1) / 2;
206*0ddb5f08SKuan-Wei Chiu eytzinger0_do_swap(base, n, size, swap_func, priv, j, k);
207ca1e02f7SKent Overstreet }
208ca1e02f7SKent Overstreet }
209ca1e02f7SKent Overstreet
210ca1e02f7SKent Overstreet /* sort */
211ca1e02f7SKent Overstreet for (i = n - 1; i > 0; --i) {
212ca1e02f7SKent Overstreet eytzinger0_do_swap(base, n, size, swap_func, priv, 0, i);
213ca1e02f7SKent Overstreet
214*0ddb5f08SKuan-Wei Chiu /* Find the sift-down path all the way to the leaves. */
215*0ddb5f08SKuan-Wei Chiu for (j = 0; k = j * 2 + 1, k + 1 < i;)
216*0ddb5f08SKuan-Wei Chiu j = eytzinger0_do_cmp(base, n, size, cmp_func, priv, k, k + 1) > 0 ? k : k + 1;
217ca1e02f7SKent Overstreet
218*0ddb5f08SKuan-Wei Chiu /* Special case for the last leaf with no sibling. */
219*0ddb5f08SKuan-Wei Chiu if (j * 2 + 2 == i)
220*0ddb5f08SKuan-Wei Chiu j = j * 2 + 1;
221ca1e02f7SKent Overstreet
222*0ddb5f08SKuan-Wei Chiu /* Backtrack to the correct location. */
223*0ddb5f08SKuan-Wei Chiu while (j && eytzinger0_do_cmp(base, n, size, cmp_func, priv, 0, j) >= 0)
224*0ddb5f08SKuan-Wei Chiu j = (j - 1) / 2;
225ca1e02f7SKent Overstreet
226*0ddb5f08SKuan-Wei Chiu /* Shift the element into its correct place. */
227*0ddb5f08SKuan-Wei Chiu for (k = j; j;) {
228*0ddb5f08SKuan-Wei Chiu j = (j - 1) / 2;
229*0ddb5f08SKuan-Wei Chiu eytzinger0_do_swap(base, n, size, swap_func, priv, j, k);
230ca1e02f7SKent Overstreet }
231ca1e02f7SKent Overstreet }
232ca1e02f7SKent Overstreet }
233ca1e02f7SKent Overstreet
eytzinger0_sort(void * base,size_t n,size_t size,cmp_func_t cmp_func,swap_func_t swap_func)234ca1e02f7SKent Overstreet void eytzinger0_sort(void *base, size_t n, size_t size,
235ca1e02f7SKent Overstreet cmp_func_t cmp_func,
236ca1e02f7SKent Overstreet swap_func_t swap_func)
237ca1e02f7SKent Overstreet {
238ca1e02f7SKent Overstreet struct wrapper w = {
239ca1e02f7SKent Overstreet .cmp = cmp_func,
2402d793e93SThorsten Blum .swap_func = swap_func,
241ca1e02f7SKent Overstreet };
242ca1e02f7SKent Overstreet
243ca1e02f7SKent Overstreet return eytzinger0_sort_r(base, n, size, _CMP_WRAPPER, SWAP_WRAPPER, &w);
244ca1e02f7SKent Overstreet }
245*0ddb5f08SKuan-Wei Chiu
246*0ddb5f08SKuan-Wei Chiu #if 0
247*0ddb5f08SKuan-Wei Chiu #include <linux/slab.h>
248*0ddb5f08SKuan-Wei Chiu #include <linux/random.h>
249*0ddb5f08SKuan-Wei Chiu #include <linux/ktime.h>
250*0ddb5f08SKuan-Wei Chiu
251*0ddb5f08SKuan-Wei Chiu static u64 cmp_count;
252*0ddb5f08SKuan-Wei Chiu
253*0ddb5f08SKuan-Wei Chiu static int mycmp(const void *a, const void *b)
254*0ddb5f08SKuan-Wei Chiu {
255*0ddb5f08SKuan-Wei Chiu u32 _a = *(u32 *)a;
256*0ddb5f08SKuan-Wei Chiu u32 _b = *(u32 *)b;
257*0ddb5f08SKuan-Wei Chiu
258*0ddb5f08SKuan-Wei Chiu cmp_count++;
259*0ddb5f08SKuan-Wei Chiu if (_a < _b)
260*0ddb5f08SKuan-Wei Chiu return -1;
261*0ddb5f08SKuan-Wei Chiu else if (_a > _b)
262*0ddb5f08SKuan-Wei Chiu return 1;
263*0ddb5f08SKuan-Wei Chiu else
264*0ddb5f08SKuan-Wei Chiu return 0;
265*0ddb5f08SKuan-Wei Chiu }
266*0ddb5f08SKuan-Wei Chiu
267*0ddb5f08SKuan-Wei Chiu static int test(void)
268*0ddb5f08SKuan-Wei Chiu {
269*0ddb5f08SKuan-Wei Chiu size_t N, i;
270*0ddb5f08SKuan-Wei Chiu ktime_t start, end;
271*0ddb5f08SKuan-Wei Chiu s64 delta;
272*0ddb5f08SKuan-Wei Chiu u32 *arr;
273*0ddb5f08SKuan-Wei Chiu
274*0ddb5f08SKuan-Wei Chiu for (N = 10000; N <= 100000; N += 10000) {
275*0ddb5f08SKuan-Wei Chiu arr = kmalloc_array(N, sizeof(u32), GFP_KERNEL);
276*0ddb5f08SKuan-Wei Chiu cmp_count = 0;
277*0ddb5f08SKuan-Wei Chiu
278*0ddb5f08SKuan-Wei Chiu for (i = 0; i < N; i++)
279*0ddb5f08SKuan-Wei Chiu arr[i] = get_random_u32();
280*0ddb5f08SKuan-Wei Chiu
281*0ddb5f08SKuan-Wei Chiu start = ktime_get();
282*0ddb5f08SKuan-Wei Chiu eytzinger0_sort(arr, N, sizeof(u32), mycmp, NULL);
283*0ddb5f08SKuan-Wei Chiu end = ktime_get();
284*0ddb5f08SKuan-Wei Chiu
285*0ddb5f08SKuan-Wei Chiu delta = ktime_us_delta(end, start);
286*0ddb5f08SKuan-Wei Chiu printk(KERN_INFO "time: %lld\n", delta);
287*0ddb5f08SKuan-Wei Chiu printk(KERN_INFO "comparisons: %lld\n", cmp_count);
288*0ddb5f08SKuan-Wei Chiu
289*0ddb5f08SKuan-Wei Chiu u32 prev = 0;
290*0ddb5f08SKuan-Wei Chiu
291*0ddb5f08SKuan-Wei Chiu eytzinger0_for_each(i, N) {
292*0ddb5f08SKuan-Wei Chiu if (prev > arr[i])
293*0ddb5f08SKuan-Wei Chiu goto err;
294*0ddb5f08SKuan-Wei Chiu prev = arr[i];
295*0ddb5f08SKuan-Wei Chiu }
296*0ddb5f08SKuan-Wei Chiu
297*0ddb5f08SKuan-Wei Chiu kfree(arr);
298*0ddb5f08SKuan-Wei Chiu }
299*0ddb5f08SKuan-Wei Chiu return 0;
300*0ddb5f08SKuan-Wei Chiu
301*0ddb5f08SKuan-Wei Chiu err:
302*0ddb5f08SKuan-Wei Chiu kfree(arr);
303*0ddb5f08SKuan-Wei Chiu return -1;
304*0ddb5f08SKuan-Wei Chiu }
305*0ddb5f08SKuan-Wei Chiu #endif
306