Software APIs
Functions
math.h File Reference

(30d7e787c7)

Math helper functions. More...

#include <stdint.h>
#include "sw/device/lib/base/macros.h"

Go to the source code of this file.

Functions

OT_WARN_UNUSED_RESULT uint64_t udiv64_slow (uint64_t a, uint64_t b, uint64_t *rem_out)
 Computes the 64-bit quotient a / b by way of schoolbook long division. More...
 
OT_WARN_UNUSED_RESULT size_t ceil_div (size_t a, size_t b)
 Computes ceil(a / b) in an overflow-safe way. More...
 

Detailed Description

Math helper functions.

Definition in file math.h.

Function Documentation

◆ ceil_div()

OT_WARN_UNUSED_RESULT size_t ceil_div ( size_t  a,
size_t  b 
)
inline

Computes ceil(a / b) in an overflow-safe way.

Extern declaration of inline function.

If b == 0, this function produces undefined behavior.

Parameters
aThe dividend.
bThe divisor.
Returns
Result, ceil(a / b).

Definition at line 60 of file math.h.

◆ udiv64_slow()

OT_WARN_UNUSED_RESULT uint64_t udiv64_slow ( uint64_t  a,
uint64_t  b,
uint64_t *  rem_out 
)

Computes the 64-bit quotient a / b by way of schoolbook long division.

This function is intentionally very slow: 64-bit divisions should not be a frequent occurence, and faster algorithms result in unreasonable code-size expenditure.

Performing division with the / operator in C code that runs on a 32-bit device can emit a polyfill like __udivdi3; normally, this would be provided by a compiler runtime like libgcc_s, but we intentionally do not link that into on-device binaries. This function should be use to resolve link errors involving __udiv symbols. We do not provide a libgcc-style linker alias for this function, because this operation should be explicit and not over-used.

If passed a non-null pointer, this function will also provide the remainder as a side-product.

If b == 0, this function produces undefined behavior (in practice, a garbage result or a loop without forward progress).

Parameters
aThe dividend.
bThe divisor.
[out]rem_outAn optional out-parameter for the remainder.
Returns
The quotient.

Definition at line 14 of file math.c.