From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 15755 invoked from network); 28 Feb 2021 19:27:37 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 28 Feb 2021 19:27:37 -0000 Received: (qmail 3516 invoked by uid 550); 28 Feb 2021 19:27:31 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 3397 invoked from network); 28 Feb 2021 19:27:31 -0000 X-Virus-Scanned: by amavisd-new at kth.se X-KTH-Auth: maandree [2001:1ba8:120c:d700:5e2a:93e6:8546:53b5] DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kth.se; s=default; t=1614540439; bh=keYWp1ZtiMCAe/SsAQZ5pC8EZYd5WXpqJU0LyevEqp0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Lmb+cX1xA5F/g9FBcUUKFZtjRlAKTqkRkYOgYRjtNyGk+NWEInDyw9Jh0ystFyz23 xsGUXQcXLHFu4B73gHxWiQSL0nVW4vSewEzCIzllRwdARPTCJmJ36euhsQhBRODuNk CrzrgumxoBlmWmLqvmgxT8Rv70QCEtDNEibK59BA= X-KTH-mail-from: maandree@kth.se From: =?UTF-8?q?Mattias=20Andr=C3=A9e?= To: musl@lists.openwall.com Cc: =?UTF-8?q?Mattias=20Andr=C3=A9e?= Date: Sun, 28 Feb 2021 20:27:12 +0100 Message-Id: <20210228192712.1666688-2-maandree@kth.se> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210228192712.1666688-1-maandree@kth.se> References: <20210228150912.1532943-1-maandree@kth.se> <20210228192712.1666688-1-maandree@kth.se> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [musl] [PATCH v3 2/2] Use modulo instead of mul+sub in __secs_to_tm On x86 modulo is free when doing division, so this removes a multiplication and at the cost of replacing a conditional move with a conditional jump, but it still appears to be faster. (Similar architectures: nds32le) ARM doesn't have modulo, instead an multiply-and-subtract operation is done after the division, so the diffence here is either none at all, or a move and a multiply-and-add being replaced with a multiply-and-subtract. (Similar architectures: or1k) RISC-V on the other hand has a separate modulo instruction and will perform a separate modulo instead of an assignment, a multiplication, and an addition with this change. GCC does change how the modulo operation is realised depending on the optimisation level. I don't know how this affects the performance, however a simple test on x86 suggests that doing a modulo operations is actually faster than assign–multiply–add. --- src/time/__secs_to_tm.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/time/__secs_to_tm.c b/src/time/__secs_to_tm.c index 62219df5..59b1fc8d 100644 --- a/src/time/__secs_to_tm.c +++ b/src/time/__secs_to_tm.c @@ -40,15 +40,21 @@ int __secs_to_tm(long long t, struct tm *tm) } c_cycles = remdays / DAYS_PER_100Y; - if (c_cycles == 4) c_cycles--; - remdays -= c_cycles * DAYS_PER_100Y; + remdays %= DAYS_PER_100Y; + if (c_cycles == 4) { + remdays += DAYS_PER_100Y; + c_cycles--; + } q_cycles = remdays / DAYS_PER_4Y; - remdays -= q_cycles * DAYS_PER_4Y; + remdays %= DAYS_PER_4Y; remyears = remdays / 365; - if (remyears == 4) remyears--; - remdays -= remyears * 365; + remdays %= 365; + if (remyears == 4) { + remdays += 365; + remyears--; + } leap = !remyears && (q_cycles || !c_cycles); yday = remdays + 31 + 28 + leap; -- 2.30.1