div.h File Reference

Integer division functions. More...

Detailed Description

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 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

static uint32_t div_u32_by_15625div512 ( uint32_t  val)
inlinestatic

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 102 of file div.h.

static uint32_t div_u32_by_44488 ( uint32_t  val)
inlinestatic
Parameters
[in]valdividend
Returns
(val / 44488)

Definition at line 136 of file div.h.

static uint32_t div_u32_mod_44488 ( uint32_t  val)
inlinestatic
Parameters
[in]valdividend
Returns
(val % 44488)

Definition at line 147 of file div.h.

static uint64_t div_u64_by_1000000 ( uint64_t  val)
inlinestatic
Parameters
[in]valdividend
Returns
(val / 1000000)

Definition at line 83 of file div.h.

static uint64_t div_u64_by_15625 ( uint64_t  val)
inlinestatic
Parameters
[in]valdividend
Returns
(val / 15625)

Definition at line 69 of file div.h.

static uint64_t div_u64_by_15625div512 ( uint64_t  val)
inlinestatic

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 116 of file div.h.