From 532d4a24e2013262dfa41fd85c06a9715c99abf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Mon, 24 Oct 2022 21:03:42 +0200 Subject: New upstream version 4.7 --- src/time.c | 62 ++++++++++++++++---------------------------------------------- 1 file changed, 16 insertions(+), 46 deletions(-) (limited to 'src/time.c') diff --git a/src/time.c b/src/time.c index e7eee3f..4daef27 100644 --- a/src/time.c +++ b/src/time.c @@ -6,6 +6,7 @@ * General Public License as published by the Free Software Foundation; * either version 2.1 or (at your option) any later version. */ +#include #include #include #include @@ -39,38 +40,13 @@ HX_timespec_neg(struct timespec *r, const struct timespec *a) EXPORT_SYMBOL struct timespec *HX_timespec_add(struct timespec *r, const struct timespec *a, const struct timespec *b) { - /* - * Split the value represented by the struct into two - * independent values that can be added individually. - */ - long nsec[2]; - nsec[0] = (a->tv_sec < 0) ? -a->tv_nsec : a->tv_nsec; - nsec[1] = (b->tv_sec < 0) ? -b->tv_nsec : b->tv_nsec; - - r->tv_sec = a->tv_sec + b->tv_sec; - r->tv_nsec = nsec[0] + nsec[1]; - if (r->tv_nsec >= NANOSECOND) { - ++r->tv_sec; - r->tv_nsec -= NANOSECOND; - } else if (r->tv_nsec <= -NANOSECOND) { - --r->tv_sec; - r->tv_nsec += NANOSECOND; - } - - /* Combine again */ - if (r->tv_sec < 0) { - if (r->tv_nsec < 0) { - r->tv_nsec = -r->tv_nsec; - } else if (r->tv_nsec > 0) { - if (++r->tv_sec == 0) - r->tv_nsec = -NANOSECOND + r->tv_nsec; - else - r->tv_nsec = NANOSECOND - r->tv_nsec; - } - } else if (r->tv_sec > 0 && r->tv_nsec < 0) { - --r->tv_sec; - r->tv_nsec = NANOSECOND + r->tv_nsec; - } + long aa = a->tv_sec * NANOSECOND_LL + + ((a->tv_sec >= 0) ? a->tv_nsec : -a->tv_nsec); + long bb = b->tv_sec * NANOSECOND_LL + + ((b->tv_sec >= 0) ? b->tv_nsec : -b->tv_nsec); + long rr = aa + bb; + r->tv_sec = rr / NANOSECOND; + r->tv_nsec = ((r->tv_sec < 0) ? -rr : rr) % NANOSECOND; return r; } @@ -119,20 +95,14 @@ HX_timespec_mulf(struct timespec *r, const struct timespec *a, double f) EXPORT_SYMBOL struct timeval *HX_timeval_sub(struct timeval *delta, const struct timeval *future, const struct timeval *past) { - delta->tv_sec = future->tv_sec - past->tv_sec; - delta->tv_usec = future->tv_usec - past->tv_usec; - if (future->tv_sec < past->tv_sec || (future->tv_sec == past->tv_sec && - future->tv_usec < past->tv_usec)) { - if (future->tv_usec > past->tv_usec) { - delta->tv_usec = -MICROSECOND + delta->tv_usec; - ++delta->tv_sec; - } - if (delta->tv_sec < 0) - delta->tv_usec *= -1; - } else if (delta->tv_usec < 0) { - delta->tv_usec += MICROSECOND; - --delta->tv_sec; - } + struct timespec d, f, p; + f.tv_sec = future->tv_sec; + f.tv_nsec = future->tv_usec * 1000; + p.tv_sec = past->tv_sec; + p.tv_nsec = past->tv_usec * 1000; + HX_timespec_sub(&d, &f, &p); + delta->tv_sec = d.tv_sec; + delta->tv_usec = d.tv_nsec / 1000; return delta; } #endif -- cgit v1.2.3