Skip to content

libc: minimal: add qsort to the minimal libc #39980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions include/sys/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,29 @@ static inline void bytecpy(void *dst, const void *src, size_t size)
}
}

/**
* @brief byte by byte swap.
*
* Swap @a size bytes between memory regions @a a and @a b. This is
* guaranteed to be done byte by byte.
*
* @param a Pointer to the the first memory region.
* @param b Pointer to the the second memory region.
* @param size The number of bytes to swap.
*/
static inline void byteswp(void *a, void *b, size_t size)
{
uint8_t t;
uint8_t *aa = (uint8_t *)a;
uint8_t *bb = (uint8_t *)b;

for (; size > 0; --size) {
t = *aa;
*aa++ = *bb;
*bb++ = t;
}
}

/**
* @brief Convert a single character into a hexadecimal nibble.
*
Expand Down
1 change: 1 addition & 0 deletions lib/libc/minimal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ zephyr_library_sources(
source/stdlib/malloc.c
source/stdlib/bsearch.c
source/stdlib/exit.c
source/stdlib/qsort.c
source/string/strncasecmp.c
source/string/strstr.c
source/string/string.c
Expand Down
11 changes: 11 additions & 0 deletions lib/libc/minimal/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ void *bsearch(const void *key, const void *array,
size_t count, size_t size,
int (*cmp)(const void *key, const void *element));

void qsort_r(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *, void *), void *arg);

#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
void _exit(int status);
Expand Down Expand Up @@ -60,6 +63,14 @@ static inline long long llabs(long long __n)
return (__n < 0LL) ? -__n : __n;
}

static inline void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
typedef int (*compar3)(const void *, const void *, void *);

qsort_r(base, nmemb, size, (compar3)compar, NULL);
}

#ifdef __cplusplus
}
#endif
Expand Down
86 changes: 86 additions & 0 deletions lib/libc/minimal/source/stdlib/qsort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2021 Friedt Professional Engineering Services, Inc
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/util.h>

typedef int (*comp3_t)(const void *, const void *, void *);

/*
* Normally parent is defined parent(k) = floor((k-1) / 2) but we can avoid a
* divide by noticing that floor((k-1) / 2) = ((k - 1) >> 1).
*/

#define parent(k) (((k) - 1) >> 1)
/*
* Normally left is defined left(k) = (2 * k + 1) but we can avoid a
* multiply by noticing that (2 * k + 1) = ((k << 1) + 1).
*/

#define left(k) (((k) << 1) + 1)

/*
* Normally right is defined right(k) = (2 * k + 2) but we can avoid a
* multiply by noticing that right(k) = left(k) + 1
*/
#define right(k) (left(k) + 1)

#define A(k) ((uint8_t *)base + size * (k))

static void sift_down(void *base, int start, int end, size_t size, comp3_t comp, void *comp_arg)
{
int root;
int child;
int swap;

for (swap = start, root = swap; left(root) < end; root = swap) {
child = left(root);

/* if root < left */
if (comp(A(swap), A(child), comp_arg) < 0) {
swap = child;
}

/* right exists and min(A(root),A(left)) < A(right) */
if (right(root) < end && comp(A(swap), A(right(root)), comp_arg) < 0) {
swap = right(root);
}

if (swap == root) {
return;
}

byteswp(A(root), A(swap), size);
}
}

static void heapify(void *base, int nmemb, size_t size, comp3_t comp, void *comp_arg)
{
int start;

for (start = parent(nmemb - 1); start >= 0; --start) {
sift_down(base, start, nmemb, size, comp, comp_arg);
}
}

static void heap_sort(void *base, int nmemb, size_t size, comp3_t comp, void *comp_arg)
{
int end;

heapify(base, nmemb, size, comp, comp_arg);

for (end = nmemb - 1; end > 0; --end) {
byteswp(A(end), A(0), size);
sift_down(base, 0, end, size, comp, comp_arg);
}
}

void qsort_r(void *base, size_t nmemb, size_t size, comp3_t comp, void *arg)
{
heap_sort(base, nmemb, size, comp, arg);
}
2 changes: 1 addition & 1 deletion samples/subsys/shell/shell_module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(shell_module)

target_sources(app PRIVATE src/main.c src/test_module.c)
target_sources_ifdef(CONFIG_SHELL_DYNAMIC_CMDS app PRIVATE src/dynamic_cmd.c src/qsort.c)
target_sources_ifdef(CONFIG_SHELL_DYNAMIC_CMDS app PRIVATE src/dynamic_cmd.c)
target_sources_ifdef(CONFIG_SHELL_BACKEND_SERIAL app PRIVATE src/uart_reinit.c)
202 changes: 0 additions & 202 deletions samples/subsys/shell/shell_module/src/qsort.c

This file was deleted.

11 changes: 10 additions & 1 deletion tests/lib/c_lib/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,14 @@ void test_exit(void)
zassert_equal(a, 0, "exit failed");
}

/**
*
* @brief Test qsort function
*
* @see qsort()
*/
extern void test_qsort(void);

/**
* @}
*/
Expand Down Expand Up @@ -1079,7 +1087,8 @@ void test_main(void)
ztest_unit_test(test_exit),
ztest_unit_test(test_str_operate),
ztest_unit_test(test_tolower_toupper),
ztest_unit_test(test_strtok_r)
ztest_unit_test(test_strtok_r),
ztest_unit_test(test_qsort)
);
ztest_run_test_suite(test_c_lib);
}
Loading