From patchwork Thu Jun 10 09:46:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1490329 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=RkdsfKn8; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=rPOhEhT+; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4G0zh73vMwz9sW7 for ; Thu, 10 Jun 2021 19:48:54 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 288343954C16 for ; Thu, 10 Jun 2021 09:48:52 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id B6A2D383540B for ; Thu, 10 Jun 2021 09:46:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B6A2D383540B 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 819931FD37; Thu, 10 Jun 2021 09:46:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1623318394; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=vlecNGWBUE1ubdV39E95LHD5RtmsKblTsMvjQr0uXJU=; b=RkdsfKn8zI/rXBkf1V918X7qfMxn44NEm/YSna/zhFxFtlCAsRfUsVhZtutU5TKRP5a5q2 lnLHZx5cm6MtcPsVNalsDakOdX3O6HnahcmncnzRNUQE5FeUM/4nZKWQAiW4LfooMS6NYu +uc0bniVvj6ncv4lWxMmmlTClh39nhI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1623318394; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=vlecNGWBUE1ubdV39E95LHD5RtmsKblTsMvjQr0uXJU=; b=rPOhEhT+CH865F2qTO62ekpisxWPLTrohHjsAwOd9MPL4O6BTZ/YvHoPsigWTZ5YyT6fvq j+PGnaksguBt6aBA== Received: from [10.163.41.62] (unknown [10.163.41.62]) (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 6115DA3B8A; Thu, 10 Jun 2021 09:46:34 +0000 (UTC) Date: Thu, 10 Jun 2021 11:46:33 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] Expose stable sort algorithm to gcc_sort_r and add vec::stablesort Message-ID: <7o7317rs-2434-80r3-n244-8032po89786@fhfr.qr> MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 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: , Cc: amonakov@ispras.ru Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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 * 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::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 +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 qsort. 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. */