From patchwork Tue Oct 3 12:22:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 728727 Delivered-To: patch@linaro.org Received: by 2002:a5d:60c8:0:b0:31d:da82:a3b4 with SMTP id x8csp2112537wrt; Tue, 3 Oct 2023 05:23:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHQGCwQIMtersRoaWiJNwAt4Ga2YwcRiA3LqZd/o677RrloScXiycsGun4mKI1tERl7QAeZ X-Received: by 2002:a17:906:5daa:b0:9b2:b763:8d6b with SMTP id n10-20020a1709065daa00b009b2b7638d6bmr11603954ejv.50.1696335798509; Tue, 03 Oct 2023 05:23:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696335798; cv=none; d=google.com; s=arc-20160816; b=CVyH7ktbiRSCeRtCyEYlAnLJjPRMSiRrbkKgZYJTuTPPZu3rUY6MkCv6tcA0EXnh/E f8E0V/25V6VY9GCx7ekOZRhCDzQpZa3EXtGWNsoESzhhDNeCdci0l8ozQh5FXnUXW7f3 /RnbgPAq6eEq9RBTVESMgYhh+TNq/L9lnfQuBBwlFHvaTzpB5c1Hxn61VuqJqQYYkV2M 0dmK2U7oOqGqkk+ITaD7PikeJD1G66/pcTjZ+UIWATw9em+JNpYu6+9zl2ihazK2UlWx G/wQOKCt4zGcLIICoO6tWazFTW33ZeH7yy74ox0Txx6Ben55qVMurPSEupK6zdHXo0T4 LLHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:dmarc-filter:delivered-to; bh=j99v4iQydOXsc8F79xhW/Un25AOKIPWlheKdtEKl/OA=; fh=ubzLPtaquqORyAJ/TX35zypB35/iXKzZJOEWlgP8mu4=; b=wL3YRf2B3auzWpis25CiyUB5eoc2N/NJpCrH/SmW0VcmXqefw/qVUSTsdliDChmz7G cAdZBWbSOm+MFZTyp1SYvVrAXVMWDSSU5c0irL5AxQlm5wgZoOrS7/HJdlMcoDL/aTj4 a6JKxZV66vSa+THaZX+Rl4yY4C6fpJN8g0uMGBTH2rSFVa6NevRBRFs1xQ21OhvTmDYP SOpUzM84HOy7UO/cSZdTMT/aaCtIXJ5vCUKooZH14mlYQcONiy/XVbFXwLsThdovjD+Z H4KIZT66EoEuDxJ7BBA4J+8MfkeEFGlj6vuTnULtiIEgdZSQgY9egukTgm+A0v5M/nY+ x68Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=waD2ZUOr; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id t13-20020a1709064f0d00b009888cc2ac57si597910eju.284.2023.10.03.05.23.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 05:23:18 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=waD2ZUOr; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 563E4385C6E2 for ; Tue, 3 Oct 2023 12:23:17 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id B4AD23857B98 for ; Tue, 3 Oct 2023 12:23:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B4AD23857B98 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1c77449a6daso7094265ad.0 for ; Tue, 03 Oct 2023 05:23:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696335782; x=1696940582; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=j99v4iQydOXsc8F79xhW/Un25AOKIPWlheKdtEKl/OA=; b=waD2ZUOruC4Ajrl8wHMVlMHZ5PKhMVn+QCU2hdefkLRxbUo40dGEYhIEk1/1rsQB0A cq8zWA8w0YtSWjn8DjBvUZG0c4SPCa0MXVpIXKBaRI8fjeozC4k+mOanBGPR76kHu3Xo TjEPtbrI0EnyS29Lt5KghnWd9UQsjee2yrlzzI/aXCiJbet7dDjZbvofKcWwz2Sht3xA 7i2Y/vc6NvI/eUVVn7iCglt9xMseg/JC9Y1h4XxJYPaBIVW536Mh6Ln8jXmOuaJFWZLZ rSyfebPYCvb/knv8QDimqwjAe9gyJijymsP6oXPQMkn77EWNRavyK/6Tui5k6I54L7ju jOJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696335782; x=1696940582; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=j99v4iQydOXsc8F79xhW/Un25AOKIPWlheKdtEKl/OA=; b=S8yw8TZfFxK1cypEbAKOz/+lxGS00J7cbJHTOGA0f2r4IhYWJMlwbRQBqOSnfrbksG P+qe0DT9Toxkb6e3RcYnLpnt361kG3SY1rBAOeWehATPgTR4H2l9OD9JuFs7RCCgPsuW E4pfxoMNsdn6ppCk6/71pEhF10eiJWueYxHndUWKiJdslXkdndM+/lOv8nm9YVUElso3 Zn3ExPZqGuhpzPG/OyegfqDtdaB37fyosSKUdVM3EV7JQlSDEQhlwqHtnPtHYpUCbbC3 ofro0Kv+FRhipp3Tt/Zfh2PJrwaKVf82zNCiapa2gjVjOY/Dmyop8SDOHbFs/lQKJbEw x58w== X-Gm-Message-State: AOJu0YwR32EjQlC9Iv+tD89intPA6BlBkgmc8VCN/p8uSbch2QieyXBM U61T09RQdWCzYZWphjW7iVec5FSxlD3O2FBd8l4rcw== X-Received: by 2002:a17:902:9b84:b0:1c5:b855:38f with SMTP id y4-20020a1709029b8400b001c5b855038fmr13403156plp.24.1696335782194; Tue, 03 Oct 2023 05:23:02 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:feaf:31ef:b40c:b4e5:77c]) by smtp.gmail.com with ESMTPSA id b1-20020a170902d30100b001c5de2f1686sm1403881plc.99.2023.10.03.05.23.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 05:23:01 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Noah Goldstein , Paul Eggert , Florian Weimer Subject: [PATCH v8 3/7] stdlib: Move insertion sort out qsort Date: Tue, 3 Oct 2023 09:22:47 -0300 Message-Id: <20231003122251.3325435-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231003122251.3325435-1-adhemerval.zanella@linaro.org> References: <20231003122251.3325435-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patch=linaro.org@sourceware.org --- stdlib/qsort.c | 101 ++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 47 deletions(-) Reviewed-by: Noah Goldstein diff --git a/stdlib/qsort.c b/stdlib/qsort.c index 072ccdfb95..5691249a9b 100644 --- a/stdlib/qsort.c +++ b/stdlib/qsort.c @@ -111,6 +111,58 @@ typedef struct #define STACK_NOT_EMPTY (stack < top) +static inline void +insertion_sort_qsort_partitions (void *const pbase, size_t total_elems, + size_t size, enum swap_type_t swap_type, + __compar_d_fn_t cmp, void *arg) +{ + char *base_ptr = (char *) pbase; + char *const end_ptr = &base_ptr[size * (total_elems - 1)]; + char *tmp_ptr = base_ptr; +#define min(x, y) ((x) < (y) ? (x) : (y)) + const size_t max_thresh = MAX_THRESH * size; + char *thresh = min(end_ptr, base_ptr + max_thresh); + char *run_ptr; + + /* Find smallest element in first threshold and place it at the + array's beginning. This is the smallest array element, + and the operation speeds up insertion sort's inner loop. */ + + for (run_ptr = tmp_ptr + size; run_ptr <= thresh; run_ptr += size) + if (cmp (run_ptr, tmp_ptr, arg) < 0) + tmp_ptr = run_ptr; + + if (tmp_ptr != base_ptr) + do_swap (tmp_ptr, base_ptr, size, swap_type); + + /* Insertion sort, running from left-hand-side up to right-hand-side. */ + + run_ptr = base_ptr + size; + while ((run_ptr += size) <= end_ptr) + { + tmp_ptr = run_ptr - size; + while (cmp (run_ptr, tmp_ptr, arg) < 0) + tmp_ptr -= size; + + tmp_ptr += size; + if (tmp_ptr != run_ptr) + { + char *trav; + + trav = run_ptr + size; + while (--trav >= run_ptr) + { + char c = *trav; + char *hi, *lo; + + for (hi = lo = trav; (lo -= size) >= tmp_ptr; hi = lo) + *hi = *lo; + *hi = c; + } + } + } +} + /* Order size using quicksort. This implementation incorporates four optimizations discussed in Sedgewick: @@ -257,51 +309,6 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, for partitions below MAX_THRESH size. BASE_PTR points to the beginning of the array to sort, and END_PTR points at the very last element in the array (*not* one beyond it!). */ - -#define min(x, y) ((x) < (y) ? (x) : (y)) - - { - char *const end_ptr = &base_ptr[size * (total_elems - 1)]; - char *tmp_ptr = base_ptr; - char *thresh = min(end_ptr, base_ptr + max_thresh); - char *run_ptr; - - /* Find smallest element in first threshold and place it at the - array's beginning. This is the smallest array element, - and the operation speeds up insertion sort's inner loop. */ - - for (run_ptr = tmp_ptr + size; run_ptr <= thresh; run_ptr += size) - if ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0) - tmp_ptr = run_ptr; - - if (tmp_ptr != base_ptr) - do_swap (tmp_ptr, base_ptr, size, swap_type); - - /* Insertion sort, running from left-hand-side up to right-hand-side. */ - - run_ptr = base_ptr + size; - while ((run_ptr += size) <= end_ptr) - { - tmp_ptr = run_ptr - size; - while ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, arg) < 0) - tmp_ptr -= size; - - tmp_ptr += size; - if (tmp_ptr != run_ptr) - { - char *trav; - - trav = run_ptr + size; - while (--trav >= run_ptr) - { - char c = *trav; - char *hi, *lo; - - for (hi = lo = trav; (lo -= size) >= tmp_ptr; hi = lo) - *hi = *lo; - *hi = c; - } - } - } - } + insertion_sort_qsort_partitions (pbase, total_elems, size, swap_type, cmp, + arg); }