timer: improve overflow checks

Probably more correct and better readable. Although the special-casing
of 0x1p63 is weird in terms of readability (the value itself is
INT64_MAX+1, so it's already outside of range, but INT64_MAX is not
exactly representable with double precision).
This commit is contained in:
wm4 2014-05-22 22:32:57 +02:00
parent cb2e784c07
commit c9b68957c9
1 changed files with 5 additions and 4 deletions

View File

@ -69,12 +69,13 @@ int64_t mp_time_relative_us(int64_t *t)
int64_t mp_add_timeout(int64_t time_us, double timeout_sec)
{
assert(time_us > 0); // mp_time_us() returns strictly positive values
double t = timeout_sec * 1000 * 1000;
if (t >= (double)(INT64_MAX - time_us))
double t = MPCLAMP(timeout_sec * (1000 * 1000), -0x1p63, 0x1p63);
int64_t ti = t == 0x1p63 ? INT64_MAX : t;
if (ti > INT64_MAX - time_us)
return INT64_MAX;
if (t <= -(double)time_us)
if (ti <= -time_us)
return 1;
return time_us + (int64_t)t;
return time_us + ti;
}
static void get_realtime(struct timespec *out_ts)