From patchwork Thu Sep 14 18:37:01 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: 722585 Delivered-To: patch@linaro.org Received: by 2002:adf:f0d1:0:b0:31d:da82:a3b4 with SMTP id x17csp551870wro; Thu, 14 Sep 2023 11:37:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHb+saC4pk3mHBMWNT4Rd9SW+5ULgb4v1vL3TT5Q8bPMIiXjOysnhC4IL2aevuxVmoCV446 X-Received: by 2002:a17:907:75d9:b0:9aa:2b9:4559 with SMTP id jl25-20020a17090775d900b009aa02b94559mr5250134ejc.41.1694716668930; Thu, 14 Sep 2023 11:37:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694716668; cv=none; d=google.com; s=arc-20160816; b=TNBXRksCZTLN/ZhO0l0An0SW0jFiPV8dvzZddF2FU8LnEkNYyH9mSMIBO6799qYh92 cuygEAebOCrMZUw35tGfGn0udnv6/xRI1FP16nMFnQBO30FOg+G5pZfJaAqLFW9sIpJX 9culIHZQI4gKdHaD3xR0Vagx7lFvcARnKf3A9L0yltKFfVE5kuT6Z7lzpEfcb3PSBIbT jc3YtKyBjkRFdHGKlXuTSczzmqU2nYcgkE67B6Ui3mdtBcgeUn8PTdnd2U4aGwey1jPP 7IwE6tF8qaZUlGFWccReKcsncQGp13QE+g/Y0CJiSELNEYnTMEscISQ5SKgix19eqXZf FkCg== 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=dvn5qwFxooFuMLzi1Dk1f26/fz9TaPl0kIsUl4ej2EuMpOQv6u6A+KaJ1I9ufgMiu7 hjGk6tN7lYvMhNYv11S268GcXMsx94pZ0SwWkBCaH2nXKLyAwSutyC1ZMG/AmF+s8LS3 nSs4J0HpCdZM19cZANRQ46kodkC5/LnQh3qMKveW/JLdk649BVWQ+lGzoi+NQ8/MzlIp n7SHODB2Eghby1Vsbxbn/7LImBTFAAoQrOlJp1jh/h/zEY55KgmobxDwg/85XG9qKRxJ qHBHg2OpeqbRr12k6mbeqpE8wIJTmLhTCE2X98v9E9D8qCqx2r2Q22aoqVZqg+lh39jS +3GQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="gZERv/Vw"; 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 qc25-20020a170906d8b900b00997e1a4b954si1814988ejb.25.2023.09.14.11.37.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 11:37:48 -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="gZERv/Vw"; 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 BBB4A385CCA4 for ; Thu, 14 Sep 2023 18:37:46 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x230.google.com (mail-oi1-x230.google.com [IPv6:2607:f8b0:4864:20::230]) by sourceware.org (Postfix) with ESMTPS id 0D3E0385CC84 for ; Thu, 14 Sep 2023 18:37:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0D3E0385CC84 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-oi1-x230.google.com with SMTP id 5614622812f47-3ab3aceaf2aso751800b6e.2 for ; Thu, 14 Sep 2023 11:37:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1694716638; x=1695321438; 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=gZERv/Vwdr710rwIR5CXw7XDlhIRTiYu55lyAO4OO90oTuSLyYfz00wEUrn4VdNza9 pNBTRGkodE5XMi8WV6O2OUqafgocHl+ARzl2xpZAmx1Sm9MSB51FCnU+Xs/ECs4iKC9Z 6eAbEfadEM4TdLlDuWJZ8bqRPznoPB7dQQwHJ5uRWrRgbT4VlccZI8UIIg2wn+XPIYhC KfVoJJeqai8rapDGfn0/dxa5m3XSzOmcOvrQDIzMgZ/A9EPGKZVPto5gIRMmpQ1IXnrU uciuhPgsHumlsNI5quWRAZYbq90107/Ea97+cZD1eKLSYaVuYaMspryUPt0UZfRsVTeC EEHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694716638; x=1695321438; 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=t/eIAajXNWNY/6/8JZ88HMp8qofklRUvdDHBu6lRVCb0HrjPDSiqPNtmoLqXHiQWyT Y29bFgVoZfzCkV3g9UD9YPGfnCtV1+ut8AaF/kGLeG9VqZ4N8Dr8m7qxUuL+cUmkVq4U 5m9922OhT9auH9EfuxmkeNfcAjdyrsjMqwLm2HygZlcaal9lIEm/lhda3exw+BLZ+SP3 MD2i2sqFV/kwf2wLKnp2GkoxxCuU0ZSXC94t1P7dz0RhkdJueSRX+SKgeH+eUMLwmOh9 OSbRyUGVvyXIulXF4zChP12NlmWl5N3ejpVliyMltnkc/Jn85EqW/ZQLSs1B0L2qYImU P3YA== X-Gm-Message-State: AOJu0YzjtlNIilpFQSwlrjy57AB2mFGLPEPYYy8h4PPd4q68RzF66Wnh wWSk3x74X+SpPs2bq/12AlkLZRfDS7CT/C3yrK0lfQ== X-Received: by 2002:a05:6870:7aa:b0:1bb:8333:ab8a with SMTP id en42-20020a05687007aa00b001bb8333ab8amr6374600oab.4.1694716638771; Thu, 14 Sep 2023 11:37:18 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:91cb:5173:d70:d8d:4d2c]) by smtp.gmail.com with ESMTPSA id cc6-20020a056871e18600b001b3d67934e9sm1056930oac.26.2023.09.14.11.37.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 11:37:17 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Noah Goldstein , Paul Eggert , Florian Weimer Subject: [PATCH v7 4/7] stdlib: qsort: Move some macros to inline function Date: Thu, 14 Sep 2023 15:37:01 -0300 Message-Id: <20230914183704.1386534-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230914183704.1386534-1-adhemerval.zanella@linaro.org> References: <20230914183704.1386534-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patch=linaro.org@sourceware.org --- stdlib/qsort.c | 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; } }