On 12/23/19 12:38 AM, Bruno Haible wrote:
> => I'm in favour of installing your new formula.

Sure, I installed the attached. The first patch is for xtime.h, the remaining
patches are for other instances of this idiom in Gnulib.
>From 6ad341ee4a0cca1a8b1744bc269282aafd765868 Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Mon, 23 Dec 2019 19:04:50 -0800
Subject: [PATCH 1/4] gethrxtime: improve xtime_sec performance
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Performanced analyzed by Bruno Haible in:
https://lists.gnu.org/r/bug-gnulib/2019-12/msg00200.html
* lib/xtime.h (xtime_sec): Redo with neither ‘%’ nor conditional
branches.
---
 ChangeLog   | 8 ++++++++
 lib/xtime.h | 2 +-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index e0ecf87b7..f6a705a7a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2019-12-23  Paul Eggert  <egg...@cs.ucla.edu>
+
+	gethrxtime: improve xtime_sec performance
+	Performanced analyzed by Bruno Haible in:
+	https://lists.gnu.org/r/bug-gnulib/2019-12/msg00200.html
+	* lib/xtime.h (xtime_sec): Redo with neither ‘%’ nor conditional
+	branches.
+
 2019-12-23  Bruno Haible  <br...@clisp.org>
 
 	setlocale-null: Export the lock function also on non-Windows platforms.
diff --git a/lib/xtime.h b/lib/xtime.h
index 5dea46767..25185d01b 100644
--- a/lib/xtime.h
+++ b/lib/xtime.h
@@ -61,7 +61,7 @@ xtime_nonnegative_sec (xtime_t t)
 XTIME_INLINE xtime_t
 xtime_sec (xtime_t t)
 {
-  return t / XTIME_PRECISION - (t % XTIME_PRECISION < 0);
+  return (t + (t < 0)) / XTIME_PRECISION - (t < 0);
 }
 
 /* Return the number of nanoseconds in T, which must be nonnegative.  */
-- 
2.17.1

>From f29bc5f35e5871452270c742f1784d9422f95c62 Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Mon, 23 Dec 2019 23:47:37 -0800
Subject: [PATCH 2/4] mktime: tweak division performance
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* config/srclist.txt: Do not sync mktime.c for now.
* lib/mktime.c (shr, ydhms_diff):
Redo with neither ‘%’ nor conditional branches.
---
 ChangeLog          | 5 +++++
 config/srclist.txt | 2 +-
 lib/mktime.c       | 6 +++---
 3 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index f6a705a7a..1f7eb19b9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2019-12-23  Paul Eggert  <egg...@cs.ucla.edu>
 
+	mktime: tweak division performance
+	* config/srclist.txt: Do not sync mktime.c for now.
+	* lib/mktime.c (shr, ydhms_diff):
+	Redo with neither ‘%’ nor conditional branches.
+
 	gethrxtime: improve xtime_sec performance
 	Performanced analyzed by Bruno Haible in:
 	https://lists.gnu.org/r/bug-gnulib/2019-12/msg00200.html
diff --git a/config/srclist.txt b/config/srclist.txt
index 55d574ab3..9a7b9597d 100644
--- a/config/srclist.txt
+++ b/config/srclist.txt
@@ -59,7 +59,7 @@ $LIBCSRC posix/regex_internal.c		lib
 $LIBCSRC posix/regex_internal.h		lib
 $LIBCSRC posix/regexec.c		lib
 $LIBCSRC time/timegm.c			lib
-$LIBCSRC time/mktime.c			lib
+#$LIBCSRC time/mktime.c			lib
 $LIBCSRC time/mktime-internal.h		lib
 
 # 
diff --git a/lib/mktime.c b/lib/mktime.c
index 11b0d5353..81c9ca1fc 100644
--- a/lib/mktime.c
+++ b/lib/mktime.c
@@ -141,7 +141,7 @@ shr (long_int a, int b)
   long_int one = 1;
   return (-one >> 1 == -1
 	  ? a >> b
-	  : a / (one << b) - (a % (one << b) < 0));
+	  : (a + (a < 0)) / (one << b) - (a < 0));
 }
 
 /* Bounds for the intersection of __time64_t and long_int.  */
@@ -211,8 +211,8 @@ ydhms_diff (long_int year1, long_int yday1, int hour1, int min1, int sec1,
      Take care to avoid integer overflow here.  */
   int a4 = shr (year1, 2) + shr (TM_YEAR_BASE, 2) - ! (year1 & 3);
   int b4 = shr (year0, 2) + shr (TM_YEAR_BASE, 2) - ! (year0 & 3);
-  int a100 = a4 / 25 - (a4 % 25 < 0);
-  int b100 = b4 / 25 - (b4 % 25 < 0);
+  int a100 = (a4 + (a4 < 0)) / 25 - (a4 < 0);
+  int b100 = (b4 + (b4 < 0)) / 25 - (b4 < 0);
   int a400 = shr (a100, 2);
   int b400 = shr (b100, 2);
   int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
-- 
2.17.1

>From 3c8e21069a9e66b7e845a3f39d22d429c263ff0c Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Mon, 23 Dec 2019 23:48:35 -0800
Subject: [PATCH 3/4] nstrftime: tweak division performance
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* lib/nstrftime.c (SHR, tm_diff, __strftime_internal):
Redo with neither ‘%’ nor conditional branches.
---
 ChangeLog       |  4 ++++
 lib/nstrftime.c | 14 ++++++++------
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 1f7eb19b9..4b4714204 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2019-12-23  Paul Eggert  <egg...@cs.ucla.edu>
 
+	nstrftime: tweak division performance
+	* lib/nstrftime.c (SHR, tm_diff, __strftime_internal):
+	Redo with neither ‘%’ nor conditional branches.
+
 	mktime: tweak division performance
 	* config/srclist.txt: Do not sync mktime.c for now.
 	* lib/mktime.c (shr, ydhms_diff):
diff --git a/lib/nstrftime.c b/lib/nstrftime.c
index 23b0a6460..62bc7a61e 100644
--- a/lib/nstrftime.c
+++ b/lib/nstrftime.c
@@ -113,7 +113,7 @@ extern char *tzname[];
 #define SHR(a, b)       \
   (-1 >> 1 == -1        \
    ? (a) >> (b)         \
-   : (a) / (1 << (b)) - ((a) % (1 << (b)) < 0))
+   : ((a) + ((a) < 0)) / (1 << (b)) - ((a) < 0))
 
 #define TM_YEAR_BASE 1900
 
