public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Expose stable sort algorithm to gcc_sort_r and add vec::stablesort
@ 2021-06-10  9:46 Richard Biener
  2021-06-10 11:02 ` Alexander Monakov
  0 siblings, 1 reply; 3+ messages in thread
From: Richard Biener @ 2021-06-10  9:46 UTC (permalink / raw)
  To: gcc-patches; +Cc: amonakov

This makes it possible to apply GCCs stable sort algorithm to vec<>
and also use it with the qsort_r compatible interface.

Alex, any comments?

Bootstrapped & tested on x86_64-unknown-linux-gnu (with some
not here included changes to actually use stablesort)

2021-06-10  Richard Biener  <rguenther@suse.de>

	* system.h (gcc_stablesort_r): Declare.
	* sort.cc (gcc_sort_r): Support stable sort.
	(gcc_stablesort_r): Define.
	* vec.h (vec<>::stablesort): Add.
---
 gcc/sort.cc  | 14 +++++++++++++-
 gcc/system.h |  1 +
 gcc/vec.h    | 24 ++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/gcc/sort.cc b/gcc/sort.cc
index fe499b5ec73..e27b90ebbdd 100644
--- a/gcc/sort.cc
+++ b/gcc/sort.cc
@@ -277,8 +277,12 @@ gcc_sort_r (void *vbase, size_t n, size_t size, sort_r_cmp_fn *cmp, void *data)
 {
   if (n < 2)
     return;
+  size_t nlim = 5;
+  bool stable = (ssize_t) size < 0;
+  if (stable)
+    nlim = 3, size = ~size;
   char *base = (char *)vbase;
-  sort_r_ctx c = {data, cmp, base, n, size, 5};
+  sort_r_ctx c = {data, cmp, base, n, size, nlim};
   long long scratch[32];
   size_t bufsz = (n / 2) * size;
   void *buf = bufsz <= sizeof scratch ? scratch : xmalloc (bufsz);
@@ -296,3 +300,11 @@ gcc_stablesort (void *vbase, size_t n, size_t size, cmp_fn *cmp)
 {
   gcc_qsort (vbase, n, ~size, cmp);
 }
+
+/* Stable sort, signature-compatible to C qsort_r.  */
+void
+gcc_stablesort_r (void *vbase, size_t n, size_t size, sort_r_cmp_fn *cmp,
+		  void *data)
+{
+  gcc_sort_r (vbase, n, ~size, cmp, data);
+}
diff --git a/gcc/system.h b/gcc/system.h
index 3c856266cc2..adde3e264b6 100644
--- a/gcc/system.h
+++ b/gcc/system.h
@@ -1250,6 +1250,7 @@ void gcc_sort_r (void *, size_t, size_t, sort_r_cmp_fn *, void *);
 void gcc_qsort (void *, size_t, size_t, int (*)(const void *, const void *));
 void gcc_stablesort (void *, size_t, size_t,
 		     int (*)(const void *, const void *));
+void gcc_stablesort_r (void *, size_t, size_t, sort_r_cmp_fn *, void *data);
 /* Redirect four-argument qsort calls to gcc_qsort; one-argument invocations
    correspond to vec::qsort, and use C qsort internally.  */
 #define PP_5th(a1, a2, a3, a4, a5, ...) a5
diff --git a/gcc/vec.h b/gcc/vec.h
index 24df2db0eeb..c02a834c171 100644
--- a/gcc/vec.h
+++ b/gcc/vec.h
@@ -612,6 +612,7 @@ public:
   void block_remove (unsigned, unsigned);
   void qsort (int (*) (const void *, const void *));
   void sort (int (*) (const void *, const void *, void *), void *);
+  void stablesort (int (*) (const void *, const void *, void *), void *);
   T *bsearch (const void *key, int (*compar)(const void *, const void *));
   T *bsearch (const void *key,
 	      int (*compar)(const void *, const void *, void *), void *);
@@ -1160,6 +1161,17 @@ vec<T, A, vl_embed>::sort (int (*cmp) (const void *, const void *, void *),
     gcc_sort_r (address (), length (), sizeof (T), cmp, data);
 }
 
+/* Sort the contents of this vector with qsort.  CMP is the comparison
+   function to pass to qsort.  */
+
+template<typename T, typename A>
+inline void
+vec<T, A, vl_embed>::stablesort (int (*cmp) (const void *, const void *,
+					     void *), void *data)
+{
+  if (length () > 1)
+    gcc_stablesort_r (address (), length (), ~sizeof (T), cmp, data);
+}
 
 /* Search the contents of the sorted vector with a binary search.
    CMP is the comparison function to pass to bsearch.  */
@@ -1488,6 +1500,7 @@ public:
   void block_remove (unsigned, unsigned);
   void qsort (int (*) (const void *, const void *));
   void sort (int (*) (const void *, const void *, void *), void *);
+  void stablesort (int (*) (const void *, const void *, void *), void *);
   T *bsearch (const void *key, int (*compar)(const void *, const void *));
   T *bsearch (const void *key,
 	      int (*compar)(const void *, const void *, void *), void *);
@@ -2053,6 +2066,17 @@ vec<T, va_heap, vl_ptr>::sort (int (*cmp) (const void *, const void *,
     m_vec->sort (cmp, data);
 }
 
+/* Sort the contents of this vector with qsort.  CMP is the comparison
+   function to pass to qsort.  */
+
+template<typename T>
+inline void
+vec<T, va_heap, vl_ptr>::stablesort (int (*cmp) (const void *, const void *,
+						 void *), void *data)
+{
+  if (m_vec)
+    m_vec->stablesort (cmp, data);
+}
 
 /* Search the contents of the sorted vector with a binary search.
    CMP is the comparison function to pass to bsearch.  */
-- 
2.26.2

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Expose stable sort algorithm to gcc_sort_r and add vec::stablesort
  2021-06-10  9:46 [PATCH] Expose stable sort algorithm to gcc_sort_r and add vec::stablesort Richard Biener
@ 2021-06-10 11:02 ` Alexander Monakov
  2021-06-11  7:28   ` Richard Biener
  0 siblings, 1 reply; 3+ messages in thread
From: Alexander Monakov @ 2021-06-10 11:02 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches

On Thu, 10 Jun 2021, Richard Biener wrote:

> This makes it possible to apply GCCs stable sort algorithm to vec<>
> and also use it with the qsort_r compatible interface.
> 
> Alex, any comments?

I'm afraid the patch is not correct, see below; (I'll also point out
errors in comments while at it).

> Bootstrapped & tested on x86_64-unknown-linux-gnu (with some
> not here included changes to actually use stablesort)
> 
> 2021-06-10  Richard Biener  <rguenther@suse.de>
> 
> 	* system.h (gcc_stablesort_r): Declare.
> 	* sort.cc (gcc_sort_r): Support stable sort.
> 	(gcc_stablesort_r): Define.
> 	* vec.h (vec<>::stablesort): Add.
> ---
>  gcc/sort.cc  | 14 +++++++++++++-
>  gcc/system.h |  1 +
>  gcc/vec.h    | 24 ++++++++++++++++++++++++
>  3 files changed, 38 insertions(+), 1 deletion(-)
> 
> diff --git a/gcc/sort.cc b/gcc/sort.cc
> index fe499b5ec73..e27b90ebbdd 100644
> --- a/gcc/sort.cc
> +++ b/gcc/sort.cc
> @@ -277,8 +277,12 @@ gcc_sort_r (void *vbase, size_t n, size_t size, sort_r_cmp_fn *cmp, void *data)
>  {
>    if (n < 2)
>      return;
> +  size_t nlim = 5;
> +  bool stable = (ssize_t) size < 0;
> +  if (stable)
> +    nlim = 3, size = ~size;
>    char *base = (char *)vbase;
> -  sort_r_ctx c = {data, cmp, base, n, size, 5};
> +  sort_r_ctx c = {data, cmp, base, n, size, nlim};
>    long long scratch[32];
>    size_t bufsz = (n / 2) * size;
>    void *buf = bufsz <= sizeof scratch ? scratch : xmalloc (bufsz);
> @@ -296,3 +300,11 @@ gcc_stablesort (void *vbase, size_t n, size_t size, cmp_fn *cmp)
>  {
>    gcc_qsort (vbase, n, ~size, cmp);
>  }
> +
> +/* Stable sort, signature-compatible to C qsort_r.  */

"Glibc qsort_r" (no _r variant in C, and BSD signature differs).

> +void
> +gcc_stablesort_r (void *vbase, size_t n, size_t size, sort_r_cmp_fn *cmp,
> +		  void *data)
> +{
> +  gcc_sort_r (vbase, n, ~size, cmp, data);
> +}
> diff --git a/gcc/system.h b/gcc/system.h
> index 3c856266cc2..adde3e264b6 100644
> --- a/gcc/system.h
> +++ b/gcc/system.h
> @@ -1250,6 +1250,7 @@ void gcc_sort_r (void *, size_t, size_t, sort_r_cmp_fn *, void *);
>  void gcc_qsort (void *, size_t, size_t, int (*)(const void *, const void *));
>  void gcc_stablesort (void *, size_t, size_t,
>  		     int (*)(const void *, const void *));
> +void gcc_stablesort_r (void *, size_t, size_t, sort_r_cmp_fn *, void *data);
>  /* Redirect four-argument qsort calls to gcc_qsort; one-argument invocations
>     correspond to vec::qsort, and use C qsort internally.  */
>  #define PP_5th(a1, a2, a3, a4, a5, ...) a5
> diff --git a/gcc/vec.h b/gcc/vec.h
> index 24df2db0eeb..c02a834c171 100644
> --- a/gcc/vec.h
> +++ b/gcc/vec.h
> @@ -612,6 +612,7 @@ public:
>    void block_remove (unsigned, unsigned);
>    void qsort (int (*) (const void *, const void *));
>    void sort (int (*) (const void *, const void *, void *), void *);
> +  void stablesort (int (*) (const void *, const void *, void *), void *);
>    T *bsearch (const void *key, int (*compar)(const void *, const void *));
>    T *bsearch (const void *key,
>  	      int (*compar)(const void *, const void *, void *), void *);
> @@ -1160,6 +1161,17 @@ vec<T, A, vl_embed>::sort (int (*cmp) (const void *, const void *, void *),
>      gcc_sort_r (address (), length (), sizeof (T), cmp, data);
>  }
>  
> +/* Sort the contents of this vector with qsort.  CMP is the comparison
> +   function to pass to qsort.  */

Not with 'qsort', but gcc_stablesort_r.

> +
> +template<typename T, typename A>
> +inline void
> +vec<T, A, vl_embed>::stablesort (int (*cmp) (const void *, const void *,
> +					     void *), void *data)
> +{
> +  if (length () > 1)
> +    gcc_stablesort_r (address (), length (), ~sizeof (T), cmp, data);
> +}

I think this is wrong. You're passing inverted size to gcc_stablesort_r, which
will invert it again, and you end up with normal non-stable sorting function.

With that fixed, I think the patch would be correct.

>  
>  /* Search the contents of the sorted vector with a binary search.
>     CMP is the comparison function to pass to bsearch.  */
> @@ -1488,6 +1500,7 @@ public:
>    void block_remove (unsigned, unsigned);
>    void qsort (int (*) (const void *, const void *));
>    void sort (int (*) (const void *, const void *, void *), void *);
> +  void stablesort (int (*) (const void *, const void *, void *), void *);
>    T *bsearch (const void *key, int (*compar)(const void *, const void *));
>    T *bsearch (const void *key,
>  	      int (*compar)(const void *, const void *, void *), void *);
> @@ -2053,6 +2066,17 @@ vec<T, va_heap, vl_ptr>::sort (int (*cmp) (const void *, const void *,
>      m_vec->sort (cmp, data);
>  }
>  
> +/* Sort the contents of this vector with qsort.  CMP is the comparison
> +   function to pass to qsort.  */

Like above, copy-paste issue in comment.

> +
> +template<typename T>
> +inline void
> +vec<T, va_heap, vl_ptr>::stablesort (int (*cmp) (const void *, const void *,
> +						 void *), void *data)
> +{
> +  if (m_vec)
> +    m_vec->stablesort (cmp, data);
> +}
>  
>  /* Search the contents of the sorted vector with a binary search.
>     CMP is the comparison function to pass to bsearch.  */
> 

Thanks.
Alexander

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Expose stable sort algorithm to gcc_sort_r and add vec::stablesort
  2021-06-10 11:02 ` Alexander Monakov
