authorgravatar for me@gasinfinity.devGasInfinity <me@gasinfinity.dev> 2026-01-01 13:17:26+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-07 00:06:57+01:00
logfa625e878f3f8f8b2dbc1d8ca0b761fb65326d02
treec7cc41038f5eaef25a954644cef134e51543ec08
parent6412aebc9025fef3ae191101abd266a2ea5079b1

feat(libzigc): add `qsort`

* also remove musl implementation

5 files changed, 44 insertions(+), 239 deletions(-)

lib/c/stdlib.zig+44
...@@ -8,6 +8,9 @@ comptime {...@@ -8,6 +8,9 @@ comptime {
8 @export(&abs, .{ .name = "abs", .linkage = common.linkage, .visibility = common.visibility });8 @export(&abs, .{ .name = "abs", .linkage = common.linkage, .visibility = common.visibility });
9 @export(&labs, .{ .name = "labs", .linkage = common.linkage, .visibility = common.visibility });9 @export(&labs, .{ .name = "labs", .linkage = common.linkage, .visibility = common.visibility });
10 @export(&llabs, .{ .name = "llabs", .linkage = common.linkage, .visibility = common.visibility });10 @export(&llabs, .{ .name = "llabs", .linkage = common.linkage, .visibility = common.visibility });
11
12 @export(&qsort_r, .{ .name = "qsort_r", .linkage = common.linkage, .visibility = common.visibility });
13 @export(&qsort, .{ .name = "qsort", .linkage = common.linkage, .visibility = common.visibility });
11 }14 }
12}15}
1316
...@@ -23,6 +26,47 @@ fn llabs(a: c_longlong) callconv(.c) c_longlong {...@@ -23,6 +26,47 @@ fn llabs(a: c_longlong) callconv(.c) c_longlong {
23 return @intCast(@abs(a));26 return @intCast(@abs(a));
24}27}
2528
29// NOTE: Despite its name, `qsort` doesn't have to use quicksort or make any complexity or stability guarantee.
30fn qsort_r(base: *anyopaque, n: usize, size: usize, compare: *const fn (a: *const anyopaque, b: *const anyopaque, arg: ?*anyopaque) callconv(.c) c_int, arg: ?*anyopaque) callconv(.c) void {
31 const Context = struct {
32 base: [*]u8,
33 size: usize,
34 compare: *const fn (a: *const anyopaque, b: *const anyopaque, arg: ?*anyopaque) callconv(.c) c_int,
35 arg: ?*anyopaque,
36
37 pub fn lessThan(ctx: @This(), a: usize, b: usize) bool {
38 return ctx.compare(&ctx.base[a * ctx.size], &ctx.base[b * ctx.size], ctx.arg) < 0;
39 }
40
41 pub fn swap(ctx: @This(), a: usize, b: usize) void {
42 const a_bytes: []u8 = ctx.base[a * ctx.size ..][0..ctx.size];
43 const b_bytes: []u8 = ctx.base[b * ctx.size ..][0..ctx.size];
44
45 for (a_bytes, b_bytes) |*ab, *bb| {
46 const tmp = ab.*;
47 ab.* = bb.*;
48 bb.* = tmp;
49 }
50 }
51 };
52
53 std.mem.sortUnstableContext(0, n, Context{
54 .base = @ptrCast(base),
55 .size = size,
56 .compare = compare,
57 .arg = arg,
58 });
59}
60
61fn qsort(base: *anyopaque, n: usize, size: usize, compare: *const fn (a: *const anyopaque, b: *const anyopaque) callconv(.c) c_int) callconv(.c) void {
62 return qsort_r(base, n, size, (struct {
63 fn wrap(a: *const anyopaque, b: *const anyopaque, arg: ?*anyopaque) callconv(.c) c_int {
64 const cmp: *const fn (a: *const anyopaque, b: *const anyopaque) callconv(.c) c_int = @ptrCast(@alignCast(arg.?));
65 return cmp(a, b);
66 }
67 }).wrap, @constCast(compare));
68}
69
26test abs {70test abs {
27 const val: c_int = -10;71 const val: c_int = -10;
28 try std.testing.expectEqual(10, abs(val));72 try std.testing.expectEqual(10, abs(val));
lib/libc/musl/src/stdlib/qsort.c deleted-221
...@@ -1,221 +0,0 @@
1/* Copyright (C) 2011 by Valentin Ochs
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22/* Minor changes by Rich Felker for integration in musl, 2011-04-27. */
23
24/* Smoothsort, an adaptive variant of Heapsort. Memory usage: O(1).
25 Run time: Worst case O(n log n), close to O(n) in the mostly-sorted case. */
26
27#define _BSD_SOURCE
28#include <stdint.h>
29#include <stdlib.h>
30#include <string.h>
31
32#include "atomic.h"
33#define ntz(x) a_ctz_l((x))
34
35typedef int (*cmpfun)(const void *, const void *, void *);
36
37static inline int pntz(size_t p[2]) {
38 int r = ntz(p[0] - 1);
39 if(r != 0 || (r = 8*sizeof(size_t) + ntz(p[1])) != 8*sizeof(size_t)) {
40 return r;
41 }
42 return 0;
43}
44
45static void cycle(size_t width, unsigned char* ar[], int n)
46{
47 unsigned char tmp[256];
48 size_t l;
49 int i;
50
51 if(n < 2) {
52 return;
53 }
54
55 ar[n] = tmp;
56 while(width) {
57 l = sizeof(tmp) < width ? sizeof(tmp) : width;
58 memcpy(ar[n], ar[0], l);
59 for(i = 0; i < n; i++) {
60 memcpy(ar[i], ar[i + 1], l);
61 ar[i] += l;
62 }
63 width -= l;
64 }
65}
66
67/* shl() and shr() need n > 0 */
68static inline void shl(size_t p[2], int n)
69{
70 if(n >= 8 * sizeof(size_t)) {
71 n -= 8 * sizeof(size_t);
72 p[1] = p[0];
73 p[0] = 0;
74 }
75 p[1] <<= n;
76 p[1] |= p[0] >> (sizeof(size_t) * 8 - n);
77 p[0] <<= n;
78}
79
80static inline void shr(size_t p[2], int n)
81{
82 if(n >= 8 * sizeof(size_t)) {
83 n -= 8 * sizeof(size_t);
84 p[0] = p[1];
85 p[1] = 0;
86 }
87 p[0] >>= n;
88 p[0] |= p[1] << (sizeof(size_t) * 8 - n);
89 p[1] >>= n;
90}
91
92static void sift(unsigned char *head, size_t width, cmpfun cmp, void *arg, int pshift, size_t lp[])
93{
94 unsigned char *rt, *lf;
95 unsigned char *ar[14 * sizeof(size_t) + 1];
96 int i = 1;
97
98 ar[0] = head;
99 while(pshift > 1) {
100 rt = head - width;
101 lf = head - width - lp[pshift - 2];
102
103 if(cmp(ar[0], lf, arg) >= 0 && cmp(ar[0], rt, arg) >= 0) {
104 break;
105 }
106 if(cmp(lf, rt, arg) >= 0) {
107 ar[i++] = lf;
108 head = lf;
109 pshift -= 1;
110 } else {
111 ar[i++] = rt;
112 head = rt;
113 pshift -= 2;
114 }
115 }
116 cycle(width, ar, i);
117}
118
119static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, size_t pp[2], int pshift, int trusty, size_t lp[])
120{
121 unsigned char *stepson,
122 *rt, *lf;
123 size_t p[2];
124 unsigned char *ar[14 * sizeof(size_t) + 1];
125 int i = 1;
126 int trail;
127
128 p[0] = pp[0];
129 p[1] = pp[1];
130
131 ar[0] = head;
132 while(p[0] != 1 || p[1] != 0) {
133 stepson = head - lp[pshift];
134 if(cmp(stepson, ar[0], arg) <= 0) {
135 break;
136 }
137 if(!trusty && pshift > 1) {
138 rt = head - width;
139 lf = head - width - lp[pshift - 2];
140 if(cmp(rt, stepson, arg) >= 0 || cmp(lf, stepson, arg) >= 0) {
141 break;
142 }
143 }
144
145 ar[i++] = stepson;
146 head = stepson;
147 trail = pntz(p);
148 shr(p, trail);
149 pshift += trail;
150 trusty = 0;
151 }
152 if(!trusty) {
153 cycle(width, ar, i);
154 sift(head, width, cmp, arg, pshift, lp);
155 }
156}
157
158void __qsort_r(void *base, size_t nel, size_t width, cmpfun cmp, void *arg)
159{
160 size_t lp[12*sizeof(size_t)];
161 size_t i, size = width * nel;
162 unsigned char *head, *high;
163 size_t p[2] = {1, 0};
164 int pshift = 1;
165 int trail;
166
167 if (!size) return;
168
169 head = base;
170 high = head + size - width;
171
172 /* Precompute Leonardo numbers, scaled by element width */
173 for(lp[0]=lp[1]=width, i=2; (lp[i]=lp[i-2]+lp[i-1]+width) < size; i++);
174
175 while(head < high) {
176 if((p[0] & 3) == 3) {
177 sift(head, width, cmp, arg, pshift, lp);
178 shr(p, 2);
179 pshift += 2;
180 } else {
181 if(lp[pshift - 1] >= high - head) {
182 trinkle(head, width, cmp, arg, p, pshift, 0, lp);
183 } else {
184 sift(head, width, cmp, arg, pshift, lp);
185 }
186
187 if(pshift == 1) {
188 shl(p, 1);
189 pshift = 0;
190 } else {
191 shl(p, pshift - 1);
192 pshift = 1;
193 }
194 }
195
196 p[0] |= 1;
197 head += width;
198 }
199
200 trinkle(head, width, cmp, arg, p, pshift, 0, lp);
201
202 while(pshift != 1 || p[0] != 1 || p[1] != 0) {
203 if(pshift <= 1) {
204 trail = pntz(p);
205 shr(p, trail);
206 pshift += trail;
207 } else {
208 shl(p, 2);
209 pshift -= 2;
210 p[0] ^= 7;
211 shr(p, 1);
212 trinkle(head - lp[pshift] - width, width, cmp, arg, p, pshift + 1, 1, lp);
213 shl(p, 1);
214 p[0] |= 1;
215 trinkle(head - width, width, cmp, arg, p, pshift, 1, lp);
216 }
217 head -= width;
218 }
219}
220
221weak_alias(__qsort_r, qsort_r);
lib/libc/musl/src/stdlib/qsort_nr.c deleted-14
...@@ -1,14 +0,0 @@
1#define _BSD_SOURCE
2#include <stdlib.h>
3
4typedef int (*cmpfun)(const void *, const void *);
5
6static int wrapper_cmp(const void *v1, const void *v2, void *cmp)
7{
8 return ((cmpfun)cmp)(v1, v2);
9}
10
11void qsort(void *base, size_t nel, size_t width, cmpfun cmp)
12{
13 __qsort_r(base, nel, width, wrapper_cmp, (void *)cmp);
14}
src/libs/musl.zig-2
...@@ -1725,8 +1725,6 @@ const src_files = [_][]const u8{...@@ -1725,8 +1725,6 @@ const src_files = [_][]const u8{
1725 "musl/src/stdlib/imaxdiv.c",1725 "musl/src/stdlib/imaxdiv.c",
1726 "musl/src/stdlib/ldiv.c",1726 "musl/src/stdlib/ldiv.c",
1727 "musl/src/stdlib/lldiv.c",1727 "musl/src/stdlib/lldiv.c",
1728 "musl/src/stdlib/qsort.c",
1729 "musl/src/stdlib/qsort_nr.c",
1730 "musl/src/stdlib/strtod.c",1728 "musl/src/stdlib/strtod.c",
1731 "musl/src/stdlib/strtol.c",1729 "musl/src/stdlib/strtol.c",
1732 "musl/src/stdlib/wcstod.c",1730 "musl/src/stdlib/wcstod.c",
src/libs/wasi_libc.zig-2
...@@ -1015,8 +1015,6 @@ const libc_top_half_src_files = [_][]const u8{...@@ -1015,8 +1015,6 @@ const libc_top_half_src_files = [_][]const u8{
1015 "musl/src/stdlib/imaxdiv.c",1015 "musl/src/stdlib/imaxdiv.c",
1016 "musl/src/stdlib/ldiv.c",1016 "musl/src/stdlib/ldiv.c",
1017 "musl/src/stdlib/lldiv.c",1017 "musl/src/stdlib/lldiv.c",
1018 "musl/src/stdlib/qsort.c",
1019 "musl/src/stdlib/qsort_nr.c",
1020 "musl/src/stdlib/strtol.c",1018 "musl/src/stdlib/strtol.c",
1021 "musl/src/string/bcopy.c",1019 "musl/src/string/bcopy.c",
1022 "musl/src/string/explicit_bzero.c",1020 "musl/src/string/explicit_bzero.c",