Skip to content

Commit f66b26e

Browse files
committed
libc: minimal: add qsort to the minimal libc
This change implements qsort() for the minimal libc via Heapsort. Heapsort time complexity is O(n log(n)) in the best, average, and worst cases. It is O(1) in space complexity (i.e. sorts in-place) and is iterative rather than recursive. Heapsort is not stable (i.e. does not preserve order of identical elements). On cortex-m0, this implementation occupies ~240 bytes. Fixes #28896 Signed-off-by: Christopher Friedt <[email protected]>
1 parent 0782693 commit f66b26e

File tree

6 files changed

+122
-203
lines changed

6 files changed

+122
-203
lines changed

include/sys/util.h

+23
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,29 @@ static inline void bytecpy(void *dst, const void *src, size_t size)
241241
}
242242
}
243243

244+
/**
245+
* @brief byte by byte swap.
246+
*
247+
* Swap @a size bytes between memory regions @a a and @a b. This is
248+
* guaranteed to be done byte by byte.
249+
*
250+
* @param a Pointer to the the first memory region.
251+
* @param b Pointer to the the second memory region.
252+
* @param size The number of bytes to swap.
253+
*/
254+
static inline void byteswp(void *a, void *b, size_t size)
255+
{
256+
uint8_t t;
257+
uint8_t *aa = (uint8_t *)a;
258+
uint8_t *bb = (uint8_t *)b;
259+
260+
for (; size > 0; --size) {
261+
t = *aa;
262+
*aa++ = *bb;
263+
*bb++ = t;
264+
}
265+
}
266+
244267
/**
245268
* @brief Convert a single character into a hexadecimal nibble.
246269
*

lib/libc/minimal/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ zephyr_library_sources(
1111
source/stdlib/malloc.c
1212
source/stdlib/bsearch.c
1313
source/stdlib/exit.c
14+
source/stdlib/qsort.c
1415
source/string/strncasecmp.c
1516
source/string/strstr.c
1617
source/string/string.c

lib/libc/minimal/include/stdlib.h

+11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ void *bsearch(const void *key, const void *array,
3030
size_t count, size_t size,
3131
int (*cmp)(const void *key, const void *element));
3232

33+
void qsort_r(void *base, size_t nmemb, size_t size,
34+
int (*compar)(const void *, const void *, void *), void *arg);
35+
3336
#define EXIT_SUCCESS 0
3437
#define EXIT_FAILURE 1
3538
void _exit(int status);
@@ -60,6 +63,14 @@ static inline long long llabs(long long __n)
6063
return (__n < 0LL) ? -__n : __n;
6164
}
6265

66+
static inline void qsort(void *base, size_t nmemb, size_t size,
67+
int (*compar)(const void *, const void *))
68+
{
69+
typedef int (*compar3)(const void *, const void *, void *);
70+
71+
qsort_r(base, nmemb, size, (compar3)compar, NULL);
72+
}
73+
6374
#ifdef __cplusplus
6475
}
6576
#endif
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2021 Friedt Professional Engineering Services, Inc
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stddef.h>
8+
#include <stdint.h>
9+
#include <sys/types.h>
10+
#include <sys/util.h>
11+
12+
typedef int (*comp3_t)(const void *, const void *, void *);
13+
14+
/*
15+
* Normally parent is defined parent(k) = floor((k-1) / 2) but we can avoid a
16+
* divide by noticing that floor((k-1) / 2) = ((k - 1) >> 1).
17+
*/
18+
19+
#define parent(k) (((k) - 1) >> 1)
20+
/*
21+
* Normally left is defined left(k) = (2 * k + 1) but we can avoid a
22+
* multiply by noticing that (2 * k + 1) = ((k << 1) + 1).
23+
*/
24+
25+
#define left(k) (((k) << 1) + 1)
26+
27+
/*
28+
* Normally right is defined right(k) = (2 * k + 2) but we can avoid a
29+
* multiply by noticing that right(k) = left(k) + 1
30+
*/
31+
#define right(k) (left(k) + 1)
32+
33+
#define A(k) ((uint8_t *)base + size * (k))
34+
35+
static void sift_down(void *base, int start, int end, size_t size, comp3_t comp, void *comp_arg)
36+
{
37+
int root;
38+
int child;
39+
int swap;
40+
41+
for (swap = start, root = swap; left(root) < end; root = swap) {
42+
child = left(root);
43+
44+
/* if root < left */
45+
if (comp(A(swap), A(child), comp_arg) < 0) {
46+
swap = child;
47+
}
48+
49+
/* right exists and min(A(root),A(left)) < A(right) */
50+
if (right(root) < end && comp(A(swap), A(right(root)), comp_arg) < 0) {
51+
swap = right(root);
52+
}
53+
54+
if (swap == root) {
55+
return;
56+
}
57+
58+
byteswp(A(root), A(swap), size);
59+
}
60+
}
61+
62+
static void heapify(void *base, int nmemb, size_t size, comp3_t comp, void *comp_arg)
63+
{
64+
int start;
65+
66+
for (start = parent(nmemb - 1); start >= 0; --start) {
67+
sift_down(base, start, nmemb, size, comp, comp_arg);
68+
}
69+
}
70+
71+
static void heap_sort(void *base, int nmemb, size_t size, comp3_t comp, void *comp_arg)
72+
{
73+
int end;
74+
75+
heapify(base, nmemb, size, comp, comp_arg);
76+
77+
for (end = nmemb - 1; end > 0; --end) {
78+
byteswp(A(end), A(0), size);
79+
sift_down(base, 0, end, size, comp, comp_arg);
80+
}
81+
}
82+
83+
void qsort_r(void *base, size_t nmemb, size_t size, comp3_t comp, void *arg)
84+
{
85+
heap_sort(base, nmemb, size, comp, arg);
86+
}

samples/subsys/shell/shell_module/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
55
project(shell_module)
66

77
target_sources(app PRIVATE src/main.c src/test_module.c)
8-
target_sources_ifdef(CONFIG_SHELL_DYNAMIC_CMDS app PRIVATE src/dynamic_cmd.c src/qsort.c)
8+
target_sources_ifdef(CONFIG_SHELL_DYNAMIC_CMDS app PRIVATE src/dynamic_cmd.c)
99
target_sources_ifdef(CONFIG_SHELL_BACKEND_SERIAL app PRIVATE src/uart_reinit.c)

samples/subsys/shell/shell_module/src/qsort.c

-202
This file was deleted.

0 commit comments

Comments
 (0)