From patchwork Mon Oct 2 19:33:07 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: 728560 Delivered-To: patch@linaro.org Received: by 2002:a5d:60c8:0:b0:31d:da82:a3b4 with SMTP id x8csp1819807wrt; Mon, 2 Oct 2023 12:33:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHY+pXhJKLhsD5I8JbIf2EMmmj4yOUakU1wwy4HDXaSZVZnCop0epCqYS1nc3RxGdOsNsDZ X-Received: by 2002:aa7:d755:0:b0:533:8eda:b8e4 with SMTP id a21-20020aa7d755000000b005338edab8e4mr9180851eds.6.1696275219405; Mon, 02 Oct 2023 12:33:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696275219; cv=none; d=google.com; s=arc-20160816; b=EqGjVLLTB6RXrVS+MM7b9s0AhWFBF/rt9118JIIaj8KzTEhkrk8OZ9ljKmJmaRx8pZ 90fgA42krdSKW2enuB1b6Yrx6g2WqPBYuHyHH/cDfx0OiNh79UZg3w2q+VdNf9L6BPs2 CmvHPgfoGTgePoU/ICQBktL8RPBh/zF5umGITKUerS6zKgFIRcotQGF6oGMngVpYTagV cnSFWkiP114RtdiuiWntN/DngyBhS+ppy9zOoK+6H/Cc66oIwKdVeGzOwEbgrZqluiwy 3nZVSyV7QUHJ2KCY1Plk/LjWX3XI6l37cyFU0F3bDZuDbIWJqbluqOdPTTw9eaVP/Jsq 4Rkg== 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=Yp2fprbaE685I0pJWoac9A/KAacJeHbDG0QVx2scAZSB9+Hl84uVGBrKLwty4T3RsG 0W4f6ylMnQT8ZDFv2ARzUXvHHm2cVVjnVTyujgXUD3vpxtqIqX1Fzdt5TNE2XlcqL6ig Dra2zPrruCS2u5DfmZlrqX0sjRvTflcAzVOHwnkqHEe3UpLT0N4oGZnC9NKLpfRDd2aP znHj22HEQ63GBawkkfv7sZMNbNk/Abd7i6FYH2MG6gPUtPN7Cjq0HE/JwFCplMIySY6b iTPcyHI6ViRERghJosDs0OFSpW6HsT3kiRZVpTcl+C07GjVP10O1vwXtZL96t78ucA02 dQSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=lXfho1Hs; 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 v7-20020aa7d9c7000000b0053603ad9b28si5898201eds.388.2023.10.02.12.33.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:33:39 -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=lXfho1Hs; 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 26882385C6C4 for ; Mon, 2 Oct 2023 19:33:38 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by sourceware.org (Postfix) with ESMTPS id A58413857BB3 for ; Mon, 2 Oct 2023 19:33:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A58413857BB3 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-x62f.google.com with SMTP id d9443c01a7336-1c723f1c80fso1321045ad.1 for ; Mon, 02 Oct 2023 12:33:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696275202; x=1696880002; 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=lXfho1HsxkYF42gTiIrfapRjzndy20weQ2OcBZGM4/M5wjUsPOkztu+A6QeIaTksoT wXG6NxoHKVKr4/aYDCKkSqXZYML410Bynto/0ale3gWC1LjzXJ0qSFLFU0mcIWx60vV/ EEUki3eYeYJdzwiz0D7DJ8lQ7HJ3Zqz72XxmEHSn+UcbYuYaMPx0Yg6COmwiCgpEhCd7 Kca5TzePPdiev858utI9320sL1A4iDRO+oxiz0utg2Mja3TPGUtHzOT9eVV+cTepAjVQ fzseKb9sicCGHR7GdM+xWV/N+KjVbainyNi6K/o3W8NeOdWIaJVTAmJkFPoU9TXzSqxK ning== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696275202; x=1696880002; 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=CHTugQiCHnaRDHZY3389eQsASqqETiKGRKO3xg3PwWHZkAqMvoWstghdZsFAWq+8nY CzVzcF2kkqoNFpHiDbBS10eVWiUnE+dAvmy/uxQk3gt2eoL3xpl9X0dxDitr9f/KiBiw /e4W96eRBqALKxN65TsxHEi6XxfSSg/Z6z2yS14/yrN0x/V/gb1hVCTdtsk8p+QAlEEp 0qQVXOynRpbie3l6ezuO58Q4YEahrKO3MNAtSMMMAbr7PbYx8egiYOo//w0q0CVKqeWr FrVzQkwyExBb6i7Ng5MNy34P9wk7wQhWn2iECP4It3FnNXV8kbcSSn1ddzZt5VXjuhGf 48lw== X-Gm-Message-State: AOJu0YybI+K3W2xRNwrC89LuDhQrXgtKcyQi+3TdaVuar4NFzFsJUv+h esMNGCphuS2dKz2S+MOt+fdenjHi0OuN5wADjPg9Bg== X-Received: by 2002:a17:902:ee45:b0:1c1:d4f4:cd3c with SMTP id 5-20020a170902ee4500b001c1d4f4cd3cmr10837875plo.31.1696275201902; Mon, 02 Oct 2023 12:33:21 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:feaf:8f32:80e:c10a:4836]) by smtp.gmail.com with ESMTPSA id u10-20020a170902b28a00b001c7453fae33sm6982828plr.280.2023.10.02.12.33.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:33:21 -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: Mon, 2 Oct 2023 16:33:07 -0300 Message-Id: <20231002193311.3985890-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231002193311.3985890-1-adhemerval.zanella@linaro.org> References: <20231002193311.3985890-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.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); }