|
|
@@ -28,7 +28,8 @@ const time_t time0;
|
|
|
if (sizeof (time_t) >= sizeof (double))
|
|
|
return time1 - time0;
|
|
|
else return (double) time1 - (double) time0;
|
|
|
- } else if (!TYPE_SIGNED(time_t)) {
|
|
|
+ }
|
|
|
+ if (!TYPE_SIGNED(time_t)) {
|
|
|
/*
|
|
|
** time_t is integral and unsigned.
|
|
|
** The difference of two unsigned values can't overflow
|
|
|
@@ -37,22 +38,26 @@ const time_t time0;
|
|
|
if (time1 >= time0)
|
|
|
return time1 - time0;
|
|
|
else return -((double) (time0 - time1));
|
|
|
- } else {
|
|
|
- /*
|
|
|
- ** time_t is integral and signed.
|
|
|
- ** Handle cases where both time1 and time0 have the same sign
|
|
|
- ** (meaning that their difference cannot overflow).
|
|
|
- ** Handle the more common case (both non-negative) first.
|
|
|
- */
|
|
|
- if (time1 >= 0 && time0 >= 0)
|
|
|
- return time1 - time0;
|
|
|
- if (time1 < 0 && time0 < 0)
|
|
|
- return time1 - time0;
|
|
|
- /*
|
|
|
- ** Punt everything else.
|
|
|
- ** Note that we can't use the % operator on time_t values;
|
|
|
- ** doing so is invalid on systems where time_t isn't integral.
|
|
|
- */
|
|
|
- return (double) time1 - (double) time0;
|
|
|
}
|
|
|
+ /*
|
|
|
+ ** time_t is integral and signed.
|
|
|
+ ** Handle cases where both time1 and time0 have the same sign
|
|
|
+ ** (meaning that their difference cannot overflow).
|
|
|
+ */
|
|
|
+ if (time1 >= 0 && time0 >= 0 || time1 < 0 && time0 < 0)
|
|
|
+ return time1 - time0;
|
|
|
+ /*
|
|
|
+ ** time1 and time0 have opposite signs.
|
|
|
+ ** Punt if unsigned long is too narrow.
|
|
|
+ */
|
|
|
+ if (sizeof (unsigned long) < sizeof (time_t))
|
|
|
+ return (double) time1 - (double) time0;
|
|
|
+ /*
|
|
|
+ ** Stay calm...decent optimizers will eliminate the complexity below.
|
|
|
+ */
|
|
|
+ if (time1 >= 0 /* && time0 < 0 */)
|
|
|
+ return (unsigned long) time1 +
|
|
|
+ (unsigned long) (-(time0 + 1)) + 1;
|
|
|
+ return -(double) ((unsigned long) time0 +
|
|
|
+ (unsigned long) (-(time1 + 1)) + 1);
|
|
|
}
|