div.h File Reference

Integer division functions. More...

Detailed Description

Integer division functions.

This header provides some integer division functions that can be used to prevent linking in compiler-generated ones, which are often larger.

Author
Kaspar Schleiser kaspa.nosp@m.r@sc.nosp@m.hleis.nosp@m.er.d.nosp@m.e
Joakim NohlgÄrd joaki.nosp@m.m.no.nosp@m.hlgar.nosp@m.d@ei.nosp@m.stec..nosp@m.se

Definition in file div.h.

#include <assert.h>
#include <stdint.h>
+ Include dependency graph for div.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

#define DIV_H_INV_15625_32   0x431bde83ul
 Approximation of (2**l)/d for d=15625, l=12, 32 bits.
 
#define DIV_H_INV_15625_64   0x431bde82d7b634dbull
 Approximation of (2**l)/d for d=15625, l=12, 64 bits.
 
#define DIV_H_INV_15625_SHIFT   12
 Required shifts for division by 15625, l above.
 
uint64_t _div_mulhi64 (const uint64_t a, const uint64_t b)
 
static uint64_t div_u64_by_15625 (uint64_t val)
 Integer divide val by 15625, 64 bit version. More...
 
static uint32_t div_u64_by_125 (uint64_t val)
 Integer divide val by 125. More...
 
static uint64_t div_u64_by_1000000 (uint64_t val)
 Integer divide val by 1000000. More...
 
static uint32_t div_u32_by_15625div512 (uint32_t val)
 Divide val by (15625/512) More...
 
static uint64_t div_u64_by_15625div512 (uint64_t val)
 Divide val by (15625/512) More...
 
static uint32_t div_u32_by_44488 (uint32_t val)
 Integer divide val by 44488. More...
 
static uint32_t div_u32_mod_44488 (uint32_t val)
 Modulo 44488. More...
 

Function Documentation

◆ div_u32_by_15625div512()

static uint32_t div_u32_by_15625div512 ( uint32_t  val)
inlinestatic

Divide val by (15625/512)

This is used to quantize a 1MHz value to the closest 32768Hz value, e.g., for timers.

The algorithm uses the modular multiplicative inverse of 15625 to use only multiplication and bit shifts to perform the division.

The result will be equal to the mathematical expression: floor((val * 512) / 15625)

Parameters
[in]valdividend
Returns
(val / (15625/512))

Definition at line 129 of file div.h.

◆ div_u32_by_44488()

static uint32_t div_u32_by_44488 ( uint32_t  val)
inlinestatic

Integer divide val by 44488.

Parameters
[in]valdividend
Returns
(val / 44488)

Definition at line 163 of file div.h.

◆ div_u32_mod_44488()

static uint32_t div_u32_mod_44488 ( uint32_t  val)
inlinestatic

Modulo 44488.

Parameters
[in]valdividend
Returns
(val % 44488)

Definition at line 174 of file div.h.

◆ div_u64_by_1000000()

static uint64_t div_u64_by_1000000 ( uint64_t  val)
inlinestatic

Integer divide val by 1000000.

Parameters
[in]valdividend
Returns
(val / 1000000)

Definition at line 110 of file div.h.

◆ div_u64_by_125()

static uint32_t div_u64_by_125 ( uint64_t  val)
inlinestatic

Integer divide val by 125.

This function can be used to convert uint64_t microsecond times (or intervals) to miliseconds and store them in uint32_t variables, with up to ~50 days worth of miliseconds ((2**32*1000) -1). Use e.g., ms = div_u64_by_125(microseconds >> 3)

Precondition
val <= 536870911999 ((2**32 * 125) -1)
Parameters
[in]valdividend
Returns
(val / 125)

Definition at line 90 of file div.h.

◆ div_u64_by_15625()

static uint64_t div_u64_by_15625 ( uint64_t  val)
inlinestatic

Integer divide val by 15625, 64 bit version.

Parameters
[in]valdividend
Returns
(val / 15625)

Definition at line 69 of file div.h.

◆ div_u64_by_15625div512()

static uint64_t div_u64_by_15625div512 ( uint64_t  val)
inlinestatic

Divide val by (15625/512)

This is used to quantize a 1MHz value to the closest 32768Hz value, e.g., for timers.

Parameters
[in]valdividend
Returns
(val / (15625/512))

Definition at line 143 of file div.h.