[tz] [PROPOSED PATCH 3/3] Tune zic.c's overflow checking

Paul Eggert eggert at cs.ucla.edu
Mon Apr 20 06:13:14 UTC 2015


* NEWS: Mention performance improvements.
* zic.c (time_overflow): New function.
(oadd, tadd): Use it.
(tadd): Use fewer comparisons.
---
 NEWS  |  4 ++++
 zic.c | 36 +++++++++++++++++++++++-------------
 2 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/NEWS b/NEWS
index 2baf42f..58ec0a6 100644
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,10 @@ Unreleased, experimental changes
     Printing Office style.  This affects only America/Adak since 1983,
     as America/Honolulu was already using the new style.
 
+  Changes affecting code
+
+   zic has some minor performance improvements.
+
 
 Release 2015c - 2015-04-11 08:55:55 -0700
 
diff --git a/zic.c b/zic.c
index ce3576b..636649b 100644
--- a/zic.c
+++ b/zic.c
@@ -2758,28 +2758,38 @@ getfields(register char *cp)
 	return array;
 }
 
+static _Noreturn void
+time_overflow(void)
+{
+  error(_("time overflow"));
+  exit(EXIT_FAILURE);
+}
+
 static ATTRIBUTE_PURE zic_t
 oadd(const zic_t t1, const zic_t t2)
 {
-	if (t1 < 0 ? t2 < ZIC_MIN - t1 : ZIC_MAX - t1 < t2) {
-		error(_("time overflow"));
-		exit(EXIT_FAILURE);
-	}
+	if (t1 < 0 ? t2 < ZIC_MIN - t1 : ZIC_MAX - t1 < t2)
+	  time_overflow();
 	return t1 + t2;
 }
 
 static ATTRIBUTE_PURE zic_t
 tadd(const zic_t t1, const zic_t t2)
 {
-	if (t1 == max_time && t2 > 0)
-		return max_time;
-	if (t1 == min_time && t2 < 0)
-		return min_time;
-	if (t1 < 0 ? t2 < min_time - t1 : max_time - t1 < t2) {
-		error(_("time overflow"));
-		exit(EXIT_FAILURE);
-	}
-	return t1 + t2;
+  if (t1 < 0) {
+    if (t2 < min_time - t1) {
+      if (t1 != min_time)
+	time_overflow();
+      return min_time;
+    }
+  } else {
+    if (max_time - t1 < t2) {
+      if (t1 != max_time)
+	time_overflow();
+      return max_time;
+    }
+  }
+  return t1 + t2;
 }
 
 /*
-- 
2.1.0



More information about the tz mailing list