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

```
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
```

```
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 -=.
```

```
On Sun, Feb 28, 2021 at 06:24:45PM +0100, Mattias Andrée wrote:
> 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 -=.
It's an unrelated change, so if it should be made it should be done as
a different commit, and in all the places not just arbitrarily in one
of them. But the above analysis is probably not indicative. You're
dividing by a variable, in which case on x86_64 idiv gets used and
there's a remainder available for free. But in the code here all the
divisions are by constants and should cause the compiler to emit code
using only multiplies.
(Note: this may not currently be the case with -Os, which is one big
reason we should be dropping -Os and instead tuning -O2 to behave
better, which is a longstanding agenda item).
Also, if these were actual div/mod operations by a variable, the
interesting case is not archs with an instruction that produces the
remainder for free, but ones where two separate operations are
required or where long division in software is required.
Rich
```

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 | 1 - 1 file changed, 1 deletion(-) diff --git a/src/time/__secs_to_tm.c b/src/time/__secs_to_tm.c index 093d9021..62219df5 100644 --- a/src/time/__secs_to_tm.c +++ b/src/time/__secs_to_tm.c @@ -44,7 +44,6 @@ 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; remyears = remdays / 365; -- 2.30.1

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

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 | 1 - 1 file changed, 1 deletion(-) diff --git a/src/time/__secs_to_tm.c b/src/time/__secs_to_tm.c index 093d9021..62219df5 100644 --- a/src/time/__secs_to_tm.c +++ b/src/time/__secs_to_tm.c @@ -44,7 +44,6 @@ 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; remyears = remdays / 365; -- 2.30.1

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

* Mattias Andrée <maandree@kth.se> [2021-02-28 20:22:10 +0100]: > On x86 modulo is free when doing division, so this removes there should be no division. div by const is transformed to mul and shift at -O1 and that's what we should be using instead of manual hacks. https://godbolt.org/z/Wsxq5h > 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. did you benchmark with CFLAGS=-O2 or -Os ? > --- > 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

On Sun, 28 Feb 2021 20:37:33 +0100 Szabolcs Nagy <nsz@port70.net> wrote: > * Mattias Andrée <maandree@kth.se> [2021-02-28 20:22:10 +0100]: > > On x86 modulo is free when doing division, so this removes > > there should be no division. > > div by const is transformed to mul and shift at -O1 and > that's what we should be using instead of manual hacks. > > https://godbolt.org/z/Wsxq5h For -Os, the currently used optimisation, it does division. But for other optimisations, it makes no difference as the compiler will do a multiply–subtract either way. > > > 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. > > did you benchmark with CFLAGS=-O2 or -Os ? I guess it must have been -O0 or -Os, but what I did was I made a trivial program and checked that assembly output, to see which method was faster. The important part here was that the compiler didn't change the division operation, so adding optimisation might have bad the test pointless. As I wrote, for RISC-V the compiler did exactly what was written, no matter the optimisation level, that is, for RISC-V I tried, -O0, -O1, -O2, -O3, and -Os. > > > --- > > 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

[-- Attachment #1: Type: text/plain, Size: 800 bytes --] On Sun, 28 Feb 2021, 19:37 Szabolcs Nagy, <nsz@port70.net> wrote: > * Mattias Andrée <maandree@kth.se> [2021-02-28 20:22:10 +0100]: > > On x86 modulo is free when doing division, so this removes > > there should be no division. > > div by const is transformed to mul and shift at -O1 and > that's what we should be using instead of manual hacks. Right. Divide by constant is cheap because compilers have a bunch of transforms to get rid of the divide in favour of one of more cheaper instructions. Note that module coming for free with division doesn't make it cheap. Integer division is far more expensive that integer multiply on ~ every architecture. Several architectures implement division in software. It's not cheap on x86, despite the dedicated instruction. Cheers [-- Attachment #2: Type: text/html, Size: 1315 bytes --]

```
On Sun, Feb 28, 2021 at 07:58:27PM +0000, Jon Chesterfield wrote:
> Note that module coming for free with division doesn't make it cheap.
> Integer division is far more expensive that integer multiply on ~ every
> architecture. Several architectures implement division in software. It's
> not cheap on x86, despite the dedicated instruction.
>
> Cheers
And then there's PowerPC, which does have a divide instruction but no
modulo. The manual explicitly states that if you need a modulo, you are
supposed to divide, multiply, and subtract.
Ciao,
Markus
```