Skip to content

Commit 1d1bb12

Browse files
cassionerialexandrebelloni
authored andcommitted
rtc: Improve performance of rtc_time64_to_tm(). Add tests.
The current implementation of rtc_time64_to_tm() contains unnecessary loops, branches and look-up tables. The new one uses an arithmetic-based algorithm appeared in [1] and is approximately 4.3 times faster (YMMV). The drawback is that the new code isn't intuitive and contains many 'magic numbers' (not unusual for this type of algorithm). However, [1] justifies all those numbers and, given this function's history, the code is unlikely to need much maintenance, if any at all. Add a KUnit test case that checks every day in a 160,000 years interval starting on 1970-01-01 against the expected result. Add a new config RTC_LIB_KUNIT_TEST symbol to give the option to run this test suite. [1] Neri, Schneider, "Euclidean Affine Functions and Applications to Calendar Algorithms". https://arxiv.org/abs/2102.06959 Signed-off-by: Cassio Neri <cassio.neri@gmail.com> Reported-by: kernel test robot <lkp@intel.com> Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com> Link: https://lore.kernel.org/r/20210624201343.85441-1-cassio.neri@gmail.com
1 parent fffd603 commit 1d1bb12

4 files changed

Lines changed: 170 additions & 27 deletions

File tree

drivers/rtc/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ config RTC_MC146818_LIB
1010
bool
1111
select RTC_LIB
1212

13+
config RTC_LIB_KUNIT_TEST
14+
tristate "KUnit test for RTC lib functions" if !KUNIT_ALL_TESTS
15+
depends on KUNIT
16+
default KUNIT_ALL_TESTS
17+
select RTC_LIB
18+
help
19+
Enable this option to test RTC library functions.
20+
21+
If unsure, say N.
22+
1323
menuconfig RTC_CLASS
1424
bool "Real Time Clock"
1525
default n

drivers/rtc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,4 @@ obj-$(CONFIG_RTC_DRV_WM8350) += rtc-wm8350.o
178178
obj-$(CONFIG_RTC_DRV_X1205) += rtc-x1205.o
179179
obj-$(CONFIG_RTC_DRV_XGENE) += rtc-xgene.o
180180
obj-$(CONFIG_RTC_DRV_ZYNQMP) += rtc-zynqmp.o
181+
obj-$(CONFIG_RTC_LIB_KUNIT_TEST) += lib_test.o

drivers/rtc/lib.c

Lines changed: 80 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Author: Alessandro Zummo <a.zummo@towertech.it>
77
*
88
* based on arch/arm/common/rtctime.c and other bits
9+
*
10+
* Author: Cassio Neri <cassio.neri@gmail.com> (rtc_time64_to_tm)
911
*/
1012

1113
#include <linux/export.h>
@@ -22,8 +24,6 @@ static const unsigned short rtc_ydays[2][13] = {
2224
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
2325
};
2426

25-
#define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
26-
2727
/*
2828
* The number of days in the month.
2929
*/
@@ -42,42 +42,95 @@ int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
4242
}
4343
EXPORT_SYMBOL(rtc_year_days);
4444

