sort.c 5.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5 6 7
/*
 * A fast, small, non-recursive O(nlog n) sort for the Linux kernel
 *
 * Jan 23 2005  Matt Mackall <mpm@selenic.com>
 */

8 9
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

10 11
#include <linux/types.h>
#include <linux/export.h>
A
Adrian Bunk 已提交
12
#include <linux/sort.h>
L
Linus Torvalds 已提交
13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/**
 * is_aligned - is this pointer & size okay for word-wide copying?
 * @base: pointer to data
 * @size: size of each element
 * @align: required aignment (typically 4 or 8)
 *
 * Returns true if elements can be copied using word loads and stores.
 * The size must be a multiple of the alignment, and the base address must
 * be if we do not have CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS.
 *
 * For some reason, gcc doesn't know to optimize "if (a & mask || b & mask)"
 * to "if ((a | b) & mask)", so we do that by hand.
 */
__attribute_const__ __always_inline
static bool is_aligned(const void *base, size_t size, unsigned char align)
29
{
30 31 32 33 34 35 36
	unsigned char lsbits = (unsigned char)size;

	(void)base;
#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
	lsbits |= (unsigned char)(uintptr_t)base;
#endif
	return (lsbits & (align - 1)) == 0;
37 38
}

39 40 41 42 43 44 45 46 47 48 49 50 51 52
/**
 * swap_words_32 - swap two elements in 32-bit chunks
 * @a, @b: pointers to the elements
 * @size: element size (must be a multiple of 4)
 *
 * Exchange the two objects in memory.  This exploits base+index addressing,
 * which basically all CPUs have, to minimize loop overhead computations.
 *
 * For some reason, on x86 gcc 7.3.0 adds a redundant test of n at the
 * bottom of the loop, even though the zero flag is stil valid from the
 * subtract (since the intervening mov instructions don't alter the flags).
 * Gcc 8.1.0 doesn't have that problem.
 */
static void swap_words_32(void *a, void *b, int size)
L
Linus Torvalds 已提交
53
{
54 55 56 57 58 59 60
	size_t n = (unsigned int)size;

	do {
		u32 t = *(u32 *)(a + (n -= 4));
		*(u32 *)(a + n) = *(u32 *)(b + n);
		*(u32 *)(b + n) = t;
	} while (n);
L
Linus Torvalds 已提交
61 62
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/**
 * swap_words_64 - swap two elements in 64-bit chunks
 * @a, @b: pointers to the elements
 * @size: element size (must be a multiple of 8)
 *
 * Exchange the two objects in memory.  This exploits base+index
 * addressing, which basically all CPUs have, to minimize loop overhead
 * computations.
 *
 * We'd like to use 64-bit loads if possible.  If they're not, emulating
 * one requires base+index+4 addressing which x86 has but most other
 * processors do not.  If CONFIG_64BIT, we definitely have 64-bit loads,
 * but it's possible to have 64-bit loads without 64-bit pointers (e.g.
 * x32 ABI).  Are there any cases the kernel needs to worry about?
 */
static void swap_words_64(void *a, void *b, int size)
79
{
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
	size_t n = (unsigned int)size;

	do {
#ifdef CONFIG_64BIT
		u64 t = *(u64 *)(a + (n -= 8));
		*(u64 *)(a + n) = *(u64 *)(b + n);
		*(u64 *)(b + n) = t;
#else
		/* Use two 32-bit transfers to avoid base+index+4 addressing */
		u32 t = *(u32 *)(a + (n -= 4));
		*(u32 *)(a + n) = *(u32 *)(b + n);
		*(u32 *)(b + n) = t;

		t = *(u32 *)(a + (n -= 4));
		*(u32 *)(a + n) = *(u32 *)(b + n);
		*(u32 *)(b + n) = t;
#endif
	} while (n);
98 99
}

100 101 102 103 104 105 106 107
/**
 * swap_bytes - swap two elements a byte at a time
 * @a, @b: pointers to the elements
 * @size: element size
 *
 * This is the fallback if alignment doesn't allow using larger chunks.
 */
static void swap_bytes(void *a, void *b, int size)
L
Linus Torvalds 已提交
108
{
109
	size_t n = (unsigned int)size;
L
Linus Torvalds 已提交
110 111

	do {
112 113 114 115
		char t = ((char *)a)[--n];
		((char *)a)[n] = ((char *)b)[n];
		((char *)b)[n] = t;
	} while (n);
L
Linus Torvalds 已提交
116 117
}

118
/**
L
Linus Torvalds 已提交
119 120 121 122
 * sort - sort an array of elements
 * @base: pointer to data to sort
 * @num: number of elements
 * @size: size of each element
123 124
 * @cmp_func: pointer to comparison function
 * @swap_func: pointer to swap function or NULL
L
Linus Torvalds 已提交
125
 *
126 127 128 129
 * This function does a heapsort on the given array.  You may provide
 * a swap_func function if you need to do something more than a memory
 * copy (e.g. fix up pointers or auxiliary data), but the built-in swap
 * isn't usually a bottleneck.
L
Linus Torvalds 已提交
130 131 132 133 134 135 136 137
 *
 * Sorting time is O(n log n) both on average and worst-case. While
 * qsort is about 20% faster on average, it suffers from exploitable
 * O(n*n) worst-case behavior and extra memory requirements that make
 * it less suitable for kernel use.
 */

void sort(void *base, size_t num, size_t size,
138 139
	  int (*cmp_func)(const void *, const void *),
	  void (*swap_func)(void *, void *, int size))
L
Linus Torvalds 已提交
140 141
{
	/* pre-scale counters for performance */
K
keios 已提交
142
	int i = (num/2 - 1) * size, n = num * size, c, r;
L
Linus Torvalds 已提交
143

144
	if (!swap_func) {
145 146 147 148
		if (is_aligned(base, size, 8))
			swap_func = swap_words_64;
		else if (is_aligned(base, size, 4))
			swap_func = swap_words_32;
149
		else
150
			swap_func = swap_bytes;
151
	}
L
Linus Torvalds 已提交
152 153 154

	/* heapify */
	for ( ; i >= 0; i -= size) {
K
keios 已提交
155 156
		for (r = i; r * 2 + size < n; r  = c) {
			c = r * 2 + size;
157 158
			if (c < n - size &&
					cmp_func(base + c, base + c + size) < 0)
L
Linus Torvalds 已提交
159
				c += size;
160
			if (cmp_func(base + r, base + c) >= 0)
L
Linus Torvalds 已提交
161
				break;
162
			swap_func(base + r, base + c, size);
L
Linus Torvalds 已提交
163 164 165 166
		}
	}

	/* sort */
S
Subbaiah Venkata 已提交
167
	for (i = n - size; i > 0; i -= size) {
168
		swap_func(base, base + i, size);
K
keios 已提交
169 170
		for (r = 0; r * 2 + size < i; r = c) {
			c = r * 2 + size;
171 172
			if (c < i - size &&
					cmp_func(base + c, base + c + size) < 0)
L
Linus Torvalds 已提交
173
				c += size;
174
			if (cmp_func(base + r, base + c) >= 0)
L
Linus Torvalds 已提交
175
				break;
176
			swap_func(base + r, base + c, size);
L
Linus Torvalds 已提交
177 178 179 180 181
		}
	}
}

EXPORT_SYMBOL(sort);