Skip to content

Commit a04cc49

Browse files
bootstrap::mlibc: remove qsort_r
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent 6340acf commit a04cc49

File tree

1 file changed

+20
-205
lines changed

1 file changed

+20
-205
lines changed

patches/mlibc/mlibc.patch

Lines changed: 20 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
From 6350f126765046a3a97d6acf40dc7d85ccddffad Mon Sep 17 00:00:00 2001
1+
From fb1766aa2abfb310b6c1b0904f849f5d6c8fb62b Mon Sep 17 00:00:00 2001
22
From: Andy-Python-Programmer <[email protected]>
3-
Date: Sat, 11 Mar 2023 15:20:58 +1100
3+
Date: Sat, 11 Mar 2023 16:55:47 +1100
44
Subject: [PATCH] <xxx>
55

66
---
7-
.gitignore | 2 +
8-
options/ansi/generic/stdlib-stubs.cpp | 183 +++++++++++++++++++++++++-
9-
options/glibc/generic/execinfo.cpp | 5 +-
10-
options/rtdl/generic/linker.cpp | 2 +-
11-
sysdeps/aero/generic/aero.cpp | 77 +++++++----
12-
sysdeps/aero/generic/filesystem.cpp | 51 ++++++-
13-
sysdeps/aero/generic/sockets.cpp | 96 +++++++++++++-
14-
sysdeps/aero/generic/time.cpp | 24 ++++
15-
sysdeps/aero/include/aero/syscall.h | 11 ++
16-
sysdeps/aero/meson.build | 1 +
17-
10 files changed, 406 insertions(+), 46 deletions(-)
7+
.gitignore | 2 +
8+
options/ansi/generic/stdlib-stubs.cpp | 6 +-
9+
options/glibc/generic/execinfo.cpp | 5 +-
10+
options/rtdl/generic/linker.cpp | 2 +-
11+
sysdeps/aero/generic/aero.cpp | 77 +++++++++++++--------
12+
sysdeps/aero/generic/filesystem.cpp | 51 ++++++++++++--
13+
sysdeps/aero/generic/sockets.cpp | 96 ++++++++++++++++++++++++++-
14+
sysdeps/aero/generic/time.cpp | 24 +++++++
15+
sysdeps/aero/include/aero/syscall.h | 11 +++
16+
sysdeps/aero/meson.build | 1 +
17+
10 files changed, 234 insertions(+), 41 deletions(-)
1818
create mode 100644 sysdeps/aero/generic/time.cpp
1919

