mailing list of musl libc
 help / color / mirror / code / Atom feed
From: "Mattias Andrée" <maandree@kth.se>
To: musl@lists.openwall.com
Cc: "Mattias Andrée" <maandree@kth.se>
Subject: [musl] [PATCH v2 2/2] Use modulo instead of mul+sub in __secs_to_tm
Date: Sun, 28 Feb 2021 20:22:10 +0100	[thread overview]
Message-ID: <20210228192210.1665554-2-maandree@kth.se> (raw)
In-Reply-To: <20210228192210.1665554-1-maandree@kth.se>

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 | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/src/time/__secs_to_tm.c b/src/time/__secs_to_tm.c
index 62219df5..348e51ec 100644
--- a/src/time/__secs_to_tm.c
+++ b/src/time/__secs_to_tm.c
@@ -39,16 +39,28 @@ int __secs_to_tm(long long t, struct tm *tm)
 		qc_cycles--;
 	}
 
+#if 1
+	c_cycles = remdays / DAYS_PER_100Y;
+	remdays %= DAYS_PER_100Y;
+	if (c_cycles == 4) {
+		remdays += DAYS_PER_100Y;
+		c_cycles--;
+	}
+#else
 	c_cycles = remdays / DAYS_PER_100Y;
 	if (c_cycles == 4) c_cycles--;
 	remdays -= c_cycles * DAYS_PER_100Y;
+#endif
 
 	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


  reply	other threads:[~2021-02-28 19:22 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-28 15:09 [musl] [PATCH] Remove unnecessary if " Mattias Andrée
2021-02-28 17:06 ` Rich Felker
2021-02-28 17:24   ` Mattias Andrée
2021-02-28 17:34     ` Rich Felker
2021-02-28 19:22 ` [musl] [PATCH v2 1/2] " Mattias Andrée
2021-02-28 19:22   ` Mattias Andrée [this message]
2021-02-28 19:37     ` [musl] [PATCH v2 2/2] Use modulo instead of mul+sub " Szabolcs Nagy
2021-02-28 19:52       ` Mattias Andrée
2021-02-28 19:58       ` Jon Chesterfield
2021-03-01 19:26         ` Markus Wichmann
2021-02-28 19:27 ` [musl] [PATCH v3 1/2] Remove unnecessary if " Mattias Andrée
2021-02-28 19:27   ` [musl] [PATCH v3 2/2] Use modulo instead of mul+sub " Mattias Andrée

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210228192210.1665554-2-maandree@kth.se \
    --to=maandree@kth.se \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).