From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 5F96F3846403 for ; Fri, 11 Jun 2021 07:29:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5F96F3846403 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 398661FD3F; Fri, 11 Jun 2021 07:28:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1623396539; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=dL6vN+d8gl+My7akKQZxGqZ/vXdNf1DhKA2IFiw/ipA=; b=0QiHRuj6T0Db+NdPusdDJbfN9e77u653orhkzFX6hNcHvTwPmArDHIxBh2nd/3JNCp/6tW JH1ZyfGAxsyH5PSGYJxsnRuEbWlKRPXMmC+NDhyuv/FqWN3Sh1nVvcMP6lG7Hqh70s9hf/ hYEOGPSCocXiApP8i7gCP1PKsqfgYz0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1623396539; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=dL6vN+d8gl+My7akKQZxGqZ/vXdNf1DhKA2IFiw/ipA=; b=hxG6TbfYEmOLPxJ361S4m4BBHANE1MSfcFV3/T3JO8RzegCt7E2X+zxrz1HSQwEdBrRTYc 2E4t2LyDEhdkTgBA== Received: from [10.163.28.26] (unknown [10.163.28.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 260DFA3B8C; Fri, 11 Jun 2021 07:28:59 +0000 (UTC) Date: Fri, 11 Jun 2021 09:28:58 +0200 (CEST) From: Richard Biener To: Alexander Monakov cc: gcc-patches@gcc.gnu.org Subject: Re: [PATCH] Expose stable sort algorithm to gcc_sort_r and add vec::stablesort In-Reply-To: Message-ID: References: <7o7317rs-2434-80r3-n244-8032po89786@fhfr.qr> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 11 Jun 2021 07:29:01 -0000 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 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 * 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::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 +inline void +vec::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::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 +inline void +vec::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