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 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; } }