From patchwork Thu Jul 13 13:25:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 72646 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 69CA43852649 for ; Thu, 13 Jul 2023 13:26:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 69CA43852649 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689254819; bh=q41TpCNKw5FqcGvI6dpTutH1CAn5jxmb3xpnBpufo3E=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=dd0gA01jIbmaSGAuOr6whu9VuA7aDNpDRUFiq9HzMaQ5L5Z9iZ31LtoO0nTaOBXEH zN4XIxkxUUUHJqC2bZhgHwFYJ9hPkeARYOz2vOOVT2LeFwFuftbzKRx5YjJLzOuwtK sbb7FvbKOnnRku16ntTuQM3GEViV1OGbj51fqHhs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x30.google.com (mail-oa1-x30.google.com [IPv6:2001:4860:4864:20::30]) by sourceware.org (Postfix) with ESMTPS id DE5B53858C74 for ; Thu, 13 Jul 2023 13:25:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DE5B53858C74 Received: by mail-oa1-x30.google.com with SMTP id 586e51a60fabf-1b056276889so555035fac.2 for ; Thu, 13 Jul 2023 06:25:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689254746; x=1691846746; 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=q41TpCNKw5FqcGvI6dpTutH1CAn5jxmb3xpnBpufo3E=; b=h4buwLJ9CL+z1O5TXyc6RN0OqgqLbrf+VhOlku50xQd+sZMrervSxuLpJbrL2hK5oA T5qmup1bcFhdRpkqmrNElk/4Wp/VJsdLme3Ak1Jh6+t7CfTOvY2uRaXuMRzOjAcupUYw 4JYpR+4kzcSFMLYaynaawQcXyrGnHYkBMgAbkUYwB6bBuPuf+0/jb/WYStYJGRirqKC+ yyaZQOziNAAdX3fWda7Ef+xVPe9f+4FOsVun5+3tQyniBAOwogj36RTsRdU2Ofj22vAg UELVHE7uE93M8SYOkkAP5w14Ey20PtjS/++wZfKCyGlPVNUk3OGYk8ChexuLezTt2yIL C7nw== X-Gm-Message-State: ABy/qLaJwluqFTfJr2DrelykE8/zr9/EqjnCtitrJCZU6u67Q6xurwx9 3zSaUP6/R/iqyMxbv72JUN03utaCAL8htCc7OSm1MQ== X-Google-Smtp-Source: APBJJlGEf4/xQV2aAUScUfnhpR/plhWfL27DF5eWFb1NtWVlMppbZPV5FHVdu9RauJLYme5ZJ6z6XA== X-Received: by 2002:a05:6870:960d:b0:1b0:3a8c:a807 with SMTP id d13-20020a056870960d00b001b03a8ca807mr2706375oaq.14.1689254746288; Thu, 13 Jul 2023 06:25:46 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:5656:8d9b:78e7:d879:79cf]) by smtp.gmail.com with ESMTPSA id d195-20020a4a52cc000000b00541fbbbcd31sm2888856oob.5.2023.07.13.06.25.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jul 2023 06:25:45 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v5 2/6] stdlib: Move insertion sort out qsort Date: Thu, 13 Jul 2023 10:25:36 -0300 Message-Id: <20230713132540.2854320-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230713132540.2854320-1-adhemerval.zanella@linaro.org> References: <20230713132540.2854320-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 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, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" --- stdlib/qsort.c | 101 ++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 47 deletions(-) diff --git a/stdlib/qsort.c b/stdlib/qsort.c index bf3326e4b9..410a8d7731 100644 --- a/stdlib/qsort.c +++ b/stdlib/qsort.c @@ -149,6 +149,58 @@ typedef struct smaller partition. This *guarantees* no more than log (total_elems) stack size is needed (actually O(1) in this case)! */ +static inline void +insertion_sort_qsort_partitions (void *const pbase, size_t total_elems, + size_t size, enum swap_type_t swap_func, + __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_func); + + /* 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; + } + } + } +} + void _quicksort (void *const pbase, size_t total_elems, size_t size, __compar_d_fn_t cmp, void *arg) @@ -271,51 +323,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); }