From: Joe Perches <joe@perches.com>
To: Cassio Neri <cassio.neri@gmail.com>,
john.stultz@linaro.org, tglx@linutronix.de
Cc: sboyd@kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] kernel/time: Improve performance of time64_to_tm. Add tests.
Date: Wed, 02 Jun 2021 11:05:54 -0700 [thread overview]
Message-ID: <ef68a1ece1a2db3dca73c326f65304fd640c6a7e.camel@perches.com> (raw)
In-Reply-To: <20210602174651.37874-1-cassio.neri@gmail.com>
On Wed, 2021-06-02 at 18:46 +0100, Cassio Neri wrote:
> The current implementation of time64_to_tm contains unnecessary loops,
> branches and look-up tables. The new one uses an arithmetic-based algorithm
> appeared in [1] and is ~3.2 times faster (YMMV).
trivia:
> diff --git a/kernel/time/timeconv.c b/kernel/time/timeconv.c
[]
> void time64_to_tm(time64_t totalsecs, int offset, struct tm *result)
> {
> - long days, rem, y;
> + long days, rem;
> int remainder;
> - const unsigned short *ip;
> +
> + u64 u64tmp, udays, century, year;
> + u32 u32tmp, day_of_century, year_of_century, day_of_year, month,
> + day, janOrFeb, is_leap;
janOrFeb is an odd name choice and it and is_leap could be bool
which _might_ improve performance in some memory access cases.
> + year_of_century = (u32) (u64tmp >> 32);
Perhaps
year_of_century = upper_32_bits(u64tmp);
> + day_of_year = ((u32) u64tmp) / 2939745 / 4;
and
day_of_year = lower_32_bits(u64tmp) / 2939745 / 4;
> + is_leap = year_of_century != 0 ?
> + year_of_century % 4 == 0 : century % 4 == 0;
> +
> + u32tmp = 2141 * day_of_year + 132377;
> + month = u32tmp >> 16;
> + day = ((u16) u32tmp) / 2141;
> +
> + /* Recall that January 01 is the 306-th day of the year in the
> + * computational (not Gregorian) calendar.
> + */
> + janOrFeb = day_of_year >= 306;
> +
> + /* Converts to the Gregorian calendar and adjusts to Unix time. */
> + year = year + janOrFeb - 6313183731940000ULL;
> + month = janOrFeb ? month - 12 : month;
> + day = day + 1;
> + day_of_year = janOrFeb ?
> + day_of_year - 306 : day_of_year + 31 + 28 + is_leap;
I believe the extended naming improves readability, thanks.
prev parent reply other threads:[~2021-06-02 18:05 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-02 17:46 Cassio Neri
2021-06-02 18:05 ` Joe Perches [this message]
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=ef68a1ece1a2db3dca73c326f65304fd640c6a7e.camel@perches.com \
--to=joe@perches.com \
--cc=cassio.neri@gmail.com \
--cc=john.stultz@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=tglx@linutronix.de \
/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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®