From patchwork Tue Jul 11 19:07:19 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: 1806496 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=w/SPBYRY; dkim-atps=neutral Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R0r5j0TwHz20WT for ; Wed, 12 Jul 2023 05:09:29 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 142033858421 for ; Tue, 11 Jul 2023 19:09:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 142033858421 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689102567; bh=bTGIbHuS9ap7dOSOW2K/APUGTuTUHzoFEgC6CKYCoVU=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=w/SPBYRYCxBeGDNXqCkr8pRjDOVjRJh7eDEkpyC6LytvYTx0ffD1IBLQ0EVC5yNuY 9Bkj2ye8YUiuZ5UV5qV6Q3D+O6UWi0G49v7mOptDIvx91xt4Cqhm+0UrdBZFc539Vr 8qdaG4fRiie1xEsco+anScw/ojxStWj8ISYjRcKw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x2d.google.com (mail-oa1-x2d.google.com [IPv6:2001:4860:4864:20::2d]) by sourceware.org (Postfix) with ESMTPS id 3610F3858418 for ; Tue, 11 Jul 2023 19:07:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3610F3858418 Received: by mail-oa1-x2d.google.com with SMTP id 586e51a60fabf-1b0719dd966so5650138fac.1 for ; Tue, 11 Jul 2023 12:07:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689102451; x=1691694451; 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=bTGIbHuS9ap7dOSOW2K/APUGTuTUHzoFEgC6CKYCoVU=; b=FrmI+ZfaEKNGEHsUlGbpWCHszUaXvnDO4Er2IhMOrIUobz182YisYUrcMCUqpm9Pvv DXvpLMWY7xZvhqkln0PjzqBYWLlWNoPRi1ZsASI8qdy3vPVpwR5diyOv4aiaXtic55il 44TUfpWl39PVN2j0d39r/AzUpwA6hqIaA1leNrlfDvmu/zhQD+hCje0cDcJ6UbABZbDA b/f9xfxE5Op65pHVvQKaKfDIdNpCg8rfDK7R5nZImydX8LZ+DozRkpIIh50bEOnJnJso QEGrwjEmHF2ua0FR//JKr8S71OFWeZ0m9EAaVIWal5S3u7GXmlqOK+6xkj7IL4mjsIbY RGEQ== X-Gm-Message-State: ABy/qLaI1fIfj7GE7AFTyM5URvJYAHH4TcLOBL8uhlGemDw0WrNUlzoV Ib1xIESVoQqvfRDL6D3JuL0B8vwzMKDjylbx1M5jwQ== X-Google-Smtp-Source: APBJJlHDYMGbaRoTxtokNFnK6UEDBSdpe/i8qILlPI9XeKtUGWN/2c4F//rxLacLY1Uh7SKJEyBLXw== X-Received: by 2002:a05:6870:5254:b0:1b3:dccc:401a with SMTP id o20-20020a056870525400b001b3dccc401amr18966197oai.2.1689102451141; Tue, 11 Jul 2023 12:07:31 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:e0c8:30b2:9ea1:dcf5:7755]) by smtp.gmail.com with ESMTPSA id du9-20020a0568703a0900b001b3d67934e9sm1286128oab.26.2023.07.11.12.07.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 12:07:30 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v4 3/6] stdlib: qsort: Move some macros to inline function Date: Tue, 11 Jul 2023 16:07:19 -0300 Message-Id: <20230711190722.4028821-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230711190722.4028821-1-adhemerval.zanella@linaro.org> References: <20230711190722.4028821-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, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" --- stdlib/qsort.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/stdlib/qsort.c b/stdlib/qsort.c index 00637208ab..134e784bd1 100644 --- a/stdlib/qsort.c +++ b/stdlib/qsort.c @@ -118,15 +118,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; +} /* Order size using quicksort. This implementation incorporates @@ -232,9 +245,9 @@ _quicksort (void *const pbase, size_t total_elems, size_t size, stack_node stack[STACK_SIZE]; stack_node *top = stack; - PUSH (NULL, NULL); + top = push (top, NULL, NULL); - while (STACK_NOT_EMPTY) + while (stack < top) { char *left_ptr; char *right_ptr; @@ -299,7 +312,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; @@ -310,13 +323,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; } }