Message ID | 20230711190722.4028821-4-adhemerval.zanella@linaro.org |
---|---|
State | New |
Headers | show |
Series | Use introsort for qsort | expand |
On Tue, Jul 11, 2023 at 2:10 PM Adhemerval Zanella via Libc-alpha <libc-alpha@sourceware.org> wrote: > > --- > stdlib/qsort.c | 33 +++++++++++++++++++++++---------- > 1 file changed, 23 insertions(+), 10 deletions(-) > > diff --git a/stdlib/qsort.c b/stdlib/qsort.c > index 00637208ab..134e784bd1 100644 > --- a/stdlib/qsort.c > +++ b/stdlib/qsort.c > @@ -118,15 +118,28 @@ typedef struct > char *hi; > } stack_node; > > -/* The next 4 #defines implement a very fast in-line stack abstraction. */ > /* The stack needs log (total_elements) entries (we could even subtract > log(MAX_THRESH)). Since total_elements has type size_t, we get as > upper bound for log (total_elements): > bits per byte (CHAR_BIT) * sizeof(size_t). */ > -#define STACK_SIZE (CHAR_BIT * sizeof (size_t)) > -#define PUSH(low, high) ((void) ((top->lo = (low)), (top->hi = (high)), ++top)) > -#define POP(low, high) ((void) (--top, (low = top->lo), (high = top->hi))) > -#define STACK_NOT_EMPTY (stack < top) > +enum { STACK_SIZE = CHAR_BIT * sizeof (size_t) }; > + > +static inline stack_node * > +push (stack_node *top, char *lo, char *hi) > +{ > + top->lo = lo; > + top->hi = hi; > + return ++top; > +} > + > +static inline stack_node * > +pop (stack_node *top, char **lo, char **hi) > +{ > + --top; > + *lo = top->lo; > + *hi = top->hi; > + return top; > +} > > > /* Order size using quicksort. This implementation incorporates > @@ -232,9 +245,9 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, > stack_node stack[STACK_SIZE]; > stack_node *top = stack; > > - PUSH (NULL, NULL); > + top = push (top, NULL, NULL); > You could also drop this push and just set `top = stack + 1;` > - while (STACK_NOT_EMPTY) > + while (stack < top) > { > char *left_ptr; > char *right_ptr; > @@ -299,7 +312,7 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, > { > if ((size_t) (hi - left_ptr) <= max_thresh) > /* Ignore both small partitions. */ > - POP (lo, hi); > + top = pop (top, &lo, &hi); > else > /* Ignore small left partition. */ > lo = left_ptr; > @@ -310,13 +323,13 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, > else if ((right_ptr - lo) > (hi - left_ptr)) > { > /* Push larger left partition indices. */ > - PUSH (lo, right_ptr); > + top = push (top, lo, right_ptr); > lo = left_ptr; > } > else > { > /* Push larger right partition indices. */ > - PUSH (left_ptr, hi); > + top = push (top, left_ptr, hi); > hi = right_ptr; > } > } > -- > 2.34.1 >
On 11/07/23 20:44, Noah Goldstein wrote: > On Tue, Jul 11, 2023 at 2:10 PM Adhemerval Zanella via Libc-alpha > <libc-alpha@sourceware.org> wrote: >> >> --- >> stdlib/qsort.c | 33 +++++++++++++++++++++++---------- >> 1 file changed, 23 insertions(+), 10 deletions(-) >> >> diff --git a/stdlib/qsort.c b/stdlib/qsort.c >> index 00637208ab..134e784bd1 100644 >> --- a/stdlib/qsort.c >> +++ b/stdlib/qsort.c >> @@ -118,15 +118,28 @@ typedef struct >> char *hi; >> } stack_node; >> >> -/* The next 4 #defines implement a very fast in-line stack abstraction. */ >> /* The stack needs log (total_elements) entries (we could even subtract >> log(MAX_THRESH)). Since total_elements has type size_t, we get as >> upper bound for log (total_elements): >> bits per byte (CHAR_BIT) * sizeof(size_t). */ >> -#define STACK_SIZE (CHAR_BIT * sizeof (size_t)) >> -#define PUSH(low, high) ((void) ((top->lo = (low)), (top->hi = (high)), ++top)) >> -#define POP(low, high) ((void) (--top, (low = top->lo), (high = top->hi))) >> -#define STACK_NOT_EMPTY (stack < top) >> +enum { STACK_SIZE = CHAR_BIT * sizeof (size_t) }; >> + >> +static inline stack_node * >> +push (stack_node *top, char *lo, char *hi) >> +{ >> + top->lo = lo; >> + top->hi = hi; >> + return ++top; >> +} >> + >> +static inline stack_node * >> +pop (stack_node *top, char **lo, char **hi) >> +{ >> + --top; >> + *lo = top->lo; >> + *hi = top->hi; >> + return top; >> +} >> >> >> /* Order size using quicksort. This implementation incorporates >> @@ -232,9 +245,9 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, >> stack_node stack[STACK_SIZE]; >> stack_node *top = stack; >> >> - PUSH (NULL, NULL); >> + top = push (top, NULL, NULL); >> > You could also drop this push and just set `top = stack + 1;` Ack.
diff --git a/stdlib/qsort.c b/stdlib/qsort.c index 00637208ab..134e784bd1 100644 --- a/stdlib/qsort.c +++ b/stdlib/qsort.c @@ -118,15 +118,28 @@ typedef struct char *hi; } stack_node; -/* The next 4 #defines implement a very fast in-line stack abstraction. */ /* The stack needs log (total_elements) entries (we could even subtract log(MAX_THRESH)). Since total_elements has type size_t, we get as upper bound for log (total_elements): bits per byte (CHAR_BIT) * sizeof(size_t). */ -#define STACK_SIZE (CHAR_BIT * sizeof (size_t)) -#define PUSH(low, high) ((void) ((top->lo = (low)), (top->hi = (high)), ++top)) -#define POP(low, high) ((void) (--top, (low = top->lo), (high = top->hi))) -#define STACK_NOT_EMPTY (stack < top) +enum { STACK_SIZE = CHAR_BIT * sizeof (size_t) }; + +static inline stack_node * +push (stack_node *top, char *lo, char *hi) +{ + top->lo = lo; + top->hi = hi; + return ++top; +} + +static inline stack_node * +pop (stack_node *top, char **lo, char **hi) +{ + --top; + *lo = top->lo; + *hi = top->hi; + return top; +} /* Order size using quicksort. This implementation incorporates @@ -232,9 +245,9 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, stack_node stack[STACK_SIZE]; stack_node *top = stack; - PUSH (NULL, NULL); + top = push (top, NULL, NULL); - while (STACK_NOT_EMPTY) + while (stack < top) { char *left_ptr; char *right_ptr; @@ -299,7 +312,7 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, { if ((size_t) (hi - left_ptr) <= max_thresh) /* Ignore both small partitions. */ - POP (lo, hi); + top = pop (top, &lo, &hi); else /* Ignore small left partition. */ lo = left_ptr; @@ -310,13 +323,13 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, else if ((right_ptr - lo) > (hi - left_ptr)) { /* Push larger left partition indices. */ - PUSH (lo, right_ptr); + top = push (top, lo, right_ptr); lo = left_ptr; } else { /* Push larger right partition indices. */ - PUSH (left_ptr, hi); + top = push (top, left_ptr, hi); hi = right_ptr; } }