From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1791) id 81E8E3858281; Tue, 31 Oct 2023 17:23:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 81E8E3858281 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1698773035; bh=1+04jhs43eTqAbfUSu8nSr1BKBemxTcEfE7qlIEqmAg=; h=From:To:Subject:Date:From; b=W6vxDilE/jQYBNHv62Jz/GpT/ySXmeSPx7jrKmLwHowRsYCdEDxSG0+bJp0MYwCZ1 ZGDIriAnZTJUOEIszSwFPMoNkBRWPuqD9/lpVpqgrhVV50ra+ScOs2hRqc+P61DKbj vjG/ZED6wewUGF8dUayxSe8quxo8W6KIYDXGu2Kw= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Adhemerval Zanella To: glibc-cvs@sourceware.org Subject: [glibc] stdlib: qsort: Move some macros to inline function X-Act-Checkin: glibc X-Git-Author: Adhemerval Zanella X-Git-Refname: refs/heads/master X-Git-Oldrev: a035a9857e11faf16ed021b5e80faf215262afd1 X-Git-Newrev: d097f3c79be55d646d86efb7ce876bf84d5ebe4e Message-Id: <20231031172355.81E8E3858281@sourceware.org> Date: Tue, 31 Oct 2023 17:23:55 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=d097f3c79be55d646d86efb7ce876bf84d5ebe4e commit d097f3c79be55d646d86efb7ce876bf84d5ebe4e Author: Adhemerval Zanella Date: Tue Oct 3 09:22:48 2023 -0300 stdlib: qsort: Move some macros to inline function Reviewed-by: Noah Goldstein Diff: --- stdlib/qsort.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/stdlib/qsort.c b/stdlib/qsort.c index 5691249a9b..80706b3357 100644 --- a/stdlib/qsort.c +++ b/stdlib/qsort.c @@ -100,15 +100,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; +} static inline void @@ -212,11 +225,9 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, char *lo = base_ptr; char *hi = &lo[size * (total_elems - 1)]; stack_node stack[STACK_SIZE]; - stack_node *top = stack; - - PUSH (NULL, NULL); + stack_node *top = stack + 1; - while (STACK_NOT_EMPTY) + while (stack < top) { char *left_ptr; char *right_ptr; @@ -281,7 +292,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; @@ -292,13 +303,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; } }