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 X-Patchwork-Id: 76001 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A6E0F3857707 for ; Thu, 14 Sep 2023 18:37:53 +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-Google-Smtp-Source: AGHT+IHjmgK43KYM/0MXojNcPIZmTBnkVGHysdTsf/hNI54U8LmDAIHG6v57nVsMvdvzdKchnrrJvg== 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+patchwork=sourceware.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; } }