Submitted by Mattias Andrée on Feb. 28, 2021, 7:22 p.m.

Message ID | 20210228192210.1665554-2-maandree@kth.se |
---|---|

State | New |

Series | "Remove unnecessary if in __secs_to_tm" |

Headers | show |

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;

* 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

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

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