2020
diff --git a/.gitignore b/.gitignore
@@ -28,204 +28,19 @@ index fdd60a0..9f811f4 100644
2828
+# clangd cache files:
2929
+.cache
3030
diff --git a/options/ansi/generic/stdlib-stubs.cpp b/options/ansi/generic/stdlib-stubs.cpp
31-
index c0e54fe..b988dea 100644
31+
index c0e54fe..ddeec8a 100644
3232
--- a/options/ansi/generic/stdlib-stubs.cpp
3333
+++ b/options/ansi/generic/stdlib-stubs.cpp
34-
@@ -319,13 +319,182 @@ void qsort(void *base, size_t count, size_t size,
35-
}
36-
}
37-
38-
-void qsort_r(void *, size_t, size_t,
39-
- int (*compare)(const void *, const void *, void *),
40-
- void *) {
41-
- (void) compare;
42-
- __ensure(!"Not implemented");
43-
- __builtin_unreachable();
44-
+
45-
+// ---------------- QSORT START ----------------
46-
+/*
47-
+ * Adapted from https://github.com/noporpoise/sort_r, original
48-
+ * copyright follows:
49-
+ *
50-
+ * Isaac Turner 29 April 2014 Public Domain
51-
+ */
52-
+#include <stdlib.h>
53-
+#include <string.h>
54-
+
55-
+#define SORT_R_SWAP(a,b,tmp) ((tmp) = (a), (a) = (b), (b) = (tmp))
56-
+
57-
+/* swap a and b */
58-
+/* a and b must not be equal! */
59-
+static void sort_r_swap(char *__restrict a, char *__restrict b,
60-
+ size_t w)
61-
+{
62-
+ char tmp, *end = a+w;
63-
+ for(; a < end; a++, b++) { SORT_R_SWAP(*a, *b, tmp); }
64-
+}
65-
+
66-
+/* swap a, b iff a>b */
67-
+/* a and b must not be equal! */
68-
+/* __restrict is same as restrict but better support on old machines */
69-
+static int sort_r_cmpswap(char *__restrict a,
70-
+ char *__restrict b, size_t w,
71-
+ int (*compar)(const void *_a,
72-
+ const void *_b,
73-
+ void *_arg),
74-
+ void *arg)
75-
+{
76-
+ if(compar(a, b, arg) > 0) {
77-
+ sort_r_swap(a, b, w);
78-
+ return 1;
79-
+ }
80-
+ return 0;
81-
+}
82-
+
83-
+/*
84-
+ Swap consecutive blocks of bytes of size na and nb starting at memory addr ptr,
85-
+ with the smallest swap so that the blocks are in the opposite order. Blocks may
86-
+ be internally re-ordered e.g.
87-
+ 12345ab -> ab34512
88-
+ 123abc -> abc123
89-
+ 12abcde -> deabc12
90-
+*/
91-
+static void sort_r_swap_blocks(char *ptr, size_t na, size_t nb)
92-
+{
93-
+ if(na > 0 && nb > 0) {
94-
+ if(na > nb) { sort_r_swap(ptr, ptr+na, nb); }
95-
+ else { sort_r_swap(ptr, ptr+nb, na); }
96-
+ }
97-
+}
98-
+
99-
+/* Implement recursive quicksort ourselves */
100-
+/* Note: quicksort is not stable, equivalent values may be swapped */
101-
+void qsort_r(void *base, size_t nel, size_t w,
102-
+ int (*compar)(const void *_a,
103-
+ const void *_b,
104-
+ void *_arg),
105-
+ void *arg)
106-
+{
107-
+ char *b = (char *)base, *end = b + nel*w;
108-
+
109-
+ /* for(size_t i=0; i<nel; i++) {printf("%4i", *(int*)(b + i*sizeof(int)));}
110-
+ printf("\n"); */
111-
+
112-
+ if(nel < 10) {
113-
+ /* Insertion sort for arbitrarily small inputs */
114-
+ char *pi, *pj;
115-
+ for(pi = b+w; pi < end; pi += w) {
116-
+ for(pj = pi; pj > b && sort_r_cmpswap(pj-w,pj,w,compar,arg); pj -= w) {}
117-
+ }
118-
+ }
119-
+ else
120-
+ {
121-
+ /* nel > 6; Quicksort */
122-
+
123-
+ int cmp;
124-
+ char *pl, *ple, *pr, *pre, *pivot;
125-
+ char *last = b+w*(nel-1), *tmp;
126-
+
127-
+ /*
128-
+ Use median of second, middle and second-last items as pivot.
129-
+ First and last may have been swapped with pivot and therefore be extreme
130-
+ */
131-
+ char *l[3];
132-
+ l[0] = b + w;
133-
+ l[1] = b+w*(nel/2);
134-
+ l[2] = last - w;
135-
+
136-
+ /* printf("pivots: %i, %i, %i\n", *(int*)l[0], *(int*)l[1], *(int*)l[2]); */
137-
+
138-
+ if(compar(l[0],l[1],arg) > 0) { SORT_R_SWAP(l[0], l[1], tmp); }
139-
+ if(compar(l[1],l[2],arg) > 0) {
140-
+ SORT_R_SWAP(l[1], l[2], tmp);
141-
+ if(compar(l[0],l[1],arg) > 0) { SORT_R_SWAP(l[0], l[1], tmp); }
142-
+ }
143-
+
144-
+ /* swap mid value (l[1]), and last element to put pivot as last element */
145-
+ if(l[1] != last) { sort_r_swap(l[1], last, w); }
146-
+
147-
+ /*
148-
+ pl is the next item on the left to be compared to the pivot
149-
+ pr is the last item on the right that was compared to the pivot
150-
+ ple is the left position to put the next item that equals the pivot
151-
+ ple is the last right position where we put an item that equals the pivot
152-
+ v- end (beyond the array)
153-
+ EEEEEELLLLLLLLuuuuuuuuGGGGGGGEEEEEEEE.
154-
+ ^- b ^- ple ^- pl ^- pr ^- pre ^- last (where the pivot is)
155-
+ Pivot comparison key:
156-
+ E = equal, L = less than, u = unknown, G = greater than, E = equal
157-
+ */
158-
+ pivot = last;
159-
+ ple = pl = b;
160-
+ pre = pr = last;
161-
+
162-
+ /*
163-
+ Strategy:
164-
+ Loop into the list from the left and right at the same time to find:
165-
+ - an item on the left that is greater than the pivot
166-
+ - an item on the right that is less than the pivot
167-
+ Once found, they are swapped and the loop continues.
168-
+ Meanwhile items that are equal to the pivot are moved to the edges of the
169-
+ array.
170-
+ */
171-
+ while(pl < pr) {
172-
+ /* Move left hand items which are equal to the pivot to the far left.
173-
+ break when we find an item that is greater than the pivot */
174-
+ for(; pl < pr; pl += w) {
175-
+ cmp = compar(pl, pivot, arg);
176-
+ if(cmp > 0) { break; }
177-
+ else if(cmp == 0) {
178-
+ if(ple < pl) { sort_r_swap(ple, pl, w); }
179-
+ ple += w;
180-
+ }
181-
+ }
182-
+ /* break if last batch of left hand items were equal to pivot */
183-
+ if(pl >= pr) { break; }
184-
+ /* Move right hand items which are equal to the pivot to the far right.
185-
+ break when we find an item that is less than the pivot */
186-
+ for(; pl < pr; ) {
187-
+ pr -= w; /* Move right pointer onto an unprocessed item */
188-
+ cmp = compar(pr, pivot, arg);
189-
+ if(cmp == 0) {
190-
+ pre -= w;
191-
+ if(pr < pre) { sort_r_swap(pr, pre, w); }
192-
+ }
193-
+ else if(cmp < 0) {
194-
+ if(pl < pr) { sort_r_swap(pl, pr, w); }
195-
+ pl += w;
196-
+ break;
197-
+ }
198-
+ }
199-
+ }
200-
+
201-
+ pl = pr; /* pr may have gone below pl */
202-
+
203-
+ /*
204-
+ Now we need to go from: EEELLLGGGGEEEE
205-
+ to: LLLEEEEEEEGGGG
206-
+ Pivot comparison key:
207-
+ E = equal, L = less than, u = unknown, G = greater than, E = equal
208-
+ */
209-
+ sort_r_swap_blocks(b, ple-b, pl-ple);
210-
+ sort_r_swap_blocks(pr, pre-pr, end-pre);
211-
+
212-
+ /*for(size_t i=0; i<nel; i++) {printf("%4i", *(int*)(b + i*sizeof(int)));}
213-
+ printf("\n");*/
214-
+
215-
+ qsort_r(b, (pl-ple)/w, w, compar, arg);
216-
+ qsort_r(end-(pre-pr), (pre-pr)/w, w, compar, arg);
217-
+ }
218-
}
219-
+// ---------------- QSORT END ----------------
220-
221-
int abs(int num) {
222-
return num < 0 ? -num : num;
223-
@@ -392,7 +561,7 @@ int mbtowc(wchar_t *__restrict wc, const char *__restrict mb, size_t max_size) {
34+
@@ -391,8 +391,10 @@ int mbtowc(wchar_t *__restrict wc, const char *__restrict mb, size_t max_size) {
35+
mlibc::code_seq<wchar_t> wseq{wc, wc + 1};
22436
mlibc::code_seq<const char> nseq{mb, mb + max_size};
22537
auto e = cc->decode_wtranscode(nseq, wseq, mbtowc_state);
226-
if (e != mlibc::charcode_error::null)
38+
- if (e != mlibc::charcode_error::null)
22739
- __ensure(!"decode_wtranscode() errors are not handled");
40+
+ if (e != mlibc::charcode_error::null) {
41+
+ errno = EILSEQ;
22842
+ return -1;
43+
+ }
22944

23045
return nseq.it - mb;
23146
} else {

0 commit comments

Comments
 (0)