Changeset View
Changeset View
Standalone View
Standalone View
lib/libc/stdlib/qsort.c
Show First 20 Lines • Show All 95 Lines • ▼ Show 20 Lines | |||||
* recursing. Also give them different names for improved debugging. | * recursing. Also give them different names for improved debugging. | ||||
*/ | */ | ||||
#if defined(I_AM_QSORT_R) | #if defined(I_AM_QSORT_R) | ||||
#define local_qsort local_qsort_r | #define local_qsort local_qsort_r | ||||
#elif defined(I_AM_QSORT_S) | #elif defined(I_AM_QSORT_S) | ||||
#define local_qsort local_qsort_s | #define local_qsort local_qsort_s | ||||
#endif | #endif | ||||
static void | static void | ||||
local_qsort(void *a, size_t n, size_t es, cmp_t *cmp, void *thunk) | local_qsort(void *a, size_t n, size_t es, cmp_t *cmp, void *thunk) | ||||
arichardson: Can we change the type of es here instead of adding the cast? | |||||
Done Inline ActionsIt's worse: lib/libc/stdlib/qsort.c:181:10: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'ssize_t' (aka 'long') [-Werror,-Wsign-compare] if (d1 > es) { ~~ ^ ~~ lib/libc/stdlib/qsort.c:184:10: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'ssize_t' (aka 'long') [-Werror,-Wsign-compare] if (d2 > es) { ~~ ^ ~~ lib/libc/stdlib/qsort.c:193:10: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'ssize_t' (aka 'long') [-Werror,-Wsign-compare] if (d2 > es) { ~~ ^ ~~ lib/libc/stdlib/qsort.c:196:10: error: comparison of integers of different signs: 'size_t' (aka 'unsigned long') and 'ssize_t' (aka 'long') [-Werror,-Wsign-compare] if (d1 > es) { ~~ ^ ~~ cem: It's worse:
```
lib/libc/stdlib/qsort.c:181:10: error: comparison of integers of different… | |||||
Done Inline ActionsIn that case, the current patch looks fine. Thanks arichardson: In that case, the current patch looks fine. Thanks | |||||
{ | { | ||||
char *pa, *pb, *pc, *pd, *pl, *pm, *pn; | char *pa, *pb, *pc, *pd, *pl, *pm, *pn; | ||||
size_t d1, d2; | size_t d1, d2; | ||||
int cmp_result; | int cmp_result; | ||||
int swap_cnt; | int swap_cnt; | ||||
loop: | loop: | ||||
swap_cnt = 0; | swap_cnt = 0; | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es) | ||||
pl -= es) | pl -= es) | ||||
swapfunc(pl, pl - es, es); | swapfunc(pl, pl - es, es); | ||||
return; | return; | ||||
} | } | ||||
pn = (char *)a + n * es; | pn = (char *)a + n * es; | ||||
d1 = MIN(pa - (char *)a, pb - pa); | d1 = MIN(pa - (char *)a, pb - pa); | ||||
vecswap(a, pb - d1, d1); | vecswap(a, pb - d1, d1); | ||||
d1 = MIN(pd - pc, pn - pd - es); | /* | ||||
* Cast es to preserve signedness of right-hand side of MIN() | |||||
* expression, to avoid sign ambiguity in the implied comparison. es | |||||
Not Done Inline ActionsPlease use C89 comments, in style. kib: Please use C89 comments, in style. | |||||
* is safely within [0, SSIZE_MAX]. | |||||
*/ | |||||
d1 = MIN(pd - pc, pn - pd - (ssize_t)es); | |||||
vecswap(pb, pn - d1, d1); | vecswap(pb, pn - d1, d1); | ||||
d1 = pb - pa; | d1 = pb - pa; | ||||
d2 = pd - pc; | d2 = pd - pc; | ||||
if (d1 <= d2) { | if (d1 <= d2) { | ||||
/* Recurse on left partition, then iterate on right partition */ | /* Recurse on left partition, then iterate on right partition */ | ||||
if (d1 > es) { | if (d1 > es) { | ||||
local_qsort(a, d1 / es, es, cmp, thunk); | local_qsort(a, d1 / es, es, cmp, thunk); | ||||
▲ Show 20 Lines • Show All 61 Lines • Show Last 20 Lines |
Can we change the type of es here instead of adding the cast?