[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] timespec-add, timespec-sub: simplify
From: |
Paul Eggert |
Subject: |
[PATCH] timespec-add, timespec-sub: simplify |
Date: |
Thu, 24 Oct 2019 17:33:10 -0700 |
* lib/timespec-add.c (timespec_add):
* lib/timespec-sub.c (timespec_sub):
Simplify, now that INT_ADD_WRAPV and INT_SUBTRACT_WRAPV
work on unsigned integers.
---
ChangeLog | 8 ++++++++
lib/timespec-add.c | 20 +++++++-------------
lib/timespec-sub.c | 20 +++++++-------------
3 files changed, 22 insertions(+), 26 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 804294150..2469945b4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2019-10-24 Paul Eggert <address@hidden>
+
+ timespec-add, timespec-sub: simplify
+ * lib/timespec-add.c (timespec_add):
+ * lib/timespec-sub.c (timespec_sub):
+ Simplify, now that INT_ADD_WRAPV and INT_SUBTRACT_WRAPV
+ work on unsigned integers.
+
2019-10-23 Paul Eggert <address@hidden>
nstrftime: speed up integer overflow checking
diff --git a/lib/timespec-add.c b/lib/timespec-add.c
index e0a9f12e1..abee15482 100644
--- a/lib/timespec-add.c
+++ b/lib/timespec-add.c
@@ -33,36 +33,30 @@ timespec_add (struct timespec a, struct timespec b)
int ns = a.tv_nsec + b.tv_nsec;
int nsd = ns - TIMESPEC_HZ;
int rns = ns;
- time_t tmin = TYPE_MINIMUM (time_t);
- time_t tmax = TYPE_MAXIMUM (time_t);
if (0 <= nsd)
{
rns = nsd;
- if (bs < tmax)
- bs++;
+ time_t bs1;
+ if (!INT_ADD_WRAPV (bs, 1, &bs1))
+ bs = bs1;
else if (rs < 0)
rs++;
else
goto high_overflow;
}
- /* INT_ADD_WRAPV is not appropriate since time_t might be unsigned.
- In theory time_t might be narrower than int, so plain
- INT_ADD_OVERFLOW does not suffice. */
- if (! INT_ADD_OVERFLOW (rs, bs) && tmin <= rs + bs && rs + bs <= tmax)
- rs += bs;
- else
+ if (INT_ADD_WRAPV (rs, bs, &rs))
{
- if (rs < 0)
+ if (bs < 0)
{
- rs = tmin;
+ rs = TYPE_MINIMUM (time_t);
rns = 0;
}
else
{
high_overflow:
- rs = tmax;
+ rs = TYPE_MAXIMUM (time_t);
rns = TIMESPEC_HZ - 1;
}
}
diff --git a/lib/timespec-sub.c b/lib/timespec-sub.c
index 48434e815..77b9353df 100644
--- a/lib/timespec-sub.c
+++ b/lib/timespec-sub.c
@@ -33,36 +33,30 @@ timespec_sub (struct timespec a, struct timespec b)
time_t bs = b.tv_sec;
int ns = a.tv_nsec - b.tv_nsec;
int rns = ns;
- time_t tmin = TYPE_MINIMUM (time_t);
- time_t tmax = TYPE_MAXIMUM (time_t);
if (ns < 0)
{
rns = ns + TIMESPEC_HZ;
- if (bs < tmax)
- bs++;
+ time_t bs1;
+ if (!INT_ADD_WRAPV (bs, 1, &bs1))
+ bs = bs1;
else if (- TYPE_SIGNED (time_t) < rs)
rs--;
else
goto low_overflow;
}
- /* INT_SUBTRACT_WRAPV is not appropriate since time_t might be unsigned.
- In theory time_t might be narrower than int, so plain
- INT_SUBTRACT_OVERFLOW does not suffice. */
- if (! INT_SUBTRACT_OVERFLOW (rs, bs) && tmin <= rs - bs && rs - bs <= tmax)
- rs -= bs;
- else
+ if (INT_SUBTRACT_WRAPV (rs, bs, &rs))
{
- if (rs < 0)
+ if (0 < bs)
{
low_overflow:
- rs = tmin;
+ rs = TYPE_MINIMUM (time_t);
rns = 0;
}
else
{
- rs = tmax;
+ rs = TYPE_MAXIMUM (time_t);
rns = TIMESPEC_HZ - 1;
}
}
--
2.21.0
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [PATCH] timespec-add, timespec-sub: simplify,
Paul Eggert <=