Skip to content

Commit e72e78a

Browse files
committed
fixup! sys: util: timeutil: add timespec manipulation utilities
1 parent 2297f6e commit e72e78a

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

include/zephyr/sys/timeutil.h

+16
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,24 @@ static inline bool timespec_normalize_overflow(struct timespec *ts)
381381
__ASSERT_NO_MSG(ts != NULL);
382382

383383
int sign = (ts->tv_nsec >= 0) - (ts->tv_nsec < 0);
384+
#ifdef CONFIG_SPEED_OPTIMIZATIONS
385+
/*
386+
* Branchless, for cores with icache, dcache, hw divide, etc. where cache hits are more
387+
* expensive than branches.
388+
*/
384389
int64_t sec = (ts->tv_nsec >= NSEC_PER_SEC) * (ts->tv_nsec / NSEC_PER_SEC) +
385390
(ts->tv_nsec < 0) * DIV_ROUND_UP(-ts->tv_nsec, NSEC_PER_SEC);
391+
#else
392+
/* size-optimized */
393+
long sec = 0;
394+
395+
if (ts->tv_nsec >= NSEC_PER_SEC) {
396+
sec = ts->tv_nsec / NSEC_PER_SEC;
397+
} else if (ts->tv_nsec < 0) {
398+
sec = DIV_ROUND_UP(-ts->tv_nsec, NSEC_PER_SEC);
399+
}
400+
#endif
401+
386402
bool overflow = __builtin_add_overflow(ts->tv_sec, sign * sec, &ts->tv_sec) ||
387403
__builtin_sub_overflow(ts->tv_nsec, (long)(sign * sec * NSEC_PER_SEC),
388404
&ts->tv_nsec);

0 commit comments

Comments
 (0)