public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org
Subject: [PATCH v5 3/6] stdlib: qsort: Move some macros to inline function
Date: Thu, 13 Jul 2023 10:25:37 -0300	[thread overview]
Message-ID: <20230713132540.2854320-4-adhemerval.zanella@linaro.org> (raw)
In-Reply-To: <20230713132540.2854320-1-adhemerval.zanella@linaro.org>

---
 stdlib/qsort.c | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)

diff --git a/stdlib/qsort.c b/stdlib/qsort.c
index 410a8d7731..53013ef281 100644
--- a/stdlib/qsort.c
+++ b/stdlib/qsort.c
@@ -114,15 +114,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
@@ -226,11 +239,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;
@@ -295,7 +306,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;
@@ -306,13 +317,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


  parent reply	other threads:[~2023-07-13 13:25 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-13 13:25 [PATCH v5 0/6] Use introsort for qsort Adhemerval Zanella
2023-07-13 13:25 ` [PATCH v5 1/6] stdlib: Optimization qsort{_r} swap implementation Adhemerval Zanella
2023-07-13 22:17   ` Paul Eggert
2023-07-14  1:01     ` Paul Eggert
2023-07-14 12:06       ` Adhemerval Zanella Netto
2023-07-17 16:57   ` Alexander Monakov
2023-07-17 17:29     ` Paul Eggert
2023-07-17 18:07       ` Adhemerval Zanella Netto
2023-07-17 18:58         ` Paul Eggert
2023-07-17 19:06           ` Adhemerval Zanella Netto
2023-07-18 14:01     ` Adhemerval Zanella Netto
2023-07-18 14:13       ` Adhemerval Zanella Netto
2023-07-13 13:25 ` [PATCH v5 2/6] stdlib: Move insertion sort out qsort Adhemerval Zanella
2023-07-13 13:25 ` Adhemerval Zanella [this message]
2023-07-13 13:25 ` [PATCH v5 4/6] stdlib: Implement introsort with qsort (BZ 19305) Adhemerval Zanella
2023-07-13 13:25 ` [PATCH v5 5/6] stdlib: Remove use of mergesort on qsort (BZ 21719) Adhemerval Zanella
2023-07-13 13:25 ` [PATCH v5 6/6] stdlib: Add more qsort{_r} coverage Adhemerval Zanella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230713132540.2854320-4-adhemerval.zanella@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).