45-
/*
46-
* rtc_time64_to_tm - Converts time64_t to rtc_time.
47-
* Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
45+
/**
46+
* rtc_time64_to_tm - converts time64_t to rtc_time.
47+
*
48+
* @time: The number of seconds since 01-01-1970 00:00:00.
49+
* (Must be positive.)
50+
* @tm: Pointer to the struct rtc_time.
4851
*/
4952
void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
5053
{
51-
unsigned int month, year, secs;
54+
unsigned int secs;
5255
int days;
5356

57+
u64 u64tmp;
58+
u32 u32tmp, udays, century, day_of_century, year_of_century, year,
59+
day_of_year, month, day;
60+
bool is_Jan_or_Feb, is_leap_year;
61+
5462
/* time must be positive */
5563
days = div_s64_rem(time, 86400, &secs);
5664

5765
/* day of the week, 1970-01-01 was a Thursday */
5866
tm->tm_wday = (days + 4) % 7;
5967

60-
year = 1970 + days / 365;
61-
days -= (year - 1970) * 365
62-
+ LEAPS_THRU_END_OF(year - 1)
63-
- LEAPS_THRU_END_OF(1970 - 1);
64-
while (days < 0) {
65-
year -= 1;
66-
days += 365 + is_leap_year(year);
67-
}
68-
tm->tm_year = year - 1900;
69-
tm->tm_yday = days + 1;
70-
71-
for (month = 0; month < 11; month++) {
72-
int newdays;
73-
74-
newdays = days - rtc_month_days(month, year);
75-
if (newdays < 0)
76-
break;
77-
days = newdays;
78-
}
79-
tm->tm_mon = month;
80-
tm->tm_mday = days + 1;
68+
/*
69+
* The following algorithm is, basically, Proposition 6.3 of Neri
70+
* and Schneider [1]. In a few words: it works on the computational
71+
* (fictitious) calendar where the year starts in March, month = 2
72+
* (*), and finishes in February, month = 13. This calendar is
73+
* mathematically convenient because the day of the year does not
74+
* depend on whether the year is leap or not. For instance:
75+
*
76+
* March 1st 0-th day of the year;
77+
* ...
78+
* April 1st 31-st day of the year;
79+
* ...
80+
* January 1st 306-th day of the year; (Important!)
81+
* ...
82+
* February 28th 364-th day of the year;
83+
* February 29th 365-th day of the year (if it exists).
84+
*
85+
* After having worked out the date in the computational calendar
86+
* (using just arithmetics) it's easy to convert it to the
87+
* corresponding date in the Gregorian calendar.
88+
*
89+
* [1] "Euclidean Affine Functions and Applications to Calendar
90+
* Algorithms". https://arxiv.org/abs/2102.06959
91+
*
92+
* (*) The numbering of months follows rtc_time more closely and
93+
* thus, is slightly different from [1].
94+
*/
95+
96+
udays = ((u32) days) + 719468;
97+
98+
u32tmp = 4 * udays + 3;
99+
century = u32tmp / 146097;
100+
day_of_century = u32tmp % 146097 / 4;
101+
102+
u32tmp = 4 * day_of_century + 3;
103+
u64tmp = 2939745ULL * u32tmp;
104+
year_of_century = upper_32_bits(u64tmp);
105+
day_of_year = lower_32_bits(u64tmp) / 2939745 / 4;
106+
107+
year = 100 * century + year_of_century;
108+
is_leap_year = year_of_century != 0 ?
109+
year_of_century % 4 == 0 : century % 4 == 0;
110+
111+
u32tmp = 2141 * day_of_year + 132377;
112+
month = u32tmp >> 16;
113+
day = ((u16) u32tmp) / 2141;
114+
115+
/*
116+
* Recall that January 01 is the 306-th day of the year in the
117+
* computational (not Gregorian) calendar.
118+
*/
119+
is_Jan_or_Feb = day_of_year >= 306;
120+
121+
/* Converts to the Gregorian calendar. */
122+
year = year + is_Jan_or_Feb;
123+
month = is_Jan_or_Feb ? month - 12 : month;
124+
day = day + 1;
125+
126+
day_of_year = is_Jan_or_Feb ?
127+
day_of_year - 306 : day_of_year + 31 + 28 + is_leap_year;
128+
129+
/* Converts to rtc_time's format. */
130+
tm->tm_year = (int) (year - 1900);
131+
tm->tm_mon = (int) month;
132+
tm->tm_mday = (int) day;
133+
tm->tm_yday = (int) day_of_year + 1;
81134

82135
tm->tm_hour = secs / 3600;
83136
secs -= tm->tm_hour * 3600;

drivers/rtc/lib_test.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// SPDX-License-Identifier: LGPL-2.1+
2+
3+
#include <kunit/test.h>
4+
#include <linux/rtc.h>
5+
6+
/*
7+
* Advance a date by one day.
8+
*/
9+
static void advance_date(int *year, int *month, int *mday, int *yday)
10+
{
11+
if (*mday != rtc_month_days(*month - 1, *year)) {
12+
++*mday;
13+
++*yday;
14+
return;
15+
}
16+
17+
*mday = 1;
18+
if (*month != 12) {
19+
++*month;
20+
++*yday;
21+
return;
22+
}
23+
24+
*month = 1;
25+
*yday = 1;
26+
++*year;
27+
}
28+
29+
/*
30+
* Checks every day in a 160000 years interval starting on 1970-01-01
31+
* against the expected result.
32+
*/
33+
static void rtc_time64_to_tm_test_date_range(struct kunit *test)
34+
{
35+
/*
36+
* 160000 years = (160000 / 400) * 400 years
37+
* = (160000 / 400) * 146097 days
38+
* = (160000 / 400) * 146097 * 86400 seconds
39+
*/
40+
time64_t total_secs = ((time64_t) 160000) / 400 * 146097 * 86400;
41+
42+
int year = 1970;
43+
int month = 1;
44+
int mday = 1;
45+
int yday = 1;
46+
47+
struct rtc_time result;
48+
time64_t secs;
49+
s64 days;
50+
51+
for (secs = 0; secs <= total_secs; secs += 86400) {
52+
53+
rtc_time64_to_tm(secs, &result);
54+
55+
days = div_s64(secs, 86400);
56+
57+
#define FAIL_MSG "%d/%02d/%02d (%2d) : %ld", \
58+
year, month, mday, yday, days
59+
60+
KUNIT_ASSERT_EQ_MSG(test, year - 1900, result.tm_year, FAIL_MSG);
61+
KUNIT_ASSERT_EQ_MSG(test, month - 1, result.tm_mon, FAIL_MSG);
62+
KUNIT_ASSERT_EQ_MSG(test, mday, result.tm_mday, FAIL_MSG);
63+
KUNIT_ASSERT_EQ_MSG(test, yday, result.tm_yday, FAIL_MSG);
64+
65+
advance_date(&year, &month, &mday, &yday);
66+
}
67+
}
68+
69+
static struct kunit_case rtc_lib_test_cases[] = {
70+
KUNIT_CASE(rtc_time64_to_tm_test_date_range),
71+
{}
72+
};
73+
74+
static struct kunit_suite rtc_lib_test_suite = {
75+
.name = "rtc_lib_test_cases",
76+
.test_cases = rtc_lib_test_cases,
77+
};
78+
79+
kunit_test_suite(rtc_lib_test_suite);

0 commit comments

Comments
 (0)