@@ -348,8 +348,8 @@ tm_diff (const struct tm *a, const struct tm *b)
      but it's OK to assume that A and B are close to each other.  */
   int a4 = SHR (a->tm_year, 2) + SHR (TM_YEAR_BASE, 2) - ! (a->tm_year & 3);
   int b4 = SHR (b->tm_year, 2) + SHR (TM_YEAR_BASE, 2) - ! (b->tm_year & 3);
-  int a100 = a4 / 25 - (a4 % 25 < 0);
-  int b100 = b4 / 25 - (b4 % 25 < 0);
+  int a100 = (a4 + (a4 < 0)) / 25 - (a4 < 0);
+  int b100 = (b4 + (b4 < 0)) / 25 - (b4 < 0);
   int a400 = SHR (a100, 2);
   int b400 = SHR (b100, 2);
   int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
@@ -927,9 +927,11 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
             }
 
           {
-            int century = tp->tm_year / 100 + TM_YEAR_BASE / 100;
-            century -= tp->tm_year % 100 < 0 && 0 < century;
-            DO_YEARISH (2, tp->tm_year < - TM_YEAR_BASE, century);
+            bool negative_year = tp->tm_year < - TM_YEAR_BASE;
+            bool zero_thru_1899 = !negative_year & (tp->tm_year < 0);
+            int century = ((tp->tm_year - 99 * zero_thru_1899) / 100
+                           + TM_YEAR_BASE / 100);
+            DO_YEARISH (2, negative_year, century);
           }
 
         case L_('x'):
-- 
2.17.1

>From 6f9ead5181f2c117b3c5a4c965fe44dc1258661b Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Mon, 23 Dec 2019 23:53:23 -0800
Subject: [PATCH 4/4] strptime: tweak division performance
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* lib/strptime.c (day_of_the_week):
Redo with neither ‘%’ nor conditional branches.
---
 ChangeLog      | 17 ++++++-----------
 lib/strptime.c |  7 ++++---
 2 files changed, 10 insertions(+), 14 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 4b4714204..168037ed1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,20 +1,15 @@
 2019-12-23  Paul Eggert  <egg...@cs.ucla.edu>
 
-	nstrftime: tweak division performance
-	* lib/nstrftime.c (SHR, tm_diff, __strftime_internal):
-	Redo with neither ‘%’ nor conditional branches.
-
-	mktime: tweak division performance
+	gethrxtime, mktime, nstrftime, strptime: tweak division performance
+	Performanced analyzed by Bruno Haible in:
+	https://lists.gnu.org/r/bug-gnulib/2019-12/msg00200.html
 	* config/srclist.txt: Do not sync mktime.c for now.
 	* lib/mktime.c (shr, ydhms_diff):
+	* lib/nstrftime.c (SHR, tm_diff, __strftime_internal):
+	* lib/strptime.c (day_of_the_week):
+	* lib/xtime.h (xtime_sec):
 	Redo with neither ‘%’ nor conditional branches.
 
-	gethrxtime: improve xtime_sec performance
-	Performanced analyzed by Bruno Haible in:
-	https://lists.gnu.org/r/bug-gnulib/2019-12/msg00200.html
-	* lib/xtime.h (xtime_sec): Redo with neither ‘%’ nor conditional
-	branches.
-
 2019-12-23  Bruno Haible  <br...@clisp.org>
 
 	setlocale-null: Export the lock function also on non-Windows platforms.
diff --git a/lib/strptime.c b/lib/strptime.c
index e6d405a44..9c2c2e38b 100644
--- a/lib/strptime.c
+++ b/lib/strptime.c
@@ -202,11 +202,12 @@ day_of_the_week (struct tm *tm)
      difference between this data in the one on TM and so determine
      the weekday.  */
   int corr_year = 1900 + tm->tm_year - (tm->tm_mon < 2);
+  int corr_quad = corr_year / 4;
   int wday = (-473
               + (365 * (tm->tm_year - 70))
-              + (corr_year / 4)
-              - ((corr_year / 4) / 25) + ((corr_year / 4) % 25 < 0)
-              + (((corr_year / 4) / 25) / 4)
+              + corr_quad
+              - (corr_quad + (corr_quad < 0)) / 25 - (corr_quad < 0)
+              + ((corr_quad / 25) / 4)
               + __mon_yday[0][tm->tm_mon]
               + tm->tm_mday - 1);
   tm->tm_wday = ((wday % 7) + 7) % 7;
-- 
2.17.1

Reply via email to