From patchwork Mon Oct 2 19:33:05 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: 728559 Delivered-To: patch@linaro.org Received: by 2002:a5d:60c8:0:b0:31d:da82:a3b4 with SMTP id x8csp1819750wrt; Mon, 2 Oct 2023 12:33:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGpqhryZ1tWBFHEdv0zKWSdSdMCfGagzSvftVrpjDN0Dm8D0jTK/zm3welD7J8OlQNv+V17 X-Received: by 2002:a17:907:2712:b0:9ad:e298:a50 with SMTP id w18-20020a170907271200b009ade2980a50mr10508749ejk.8.1696275208198; Mon, 02 Oct 2023 12:33:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696275208; cv=none; d=google.com; s=arc-20160816; b=0YRlhCBTJHmRm8hwveYp4F+umQabgr68tfp5deDihyKXvLXxaIHmifTtyx53dVpkbe YccaA9oYnL2afa2bFdfnpbHtajobaC+D5qbINAEFZJBS1H4JVOIptduTaUeG2F598QMY kwpSNixJzCRDo0DvDLd9S0cgLU6vjOR/KrzLGzChTwBXCmNvx2DksTekzKsgQhOCdhp9 vqWuqqrADJ95bPKYn+4VIw6qMqvXID+YRUZGn8xrI+Rays91cdoleuK7klleEtmuwDiu 8yGHCoGmKqWstR8RM6kImaGgWsDEy+GmH5lTJ/CU8ZSy3Mx0MxPwtbeZCQIV0UC0wzkv EQ8Q== 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=3gxuH9ByZCz9s0Dh7UOISa4Ep/Exr8sPymluuI3E+qI=; fh=ubzLPtaquqORyAJ/TX35zypB35/iXKzZJOEWlgP8mu4=; b=U2Wv6wkjX1m2h5Eco4GyZfcfLLlLp+K8PbFlEv2JJ8Jzu49oRoLv7r1eW16kHoDZ11 ukHF87BK+BUIvIhl8nXgLU32HPrmSOHhhFjEz4YflGtsriehcfzETjfdW0XKDjH0gRfA eRYRSMW2vE5E5eu2YlsF2FdOB1IVNXNNNb4Ha3xJV4TOWg7RJ2tMLhiZMZkAd+kJ7uxH 53WshSE7pvB5PErCJkD/cc6APDq1T+Ikis6ZbPCrXcK3hvfs9RpKDTW6H82JtAXVqpdt t4Ax7CmgkuQlmFS8pe2vVt+h8/U7aY/tZKkVDgYUSt9MZUaq9RK1epjHisFA7RJIKDC1 a6IQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tg0NNG47; 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 y16-20020a1709064b1000b00988907e3aaesi18486703eju.428.2023.10.02.12.33.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:33:28 -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=tg0NNG47; 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 377053856DE2 for ; Mon, 2 Oct 2023 19:33:27 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by sourceware.org (Postfix) with ESMTPS id 4ABC53858D33 for ; Mon, 2 Oct 2023 19:33:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4ABC53858D33 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-x62a.google.com with SMTP id d9443c01a7336-1bf6ea270b2so1415905ad.0 for ; Mon, 02 Oct 2023 12:33:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696275198; x=1696879998; 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=3gxuH9ByZCz9s0Dh7UOISa4Ep/Exr8sPymluuI3E+qI=; b=tg0NNG47gvzCISwr+iPWxvxcaa7yM+LDmWmNzI5O3zPLSFXGbCY//u8X5eHhbYM5Po bFB8zV7ZMFJMUfUDP8+yrY7ujMNaHjUDaVTiJ3PcQ886D9WHZimQQKn8YeFXfZJP6uyZ LC89ekvu2Ke39FiYnbFXwYD2nXWSbZRVp38kxIpNnwRzo34j4/VXWBCedl9mx/ppgP3X 5YrgfdWzxnz2bp+CZv0IoaoZ9qx7Fxs5ktN+cKWxBMlHhN0RddPI1U39aqJ/afai/Yig Kfvd8tm5zJ+IAnBSrZxPGnSDTeN8rvDs2cJe2ZKnEGHgvX/zh2dKAr7SGBFtkW06g6xK gn0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696275198; x=1696879998; 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=3gxuH9ByZCz9s0Dh7UOISa4Ep/Exr8sPymluuI3E+qI=; b=nn2m1ccR5M/693JJRHYy2/w9N2SNWXbNlnR3taiCB89a7YpgyTIawCWGZ1ttd2PIjO FFAOIe5K/Lcyn9od46iFyTZ9f+rzr27K2nAWjWYl5/04SrzbQGEFu8wJPpHRpyjsexAJ oCaVkefZ1+7La2QyO9mhCl8BlqxBIaw4hKTcI8gn5c/mkhPK9Gzf0KVrWEy266PJdd3/ SPzMpLyaxfpIm9iD19nLzOBvSD2OcQxifijqVN5MT4WRVn4YdThajT8HPg3ZKMWGs8r+ 7xSZPfEwVOqzE1ORVyZ/N5jy6J8yNPXaZWARCJEhWQRfR0ZN88DDphoOivdP4jswZnMv 0Vgw== X-Gm-Message-State: AOJu0YybJJehO1vtvuddtWvY+TTOihKE2o0/kkvZqhetMHAW1ooIYoP4 XzegUhe9NBKRZlqHguYWcGF5VEY8vR9tQL0LErf2vA== X-Received: by 2002:a17:902:ecc1:b0:1c5:d8a3:8789 with SMTP id a1-20020a170902ecc100b001c5d8a38789mr12753908plh.4.1696275197634; Mon, 02 Oct 2023 12:33:17 -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.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:33:17 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Noah Goldstein , Paul Eggert , Florian Weimer Subject: [PATCH v8 1/7] string: Add internal memswap implementation Date: Mon, 2 Oct 2023 16:33:05 -0300 Message-Id: <20231002193311.3985890-2-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.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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 The prototype is: void __memswap (void *restrict p1, void *restrict p2, size_t n) The function swaps the content of two memory blocks P1 and P2 of len N. Memory overlap is NOT handled. It will be used on qsort optimization. Checked on x86_64-linux-gnu and aarch64-linux-gnu. --- include/string.h | 3 + string/Makefile | 13 +++ string/memswap.c | 41 +++++++++ string/test-memswap.c | 191 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 248 insertions(+) create mode 100644 string/memswap.c create mode 100644 string/test-memswap.c diff --git a/include/string.h b/include/string.h index 86d1fa4abe..f31a7f6006 100644 --- a/include/string.h +++ b/include/string.h @@ -45,6 +45,9 @@ extern void *__memrchr (const void *__s, int __c, size_t __n) extern void *__memchr (const void *__s, int __c, size_t __n) __attribute_pure__; +extern void __memswap (void *restrict __p1, void *restrict __p2, size_t __n) + attribute_hidden; + extern void __bzero (void *__s, size_t __n) __THROW __nonnull ((1)); extern int __ffs (int __i) __attribute__ ((const)); diff --git a/string/Makefile b/string/Makefile index 8cdfd5b000..3d0a3d5682 100644 --- a/string/Makefile +++ b/string/Makefile @@ -69,6 +69,7 @@ routines := \ mempcpy \ memrchr \ memset \ + memswap \ rawmemchr \ sigabbrev_np \ sigdescr_np \ @@ -209,6 +210,18 @@ tests := \ tst-xbzero-opt \ # tests +tests-static-internal := \ + test-memswap \ +# tests-static-internal + +tests-internal := \ + $(tests-static-internal) \ + # tests-internal + +tests-static := \ + $(tests-static-internal) \ + # tests-static + # Both tests require the .mo translation files generated by msgfmt. tests-translation := \ tst-strerror \ diff --git a/string/memswap.c b/string/memswap.c new file mode 100644 index 0000000000..37640c47ad --- /dev/null +++ b/string/memswap.c @@ -0,0 +1,41 @@ +/* Swap the content of two memory blocks, overlap is NOT handled. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +void +__memswap (void *restrict p1, void *restrict p2, size_t n) +{ + /* Use multiple small memcpys with constant size to enable inlining on most + targets. */ + enum { SWAP_GENERIC_SIZE = 32 }; + unsigned char tmp[SWAP_GENERIC_SIZE]; + while (n > SWAP_GENERIC_SIZE) + { + memcpy (tmp, p1, SWAP_GENERIC_SIZE); + p1 = __mempcpy (p1, p2, SWAP_GENERIC_SIZE); + p2 = __mempcpy (p2, tmp, SWAP_GENERIC_SIZE); + n -= SWAP_GENERIC_SIZE; + } + while (n > 0) + { + unsigned char t = ((unsigned char *)p1)[--n]; + ((unsigned char *)p1)[n] = ((unsigned char *)p2)[n]; + ((unsigned char *)p2)[n] = t; + } +} diff --git a/string/test-memswap.c b/string/test-memswap.c new file mode 100644 index 0000000000..7696c43711 --- /dev/null +++ b/string/test-memswap.c @@ -0,0 +1,191 @@ +/* Test and measure memcpy functions. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +#define TEST_MAIN +#define BUF1PAGES 3 +#include "test-string.h" + +static unsigned char *ref1; +static unsigned char *ref2; + +static void +do_one_test (unsigned char *p1, unsigned char *ref1, unsigned char *p2, + unsigned char *ref2, size_t len) +{ + __memswap (p1, p2, len); + + TEST_COMPARE_BLOB (p1, len, ref2, len); + TEST_COMPARE_BLOB (p2, len, ref1, len); +} + +static inline void +do_test (size_t align1, size_t align2, size_t len) +{ + align1 &= page_size; + if (align1 + len >= page_size) + return; + + align2 &= page_size; + if (align2 + len >= page_size) + return; + + unsigned char *p1 = buf1 + align1; + unsigned char *p2 = buf2 + align2; + for (size_t repeats = 0; repeats < 2; ++repeats) + { + size_t i, j; + for (i = 0, j = 1; i < len; i++, j += 23) + { + ref1[i] = p1[i] = j; + ref2[i] = p2[i] = UCHAR_MAX - j; + } + + do_one_test (p1, ref1, p2, ref2, len); + } +} + +static void +do_random_tests (void) +{ + for (size_t n = 0; n < ITERATIONS; n++) + { + size_t len, size, size1, size2, align1, align2; + + if (n == 0) + { + len = getpagesize (); + size = len + 512; + size1 = size; + size2 = size; + align1 = 512; + align2 = 512; + } + else + { + if ((random () & 255) == 0) + size = 65536; + else + size = 768; + if (size > page_size) + size = page_size; + size1 = size; + size2 = size; + size_t i = random (); + if (i & 3) + size -= 256; + if (i & 1) + size1 -= 256; + if (i & 2) + size2 -= 256; + if (i & 4) + { + len = random () % size; + align1 = size1 - len - (random () & 31); + align2 = size2 - len - (random () & 31); + if (align1 > size1) + align1 = 0; + if (align2 > size2) + align2 = 0; + } + else + { + align1 = random () & 63; + align2 = random () & 63; + len = random () % size; + if (align1 + len > size1) + align1 = size1 - len; + if (align2 + len > size2) + align2 = size2 - len; + } + } + unsigned char *p1 = buf1 + page_size - size1; + unsigned char *p2 = buf2 + page_size - size2; + size_t j = align1 + len + 256; + if (j > size1) + j = size1; + for (size_t i = 0; i < j; ++i) + ref1[i] = p1[i] = random () & 255; + + j = align2 + len + 256; + if (j > size2) + j = size2; + + for (size_t i = 0; i < j; ++i) + ref2[i] = p2[i] = random () & 255; + + do_one_test (p1 + align1, ref1 + align1, p2 + align2, ref2 + align2, len); + } +} + +static int +test_main (void) +{ + test_init (); + /* Use the start of buf1 for reference buffers. */ + ref1 = buf1; + ref2 = buf1 + page_size; + buf1 = ref2 + page_size; + + printf ("%23s", ""); + printf ("\t__memswap\n"); + + for (size_t i = 0; i < 18; ++i) + { + do_test (0, 0, 1 << i); + do_test (i, 0, 1 << i); + do_test (0, i, 1 << i); + do_test (i, i, 1 << i); + } + + for (size_t i = 0; i < 32; ++i) + { + do_test (0, 0, i); + do_test (i, 0, i); + do_test (0, i, i); + do_test (i, i, i); + } + + for (size_t i = 3; i < 32; ++i) + { + if ((i & (i - 1)) == 0) + continue; + do_test (0, 0, 16 * i); + do_test (i, 0, 16 * i); + do_test (0, i, 16 * i); + do_test (i, i, 16 * i); + } + + for (size_t i = 19; i <= 25; ++i) + { + do_test (255, 0, 1 << i); + do_test (0, 4000, 1 << i); + do_test (0, 255, i); + do_test (0, 4000, i); + } + + do_test (0, 0, getpagesize ()); + + do_random_tests (); + + return 0; +} + +#include From patchwork Mon Oct 2 19:33:06 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: 728561 Delivered-To: patch@linaro.org Received: by 2002:a5d:60c8:0:b0:31d:da82:a3b4 with SMTP id x8csp1819901wrt; Mon, 2 Oct 2023 12:33:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFWUK4wJogTqAANeuosEs9tNDGyYqyWa35MKyYatAtly9eRKULp0DqUbGffkXhRI7kN2ln7 X-Received: by 2002:a17:906:538f:b0:9ae:699d:8a2a with SMTP id g15-20020a170906538f00b009ae699d8a2amr11796373ejo.5.1696275235329; Mon, 02 Oct 2023 12:33:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696275235; cv=none; d=google.com; s=arc-20160816; b=AWK1EgylZxa9MwqJ5KmpWybZXXIQ4N046gj34XSxD+x+W8dbZ+ZFHaTWafqE3sxomb JRfRj5+Sacd/QJnedPfYcIwWmqIjlk0F5SgIV8j0pMTXyP4oqvfIY1SY2xsK4yaMpACL b41nHrOUvhYKsxlFwmvxMpRS7iDd3D/TgSEyBY5/fkEee7W6M+WEHD+/kEcfu8m3sMrg e95XQwPGc/Xuf0GWlSaOlfk5RjokUbqTAqRWMFS/YzKNXYGT2V1R09eTWKrusRBia0G7 +wpxT0bRg1KFQeQkzp2EeE5PjhJXwV1EHuGY/oQTghtjBY3fMOdDErPdwUqUwr0+6oIA H4Mg== 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=I/mdUPzISC+XmuJBFaDw8dL6gLvuN2O/2+RJn1xOxGA=; fh=ubzLPtaquqORyAJ/TX35zypB35/iXKzZJOEWlgP8mu4=; b=JPy0WpXiboDcdjcZ4u362ylEULiKJ8td833nFDtrbaTb1SJFvJeqcdqShoT6xnhusm x6T5oqZnRuWmgOqJ7mW0zAQC+AEqbiVNQNEzfK6vPdaUW1QAMkYQU1NxIhmGcrH9JJfy MdQjNbAh5BkEHTPA4blcUQ8C7+5gZVhNKJfCi8ezN7eJNdwjm3JNtv9gYxDx95xWX6C6 6EOqRr7snJkh0stNOg8eQ2cx/IkxQJK76wt6EcyQOOl3Tp6MpGOigXldhL6rgg5l3+Ox TaAeTGYXHGnF6Khi1eVkizSU26gnqbpzi26C7j3jL2uvnAstBqvEJ02VYr11ua6m8ex/ Cwyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=tMmH6nhr; 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 fj9-20020a1709069c8900b009b2ca7524cesi4273823ejc.945.2023.10.02.12.33.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:33:55 -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=tMmH6nhr; 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 062F7385CCA8 for ; Mon, 2 Oct 2023 19:33:54 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by sourceware.org (Postfix) with ESMTPS id 5340A38582A3 for ; Mon, 2 Oct 2023 19:33:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5340A38582A3 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-x630.google.com with SMTP id d9443c01a7336-1c1ff5b741cso1296935ad.2 for ; Mon, 02 Oct 2023 12:33:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696275200; x=1696880000; 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=I/mdUPzISC+XmuJBFaDw8dL6gLvuN2O/2+RJn1xOxGA=; b=tMmH6nhr2lNvqjO11qeoE4w5SZY4zNpSfEI1QvTBRwxwTD9JDOxL+u9l/KzC9btDlo tsnnFylGrMs6A7mk9oqTzZ1NmCR3bAzbjF+IYHd6x0177Yy48Wvy9GfrrA15jToFQYJ3 p6gzoaUw4xBFweOX9KxLGcoGB0QZBD6GMmnuP+EleF3GeQCPbcJoFexm+ROTGvAVSwxD FpZBwbJSECvdBoAxBYHVIpm1Jls3NOn9HpWuMYJztPX0smkNM8pb4t3F1d05u8IwO578 mhfsTG5h6rnlBt0+vpW5Uwb2l73VNIZsEglgUTlvOlHhylRIGGj9DCZ7S3iUoG0IlWft ZD3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696275200; x=1696880000; 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=I/mdUPzISC+XmuJBFaDw8dL6gLvuN2O/2+RJn1xOxGA=; b=w9EoqJT0+95GkD+7fXXX47RNKwPc1v+I5giOxqnMp6oXLY2PZ2Qib9LrWSNOTVgN2L A655gj6lGAZIC+LfpTLZHFTOQEQA9yTNOFIfIKcYAog6eTmHqABkLzrm7o7ETVeFt0cI sxcmCZVfYod2L4VKpTCV5S8yaeT0Up+n6NmUoDPnf5qRrWzpR/oMl6iKAn9v916QBP4y SB+3gbrlzCK1M1i6XNENj7S79/dEGJ5hKN7EHcAX0IfSs8A354DJt832e7EFNKIEtrX/ 87fNWMWfvZ1s0v1cgBcG8arELTezISQ5kMejSPaE+dUIjS1jRElG3ncrZCIknhSr3t1O dKJg== X-Gm-Message-State: AOJu0YzrCrcT9rYrquH6zesuciHRGAfZvqGhMw+30auK4Jq8ma6lfPnv 2PVLlTuDfFG42D8n+NVRMD9WozT0oeazkKZkGFRv9Q== X-Received: by 2002:a17:902:f7d3:b0:1c1:ed61:e058 with SMTP id h19-20020a170902f7d300b001c1ed61e058mr11658035plw.16.1696275199759; Mon, 02 Oct 2023 12:33:19 -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.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:33:19 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Noah Goldstein , Paul Eggert , Florian Weimer Subject: [PATCH v8 2/7] stdlib: Optimization qsort{_r} swap implementation Date: Mon, 2 Oct 2023 16:33:06 -0300 Message-Id: <20231002193311.3985890-3-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.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 The optimization takes in consideration both the most common elements are either 32 or 64 bit in size and inputs are aligned to the word boundary. This is similar to what msort does. For large buffer the swap operation uses memcpy/mempcpy with a small fixed size buffer (so compiler might inline the operations). Checked on x86_64-linux-gnu. --- stdlib/qsort.c | 94 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 18 deletions(-) diff --git a/stdlib/qsort.c b/stdlib/qsort.c index 728a0ed370..bba9783191 100644 --- a/stdlib/qsort.c +++ b/stdlib/qsort.c @@ -23,20 +23,70 @@ #include #include #include +#include -/* Byte-wise swap two items of size SIZE. */ -#define SWAP(a, b, size) \ - do \ - { \ - size_t __size = (size); \ - char *__a = (a), *__b = (b); \ - do \ - { \ - char __tmp = *__a; \ - *__a++ = *__b; \ - *__b++ = __tmp; \ - } while (--__size > 0); \ - } while (0) +/* Swap SIZE bytes between addresses A and B. These helpers are provided + along the generic one as an optimization. */ + +enum swap_type_t + { + SWAP_WORDS_64, + SWAP_WORDS_32, + SWAP_BYTES + }; + +/* If this function returns true, elements can be safely copied using word + loads and stores. Otherwise, it might not be safe. BASE (as an integer) + must be a multiple of the word alignment. SIZE must be a multiple of + WORDSIZE. Since WORDSIZE must be a multiple of the word alignment, and + WORDSIZE is a power of two on all supported platforms, this function for + speed merely checks that BASE and SIZE are both multiples of the word + size. */ +static inline bool +is_aligned (const void *base, size_t size, size_t wordsize) +{ + return (((uintptr_t) base | size) & (wordsize - 1)) == 0; +} + +static inline void +swap_words_64 (void * restrict a, void * restrict b, size_t n) +{ + typedef uint64_t __attribute__ ((__may_alias__)) u64_alias_t; + do + { + n -= 8; + u64_alias_t t = *(u64_alias_t *)(a + n); + *(u64_alias_t *)(a + n) = *(u64_alias_t *)(b + n); + *(u64_alias_t *)(b + n) = t; + } while (n); +} + +static inline void +swap_words_32 (void * restrict a, void * restrict b, size_t n) +{ + typedef uint32_t __attribute__ ((__may_alias__)) u32_alias_t; + do + { + n -= 4; + u32_alias_t t = *(u32_alias_t *)(a + n); + *(u32_alias_t *)(a + n) = *(u32_alias_t *)(b + n); + *(u32_alias_t *)(b + n) = t; + } while (n); +} + +/* Replace the indirect call with a serie of if statements. It should help + the branch predictor. */ +static void +do_swap (void * restrict a, void * restrict b, size_t size, + enum swap_type_t swap_type) +{ + if (swap_type == SWAP_WORDS_64) + swap_words_64 (a, b, size); + else if (swap_type == SWAP_WORDS_32) + swap_words_32 (a, b, size); + else + __memswap (a, b, size); +} /* Discontinue quicksort algorithm when partition gets below this size. This particular magic number was chosen to work best on a Sun 4/260. */ @@ -96,6 +146,14 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, /* Avoid lossage with unsigned arithmetic below. */ return; + enum swap_type_t swap_type; + if (is_aligned (pbase, size, 8)) + swap_type = SWAP_WORDS_64; + else if (is_aligned (pbase, size, 4)) + swap_type = SWAP_WORDS_32; + else + swap_type = SWAP_BYTES; + if (total_elems > MAX_THRESH) { char *lo = base_ptr; @@ -119,13 +177,13 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, char *mid = lo + size * ((hi - lo) / size >> 1); if ((*cmp) ((void *) mid, (void *) lo, arg) < 0) - SWAP (mid, lo, size); + do_swap (mid, lo, size, swap_type); if ((*cmp) ((void *) hi, (void *) mid, arg) < 0) - SWAP (mid, hi, size); + do_swap (mid, hi, size, swap_type); else goto jump_over; if ((*cmp) ((void *) mid, (void *) lo, arg) < 0) - SWAP (mid, lo, size); + do_swap (mid, lo, size, swap_type); jump_over:; left_ptr = lo + size; @@ -144,7 +202,7 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, if (left_ptr < right_ptr) { - SWAP (left_ptr, right_ptr, size); + do_swap (left_ptr, right_ptr, size, swap_type); if (mid == left_ptr) mid = right_ptr; else if (mid == right_ptr) @@ -216,7 +274,7 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, tmp_ptr = run_ptr; if (tmp_ptr != base_ptr) - SWAP (tmp_ptr, base_ptr, size); + do_swap (tmp_ptr, base_ptr, size, swap_type); /* Insertion sort, running from left-hand-side up to right-hand-side. */ 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); } From patchwork Mon Oct 2 19:33:08 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: 728562 Delivered-To: patch@linaro.org Received: by 2002:a5d:60c8:0:b0:31d:da82:a3b4 with SMTP id x8csp1819962wrt; Mon, 2 Oct 2023 12:34:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHqcaFcLnbAS+rpzf+wCEHCsxqnZq53rOmqTjfnIa1ATpCu9jcQUtTHF2e3sWsYZvrXZHsj X-Received: by 2002:a17:906:30d2:b0:9b6:53f4:6d9e with SMTP id b18-20020a17090630d200b009b653f46d9emr2079752ejb.30.1696275246603; Mon, 02 Oct 2023 12:34:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696275246; cv=none; d=google.com; s=arc-20160816; b=M2D72zeNbRY8bHUuPQ1A2Becig7WErDbXEqhrGUqO2hihdSX1pKl76CU6qNo2ux8UB TC5juL2uyxGLsp1wHzjEAo7gU42X1+7bbkqm0kr1qVibVtcyscDVG8mirpo7cr/cuXch xFOs80tM2SYjUcva8y2yzCGLyEBkxEd5YCEeJdh+CiCazy65xuPgZqmKtepRik4NtaP4 xxh95QNEU5dRhdlV7vNIvmtRcrI5+cxj2kjRVT/RaapgFF2ID53ppb8/kwLRTHgBsmFh 6kpeo3vG5c6GjeGJmsLkO8PKS4BAFEGO/3wQs0Q4f70STzbcXNwCfYtzNZFQUib1pIkZ V5UQ== 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=sADUp0XaqB4TBVHySLrhuZFvYiKFPkZWZjsa2k1NPuM=; fh=ubzLPtaquqORyAJ/TX35zypB35/iXKzZJOEWlgP8mu4=; b=t1FVjETm6rtxVFLR9Dm9JGUiB+ZlwAeBdGkeyQLfPxyPL3LgzSzT9v6ha0gVr4XWFI Va5bDgOIDkYZcJSk2VdBMOYYkO6wkbOqzW4SbLvKqLSTEdvqwq5dOa9yf9jAevlmH7+A Y+wgH5ezHnLteyhTNEfTsXkUvLfFh+mlhY+lOzMs6t0TOcY/1ng3hXZ+zDQNQJCCKyeY qPuhKwVeYQDXjWaoDmqvmIXB0vPYVa6lpC06WGvLWTfUdj+hRUtJSX2TNDqCan6qzjHI 0scGLZBjhla7duUKUglAzkJdYmssS3UoT8mcVYM+eGyW/1xpgp9xx8jtODHz9dDtMk2Q esCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ojRiwOSL; 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 rs22-20020a170907037600b009a1c2fcdceasi18900094ejb.109.2023.10.02.12.34.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:34:06 -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=ojRiwOSL; 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 0B14E385C6E9 for ; Mon, 2 Oct 2023 19:34:05 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id 67136385773F for ; Mon, 2 Oct 2023 19:33:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 67136385773F 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-x632.google.com with SMTP id d9443c01a7336-1c3bd829b86so1455365ad.0 for ; Mon, 02 Oct 2023 12:33:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696275204; x=1696880004; 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=sADUp0XaqB4TBVHySLrhuZFvYiKFPkZWZjsa2k1NPuM=; b=ojRiwOSL7/rUGSjZqB11VBZ8U4ywbV8qdxrvQmWKFNNmU3nDi0CxJ0y2z3LGGijTgi bHXjXwNe1R1EXmB5YFI8xceYjC/TaUzaTHCiDTPVFIfJyyiNm6cLgHzxThEBepgxxOTF mShtyGhliMvLOOCUdTt5VN9do/IxRW6MIS4vO6Oi5cURuiZDytytTdSebKEb4rq9YURl 4RurOZ36o/IH0SP3j37RaA5iOzuGhfSNBNNXGbuLZxc00nLJ09hjgjYfejke/Me0ZrVI 70cdodcu7rn0+toHuhG3bQ2AENxcrsqL8ETTC18iYwO4XN9e0SpP1LAzfGrCraZ7a3lF uXsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696275204; x=1696880004; 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=sADUp0XaqB4TBVHySLrhuZFvYiKFPkZWZjsa2k1NPuM=; b=BzW3+UUgqUtD1ewpKXb8SRE2fUv3IbYdtjirjSlepuCO10IZ0ItU9F32O8ZcguH23z 3UVav48La+Cj82ss3kxVgczDuF3ZivPgNum49GUeglCYDO5K57lYTujkusTSItpVN697 ACpCVPB3rv29nQso0r2Fm8sSLA8VXFNpZ7Tt8aacNe65IWDsUsBUuO97Y7JYc9NgLKis a9sBjSFrWVl2A+X4lijh0RiFM6AE5gAyu+RoLYvOQUT5t3p+rgTO1VwmWmtBx9N8IuvC B4XXWiyfUoe+1tB29+N6jYHkKPwYJr+02P7i1n7EoegReKGAvDE6hyfjG3IL/GviErUs OUfQ== X-Gm-Message-State: AOJu0Yx20kg7stitxfdybc3rjPFA2/OV9iuBP9SfsXwO3zERDruNqO4a ugLsd0AL4AV6hZppII3i/kezadkDvE23vc/QGqOoyQ== X-Received: by 2002:a17:902:e5c1:b0:1c3:aa8d:4daa with SMTP id u1-20020a170902e5c100b001c3aa8d4daamr11464664plf.44.1696275204101; Mon, 02 Oct 2023 12:33:24 -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.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:33:23 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Noah Goldstein , Paul Eggert , Florian Weimer Subject: [PATCH v8 4/7] stdlib: qsort: Move some macros to inline function Date: Mon, 2 Oct 2023 16:33:08 -0300 Message-Id: <20231002193311.3985890-5-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 | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/stdlib/qsort.c b/stdlib/qsort.c index 30e2999005..4082f5f9c1 100644 --- a/stdlib/qsort.c +++ b/stdlib/qsort.c @@ -99,15 +99,28 @@ typedef struct char *hi; } stack_node; -/* The next 4 #defines implement a very fast in-line stack abstraction. */ /* The stack needs log (total_elements) entries (we could even subtract log(MAX_THRESH)). Since total_elements has type size_t, we get as upper bound for log (total_elements): bits per byte (CHAR_BIT) * sizeof(size_t). */ -#define STACK_SIZE (CHAR_BIT * sizeof (size_t)) -#define PUSH(low, high) ((void) ((top->lo = (low)), (top->hi = (high)), ++top)) -#define POP(low, high) ((void) (--top, (low = top->lo), (high = top->hi))) -#define STACK_NOT_EMPTY (stack < top) +enum { STACK_SIZE = CHAR_BIT * sizeof (size_t) }; + +static inline stack_node * +push (stack_node *top, char *lo, char *hi) +{ + top->lo = lo; + top->hi = hi; + return ++top; +} + +static inline stack_node * +pop (stack_node *top, char **lo, char **hi) +{ + --top; + *lo = top->lo; + *hi = top->hi; + return top; +} static inline void @@ -211,11 +224,9 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, char *lo = base_ptr; char *hi = &lo[size * (total_elems - 1)]; stack_node stack[STACK_SIZE]; - stack_node *top = stack; - - PUSH (NULL, NULL); + stack_node *top = stack + 1; - while (STACK_NOT_EMPTY) + while (stack < top) { char *left_ptr; char *right_ptr; @@ -280,7 +291,7 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, { if ((size_t) (hi - left_ptr) <= max_thresh) /* Ignore both small partitions. */ - POP (lo, hi); + top = pop (top, &lo, &hi); else /* Ignore small left partition. */ lo = left_ptr; @@ -291,13 +302,13 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, else if ((right_ptr - lo) > (hi - left_ptr)) { /* Push larger left partition indices. */ - PUSH (lo, right_ptr); + top = push (top, lo, right_ptr); lo = left_ptr; } else { /* Push larger right partition indices. */ - PUSH (left_ptr, hi); + top = push (top, left_ptr, hi); hi = right_ptr; } } From patchwork Mon Oct 2 19:33:09 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: 728563 Delivered-To: patch@linaro.org Received: by 2002:a5d:60c8:0:b0:31d:da82:a3b4 with SMTP id x8csp1820046wrt; Mon, 2 Oct 2023 12:34:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGskLAeJt47UXNHo57wSGBUOql8wa/UWUmOai4OhoWjXd6VgwLiNRqnE5ztm/28A+bsSiEi X-Received: by 2002:aa7:c0ca:0:b0:521:d2ab:e4df with SMTP id j10-20020aa7c0ca000000b00521d2abe4dfmr520212edp.19.1696275259982; Mon, 02 Oct 2023 12:34:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696275259; cv=none; d=google.com; s=arc-20160816; b=lgHM5n6sb0yX3QV0YEnsEJK46GZra7o55fUdg6aMWauE4TnENsGPlwsL734c8HpA5G M3Cn9YSjBnwPB50O5CfILEWm6ppunVJHTVPIwoK0+6dv/981CTmtMd4JSGJbNlPN3q+I DfIGJrreyIUzpzPdyB0Y30gIc3bQNONOoOUQysJTiKXkM7qS7kg3oNXGTs1eBRgLPxE2 /+fZujuzhHbeKSLz5d9UpILowpAHwhwbW6XW2C1siVBkXX8PutI9hShrSLpW+3zDj05k hm2kZqCoRGLNXhG2Q7iIktUxSBHjKjmVj02YffXEqNgYzUHEUBzhVSaBRc6hS3oB3j7T ge7Q== 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=LVH9FPyqXhMnof20qz68Oz9ngaGXWyE1ZxEz6o7y/lg=; fh=ubzLPtaquqORyAJ/TX35zypB35/iXKzZJOEWlgP8mu4=; b=iVBNdN2x2zri+4wTnjbdf1gs+Y7aIA9p1ezZtiukJPEGj5gdlUL4fuwpoUvD5y7xVW mHiFFWOvL+j9hufxTtkXgBqj9QkueaIC0472zPDwVuq3CsuyjDmp1y25UCMc26vldi4i KkrdQ5paXVjOYMrWbsVKFicn5zwRRBbL4anNEbb4tdhNqsdWxqZwqW9PNJig07k7I8wm uwZapyv4yU3WGMvxn8GtTNAhlCtu56o/NW+SefsrRwJU00R3te2LWInB8xmQwKSN+jgF WbHKdZ6cscB5xrr2fGUuNfR9kksuoMbxdhGs8E/dpgfhVMD/JcLoCg4dOyVgDxPwPp/n DMkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="W/Y+ofvJ"; 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 z5-20020aa7d405000000b00532c6fcb85esi18356525edq.143.2023.10.02.12.34.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:34:19 -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="W/Y+ofvJ"; 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 A90B1386188F for ; Mon, 2 Oct 2023 19:34:18 +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 B54A63858024 for ; Mon, 2 Oct 2023 19:33:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B54A63858024 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-1c60778a3bfso1301935ad.1 for ; Mon, 02 Oct 2023 12:33:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696275206; x=1696880006; 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=LVH9FPyqXhMnof20qz68Oz9ngaGXWyE1ZxEz6o7y/lg=; b=W/Y+ofvJdq4b8xnt5gXpF0UgeUdZ/oMSZQlyJVgMj7pCOeLVW5CZq3vG2/46qbwqNZ Gz4EULsJbaMi7U1+G7iOX/qqqI/8PU9qLyT+O2mbgUEwaDQXq/pB5RCom4UnNEgGm1Y7 1W7bGm6TO9+Wv5mUVi1tjT2R/hIIQuXFZ73eOZZsm+xMrvEJhShcqlO/bJWKrXSAeYxC 7tmjcQwnbLHpilSBXcZBEcb1gcgCeI7spUvjaAS4NYr4jQ58vjOmZq2DheARs4zYFAF+ 3k6r+RdYBidbaBqiDZy8JpLEFR/2nJdVxBtSOPk7U6oGiG3XKMqMyU8EVrGZYT7kcOue mOUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696275206; x=1696880006; 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=LVH9FPyqXhMnof20qz68Oz9ngaGXWyE1ZxEz6o7y/lg=; b=PyxHUE2lHxfFM7/X/1z5AT1BhOuMKq1wGJZqAPuWfCkn1QuGJvHdUwGbiMDylXXOeI M9XY8xt9RB9JEmgCvYZcpSf7R1CnE2NVUAHxFLlU7zYsWjy3Yw/OdUkGGlVdipnelCC/ 3Sv6dPmDXq2nYcHxXgfd1zXhiV7dMsyeVRQ+T4XLbgtQcFI2pFbgwC8PefJ/zkarKDYi Iymolo22cyYzogTRww6H9D7dRQpYiB/FBkKyfiuWuVcoWr9NVMXK4+6L2pycmYiNWSgc k2qETQ4p6hfn3AnnQYMFfjH4G3+WAOhELoUwosIgzlfNZf8K4KSdDvtFsM7s5v6DRTkW ccmw== X-Gm-Message-State: AOJu0YxAEFkTDsg3db4sA5I1UPLhah18syCQDdcgOKAhoBFdLlUPOH/E 4cOcBZSMLRhXGNd7yUQCheBrvvCiZXi38M70j3hFzA== X-Received: by 2002:a17:902:9a44:b0:1bd:aeb3:9504 with SMTP id x4-20020a1709029a4400b001bdaeb39504mr11784918plv.15.1696275206267; Mon, 02 Oct 2023 12:33:26 -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.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:33:25 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Noah Goldstein , Paul Eggert , Florian Weimer Subject: [PATCH v8 5/7] stdlib: Implement introsort for qsort (BZ 19305) Date: Mon, 2 Oct 2023 16:33:09 -0300 Message-Id: <20231002193311.3985890-6-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 This patch makes the quicksort implementation to acts as introsort, to avoid worse-case performance (and thus making it O(nlog n)). It switch to heapsort when the depth level reaches 2*log2(total elements). The heapsort is a textbook implementation. Checked on x86_64-linux-gnu. --- stdlib/qsort.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 7 deletions(-) diff --git a/stdlib/qsort.c b/stdlib/qsort.c index 4082f5f9c1..e160932657 100644 --- a/stdlib/qsort.c +++ b/stdlib/qsort.c @@ -97,6 +97,7 @@ typedef struct { char *lo; char *hi; + size_t depth; } stack_node; /* The stack needs log (total_elements) entries (we could even subtract @@ -106,22 +107,83 @@ typedef struct enum { STACK_SIZE = CHAR_BIT * sizeof (size_t) }; static inline stack_node * -push (stack_node *top, char *lo, char *hi) +push (stack_node *top, char *lo, char *hi, size_t depth) { top->lo = lo; top->hi = hi; + top->depth = depth; return ++top; } static inline stack_node * -pop (stack_node *top, char **lo, char **hi) +pop (stack_node *top, char **lo, char **hi, size_t *depth) { --top; *lo = top->lo; *hi = top->hi; + *depth = top->depth; return top; } +/* A fast, small, non-recursive O(nlog n) heapsort, adapted from Linux + lib/sort.c. Used on introsort implementation as a fallback routine with + worst-case performance of O(nlog n) and worst-case space complexity of + O(1). */ + +static inline void +siftdown (void *base, size_t size, size_t k, size_t n, + enum swap_type_t swap_type, __compar_d_fn_t cmp, void *arg) +{ + while (k <= n / 2) + { + size_t j = 2 * k; + if (j < n && cmp (base + (j * size), base + ((j + 1) * size), arg) < 0) + j++; + + if (cmp (base + (k * size), base + (j * size), arg) >= 0) + break; + + do_swap (base + (size * j), base + (k * size), size, swap_type); + k = j; + } +} + +static inline void +heapify (void *base, size_t size, size_t n, enum swap_type_t swap_type, + __compar_d_fn_t cmp, void *arg) +{ + size_t k = n / 2; + while (1) + { + siftdown (base, size, k, n, swap_type, cmp, arg); + if (k-- == 0) + break; + } +} + +static void +heapsort_r (void *base, void *end, size_t size, enum swap_type_t swap_type, + __compar_d_fn_t cmp, void *arg) +{ + const size_t count = ((uintptr_t) end - (uintptr_t) base) / size; + + if (count < 2) + return; + + size_t n = count - 1; + + /* Build the binary heap, largest value at the base[0]. */ + heapify (base, size, n, swap_type, cmp, arg); + + /* On each iteration base[0:n] is the binary heap, while base[n:count] + is sorted. */ + while (n > 0) + { + do_swap (base, base + (n * size), size, swap_type); + n--; + siftdown (base, size, 0, n, swap_type, cmp, arg); + } +} static inline void insertion_sort_qsort_partitions (void *const pbase, size_t total_elems, @@ -207,7 +269,7 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, const size_t max_thresh = MAX_THRESH * size; - if (total_elems == 0) + if (total_elems <= 1) /* Avoid lossage with unsigned arithmetic below. */ return; @@ -219,15 +281,26 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, else swap_type = SWAP_BYTES; + /* Maximum depth before quicksort switches to heapsort. */ + size_t depth = 2 * (sizeof (size_t) * CHAR_BIT - 1 + - __builtin_clzl (total_elems)); + if (total_elems > MAX_THRESH) { char *lo = base_ptr; char *hi = &lo[size * (total_elems - 1)]; stack_node stack[STACK_SIZE]; - stack_node *top = stack + 1; + stack_node *top = push (stack, NULL, NULL, depth); while (stack < top) { + if (depth == 0) + { + heapsort_r (lo, hi, size, swap_type, cmp, arg); + top = pop (top, &lo, &hi, &depth); + continue; + } + char *left_ptr; char *right_ptr; @@ -291,7 +364,7 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, { if ((size_t) (hi - left_ptr) <= max_thresh) /* Ignore both small partitions. */ - top = pop (top, &lo, &hi); + top = pop (top, &lo, &hi, &depth); else /* Ignore small left partition. */ lo = left_ptr; @@ -302,13 +375,13 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, else if ((right_ptr - lo) > (hi - left_ptr)) { /* Push larger left partition indices. */ - top = push (top, lo, right_ptr); + top = push (top, lo, right_ptr, depth - 1); lo = left_ptr; } else { /* Push larger right partition indices. */ - top = push (top, left_ptr, hi); + top = push (top, left_ptr, hi, depth - 1); hi = right_ptr; } } From patchwork Mon Oct 2 19:33:10 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: 728565 Delivered-To: patch@linaro.org Received: by 2002:a5d:60c8:0:b0:31d:da82:a3b4 with SMTP id x8csp1820179wrt; Mon, 2 Oct 2023 12:34:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF85/6hjdzgspHLLw2byBTa7EKwU3EW264pn7UPfueXVV8vxCTxSVvGCAcEodJWQamUZkut X-Received: by 2002:aa7:c55a:0:b0:532:c81b:99b1 with SMTP id s26-20020aa7c55a000000b00532c81b99b1mr10851058edr.18.1696275283287; Mon, 02 Oct 2023 12:34:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696275283; cv=none; d=google.com; s=arc-20160816; b=pcrndxusAqS+9CCUffQPrv/Tk9AdiXnfUcAOldDj+yRqr55PqwC1f7CwsPrD7ZwYeM 5pKucD5WkCVhEVdheFC0DoKwnZY+8SZauzpW36SudMCSCR9Pa6XigR0SDp2YfMJqgpym XGscqijI2abSbrl+9rgr/FADmUbwSX87tdSh5O9ElWO5Pik697C5sxA7b0OJFHJ7lMV+ GjuDcEW0RhMXmeNFtX7JUJ/wHNT54ZhlA1qDMPWLVvhbguNRH3yy4U8RsL87DM2Di3HG wWmwmZDezFbxLeNWPSlhtX/I8BPbayvbLtKhCsPDebBrR9gnkG5I6JDtU6EclVaqPtkq CaUg== 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=llw7zqHijMQZ7ttL5JQP68kCyMBi6d2oaS8V1jxVogk=; fh=ubzLPtaquqORyAJ/TX35zypB35/iXKzZJOEWlgP8mu4=; b=cSYLjUrflrwMKrIisZ0K8giMU6FDJ8+lFyC3zANEwx9jC74zfW+kLok4Pf/OP7F6+D k9vxo0b9IchXfw38UANCeXWUtq7nkPxQd01bKuWzM9RNX8cJ50x/dOAcXTtZ1Mib1TR6 u0C8k946Qqfhrisk08ZxlvX3O9Jc00VFP0e3fW4TYRBSE/pN7aSJzYYfqgeNAo6RNqQ7 lhueVw6vcf6BPMfXV0B5Zvxw2cDtza5HjKu7pOGZ+b4oCY3RCGZeH9eIEA6dcHz3gBre RYNL085XVLbTIUkNHFwYQyPfitfIu9nvEAuf7Ed5wyjxi+L/vHr88pvRZh9MGjR1Id3f cPUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TajqQg8C; 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 bo9-20020a0564020b2900b00530e8a1bc9fsi2841313edb.208.2023.10.02.12.34.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:34:43 -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=TajqQg8C; 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 301E338323C8 for ; Mon, 2 Oct 2023 19:34:42 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by sourceware.org (Postfix) with ESMTPS id 0D3CB3858C62 for ; Mon, 2 Oct 2023 19:33:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0D3CB3858C62 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-x631.google.com with SMTP id d9443c01a7336-1c0ecb9a075so1262595ad.2 for ; Mon, 02 Oct 2023 12:33:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696275208; x=1696880008; 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=llw7zqHijMQZ7ttL5JQP68kCyMBi6d2oaS8V1jxVogk=; b=TajqQg8Cx8AS4j2YsaAOtz7fywPWq00/N9qQH/X4tyYbaK91uRwopBdMXMYSoIAzgT qfPAjKVhEOQ8A0pIluU98pJUl3T4N80UXJ3UgJGEpevFq2zaasBG5HgQwPTUnxmVjnmb q4LWtEoSQ3FtdOi8095lBYudp9GKNXPRQsqNiuJJ5o/FXbzLsNVWGl3rDJ7xHSTLmXl6 tQlq/+nZJr5FXwJH7LNXeUTcQJPsS239A+JEgIC7NSq9BUO7/AjmyObkwji7NVFfzvma R/EgNeQ1/2PYIpMCXlvIzxXCihSyTVMLE2nj8ewlh6Oy7XiflMLpib2KgVPPHKYV2t/N tq7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696275208; x=1696880008; 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=llw7zqHijMQZ7ttL5JQP68kCyMBi6d2oaS8V1jxVogk=; b=fjP1zs0ZThvJ8Vzf1q+fP57N5M3aXZy7mnYOzABBt6QGb6ZTgJkdZ/0ljQjh8gCcFc rlW16lIcw9RLjBRvFoiL0ZCnKqVkKTPwgLfsWzK3qAIwBwmsJn7Pbeo2uk7NcdqbfolE EEvP/ylBhwxrcuw+DBCAlskeNFDqTN/BoNqMAMwslkf6D8rq6XgmAuVMLQYTXWlHKyfT jx3O45I1Pq3A5vjda3twfQC4rx93HOv4do3VN8P+zFGDxQddz44zBTWhAK9p/afu/V0W IiOZtOF4Fu6sQHhx9hDvHbo3Q00hEWU0S3VzTw27a+GIQ5MdKwmWYC33WAkJ35Y66Dqf Rqsw== X-Gm-Message-State: AOJu0Yxomai7tgNFI2/XD8XzfZexD5hGNf5pFtRBTJRRvDN36lT8eYDq imeKHLEBDwExaNDyi3gS+904seGuy3zaYSFYWO/T/w== X-Received: by 2002:a17:902:ea0c:b0:1c6:3157:29f3 with SMTP id s12-20020a170902ea0c00b001c6315729f3mr11394806plg.36.1696275208326; Mon, 02 Oct 2023 12:33:28 -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.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:33:27 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Noah Goldstein , Paul Eggert , Florian Weimer Subject: [PATCH v8 6/7] stdlib: Remove use of mergesort on qsort (BZ 21719) Date: Mon, 2 Oct 2023 16:33:10 -0300 Message-Id: <20231002193311.3985890-7-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.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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 This patch removes the mergesort optimization on qsort implementation and uses the introsort instead. The mergesort implementation has some issues: - It is as-safe only for certain types sizes (if total size is less than 1 KB with large element sizes also forcing memory allocation) which contradicts the function documentation. Although not required by the C standard, it is preferable and doable to have an O(1) space implementation. - The malloc for certain element size and element number adds arbitrary latency (might even be worse if malloc is interposed). - To avoid trigger swap from memory allocation the implementation relies on system information that might be virtualized (for instance VMs with overcommit memory) which might lead to potentially use of swap even if system advertise more memory than actually has. The check also have the downside of issuing syscalls where none is expected (although only once per execution). - The mergesort is suboptimal on an already sorted array (BZ#21719). The introsort implementation is already optimized to use constant extra space (due to the limit of total number of elements from maximum VM size) and thus can be used to avoid the malloc usage issues. Resulting performance is slower due the usage of qsort, specially in the worst-case scenario (partialy or sorted arrays) and due the fact mergesort uses a slight improved swap operations. This change also renders the BZ#21719 fix unrequired (since it is meant to fix the sorted input performance degradation for mergesort). The manual is also updated to indicate the function is now async-cancel safe. Checked on x86_64-linux-gnu. --- include/stdlib.h | 2 - manual/argp.texi | 2 +- manual/locale.texi | 3 +- manual/search.texi | 7 +- stdlib/Makefile | 2 - stdlib/msort.c | 309 --------------------------------------------- stdlib/qsort.c | 14 +- 7 files changed, 16 insertions(+), 323 deletions(-) delete mode 100644 stdlib/msort.c diff --git a/include/stdlib.h b/include/stdlib.h index 0ed8271d9b..580da9be15 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -149,8 +149,6 @@ extern int __posix_openpt (int __oflag) attribute_hidden; extern int __add_to_environ (const char *name, const char *value, const char *combines, int replace) attribute_hidden; -extern void _quicksort (void *const pbase, size_t total_elems, - size_t size, __compar_d_fn_t cmp, void *arg); extern int __on_exit (void (*__func) (int __status, void *__arg), void *__arg); diff --git a/manual/argp.texi b/manual/argp.texi index 0023441812..b77ad68285 100644 --- a/manual/argp.texi +++ b/manual/argp.texi @@ -735,7 +735,7 @@ for options, bad phase of the moon, etc. @c hol_set_group ok @c hol_find_entry ok @c hol_sort @mtslocale @acucorrupt -@c qsort dup @acucorrupt +@c qsort dup @c hol_entry_qcmp @mtslocale @c hol_entry_cmp @mtslocale @c group_cmp ok diff --git a/manual/locale.texi b/manual/locale.texi index 720e0ca952..f6afa5dc44 100644 --- a/manual/locale.texi +++ b/manual/locale.texi @@ -253,7 +253,7 @@ The symbols in this section are defined in the header file @file{locale.h}. @c calculate_head_size ok @c __munmap ok @c compute_hashval ok -@c qsort dup @acucorrupt +@c qsort dup @c rangecmp ok @c malloc @ascuheap @acsmem @c strdup @ascuheap @acsmem @@ -275,7 +275,6 @@ The symbols in this section are defined in the header file @file{locale.h}. @c realloc @ascuheap @acsmem @c realloc @ascuheap @acsmem @c fclose @ascuheap @asulock @acsmem @acsfd @aculock -@c qsort @ascuheap @acsmem @c alias_compare dup @c libc_lock_unlock @aculock @c _nl_explode_name @ascuheap @acsmem diff --git a/manual/search.texi b/manual/search.texi index 5691bf2f2b..a550858478 100644 --- a/manual/search.texi +++ b/manual/search.texi @@ -159,7 +159,7 @@ To sort an array using an arbitrary comparison function, use the @deftypefun void qsort (void *@var{array}, size_t @var{count}, size_t @var{size}, comparison_fn_t @var{compare}) @standards{ISO, stdlib.h} -@safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@acucorrupt{}}} +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} The @code{qsort} function sorts the array @var{array}. The array contains @var{count} elements, each of which is of size @var{size}. @@ -199,9 +199,8 @@ Functions}): The @code{qsort} function derives its name from the fact that it was originally implemented using the ``quick sort'' algorithm. -The implementation of @code{qsort} in this library might not be an -in-place sort and might thereby use an extra amount of memory to store -the array. +The implementation of @code{qsort} in this library is an in-place sort +and uses a constant extra space (allocated on the stack). @end deftypefun @node Search/Sort Example diff --git a/stdlib/Makefile b/stdlib/Makefile index 25e42a77e7..095518eef4 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -96,7 +96,6 @@ routines := \ mbtowc \ mrand48 \ mrand48_r \ - msort \ nrand48 \ nrand48_r \ old_atexit \ @@ -380,7 +379,6 @@ generated += \ # generated CFLAGS-bsearch.c += $(uses-callbacks) -CFLAGS-msort.c += $(uses-callbacks) CFLAGS-qsort.c += $(uses-callbacks) CFLAGS-system.c += -fexceptions CFLAGS-system.os = -fomit-frame-pointer diff --git a/stdlib/msort.c b/stdlib/msort.c deleted file mode 100644 index bbaa5e9f82..0000000000 --- a/stdlib/msort.c +++ /dev/null @@ -1,309 +0,0 @@ -/* An alternative to qsort, with an identical interface. - This file is part of the GNU C Library. - Copyright (C) 1992-2023 Free Software Foundation, Inc. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#include -#include -#include -#include -#include -#include -#include -#include - -struct msort_param -{ - size_t s; - size_t var; - __compar_d_fn_t cmp; - void *arg; - char *t; -}; -static void msort_with_tmp (const struct msort_param *p, void *b, size_t n); - -static void -msort_with_tmp (const struct msort_param *p, void *b, size_t n) -{ - char *b1, *b2; - size_t n1, n2; - - if (n <= 1) - return; - - n1 = n / 2; - n2 = n - n1; - b1 = b; - b2 = (char *) b + (n1 * p->s); - - msort_with_tmp (p, b1, n1); - msort_with_tmp (p, b2, n2); - - char *tmp = p->t; - const size_t s = p->s; - __compar_d_fn_t cmp = p->cmp; - void *arg = p->arg; - switch (p->var) - { - case 0: - while (n1 > 0 && n2 > 0) - { - if ((*cmp) (b1, b2, arg) <= 0) - { - *(uint32_t *) tmp = *(uint32_t *) b1; - b1 += sizeof (uint32_t); - --n1; - } - else - { - *(uint32_t *) tmp = *(uint32_t *) b2; - b2 += sizeof (uint32_t); - --n2; - } - tmp += sizeof (uint32_t); - } - break; - case 1: - while (n1 > 0 && n2 > 0) - { - if ((*cmp) (b1, b2, arg) <= 0) - { - *(uint64_t *) tmp = *(uint64_t *) b1; - b1 += sizeof (uint64_t); - --n1; - } - else - { - *(uint64_t *) tmp = *(uint64_t *) b2; - b2 += sizeof (uint64_t); - --n2; - } - tmp += sizeof (uint64_t); - } - break; - case 2: - while (n1 > 0 && n2 > 0) - { - unsigned long *tmpl = (unsigned long *) tmp; - unsigned long *bl; - - tmp += s; - if ((*cmp) (b1, b2, arg) <= 0) - { - bl = (unsigned long *) b1; - b1 += s; - --n1; - } - else - { - bl = (unsigned long *) b2; - b2 += s; - --n2; - } - while (tmpl < (unsigned long *) tmp) - *tmpl++ = *bl++; - } - break; - case 3: - while (n1 > 0 && n2 > 0) - { - if ((*cmp) (*(const void **) b1, *(const void **) b2, arg) <= 0) - { - *(void **) tmp = *(void **) b1; - b1 += sizeof (void *); - --n1; - } - else - { - *(void **) tmp = *(void **) b2; - b2 += sizeof (void *); - --n2; - } - tmp += sizeof (void *); - } - break; - default: - while (n1 > 0 && n2 > 0) - { - if ((*cmp) (b1, b2, arg) <= 0) - { - tmp = (char *) __mempcpy (tmp, b1, s); - b1 += s; - --n1; - } - else - { - tmp = (char *) __mempcpy (tmp, b2, s); - b2 += s; - --n2; - } - } - break; - } - - if (n1 > 0) - memcpy (tmp, b1, n1 * s); - memcpy (b, p->t, (n - n2) * s); -} - - -void -__qsort_r (void *b, size_t n, size_t s, __compar_d_fn_t cmp, void *arg) -{ - size_t size = n * s; - char *tmp = NULL; - struct msort_param p; - - /* For large object sizes use indirect sorting. */ - if (s > 32) - size = 2 * n * sizeof (void *) + s; - - if (size < 1024) - /* The temporary array is small, so put it on the stack. */ - p.t = __alloca (size); - else - { - /* We should avoid allocating too much memory since this might - have to be backed up by swap space. */ - static long int phys_pages; - static int pagesize; - - if (pagesize == 0) - { - phys_pages = __sysconf (_SC_PHYS_PAGES); - - if (phys_pages == -1) - /* Error while determining the memory size. So let's - assume there is enough memory. Otherwise the - implementer should provide a complete implementation of - the `sysconf' function. */ - phys_pages = (long int) (~0ul >> 1); - - /* The following determines that we will never use more than - a quarter of the physical memory. */ - phys_pages /= 4; - - /* Make sure phys_pages is written to memory. */ - atomic_write_barrier (); - - pagesize = __sysconf (_SC_PAGESIZE); - } - - /* Just a comment here. We cannot compute - phys_pages * pagesize - and compare the needed amount of memory against this value. - The problem is that some systems might have more physical - memory then can be represented with a `size_t' value (when - measured in bytes. */ - - /* If the memory requirements are too high don't allocate memory. */ - if (size / pagesize > (size_t) phys_pages) - { - _quicksort (b, n, s, cmp, arg); - return; - } - - /* It's somewhat large, so malloc it. */ - int save = errno; - tmp = malloc (size); - __set_errno (save); - if (tmp == NULL) - { - /* Couldn't get space, so use the slower algorithm - that doesn't need a temporary array. */ - _quicksort (b, n, s, cmp, arg); - return; - } - p.t = tmp; - } - - p.s = s; - p.var = 4; - p.cmp = cmp; - p.arg = arg; - - if (s > 32) - { - /* Indirect sorting. */ - char *ip = (char *) b; - void **tp = (void **) (p.t + n * sizeof (void *)); - void **t = tp; - void *tmp_storage = (void *) (tp + n); - - while ((void *) t < tmp_storage) - { - *t++ = ip; - ip += s; - } - p.s = sizeof (void *); - p.var = 3; - msort_with_tmp (&p, p.t + n * sizeof (void *), n); - - /* tp[0] .. tp[n - 1] is now sorted, copy around entries of - the original array. Knuth vol. 3 (2nd ed.) exercise 5.2-10. */ - char *kp; - size_t i; - for (i = 0, ip = (char *) b; i < n; i++, ip += s) - if ((kp = tp[i]) != ip) - { - size_t j = i; - char *jp = ip; - memcpy (tmp_storage, ip, s); - - do - { - size_t k = (kp - (char *) b) / s; - tp[j] = jp; - memcpy (jp, kp, s); - j = k; - jp = kp; - kp = tp[k]; - } - while (kp != ip); - - tp[j] = jp; - memcpy (jp, tmp_storage, s); - } - } - else - { - if ((s & (sizeof (uint32_t) - 1)) == 0 - && ((uintptr_t) b) % __alignof__ (uint32_t) == 0) - { - if (s == sizeof (uint32_t)) - p.var = 0; - else if (s == sizeof (uint64_t) - && ((uintptr_t) b) % __alignof__ (uint64_t) == 0) - p.var = 1; - else if ((s & (sizeof (unsigned long) - 1)) == 0 - && ((uintptr_t) b) - % __alignof__ (unsigned long) == 0) - p.var = 2; - } - msort_with_tmp (&p, b, n); - } - free (tmp); -} -libc_hidden_def (__qsort_r) -weak_alias (__qsort_r, qsort_r) - - -void -qsort (void *b, size_t n, size_t s, __compar_fn_t cmp) -{ - return __qsort_r (b, n, s, (__compar_d_fn_t) cmp, NULL); -} -libc_hidden_def (qsort) diff --git a/stdlib/qsort.c b/stdlib/qsort.c index e160932657..470b09b10e 100644 --- a/stdlib/qsort.c +++ b/stdlib/qsort.c @@ -19,7 +19,6 @@ Engineering a sort function; Jon Bentley and M. Douglas McIlroy; Software - Practice and Experience; Vol. 23 (11), 1249-1265, 1993. */ -#include #include #include #include @@ -262,8 +261,8 @@ insertion_sort_qsort_partitions (void *const pbase, size_t total_elems, stack size is needed (actually O(1) in this case)! */ void -_quicksort (void *const pbase, size_t total_elems, size_t size, - __compar_d_fn_t cmp, void *arg) +__qsort_r (void *const pbase, size_t total_elems, size_t size, + __compar_d_fn_t cmp, void *arg) { char *base_ptr = (char *) pbase; @@ -395,3 +394,12 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, insertion_sort_qsort_partitions (pbase, total_elems, size, swap_type, cmp, arg); } +libc_hidden_def (__qsort_r) +weak_alias (__qsort_r, qsort_r) + +void +qsort (void *b, size_t n, size_t s, __compar_fn_t cmp) +{ + return __qsort_r (b, n, s, (__compar_d_fn_t) cmp, NULL); +} +libc_hidden_def (qsort) From patchwork Mon Oct 2 19:33:11 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: 728564 Delivered-To: patch@linaro.org Received: by 2002:a5d:60c8:0:b0:31d:da82:a3b4 with SMTP id x8csp1820097wrt; Mon, 2 Oct 2023 12:34:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHD6Oz5y3fItIvYOHBAUDMBOgoJLn1tJt0DkUJ0hhfusdTTtHjP7LCQ71kIWUWxv2uNOu78 X-Received: by 2002:a17:906:530b:b0:9a9:9c6c:c4ef with SMTP id h11-20020a170906530b00b009a99c6cc4efmr11442160ejo.23.1696275269500; Mon, 02 Oct 2023 12:34:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696275269; cv=none; d=google.com; s=arc-20160816; b=d3KTwcsh88czFOMpPVG+SiIxhg6JRfe/4/pNfAH5fS1t8xi+eBmzRzlAcRB7bS/WI5 Dg4jg3C6ZSsmVGuVHAtn8dlY+1he8t2qgS1SBkijJcN4v2GJB7TPJycZtbOZt0psFQoU dDN/+g7UqrX/39ma6fUbhv12suIPtpR7XVJdJfIEEh6VRd6aGDvvsw7Mhin44Y+4T4mn Vtl8Sz3xTPH1niKqADjpGgYRDqH7fJpeMy4qw1jPBN8nOZ4re0jYaZqbpkYrc7rQS6R1 iow9CtgfgVq6CG3jjmHJkYdNfwO9b+T/BzgHeG19pJJaow6DdRwSIYvGQKzzQHRgNagF CxjA== 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=7TcY49J55BYUyAkq/YRTuBz+HDpV/m8KrGvfXTPX2bw=; fh=ubzLPtaquqORyAJ/TX35zypB35/iXKzZJOEWlgP8mu4=; b=ljxqGr+Oqm7VcQylP9E1IsF3r5b1e/0hDD7qUy2qaJHNKuWVdQuz+2TYbayp1IZCw8 49Rm5w2tkZ0agzYgX/Ojc+LBW4zLaLkuLKCEv/dLIau4yqpYwJ42fKQedw8Vj45rkqCF W2KUjnKz8rwCeMtchBP/QrwvfoquTgHS6uNtIMAL53BDs1/9jj+RKv0A79gCZj7yV8gR fbOQWU2SSA35T5Y9KG0JlUYgZ5O7MYowifVhyznDbJNdZEFtB1Gkg86Nl55mFR9sW4kG 7fKsmbd5RG8gJhF+g0i8PsOHscriZMSlAf5taGYenujGJTyzxdpvxU37H8e89ZPPJhDv oAUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=jTWwYFmF; 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 a11-20020a17090682cb00b00997dbbda750si19667164ejy.839.2023.10.02.12.34.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:34:29 -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=jTWwYFmF; 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 2D3F03865C32 for ; Mon, 2 Oct 2023 19:34:28 +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 181CA3857026 for ; Mon, 2 Oct 2023 19:33:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 181CA3857026 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-1c724577e1fso1366185ad.0 for ; Mon, 02 Oct 2023 12:33:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696275210; x=1696880010; 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=7TcY49J55BYUyAkq/YRTuBz+HDpV/m8KrGvfXTPX2bw=; b=jTWwYFmFNx/k8nD4pY/ns72xRpGAR1EoXqXXpSEPuesoyU5BM5P1xq4Pnu/x9zl7kK 5UsCyIyUjqDvketE4WFxCLZQEPHXTb0k5uHMb8foIJJALDYpPzcD31/cZjmHxsgk8DCw cRKaSRdHUVgBGUY8uBeD3l6xYtPTISIAtRcw7a1Sxu8mISoVFO2Kp6HDnsOrccEflo2J vDipTzmBPEtcpurPHP5m+JOcq/3JUYh6xxkbZnHm3prGUgiJi2XDKs9a2R1TPMlOaH7n FVZ1R7CkAOrs7/3cJC4ef5cg0WR26aOgcctOy1n7XOLs5o9Alp7u0BDG7TJCDGKg50jX Xl9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696275210; x=1696880010; 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=7TcY49J55BYUyAkq/YRTuBz+HDpV/m8KrGvfXTPX2bw=; b=CbMX7Dmu86n0tDidY8w5+Et2qtQqpYX8qzcRQNDBg/zDc9X1X+aFTx9Z527pOirjRh Zm/s4mc3QKlaPWiqMOU8QrSbkTJHdz7QgJh/zBzHLs0I1U+6rorjGOwAvDZl8ESV4zch nxz4Xu4Hyx0Pa4oPUx9XL7L/nlVWoZGd9ZzFYsQs6CAUvMxEq3twIU56P/pU2i5hqxPy I/bPHX+VCY1HTFal6xJQ2IQo85AC1A0j5v2CHusmIFfbDqF3sAJOnd4+eisS+IaRVpN7 mV3MfQoWPCeTWZ6KObtgWJf8HfhGWLYAvAZxHSx69h54jHBXSDFLdxskS9C3BNcnfFFW COdQ== X-Gm-Message-State: AOJu0YwgJK2wnuawp/KHqEjaUgymasZRco1gD4JrQcJNwXU2Nc7vBIvR WoE3bYwXegW8wbP+9Qu7XSPEIWZtx/tOxvLyS7oDPw== X-Received: by 2002:a17:902:f687:b0:1c7:56d8:9068 with SMTP id l7-20020a170902f68700b001c756d89068mr7354675plg.31.1696275210591; Mon, 02 Oct 2023 12:33:30 -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.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 12:33:29 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Noah Goldstein , Paul Eggert , Florian Weimer Subject: [PATCH v8 7/7] stdlib: Add more qsort{_r} coverage Date: Mon, 2 Oct 2023 16:33:11 -0300 Message-Id: <20231002193311.3985890-8-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.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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 This patch adds a qsort and qsort_r to trigger the worst case scenario for the quicksort (which glibc current lacks coverage). The test is done with random input, dfferent internal types (uint8_t, uint16_t, uint32_t, uint64_t, large size), and with different set of element numbers. Checked on x86_64-linux-gnu and i686-linux-gnu. --- stdlib/Makefile | 1 + stdlib/tst-qsort3.c | 366 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 367 insertions(+) create mode 100644 stdlib/tst-qsort3.c diff --git a/stdlib/Makefile b/stdlib/Makefile index 095518eef4..6af606136e 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -214,6 +214,7 @@ tests := \ tst-on_exit \ tst-qsort \ tst-qsort2 \ + tst-qsort3 \ tst-quick_exit \ tst-rand48 \ tst-rand48-2 \ diff --git a/stdlib/tst-qsort3.c b/stdlib/tst-qsort3.c new file mode 100644 index 0000000000..421560d744 --- /dev/null +++ b/stdlib/tst-qsort3.c @@ -0,0 +1,366 @@ +/* qsort(_r) tests to trigger worst case for quicksort. + Copyright (C) 2023 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef enum +{ + Sorted, + Random, + Repeated, + Bitonic, + Duplicated, +} arraytype_t; + +/* Ratio of total of elements which will be repeated. */ +static const double RepeatedRatio = 0.2; + +/* Ratio of duplicated element . */ +static const double DuplicatedRatio = 0.4; + +struct array_t +{ + arraytype_t type; + const char *name; +} static const arraytypes[] = +{ + { Sorted, "Sorted" }, + { Random, "Random" }, + { Repeated, "Repeated" }, + { Bitonic, "Bitonic" }, + { Duplicated, "Duplicated" }, +}; + +/* Return the index of BASE as interpreted as an array of elements + of size SIZE. */ +static inline void * +arr (void *base, size_t idx, size_t size) +{ + return (void*)((uintptr_t)base + (idx * size)); +} + +/* Functions used to check qsort. */ +static int +uint8_t_cmp (const void *a, const void *b) +{ + uint8_t ia = *(uint8_t*)a; + uint8_t ib = *(uint8_t*)b; + return (ia > ib) - (ia < ib); +} + +static int +uint16_t_cmp (const void *a, const void *b) +{ + uint16_t ia = *(uint16_t*)a; + uint16_t ib = *(uint16_t*)b; + return (ia > ib) - (ia < ib); +} + +static int +uint32_t_cmp (const void *a, const void *b) +{ + uint32_t ia = *(uint32_t*)a; + uint32_t ib = *(uint32_t*)b; + return (ia > ib) - (ia < ib); +} + +static int +uint64_t_cmp (const void *a, const void *b) +{ + uint64_t ia = *(uint64_t*)a; + uint64_t ib = *(uint64_t*)b; + return (ia > ib) - (ia < ib); +} + +#define LARGE_SIZE 47 + +static int +large_cmp (const void *a, const void *b) +{ + return memcmp (a, b, LARGE_SIZE); +} + +/* Function used to check qsort_r. */ +typedef enum +{ + UINT8_CMP_T, + UINT16_CMP_T, + UINT32_CMP_T, + UINT64_CMP_T, + LARGE_CMP_T +} type_cmp_t; + +static type_cmp_t +uint_t_cmp_type (size_t sz) +{ + switch (sz) + { + case sizeof (uint8_t): return UINT8_CMP_T; + case sizeof (uint16_t): return UINT16_CMP_T; + case sizeof (uint64_t): return UINT64_CMP_T; + case sizeof (uint32_t): return UINT32_CMP_T; + default: return LARGE_CMP_T; + } +} + +static int +uint_t_cmp (const void *a, const void *b, void *arg) +{ + type_cmp_t type = *(type_cmp_t*) arg; + switch (type) + { + case UINT8_CMP_T: return uint8_t_cmp (a, b); + case UINT32_CMP_T: return uint32_t_cmp (a, b); + case UINT16_CMP_T: return uint16_t_cmp (a, b); + case UINT64_CMP_T: return uint64_t_cmp (a, b); + default: return large_cmp (a, b); + } +} + +static void +seq (void *elem, size_t type_size, int value) +{ + if (type_size == sizeof (uint8_t)) + *(uint8_t*)elem = value; + else if (type_size == sizeof (uint16_t)) + *(uint16_t*)elem = value; + else if (type_size == sizeof (uint32_t)) + *(uint32_t*)elem = value; + else if (type_size == sizeof (uint64_t)) + *(uint64_t*)elem = value; + else + memset (elem, value, type_size); +} + +static void +fill_array (void *array, void *refarray, size_t nmemb, size_t type_size, + arraytype_t type) +{ + size_t size = nmemb * type_size; + + switch (type) + { + case Sorted: + for (size_t i = 0; i < nmemb; i++) + seq (arr (array, i, type_size), type_size, i); + break; + + case Random: + arc4random_buf (array, size); + break; + + case Repeated: + { + arc4random_buf (array, size); + + void *randelem = xmalloc (type_size); + arc4random_buf (randelem, type_size); + + /* Repeat REPEATED elements (based on RepeatRatio ratio) in the random + array. */ + size_t repeated = (size_t)(nmemb * RepeatedRatio); + for (size_t i = 0; i < repeated; i++) + { + size_t pos = arc4random_uniform (nmemb - 1); + memcpy (arr (array, pos, type_size), randelem, type_size); + } + free (randelem); + } + break; + + case Bitonic: + { + size_t i; + for (i = 0; i < nmemb / 2; i++) + seq (arr (array, i, type_size), type_size, i); + for ( ; i < nmemb; i++) + seq (arr (array, i, type_size), type_size, (nmemb - 1) - i); + } + break; + + case Duplicated: + { + int randelem1 = arc4random (); + for (size_t i = 0; i < nmemb; i++) + seq (arr (array, i, type_size), type_size, randelem1); + + size_t duplicates = (size_t)(nmemb * DuplicatedRatio); + int randelem2 = arc4random (); + for (size_t i = 0; i < duplicates; i++) + { + size_t pos = arc4random_uniform (nmemb - 1); + seq (arr (array, pos, type_size), type_size, randelem2); + } + } + break; + } + + memcpy (refarray, array, size); +} + +typedef int (*cmpfunc_t)(const void *, const void *); + +/* Simple insertion sort to use as reference sort. */ +static void +qsort_r_ref (void *p, size_t n, size_t s, __compar_d_fn_t cmp, void *arg) +{ + if (n <= 1) + return; + + int i = 1; + char tmp[s]; + while (i < n) + { + memcpy (tmp, arr (p, i, s), s); + int j = i - 1; + while (j >= 0 && cmp (arr (p, j, s), tmp, arg) > 0) + { + memcpy (arr (p, j + 1, s), arr (p, j, s), s); + j = j - 1; + } + memcpy (arr (p, j + 1, s), tmp, s); + i = i + 1; + } +} + +static void +qsort_ref (void *b, size_t n, size_t s, __compar_fn_t cmp) +{ + return qsort_r_ref (b, n, s, (__compar_d_fn_t) cmp, NULL); +} + +/* Check if ARRAY of total NMEMB element of size SIZE is sorted + based on CMPFUNC. */ +static void +check_array (void *array, void *refarray, size_t nmemb, size_t type_size, + cmpfunc_t cmpfunc) +{ + for (size_t i = 1; i < nmemb; i++) + { + int ret = cmpfunc (arr (array, i, type_size), + arr (array, i-1, type_size)); + TEST_VERIFY_EXIT (ret >= 0); + } + + size_t size = nmemb * type_size; + TEST_COMPARE_BLOB (array, size, refarray, size); +} + +static void +check_qsort (void *buf, void *refbuf, size_t nelem, size_t type_size, + arraytype_t type, cmpfunc_t cmpfunc) +{ + fill_array (buf, refbuf, nelem, type_size, type); + + qsort (buf, nelem, type_size, cmpfunc); + qsort_ref (refbuf, nelem, type_size, cmpfunc); + + check_array (buf, refbuf, nelem, type_size, cmpfunc); +} + +static void +check_qsort_r (void *buf, void *refbuf, size_t nelem, size_t type_size, + arraytype_t type, cmpfunc_t cmpfunc) +{ + fill_array (buf, refbuf, nelem, type_size, type); + + type_cmp_t typecmp = uint_t_cmp_type (type_size); + + qsort_r (buf, nelem, type_size, uint_t_cmp, &typecmp); + qsort_r_ref (refbuf, nelem, type_size, uint_t_cmp, &typecmp); + + check_array (buf, refbuf, nelem, type_size, cmpfunc); +} + +static int +do_test (void) +{ + /* Some random sizes. */ + static const size_t nelems[] = { 0, 1, 7, 20, 32, 100, 256, 1024, 4256 }; + size_t max_nelems = 0; + for (int i = 0; i < array_length (nelems); i++) + if (nelems[i] > max_nelems) + max_nelems = nelems[i]; + + static const struct test_t + { + size_t type_size; + cmpfunc_t cmpfunc; + } + tests[] = + { + { sizeof (uint8_t), uint8_t_cmp }, + { sizeof (uint16_t), uint16_t_cmp }, + { sizeof (uint32_t), uint32_t_cmp }, + { sizeof (uint64_t), uint64_t_cmp }, + /* Test swap with large elements. */ + { LARGE_SIZE, large_cmp }, + }; + size_t max_type_size = 0; + for (int i = 0; i < array_length (tests); i++) + if (tests[i].type_size > max_type_size) + max_type_size = tests[i].type_size; + + void *buf = reallocarray (NULL, max_nelems, max_type_size); + TEST_VERIFY_EXIT (buf != NULL); + void *refbuf = reallocarray (NULL, max_nelems, max_type_size); + TEST_VERIFY_EXIT (refbuf != NULL); + + for (const struct test_t *test = tests; test < array_end (tests); ++test) + { + if (test_verbose > 0) + printf ("info: testing qsort with type_size=%zu\n", test->type_size); + for (const struct array_t *arraytype = arraytypes; + arraytype < array_end (arraytypes); + ++arraytype) + { + if (test_verbose > 0) + printf (" distribution=%s\n", arraytype->name); + for (const size_t *nelem = nelems; + nelem < array_end (nelems); + ++nelem) + { + if (test_verbose > 0) + printf (" nelem=%zu, total size=%zu\n", *nelem, + *nelem * test->type_size); + + check_qsort (buf, refbuf, *nelem, test->type_size, + arraytype->type, test->cmpfunc); + check_qsort_r (buf, refbuf, *nelem, test->type_size, + arraytype->type, test->cmpfunc); + } + } + } + + free (buf); + free (refbuf); + + return 0; +} + +#include