From patchwork Fri Sep 3 17:11:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 44844 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 DEBE33847816 for ; Fri, 3 Sep 2021 17:15:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DEBE33847816 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1630689304; bh=2vu6qRs3omrH9PNdjhyh+WqjLUgNyWC23FMqcmAI5hQ=; 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=rlzCxIe8sOgZwm0auxuqP6pzGbAwWwjtdLHXWpWBPIk/Ski6H6yAegAi3Qmg6br3B u4OP24E4CkcjGMYsDLm+6xEZiAEl+8Ok8ZzXb6TlMjX5mjxSWoSVSV6dOq0wGY0NWI rPu7hbEIauaxjm+/ZamNEVzj98VdrsNECpH7fzyY= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qv1-xf31.google.com (mail-qv1-xf31.google.com [IPv6:2607:f8b0:4864:20::f31]) by sourceware.org (Postfix) with ESMTPS id 4033A3848402 for ; Fri, 3 Sep 2021 17:11:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4033A3848402 Received: by mail-qv1-xf31.google.com with SMTP id bn14so3547161qvb.12 for ; Fri, 03 Sep 2021 10:11:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2vu6qRs3omrH9PNdjhyh+WqjLUgNyWC23FMqcmAI5hQ=; b=fuRDyLbH4xD1nZ1kF52kdZdavTmpCkn2n0SbjsQGwX7EAVlp4hAvfhWR+M4iSUF+yK CyskMni0C11jdO8MAKJ+cLMr91E+NYPaF75hz/KhH4ineZHJAEl6y7YkfL4hIdzrOA0a 9NXn9qQavRgG71kgLDbXs4Qp+himWP54+c2uO1Jl2yUuKHZIaXMU46bffw5wvTS1TJr5 9W6JXFSI3BVYaVMQJtIbPUBbXbwfRzdIUE4dXF5RKRaIkiIA4RKltHxglVtX+evvzK2B qk7QC+GG2r0ifSs3hP5pUW7ua2lOtb+QKhENRLjYbaevVsuydJDxebusFp1pIXz4d8Fo vpHg== X-Gm-Message-State: AOAM532DxdA9wp1k8cnO7UFV/RN5WHHLmYNZLjCfM7WfHnJxY2P6OEVb AGSWx43hzd1dszOxCF1DwtOpj5xB6qxHsQ== X-Google-Smtp-Source: ABdhPJwMwyGHsKkqO39HtVx2xQUvLjfNMg1mPE5triFgQKyBGKqCenCCNil6siFYXZ8zme/LU0sKBg== X-Received: by 2002:a0c:f58c:: with SMTP id k12mr109881qvm.38.1630689113717; Fri, 03 Sep 2021 10:11:53 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:733d:fff8:7487:556e:e293]) by smtp.gmail.com with ESMTPSA id r4sm3207071qtw.5.2021.09.03.10.11.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 10:11:53 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v3 4/7] stdlib: Move insertion sort out qsort Date: Fri, 3 Sep 2021 14:11:41 -0300 Message-Id: <20210903171144.952737-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210903171144.952737-1-adhemerval.zanella@linaro.org> References: <20210903171144.952737-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" --- stdlib/qsort.c | 100 ++++++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 47 deletions(-) Reviewed-by: Fangrui Song diff --git a/stdlib/qsort.c b/stdlib/qsort.c index 59458d151b..b69417dedd 100644 --- a/stdlib/qsort.c +++ b/stdlib/qsort.c @@ -150,6 +150,58 @@ typedef struct smaller partition. This *guarantees* no more than log (total_elems) stack size is needed (actually O(1) in this case)! */ +static void +insertion_sort (void *const pbase, size_t total_elems, size_t size, + swap_func_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) @@ -272,51 +324,5 @@ _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_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) ((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 (pbase, total_elems, size, swap_func, cmp, arg); }