[libc-commits] [libc] [llvm] [libc] Improve qsort (PR #120450)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Wed Dec 18 16:35:32 PST 2024
================
@@ -27,11 +27,46 @@
namespace LIBC_NAMESPACE_DECL {
namespace internal {
+template <typename A, typename F> void sort_inst(A &array, const F &is_less) {
#if LIBC_QSORT_IMPL == LIBC_QSORT_QUICK_SORT
-constexpr auto sort = quick_sort;
+ quick_sort(array, is_less);
#elif LIBC_QSORT_IMPL == LIBC_QSORT_HEAP_SORT
-constexpr auto sort = heap_sort;
+ heap_sort(array, is_less);
#endif
+}
+
+template <typename F>
+void unstable_sort(void *array, size_t array_len, size_t elem_size,
+ const F &is_less) {
+ if (array == nullptr || array_len == 0 || elem_size == 0) {
+ return;
+ }
+
+ uint8_t *array_base = reinterpret_cast<uint8_t *>(array);
+
+ switch (elem_size) {
+ case 4: {
+ auto arr_fixed_size = internal::ArrayFixedSize<4>(array_base, array_len);
+ sort_inst(arr_fixed_size, is_less);
+ return;
+ }
+ case 8: {
+ auto arr_fixed_size = internal::ArrayFixedSize<8>(array_base, array_len);
+ sort_inst(arr_fixed_size, is_less);
+ return;
+ }
+ case 16: {
+ auto arr_fixed_size = internal::ArrayFixedSize<16>(array_base, array_len);
+ sort_inst(arr_fixed_size, is_less);
+ return;
+ }
----------------
michaelrj-google wrote:
that's what `LIBC_QSORT_IMPL` is intended to do, allow us to configure the `qsort` functions to only use heap sort. It might be simpler to make it so that `LIBC_QSORT_IMPL == LIBC_QSORT_QUICK_SORT` goes through these specializations whereas `LIBC_QSORT_IMPL == LIBC_QSORT_HEAP_SORT` just puts it directly through a generic array and `heap_sort`.
https://github.com/llvm/llvm-project/pull/120450
More information about the libc-commits
mailing list