From patchwork Thu Sep 14 18:37:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 722584 Delivered-To: patch@linaro.org Received: by 2002:adf:f0d1:0:b0:31d:da82:a3b4 with SMTP id x17csp551762wro; Thu, 14 Sep 2023 11:37:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFv7KenjNptjyRbU/gXPC7K5Pnguan4oiR4DD9Iy4lF2OdmSahC/v5RT/cnCV6/bBEfaUpM X-Received: by 2002:a5d:4533:0:b0:31c:3013:9fa5 with SMTP id j19-20020a5d4533000000b0031c30139fa5mr5513652wra.59.1694716652395; Thu, 14 Sep 2023 11:37:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694716652; cv=none; d=google.com; s=arc-20160816; b=G3VYqjiXJHYfNkp9t8ZhUMkmevtd3/Xz+8Tg7ZNeGySkyRjv1B/rHk+A4xjrJudCnf 4fAWDmBw5JCW+6oYP5agb+NtE+U3RaAYUzdscwf3YBqcROPJlVcjUBdYSoBhYu4S8+RX +rLF/po11zdpraEOz8xVsbtl71kQ472ncIWVWM+mkMlgVWwXhH1LnxRMo0gHJZW26qHi dNntnoFyXVbyaz1DbV1ozUpg1QQVdX8GHGWcivKeGlGUrbSGZxawb06jPetUopRubRz2 7rQTLIHpo7/BLigeoUkn1MYSrs3m2pKAbFfodJOLNpaLllFpksiZOWWPUtM/vtv8+ygI MMqA== 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=M3zU+xyjjiEt0GUXw16fcI1DylY5neN39pRNE2Kebrw=; fh=ubzLPtaquqORyAJ/TX35zypB35/iXKzZJOEWlgP8mu4=; b=RFrbc0MsFgnlvcUDFoUFErfsRpgUpzEnG37d4p9+O6j/U+YL5ZB/QUaudWdI6c5nDF GCAqn7Oxq0eOjd+EL8UM0nNiZNNph9MtH+8quDcMMY/+yRQfGkf87UIpFhS0nBTnQwFM XTCL1UOWUFMxdpCbRV3uwoT5hQxw9fxOetjUS1ewWWOcKe4Ewrzb5nZAZfWtvTRg5qnv mRgmI+Tpj5Pa9AGIMc/a8bKx79hPZHlWam0jrZFtZZT9FJt8Ed1MlfByNSkJbnHS2NFW UmGcipO1s7t4sTi9fDOyFmlPMf59hQa/63xZwfOCW8UVIUzMuzAOkYCaswXjXylCWZEN ZEtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EXAVT8+d; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id y12-20020a056402134c00b0052e6f6e4e7fsi1853477edw.278.2023.09.14.11.37.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 11:37:32 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EXAVT8+d; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 335623853567 for ; Thu, 14 Sep 2023 18:37:31 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x22a.google.com (mail-oi1-x22a.google.com [IPv6:2607:f8b0:4864:20::22a]) by sourceware.org (Postfix) with ESMTPS id 8B358385697B for ; Thu, 14 Sep 2023 18:37:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8B358385697B 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-oi1-x22a.google.com with SMTP id 5614622812f47-3a7ca8720a0so783074b6e.2 for ; Thu, 14 Sep 2023 11:37:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1694716636; x=1695321436; 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=M3zU+xyjjiEt0GUXw16fcI1DylY5neN39pRNE2Kebrw=; b=EXAVT8+d8MFqNFuDqcq/D19YUqVAmlQRS9p3pU4Klfwi5e4u+qbpvdiNfykp4gY9xl DaBvKnIkbTpdcEU/1xmwn61Hh08bcQLp73j6WIuRfAKWeAcvAIQJn32l9Yn56DhPLW00 uNmmrOM/IDk//hfIG71bLnehx0XfNRryhfefGTM4pl8zWCOikKeG8EQ9E0DWTWPPsGPV br+SFsNbC3IACl4eDcwydFU0uTj4eX7hHuAiOMRmTBIUJV7ADZIJcAmRdwNzJwiKzBW7 tBriBhETPbbn7V7VBrZizdl2VIxSoQQnEjsaY13+rDUBEHKrTIcIl9EO8Tx/N/GEBxGY w+cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694716636; x=1695321436; 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=M3zU+xyjjiEt0GUXw16fcI1DylY5neN39pRNE2Kebrw=; b=OsuFAWf9UU0LoHf5U+1K0Q1BJpp93J4ap2gAFeWXC4TqmAgsF8VQTqOdj/LIoY76j9 ziGniQFoWxTRzfHTqmJohOk/0DzkWmNANkkyHmT4vh71bZ6aDSGpqBGf5N5FOI9vUZoy GScbDmPigBYFBzbAP7jVyxxp+EaIY6IABwKRMFjrLfJDQV6u+zF/iKCWtfPBYHd+jXzb Ynw7m7nb1NSA4ODGxvIZYg4StI4FYAkt0FP1S8vspc8d/25ZtwJB4h5rlgG44PitHbm3 tjx01czH9ouiQ+RPSrGfsmLMv1QyKc3Tf7+iwKASkL1bWJsGK3MnuIP1Zxubke0qNYTw 2REw== X-Gm-Message-State: AOJu0Yzn+ZWpGoFNnPGAdsl2q+YgsiO/+Rbk2fl2kRSbyTBaCRfQPKY6 qKAqNpr8eQmiW2djluS7fxIq3yWwHozc4Z0Xgs3HVA== X-Received: by 2002:a05:6870:5621:b0:1d6:925:840d with SMTP id m33-20020a056870562100b001d60925840dmr7201995oao.55.1694716636318; Thu, 14 Sep 2023 11:37:16 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:91cb:5173:d70:d8d:4d2c]) by smtp.gmail.com with ESMTPSA id cc6-20020a056871e18600b001b3d67934e9sm1056930oac.26.2023.09.14.11.37.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 11:37:15 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Noah Goldstein , Paul Eggert , Florian Weimer Subject: [PATCH v7 3/7] stdlib: Move insertion sort out qsort Date: Thu, 14 Sep 2023 15:37:00 -0300 Message-Id: <20230914183704.1386534-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230914183704.1386534-1-adhemerval.zanella@linaro.org> References: <20230914183704.1386534-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(-) diff --git a/stdlib/qsort.c b/stdlib/qsort.c index bba9783191..30e2999005 100644 --- a/stdlib/qsort.c +++ b/stdlib/qsort.c @@ -110,6 +110,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: @@ -256,51 +308,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); }