Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
64-bit division

64-bit by 32-bit division More...

Files

file  div64.h
 64-bit by 32-bit division without a 64-bit divide instruction.

Data Structures

struct  sys_div64_recip
 Reciprocal of a 32-bit divisor known only at run time. More...

Macros

#define sys_div64_u32_const(n, d)
 Divide a 64-bit value by a 32-bit constant.
#define sys_div64_u32_const_inline(n, d)
 Divide a 64-bit value by a 32-bit constant, inline.
#define sys_mod64_u32_const(n, d)
 Remainder of a 64-bit value divided by a 32-bit constant.
#define sys_sdiv64_u32_const(n, d)
 Divide a signed 64-bit value by a 32-bit constant.
#define SYS_DIV64_RECIP_INIT(d)
 Static initializer for a reciprocal of a constant divisor.

Functions

static void sys_div64_recip_init (struct sys_div64_recip *r, uint32_t d)
 Set up the reciprocal of a divisor.
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.
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.

Detailed Description

64-bit by 32-bit division

On a 32-bit target, n / d with a 64-bit n is a libgcc call that divides bit by bit, even for a constant d. These helpers multiply by a reciprocal of d instead. 64-bit targets divide in hardware.

Macro Definition Documentation

◆ SYS_DIV64_RECIP_INIT

#define SYS_DIV64_RECIP_INIT ( d)

#include <zephyr/sys/div64.h>

Value:
{ \
.div = (d), \
.flags = IS_POWER_OF_TWO(d) ? (Z_DIV64_ILOG2(d) | Z_DIV64_FLAG_SHIFT) \
: Z_DIV64_FLAGS(d), \
.m = IS_POWER_OF_TWO(d) ? 0U : Z_DIV64_M(d), \
}
#define IS_POWER_OF_TWO(x)
Check if a x is a power of two.
Definition util_macro.h:95
flags
Definition parser.h:86

Static initializer for a reciprocal of a constant divisor.

Parameters
dDivisor, an integer constant expression from 1 to UINT32_MAX.

◆ sys_div64_u32_const

#define sys_div64_u32_const ( n,
d )

#include <zephyr/sys/div64.h>

Value:
Z_DIV64_U32_CONST(n, d, IS_ENABLED(CONFIG_SPEED_OPTIMIZATIONS))
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition util_macro.h:166

Divide a 64-bit value by a 32-bit constant.

On a 32-bit target this multiplies by a reciprocal of d worked out at build time, in a shared out-of-line helper, or inline when optimizing for speed. Without the optimizer, or for a non-constant d, it divides plainly. A constant n folds to a constant, so the result may initialize a static object.

Parameters
nDividend (uint64_t).
dDivisor, a constant from 1 to UINT32_MAX.
Returns
n / d (uint64_t).

◆ sys_div64_u32_const_inline

#define sys_div64_u32_const_inline ( n,
d )

#include <zephyr/sys/div64.h>

Value:
Z_DIV64_U32_CONST(n, d, true)

Divide a 64-bit value by a 32-bit constant, inline.

Same as sys_div64_u32_const(), but the multiplication is always inline, for hot paths that are worth a few dozen bytes per call site.

Parameters
nDividend (uint64_t).
dDivisor, a constant from 1 to UINT32_MAX.
Returns
n / d (uint64_t).

◆ sys_mod64_u32_const

#define sys_mod64_u32_const ( n,
d )

#include <zephyr/sys/div64.h>

Value:
Z_DIV64_IF_CONST(n, (uint32_t)((uint64_t)(n) % (d)), z_mod64_u32_const((n), (d)))
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91

Remainder of a 64-bit value divided by a 32-bit constant.

Paired with sys_div64_u32_const() on the same operands, the compiler computes the quotient once.

Parameters
nDividend (uint64_t).
dDivisor, a constant from 1 to UINT32_MAX.
Returns
n % d (uint32_t).

◆ sys_sdiv64_u32_const

#define sys_sdiv64_u32_const ( n,
d )

#include <zephyr/sys/div64.h>

Value:
Z_DIV64_IF_CONST(n, (int64_t)(n) / (int64_t)(d), z_sdiv64_u32_const((n), (d)))
__INT64_TYPE__ int64_t
Definition stdint.h:75

Divide a signed 64-bit value by a 32-bit constant.

Rounds toward zero, like the C division operator.

Parameters
nDividend (int64_t).
dDivisor, a constant from 1 to UINT32_MAX.
Returns
n / d (int64_t).

Function Documentation

◆ sys_div64_recip_div()

uint64_t sys_div64_recip_div ( uint64_t n,
const struct sys_div64_recip * r )
inlinestatic

#include <zephyr/sys/div64.h>

Divide a 64-bit value by a reciprocal's divisor.

Parameters
nDividend.
rReciprocal of the divisor.
Returns
n divided by the divisor of r.

◆ sys_div64_recip_init()

void sys_div64_recip_init ( struct sys_div64_recip * r,
uint32_t d )
inlinestatic

#include <zephyr/sys/div64.h>

Set up the reciprocal of a divisor.

Takes four 32-bit divisions on a 32-bit target, so do it where the divisor changes, not before each division.

Parameters
rReciprocal to set up.
dDivisor, from 1 to UINT32_MAX.

◆ sys_div64_recip_mod()

uint32_t sys_div64_recip_mod ( uint64_t n,
const struct sys_div64_recip * r )
inlinestatic

#include <zephyr/sys/div64.h>

Remainder of a 64-bit value divided by a reciprocal's divisor.

Parameters
nDividend.
rReciprocal of the divisor.
Returns
n modulo the divisor of r.