From: "Mattias Andrée" <maandree@kth.se>
To: musl@lists.openwall.com
Subject: Re: [musl] [PATCH] Remove unnecessary if in __secs_to_tm
Date: Sun, 28 Feb 2021 18:24:45 +0100 [thread overview]
Message-ID: <20210228182445.66f1de08.maandree@kth.se> (raw)
In-Reply-To: <20210228170614.GF32655@brightrain.aerifal.cx>
On Sun, 28 Feb 2021 12:06:15 -0500
Rich Felker <dalias@libc.org> wrote:
> On Sun, Feb 28, 2021 at 04:09:12PM +0100, Mattias Andrée wrote:
> > Since years divisible by 100 but not by 400 are not leap years,
> > q_cycles can at most be 24 (DAYS_PER_100Y / DAYS_PER_4Y == 24).
> > ---
> > src/time/__secs_to_tm.c | 3 +--
> > 1 file changed, 1 insertion(+), 2 deletions(-)
> >
> > diff --git a/src/time/__secs_to_tm.c b/src/time/__secs_to_tm.c
> > index 093d9021..2d0c0b2c 100644
> > --- a/src/time/__secs_to_tm.c
> > +++ b/src/time/__secs_to_tm.c
> > @@ -44,8 +44,7 @@ int __secs_to_tm(long long t, struct tm *tm)
> > remdays -= c_cycles * DAYS_PER_100Y;
> >
> > q_cycles = remdays / DAYS_PER_4Y;
> > - if (q_cycles == 25) q_cycles--;
> > - remdays -= q_cycles * DAYS_PER_4Y;
> > + remdays %= DAYS_PER_4Y;
> >
> > remyears = remdays / 365;
> > if (remyears == 4) remyears--;
>
> I think you're right about the condition being impossible -- it looks
> like the error in thinking was that, while 400Y and 4Y are strictly
> larger than 4*100Y and 4*1Y respectively, 100Y is smaller than 25*4Y.
>
> However, changing the -= to %= is not desirable. The point of the -=
> has nothing to do with the edge case that can't happen; it's to avoid
> a modulo operation. Since the divisor is a constant though maybe the
> compiler can generate the same code for both, anyway..?
>
> Rich
For x86_64 `remdays %= DAYS_PER_4Y` just becomes a move.
divmod in
int r = 52, q;
void divmod(void)
{
q = r / 111;
r %= 111;
}
becomes
movl r(%rip), %eax
movl $111, %ecx
cltd
idivl %ecx
movl %eax, q(%rip)
movl %edx, r(%rip)
ret
`remdays -= q_cycles * DAYS_PER_4Y;` on the other hand
becomes a move, a multiplication, and an addition.
divmod in
int r = 52, q;
void divmod(void)
{
q = r / 111;
r -= q * 111;
}
becomes
movl r(%rip), %eax
movl $111, %ecx
cltd
idivl %ecx
movl %eax, q(%rip)
imull $-111, %eax, %eax
addl r(%rip), %eax
movl %eax, r(%rip)
ret
So I would say %= is the better option, at least for x86_64.
Of course, if you prefer, I will change it to use -=.
next prev parent reply other threads:[~2021-02-28 17:25 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-28 15:09 Mattias Andrée
2021-02-28 17:06 ` Rich Felker
2021-02-28 17:24 ` Mattias Andrée [this message]
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 ` [musl] [PATCH v2 2/2] Use modulo instead of mul+sub " Mattias Andrée
2021-02-28 19:37 ` 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=20210228182445.66f1de08.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).