From 5833f433055cbc259bfe53286a6d3f6687fdd7db Mon Sep 17 00:00:00 2001 From: Lionel Landwerlin Date: Wed, 21 Aug 2019 11:21:05 +0200 Subject: [PATCH] util/timespec: use unsigned 64 bit integers for nsec values We added this utility for vulkan where all timeouts are given as uint64_t values. We can switch from signed to unsigned as this is the only user and if we ever deal with signed integers somewhere else we'll have to be careful to use the corresponding timespec_(add|sub)_msec and always pass absolute values. v2: Forgot to drop the test calling add_nsec() with a negative number Signed-off-by: Lionel Landwerlin Reported-by: Juan A. Suarez Romero Fixes: d2d70c3bb5 ("util: add a timespec helper") Acked-by: Daniel Stone --- src/util/tests/timespec/timespec_test.cpp | 25 ++++---------------- src/util/timespec.h | 28 +++++++++++------------ 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/src/util/tests/timespec/timespec_test.cpp b/src/util/tests/timespec/timespec_test.cpp index 51a57c507f2..4182af0771a 100644 --- a/src/util/tests/timespec/timespec_test.cpp +++ b/src/util/tests/timespec/timespec_test.cpp @@ -136,27 +136,6 @@ TEST(timespec_test, timespec_add_nsec) EXPECT_EQ(r.tv_sec, 3); EXPECT_EQ(r.tv_nsec, 1); - a.tv_sec = 1; - a.tv_nsec = 1; - timespec_add_nsec(&r, &a, -2); - EXPECT_EQ(r.tv_sec, 0); - EXPECT_EQ(r.tv_nsec, NSEC_PER_SEC - 1); - - a.tv_nsec = 0; - timespec_add_nsec(&r, &a, -NSEC_PER_SEC); - EXPECT_EQ(0, r.tv_sec); - EXPECT_EQ(0, r.tv_nsec); - - a.tv_nsec = 0; - timespec_add_nsec(&r, &a, -NSEC_PER_SEC + 1); - EXPECT_EQ(0, r.tv_sec); - EXPECT_EQ(1, r.tv_nsec); - - a.tv_nsec = 50; - timespec_add_nsec(&r, &a, (-NSEC_PER_SEC * 10ULL)); - EXPECT_EQ(-9, r.tv_sec); - EXPECT_EQ(50, r.tv_nsec); - r.tv_sec = 4; r.tv_nsec = 0; timespec_add_nsec(&r, &r, NSEC_PER_SEC + 10ULL); @@ -224,6 +203,10 @@ TEST(timespec_test, timespec_from_nsec) timespec_from_nsec(&a, (5LL * NSEC_PER_SEC) + 1); EXPECT_EQ(5, a.tv_sec); EXPECT_EQ(1, a.tv_nsec); + + timespec_from_nsec(&a, UINT64_MAX); + EXPECT_EQ(a.tv_nsec, UINT64_MAX % NSEC_PER_SEC); + EXPECT_EQ(a.tv_sec, UINT64_MAX / NSEC_PER_SEC); } TEST(timespec_test, timespec_from_usec) diff --git a/src/util/timespec.h b/src/util/timespec.h index ab112e4e621..cbfcc730f89 100644 --- a/src/util/timespec.h +++ b/src/util/timespec.h @@ -85,7 +85,7 @@ timespec_sub(struct timespec *r, * \param b[in] operand in nanoseconds */ static inline void -timespec_add_nsec(struct timespec *r, const struct timespec *a, int64_t b) +timespec_add_nsec(struct timespec *r, const struct timespec *a, uint64_t b) { r->tv_sec = a->tv_sec + (b / NSEC_PER_SEC); r->tv_nsec = a->tv_nsec + (b % NSEC_PER_SEC); @@ -107,7 +107,7 @@ timespec_add_nsec(struct timespec *r, const struct timespec *a, int64_t b) * \param b[in] operand in milliseconds */ static inline void -timespec_add_msec(struct timespec *r, const struct timespec *a, int64_t b) +timespec_add_msec(struct timespec *r, const struct timespec *a, uint64_t b) { timespec_add_nsec(r, a, b * 1000000); } @@ -118,10 +118,10 @@ timespec_add_msec(struct timespec *r, const struct timespec *a, int64_t b) * \param a timespec * \return nanoseconds */ -static inline int64_t +static inline uint64_t timespec_to_nsec(const struct timespec *a) { - return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec; + return (uint64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec; } /** @@ -131,7 +131,7 @@ timespec_to_nsec(const struct timespec *a) * \param b[in] operand * \return to_nanoseconds(a - b) */ -static inline int64_t +static inline uint64_t timespec_sub_to_nsec(const struct timespec *a, const struct timespec *b) { struct timespec r; @@ -147,10 +147,10 @@ timespec_sub_to_nsec(const struct timespec *a, const struct timespec *b) * * Rounding to integer milliseconds happens always down (floor()). */ -static inline int64_t +static inline uint64_t timespec_to_msec(const struct timespec *a) { - return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000; + return (uint64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000; } /** @@ -160,7 +160,7 @@ timespec_to_msec(const struct timespec *a) * \param b[in] operand * \return to_milliseconds(a - b) */ -static inline int64_t +static inline uint64_t timespec_sub_to_msec(const struct timespec *a, const struct timespec *b) { return timespec_sub_to_nsec(a, b) / 1000000; @@ -174,10 +174,10 @@ timespec_sub_to_msec(const struct timespec *a, const struct timespec *b) * * Rounding to integer microseconds happens always down (floor()). */ -static inline int64_t +static inline uint64_t timespec_to_usec(const struct timespec *a) { - return (int64_t)a->tv_sec * 1000000 + a->tv_nsec / 1000; + return (uint64_t)a->tv_sec * 1000000 + a->tv_nsec / 1000; } /** @@ -212,7 +212,7 @@ timespec_to_proto(const struct timespec *a, uint32_t *tv_sec_hi, * \param b nanoseconds */ static inline void -timespec_from_nsec(struct timespec *a, int64_t b) +timespec_from_nsec(struct timespec *a, uint64_t b) { a->tv_sec = b / NSEC_PER_SEC; a->tv_nsec = b % NSEC_PER_SEC; @@ -225,7 +225,7 @@ timespec_from_nsec(struct timespec *a, int64_t b) * \param b microseconds */ static inline void -timespec_from_usec(struct timespec *a, int64_t b) +timespec_from_usec(struct timespec *a, uint64_t b) { timespec_from_nsec(a, b * 1000); } @@ -237,7 +237,7 @@ timespec_from_usec(struct timespec *a, int64_t b) * \param b milliseconds */ static inline void -timespec_from_msec(struct timespec *a, int64_t b) +timespec_from_msec(struct timespec *a, uint64_t b) { timespec_from_nsec(a, b * 1000000); } @@ -290,7 +290,7 @@ timespec_eq(const struct timespec *a, const struct timespec *b) * \param mhz frequency in mHz, not zero * \return period in nanoseconds */ -static inline int64_t +static inline uint64_t millihz_to_nsec(uint32_t mhz) { assert(mhz > 0); -- 2.30.2