@ 2021-06-11  7:28   ` Richard Biener
  0 siblings, 0 replies; 3+ messages in thread
From: Richard Biener @ 2021-06-11  7:28 UTC (permalink / raw)
  To: Alexander Monakov; +Cc: gcc-patches

On Thu, 10 Jun 2021, Alexander Monakov wrote:

> On Thu, 10 Jun 2021, Richard Biener wrote:
> 
> > This makes it possible to apply GCCs stable sort algorithm to vec<>
> > and also use it with the qsort_r compatible interface.
> > 
> > Alex, any comments?
> 
> I'm afraid the patch is not correct, see below; (I'll also point out
> errors in comments while at it).

Whoops - thanks for noticing.  This is what you get when copy-editing
adding gcc_stablesort_r in (which I had originally omitted) ...

Fixed, re-bootstrapped and tested on x86_64-unknown-linux-gnu and pushed.

Thanks,
Richard.

From 0a9a35b9b07dfc82239545ec43dacbc9091543fa Mon Sep 17 00:00:00 2001
From: Richard Biener <rguenther@suse.de>
Date: Thu, 10 Jun 2021 11:03:55 +0200
Subject: [PATCH] Expose stable sort algorithm to gcc_sort_r and add
 vec::stablesort
To: gcc-patches@gcc.gnu.org

This makes it possible to apply GCCs stable sort algorithm to vec<>
and also use it with the qsort_r compatible interface.

2021-06-10  Richard Biener  <rguenther@suse.de>

	* system.h (gcc_stablesort_r): Declare.
	* sort.cc (gcc_sort_r): Support stable sort.
	(gcc_stablesort_r): Define.
	* vec.h (vec<>::stablesort): Add.
---
 gcc/sort.cc  | 14 +++++++++++++-
 gcc/system.h |  1 +
 gcc/vec.h    | 24 ++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/gcc/sort.cc b/gcc/sort.cc
index fe499b5ec73..1c83c62008d 100644
--- a/gcc/sort.cc
+++ b/gcc/sort.cc
@@ -277,8 +277,12 @@ gcc_sort_r (void *vbase, size_t n, size_t size, sort_r_cmp_fn *cmp, void *data)
 {
   if (n < 2)
     return;
+  size_t nlim = 5;
+  bool stable = (ssize_t) size < 0;
+  if (stable)
+    nlim = 3, size = ~size;
   char *base = (char *)vbase;
-  sort_r_ctx c = {data, cmp, base, n, size, 5};
+  sort_r_ctx c = {data, cmp, base, n, size, nlim};
   long long scratch[32];
   size_t bufsz = (n / 2) * size;
   void *buf = bufsz <= sizeof scratch ? scratch : xmalloc (bufsz);
@@ -296,3 +300,11 @@ gcc_stablesort (void *vbase, size_t n, size_t size, cmp_fn *cmp)
 {
   gcc_qsort (vbase, n, ~size, cmp);
 }
+
+/* Stable sort, signature-compatible to Glibc qsort_r.  */
+void
+gcc_stablesort_r (void *vbase, size_t n, size_t size, sort_r_cmp_fn *cmp,
+		  void *data)
+{
+  gcc_sort_r (vbase, n, ~size, cmp, data);
+}
diff --git a/gcc/system.h b/gcc/system.h
index 3c856266cc2..adde3e264b6 100644
--- a/gcc/system.h
+++ b/gcc/system.h
@@ -1250,6 +1250,7 @@ void gcc_sort_r (void *, size_t, size_t, sort_r_cmp_fn *, void *);
 void gcc_qsort (void *, size_t, size_t, int (*)(const void *, const void *));
 void gcc_stablesort (void *, size_t, size_t,
 		     int (*)(const void *, const void *));
+void gcc_stablesort_r (void *, size_t, size_t, sort_r_cmp_fn *, void *data);
 /* Redirect four-argument qsort calls to gcc_qsort; one-argument invocations
    correspond to vec::qsort, and use C qsort internally.  */
 #define PP_5th(a1, a2, a3, a4, a5, ...) a5
diff --git a/gcc/vec.h b/gcc/vec.h
index 24df2db0eeb..193377cb69c 100644
--- a/gcc/vec.h
+++ b/gcc/vec.h
@@ -612,6 +612,7 @@ public:
   void block_remove (unsigned, unsigned);
   void qsort (int (*) (const void *, const void *));
   void sort (int (*) (const void *, const void *, void *), void *);
+  void stablesort (int (*) (const void *, const void *, void *), void *);
   T *bsearch (const void *key, int (*compar)(const void *, const void *));
   T *bsearch (const void *key,
 	      int (*compar)(const void *, const void *, void *), void *);
@@ -1160,6 +1161,17 @@ vec<T, A, vl_embed>::sort (int (*cmp) (const void *, const void *, void *),
     gcc_sort_r (address (), length (), sizeof (T), cmp, data);
 }
 
+/* Sort the contents of this vector with gcc_stablesort_r.  CMP is the
+   comparison function to pass to qsort.  */
+
+template<typename T, typename A>
+inline void
+vec<T, A, vl_embed>::stablesort (int (*cmp) (const void *, const void *,
+					     void *), void *data)
+{
+  if (length () > 1)
+    gcc_stablesort_r (address (), length (), sizeof (T), cmp, data);
+}
 
 /* Search the contents of the sorted vector with a binary search.
    CMP is the comparison function to pass to bsearch.  */
@@ -1488,6 +1500,7 @@ public:
   void block_remove (unsigned, unsigned);
   void qsort (int (*) (const void *, const void *));
   void sort (int (*) (const void *, const void *, void *), void *);
+  void stablesort (int (*) (const void *, const void *, void *), void *);
   T *bsearch (const void *key, int (*compar)(const void *, const void *));
   T *bsearch (const void *key,
 	      int (*compar)(const void *, const void *, void *), void *);
@@ -2053,6 +2066,17 @@ vec<T, va_heap, vl_ptr>::sort (int (*cmp) (const void *, const void *,
     m_vec->sort (cmp, data);
 }
 
+/* Sort the contents of this vector with gcc_stablesort_r.  CMP is the
+   comparison function to pass to qsort.  */
+
+template<typename T>
+inline void
+vec<T, va_heap, vl_ptr>::stablesort (int (*cmp) (const void *, const void *,
+						 void *), void *data)
+{
+  if (m_vec)
+    m_vec->stablesort (cmp, data);
+}
 
 /* Search the contents of the sorted vector with a binary search.
    CMP is the comparison function to pass to bsearch.  */
-- 
2.26.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-06-11  7:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-10  9:46 [PATCH] Expose stable sort algorithm to gcc_sort_r and add vec::stablesort Richard Biener
2021-06-10 11:02 ` Alexander Monakov
2021-06-11  7:28   ` Richard Biener

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).