blob: d6b7a202b0b648ea4b45bd1b979b2562dddbb8f1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * A fast, small, non-recursive O(nlog n) sort for the Linux kernel
4 *
5 * Jan 23 2005 Matt Mackall <mpm@selenic.com>
6 */
7
Kostenzer Felixc5adae92017-02-24 15:01:07 -08008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Rasmus Villemoes42cf8092015-02-12 15:02:35 -080010#include <linux/types.h>
11#include <linux/export.h>
Adrian Bunkecec4cb2005-09-10 00:26:59 -070012#include <linux/sort.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Daniel Wagnerca96ab82015-06-25 15:02:14 -070014static int alignment_ok(const void *base, int align)
15{
16 return IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
17 ((unsigned long)base & (align - 1)) == 0;
18}
19
Adrian Bunkecec4cb2005-09-10 00:26:59 -070020static void u32_swap(void *a, void *b, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
22 u32 t = *(u32 *)a;
23 *(u32 *)a = *(u32 *)b;
24 *(u32 *)b = t;
25}
26
Daniel Wagnerca96ab82015-06-25 15:02:14 -070027static void u64_swap(void *a, void *b, int size)
28{
29 u64 t = *(u64 *)a;
30 *(u64 *)a = *(u64 *)b;
31 *(u64 *)b = t;
32}
33
Adrian Bunkecec4cb2005-09-10 00:26:59 -070034static void generic_swap(void *a, void *b, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 char t;
37
38 do {
39 t = *(char *)a;
40 *(char *)a++ = *(char *)b;
41 *(char *)b++ = t;
42 } while (--size > 0);
43}
44
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080045/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * sort - sort an array of elements
47 * @base: pointer to data to sort
48 * @num: number of elements
49 * @size: size of each element
Wu Fengguangb53907c2009-01-07 18:09:11 -080050 * @cmp_func: pointer to comparison function
51 * @swap_func: pointer to swap function or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 *
53 * This function does a heapsort on the given array. You may provide a
Wu Fengguangb53907c2009-01-07 18:09:11 -080054 * swap_func function optimized to your element type.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 *
56 * Sorting time is O(n log n) both on average and worst-case. While
57 * qsort is about 20% faster on average, it suffers from exploitable
58 * O(n*n) worst-case behavior and extra memory requirements that make
59 * it less suitable for kernel use.
60 */
61
62void sort(void *base, size_t num, size_t size,
Wu Fengguangb53907c2009-01-07 18:09:11 -080063 int (*cmp_func)(const void *, const void *),
64 void (*swap_func)(void *, void *, int size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 /* pre-scale counters for performance */
keiosd3717bd2006-10-03 01:13:49 -070067 int i = (num/2 - 1) * size, n = num * size, c, r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Daniel Wagnerca96ab82015-06-25 15:02:14 -070069 if (!swap_func) {
70 if (size == 4 && alignment_ok(base, 4))
71 swap_func = u32_swap;
72 else if (size == 8 && alignment_ok(base, 8))
73 swap_func = u64_swap;
74 else
75 swap_func = generic_swap;
76 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 /* heapify */
79 for ( ; i >= 0; i -= size) {
keiosd3717bd2006-10-03 01:13:49 -070080 for (r = i; r * 2 + size < n; r = c) {
81 c = r * 2 + size;
Wu Fengguangb53907c2009-01-07 18:09:11 -080082 if (c < n - size &&
83 cmp_func(base + c, base + c + size) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 c += size;
Wu Fengguangb53907c2009-01-07 18:09:11 -080085 if (cmp_func(base + r, base + c) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 break;
Wu Fengguangb53907c2009-01-07 18:09:11 -080087 swap_func(base + r, base + c, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89 }
90
91 /* sort */
Subbaiah Venkata995e4282007-10-16 23:27:06 -070092 for (i = n - size; i > 0; i -= size) {
Wu Fengguangb53907c2009-01-07 18:09:11 -080093 swap_func(base, base + i, size);
keiosd3717bd2006-10-03 01:13:49 -070094 for (r = 0; r * 2 + size < i; r = c) {
95 c = r * 2 + size;
Wu Fengguangb53907c2009-01-07 18:09:11 -080096 if (c < i - size &&
97 cmp_func(base + c, base + c + size) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 c += size;
Wu Fengguangb53907c2009-01-07 18:09:11 -080099 if (cmp_func(base + r, base + c) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 break;
Wu Fengguangb53907c2009-01-07 18:09:11 -0800101 swap_func(base + r, base + c, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103 }
104}
105
106EXPORT_SYMBOL(sort);