Skip to content

Commit d02235c

Browse files
cfriedtnashif
authored andcommitted
timeutil: timespec: mitigate warnings when -Wtype-limits is used
Previously, warnings could be promoted to errors in timeutil.h. Those warnings were of the form below. warning: comparison is always true due to limited range of data type warning: comparison is always false due to limited range of data type Specifically, in the speed-optimized version of timespec_normalize() and in the macro SYS_TICKS_TO_NSECS(). The speed-optimized version of timespec_normalize(), which used the __builtin_add_overflow() function, was mainly intended to be branchless. However, many targets generate branching instructions regardless. The speed optimized version is less valuable in that case, so remove it. Additionally, SYS_TICKS_TO_NSECS() does not generate the above warnings when k_ticks_to_ns_floor64() is used instead of k_ticks_to_ns_floor32(), so use the former. Signed-off-by: Chris Friedt <[email protected]>
1 parent ed6b4a2 commit d02235c

File tree

1 file changed

+1
-32
lines changed

1 file changed

+1
-32
lines changed

include/zephyr/sys/timeutil.h

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ extern "C" {
5353
#define SYS_TICKS_TO_NSECS(ticks) \
5454
(((uint64_t)(ticks) >= (uint64_t)K_TICKS_FOREVER) \
5555
? (NSEC_PER_SEC - 1) \
56-
: k_ticks_to_ns_floor32((uint64_t)(ticks) % CONFIG_SYS_CLOCK_TICKS_PER_SEC))
56+
: k_ticks_to_ns_floor64((uint64_t)(ticks) % CONFIG_SYS_CLOCK_TICKS_PER_SEC))
5757

5858
/* Define a timespec */
5959
#define SYS_TIMESPEC(sec, nsec) \
@@ -419,36 +419,6 @@ static inline bool timespec_normalize(struct timespec *ts)
419419
{
420420
__ASSERT_NO_MSG(ts != NULL);
421421

422-
#if defined(CONFIG_SPEED_OPTIMIZATIONS) && HAS_BUILTIN(__builtin_add_overflow)
423-
424-
int64_t sec = 0;
425-
int sign = (ts->tv_nsec >= 0) - (ts->tv_nsec < 0);
426-
427-
/* only one of the following should be non-zero */
428-
sec += (ts->tv_nsec >= (long)NSEC_PER_SEC) * (ts->tv_nsec / (long)NSEC_PER_SEC);
429-
sec += ((sizeof(ts->tv_nsec) != sizeof(int64_t)) && (ts->tv_nsec != LONG_MIN) &&
430-
(ts->tv_nsec < 0)) *
431-
DIV_ROUND_UP((unsigned long)-ts->tv_nsec, (long)NSEC_PER_SEC);
432-
sec += ((sizeof(ts->tv_nsec) == sizeof(int64_t)) && (ts->tv_nsec != INT64_MIN) &&
433-
(ts->tv_nsec < 0)) *
434-
DIV_ROUND_UP((uint64_t)-ts->tv_nsec, NSEC_PER_SEC);
435-
sec += ((sizeof(ts->tv_nsec) != sizeof(int64_t)) && (ts->tv_nsec == LONG_MIN)) *
436-
((LONG_MAX / NSEC_PER_SEC) + 1);
437-
sec += ((sizeof(ts->tv_nsec) == sizeof(int64_t)) && (ts->tv_nsec == INT64_MIN)) *
438-
((INT64_MAX / NSEC_PER_SEC) + 1);
439-
440-
ts->tv_nsec -= sec * sign * NSEC_PER_SEC;
441-
442-
bool overflow = __builtin_add_overflow(ts->tv_sec, sign * sec, &ts->tv_sec);
443-
444-
if (!overflow) {
445-
__ASSERT_NO_MSG(timespec_is_valid(ts));
446-
}
447-
448-
return !overflow;
449-
450-
#else
451-
452422
long sec;
453423

454424
if (ts->tv_nsec >= (long)NSEC_PER_SEC) {
@@ -489,7 +459,6 @@ static inline bool timespec_normalize(struct timespec *ts)
489459
__ASSERT_NO_MSG(timespec_is_valid(ts));
490460

491461
return true;
492-
#endif
493462
}
494463

495464
/**

0 commit comments

Comments
 (0)