diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2019-10-24 17:30:44 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2019-10-24 17:32:55 -0700 |
commit | 06011ed74e978613422aca43c0bd92dc44213933 (patch) | |
tree | 64d28fc78410be7217c523497da7303541be2939 /lib/timespec-add.c | |
parent | 56ca994d471611b44829fa89b58747b288fa66fc (diff) | |
download | gnulib-06011ed74e978613422aca43c0bd92dc44213933.tar.gz |
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.
Diffstat (limited to 'lib/timespec-add.c')
-rw-r--r-- | lib/timespec-add.c | 20 |
1 files changed, 7 insertions, 13 deletions
diff --git a/lib/timespec-add.c b/lib/timespec-add.c index e0a9f12e14..abee154820 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; } } |