12#ifndef ZEPHYR_INCLUDE_SYS_DIV64_H_
13#define ZEPHYR_INCLUDE_SYS_DIV64_H_
52#if HAS_BUILTIN(__builtin_constant_p) && HAS_BUILTIN(__builtin_clz) && HAS_BUILTIN(__builtin_ctz)
53#define Z_DIV64_IF_CONST(n, a, b) (0 + (__builtin_constant_p(n) ? (a) : (b)))
55#define Z_DIV64_NO_BUILTINS
56#define Z_DIV64_IF_CONST(n, a, b) (a)
60#if defined(CONFIG_64BIT) || defined(CONFIG_NO_OPTIMIZATIONS) || defined(Z_DIV64_NO_BUILTINS)
65#if defined(CONFIG_64BIT) || defined(Z_DIV64_NO_BUILTINS)
66#define Z_DIV64_PLAIN_RECIP
93#define Z_DIV64_LOG2(d) (31 - __builtin_clz(((uint32_t)(d) - 1U) | 1U))
94#define Z_DIV64_P(d) (1ULL << Z_DIV64_LOG2(d))
95#define Z_DIV64_Q(d) (UINT64_MAX / (d))
96#define Z_DIV64_R(d) ((UINT64_MAX % (d)) + 1ULL)
97#define Z_DIV64_REM(d) ((Z_DIV64_R(d) * Z_DIV64_P(d)) % (d))
98#define Z_DIV64_BIAS(d) ((Z_DIV64_REM(d) != 0U) && (Z_DIV64_REM(d) < (d) - Z_DIV64_P(d)))
99#define Z_DIV64_M0(d) \
100 ((Z_DIV64_Q(d) * Z_DIV64_P(d)) + ((Z_DIV64_R(d) * Z_DIV64_P(d)) / (d)) + \
101 (((Z_DIV64_REM(d) != 0U) && !Z_DIV64_BIAS(d)) ? 1U : 0U))
102#define Z_DIV64_ZEROS(d) __builtin_ctz((uint32_t)Z_DIV64_M0(d) | (uint32_t)Z_DIV64_P(d))
103#define Z_DIV64_M(d) (Z_DIV64_M0(d) >> Z_DIV64_ZEROS(d))
104#define Z_DIV64_SHIFT(d) ((uint32_t)(Z_DIV64_LOG2(d) - Z_DIV64_ZEROS(d)))
105#define Z_DIV64_LO(x) ((x) & 0xffffffffULL)
106#define Z_DIV64_HI(x) ((x) >> 32)
108#define Z_DIV64_FLAG_BIAS 0x80U
109#define Z_DIV64_FLAG_SHIFT 0x40U
110#define Z_DIV64_SHIFT_MASK 0x3fU
111#define Z_DIV64_FLAGS(d) (Z_DIV64_SHIFT(d) | (Z_DIV64_BIAS(d) ? Z_DIV64_FLAG_BIAS : 0U))
114#define Z_DIV64_NO_OVF(d) ((Z_DIV64_HI(Z_DIV64_M(d)) + Z_DIV64_LO(Z_DIV64_M(d))) <= UINT32_MAX)
116#define Z_DIV64_ILOG2(d) (31 - __builtin_clz((uint32_t)(d)))
131 x = ((
uint64_t)m_lo * n_lo) + (bias ? m : 0U);
138 x = ((
uint64_t)m_lo * n_lo) + (bias ? m_lo : 0U);
158#if defined(CONFIG_SYS_CLOCK_TICKS_PER_SEC)
159#define Z_DIV64_TICKS ((uint32_t)CONFIG_SYS_CLOCK_TICKS_PER_SEC)
161#define Z_DIV64_TICKS 0U
164#if defined(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC) && \
165 !defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
166#define Z_DIV64_CYCLES ((uint32_t)CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC)
168#define Z_DIV64_CYCLES 0U
172#define Z_DIV64_RATIO(a, b) (((b) != 0U) ? ((uint32_t)(a) / (((b) != 0U) ? (b) : 1U)) : 0U)
177#define Z_DIV64_SHARED_LIST(X) \
179 X(1000000, 1000000U) \
180 X(1000000000, 1000000000U) \
181 X(ticks, Z_DIV64_TICKS) \
182 X(cycles, Z_DIV64_CYCLES) \
183 X(ms_per_tick, Z_DIV64_RATIO(1000U, Z_DIV64_TICKS)) \
184 X(us_per_tick, Z_DIV64_RATIO(1000000U, Z_DIV64_TICKS)) \
185 X(ns_per_tick, Z_DIV64_RATIO(1000000000U, Z_DIV64_TICKS)) \
186 X(ticks_per_ms, Z_DIV64_RATIO(Z_DIV64_TICKS, 1000U)) \
187 X(cycles_per_ms, Z_DIV64_RATIO(Z_DIV64_CYCLES, 1000U)) \
188 X(cycles_per_us, Z_DIV64_RATIO(Z_DIV64_CYCLES, 1000000U)) \
189 X(cycles_per_tick, Z_DIV64_RATIO(Z_DIV64_CYCLES, Z_DIV64_TICKS)) \
190 X(ns_per_cycle, Z_DIV64_RATIO(1000000000U, Z_DIV64_CYCLES))
192#define Z_DIV64_SHARED_DECLARE(name, v) extern const struct sys_div64_recip z_div64_recip_##name;
193Z_DIV64_SHARED_LIST(Z_DIV64_SHARED_DECLARE)
195#define Z_DIV64_SHARED_MATCH(name, v) \
196 if (((v) > 1U) && (d == (v))) { \
197 return &z_div64_recip_##name; \
205 Z_DIV64_SHARED_LIST(Z_DIV64_SHARED_MATCH)
214#if defined(Z_DIV64_PLAIN)
225 q = z_div64_mulhi(n, Z_DIV64_M(d), Z_DIV64_BIAS(d), Z_DIV64_NO_OVF(d)) >>
227 }
else if (z_div64_shared(d) != NULL) {
228 q = z_div64_shared_div(n, z_div64_shared(d));
230 q = z_div64_u32_recip(n, Z_DIV64_M(d), Z_DIV64_FLAGS(d));
236 return __builtin_constant_p(n) ? (n / d) : q;
240#define Z_DIV64_U32_CONST(n, d, inl) \
241 Z_DIV64_IF_CONST(n, (uint64_t)(n) / (d), z_div64_u32_const((n), (d), (inl)))
246 (z_div64_u32_const(n, d,
IS_ENABLED(CONFIG_SPEED_OPTIMIZATIONS)) * d));
251#if defined(Z_DIV64_PLAIN)
277#define sys_div64_u32_const(n, d) Z_DIV64_U32_CONST(n, d, IS_ENABLED(CONFIG_SPEED_OPTIMIZATIONS))
290#define sys_div64_u32_const_inline(n, d) Z_DIV64_U32_CONST(n, d, true)
303#define sys_mod64_u32_const(n, d) \
304 Z_DIV64_IF_CONST(n, (uint32_t)((uint64_t)(n) % (d)), z_mod64_u32_const((n), (d)))
316#define sys_sdiv64_u32_const(n, d) \
317 Z_DIV64_IF_CONST(n, (int64_t)(n) / (int64_t)(d), z_sdiv64_u32_const((n), (d)))
328#if !defined(Z_DIV64_PLAIN_RECIP)
340#if defined(Z_DIV64_PLAIN_RECIP)
341#define SYS_DIV64_RECIP_INIT(d) {.div = (d)}
343#define SYS_DIV64_RECIP_INIT(d) \
346 .flags = IS_POWER_OF_TWO(d) ? (Z_DIV64_ILOG2(d) | Z_DIV64_FLAG_SHIFT) \
347 : Z_DIV64_FLAGS(d), \
348 .m = IS_POWER_OF_TWO(d) ? 0U : Z_DIV64_M(d), \
352#if defined(Z_DIV64_PLAIN_RECIP) || defined(__DOXYGEN__)
384#if defined(Z_DIV64_PLAIN_RECIP)
387 return z_div64_u32_recip(n, r->m, r->flags);
401#if defined(Z_DIV64_PLAIN_RECIP)
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition util_macro.h:166
#define IS_POWER_OF_TWO(x)
Check if a x is a power of two.
Definition util_macro.h:95
static void sys_div64_recip_init(struct sys_div64_recip *r, uint32_t d)
Set up the reciprocal of a divisor.
Definition div64.h:363
static uint64_t sys_div64_recip_div(uint64_t n, const struct sys_div64_recip *r)
Divide a 64-bit value by a reciprocal's divisor.
Definition div64.h:382
static uint32_t sys_div64_recip_mod(uint64_t n, const struct sys_div64_recip *r)
Remainder of a 64-bit value divided by a reciprocal's divisor.
Definition div64.h:399
flags
Definition parser.h:86
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
#define UINT32_MAX
Definition stdint.h:29
__INT64_TYPE__ int64_t
Definition stdint.h:75
Reciprocal of a 32-bit divisor known only at run time.
Definition div64.h:325