Skip to content

Commit 86a1dca

Browse files
committed
clock: generic optimization for constant divisor on 32-bit machines
Signed-off-by: ligd <liguiding1@xiaomi.com>
1 parent 574e08b commit 86a1dca

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

include/nuttx/clock.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <time.h>
3434

3535
#include <nuttx/compiler.h>
36+
#include <nuttx/lib/math32.h>
3637

3738
/****************************************************************************
3839
* Pre-processor Definitions
@@ -177,11 +178,11 @@
177178

178179
/* ?SEC2TIC rounds up */
179180

180-
#define NSEC2TICK(nsec) (((nsec) + (NSEC_PER_TICK - 1)) / NSEC_PER_TICK)
181-
#define USEC2TICK(usec) (((usec) + (USEC_PER_TICK - 1)) / USEC_PER_TICK)
181+
#define NSEC2TICK(nsec) div_const_roundup(nsec, NSEC_PER_TICK)
182+
#define USEC2TICK(usec) div_const_roundup(usec, USEC_PER_TICK)
182183

183184
#if (MSEC_PER_TICK * USEC_PER_MSEC) == USEC_PER_TICK
184-
# define MSEC2TICK(msec) (((msec) + (MSEC_PER_TICK - 1)) / MSEC_PER_TICK)
185+
# define MSEC2TICK(msec) div_const_roundup(msec, MSEC_PER_TICK)
185186
#else
186187
# define MSEC2TICK(msec) USEC2TICK((msec) * USEC_PER_MSEC)
187188
#endif
@@ -196,14 +197,18 @@
196197
#if (MSEC_PER_TICK * USEC_PER_MSEC) == USEC_PER_TICK
197198
# define TICK2MSEC(tick) ((tick) * MSEC_PER_TICK)
198199
#else
199-
# define TICK2MSEC(tick) (((tick) * USEC_PER_TICK) / USEC_PER_MSEC)
200+
# define TICK2MSEC(tick) div_const(((tick) * USEC_PER_TICK), USEC_PER_MSEC)
200201
#endif
201202

202203
/* TIC2?SEC rounds to nearest */
203204

204-
#define TICK2DSEC(tick) (((tick) + (TICK_PER_DSEC / 2)) / TICK_PER_DSEC)
205-
#define TICK2HSEC(tick) (((tick) + (TICK_PER_HSEC / 2)) / TICK_PER_HSEC)
206-
#define TICK2SEC(tick) (((tick) + (TICK_PER_SEC / 2)) / TICK_PER_SEC)
205+
#define TICK2DSEC(tick) div_const_roundnearest(tick, TICK_PER_DSEC)
206+
#define TICK2HSEC(tick) div_const_roundnearest(tick, TICK_PER_HSEC)
207+
#define TICK2SEC(tick) div_const_roundnearest(tick, TICK_PER_SEC)
208+
209+
/* USEC2SEC */
210+
211+
#define USEC2SEC(usec) div_const(usec, USEC_PER_SEC)
207212

208213
#if defined(CONFIG_DEBUG_SCHED) && defined(CONFIG_SYSTEM_TIME64) && \
209214
!defined(CONFIG_SCHED_TICKLESS)

0 commit comments

Comments
 (0)