Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
Math extras

Extra arithmetic and bit manipulation functions. More...

Data Structures

struct  int128_t
 128-bit integer structure. More...

Unsigned integer addition with overflow detection.

These functions compute a + b and store the result in *result, returning true if the operation overflowed.

static bool u16_add_overflow (uint16_t a, uint16_t b, uint16_t *result)
 Add two unsigned 16-bit integers.
static bool u32_add_overflow (uint32_t a, uint32_t b, uint32_t *result)
 Add two unsigned 32-bit integers.
static bool u64_add_overflow (uint64_t a, uint64_t b, uint64_t *result)
 Add two unsigned 64-bit integers.
static bool size_add_overflow (size_t a, size_t b, size_t *result)
 Add two size_t integers.

Unsigned integer multiplication with overflow detection.

These functions compute a * b and store the result in *result, returning true if the operation overflowed.

static bool u16_mul_overflow (uint16_t a, uint16_t b, uint16_t *result)
 Multiply two unsigned 16-bit integers.
static bool u32_mul_overflow (uint32_t a, uint32_t b, uint32_t *result)
 Multiply two unsigned 32-bit integers.
static bool u64_mul_overflow (uint64_t a, uint64_t b, uint64_t *result)
 Multiply two unsigned 64-bit integers.
static bool size_mul_overflow (size_t a, size_t b, size_t *result)
 Multiply two size_t integers.

Count leading zeros.

Count the number of leading zero bits in the bitwise representation of x.

When x = 0, this is the size of x in bits.

static int u32_count_leading_zeros (uint32_t x)
 Count the number of leading zero bits in a 32-bit integer.
static int u64_count_leading_zeros (uint64_t x)
 Count the number of leading zero bits in a 64-bit integer.

Count trailing zeros.

Count the number of trailing zero bits in the bitwise representation of x.

When x = 0, this is the size of x in bits.

static int u32_count_trailing_zeros (uint32_t x)
 Count the number of trailing zero bits in a 32-bit integer.
static int u64_count_trailing_zeros (uint64_t x)
 Count the number of trailing zero bits in a 64-bit integer.

128-bit arithmetic.

Functions for performing arithmetic operations on 128-bit integers.

These functions provide support for high-precision calculations on platforms where native 128-bit types are not available.

static void i128_multiply_i64_i64 (int64_t a, int64_t b, int128_t *result)
 Multiply two signed 64-bit integers and store the result in a 128-bit integer.

Detailed Description

Extra arithmetic and bit manipulation functions.

Portable wrapper functions for a number of arithmetic and bit-counting functions that are often provided by compiler builtins. If the compiler does not have an appropriate builtin, a portable C implementation is used instead.

Function Documentation

◆ i128_multiply_i64_i64()

void i128_multiply_i64_i64 ( int64_t a,
int64_t b,
int128_t * result )
static

#include <zephyr/sys/math_extras.h>

Multiply two signed 64-bit integers and store the result in a 128-bit integer.

This function performs a full precision multiplication of two signed 64-bit values. The result is guaranteed to fit in a 128-bit representation without overflow.

Parameters
aMultiplicand.
bMultiplier.
resultPointer to the int128_t structure where the result will be stored.

◆ size_add_overflow()

bool size_add_overflow ( size_t a,
size_t b,
size_t * result )
static

#include <zephyr/sys/math_extras.h>

Add two size_t integers.

Parameters
aFirst operand.
bSecond operand.
resultPointer to the result.
Returns
true if the operation overflowed.

◆ size_mul_overflow()

bool size_mul_overflow ( size_t a,
size_t b,
size_t * result )
static

#include <zephyr/sys/math_extras.h>

Multiply two size_t integers.

Parameters
aFirst operand.
bSecond operand.
resultPointer to the result.
Returns
true if the operation overflowed.

◆ u16_add_overflow()

bool u16_add_overflow ( uint16_t a,
uint16_t b,
uint16_t * result )
static

#include <zephyr/sys/math_extras.h>

Add two unsigned 16-bit integers.

Parameters
aFirst operand.
bSecond operand.
resultPointer to the result.
Returns
true if the operation overflowed.

◆ u16_mul_overflow()

bool u16_mul_overflow ( uint16_t a,
uint16_t b,
uint16_t * result )
static

#include <zephyr/sys/math_extras.h>

Multiply two unsigned 16-bit integers.

Parameters
aFirst operand.
bSecond operand.
resultPointer to the result.
Returns
true if the operation overflowed.

◆ u32_add_overflow()

bool u32_add_overflow ( uint32_t a,
uint32_t b,
uint32_t * result )
static

#include <zephyr/sys/math_extras.h>

Add two unsigned 32-bit integers.

Parameters
aFirst operand.
bSecond operand.
resultPointer to the result.
Returns
true if the operation overflowed.

◆ u32_count_leading_zeros()

int u32_count_leading_zeros ( uint32_t x)
static

#include <zephyr/sys/math_extras.h>

Count the number of leading zero bits in a 32-bit integer.

Parameters
xInteger to count leading zeros in.
Returns
Number of leading zero bits in x.

◆ u32_count_trailing_zeros()

int u32_count_trailing_zeros ( uint32_t x)
static

#include <zephyr/sys/math_extras.h>

Count the number of trailing zero bits in a 32-bit integer.

Parameters
xInteger to count trailing zeros in.
Returns
Number of trailing zero bits in x.

◆ u32_mul_overflow()

bool u32_mul_overflow ( uint32_t a,
uint32_t b,
uint32_t * result )
static

#include <zephyr/sys/math_extras.h>

Multiply two unsigned 32-bit integers.

Parameters
aFirst operand.
bSecond operand.
resultPointer to the result.
Returns
true if the operation overflowed.

◆ u64_add_overflow()

bool u64_add_overflow ( uint64_t a,
uint64_t b,
uint64_t * result )
static

#include <zephyr/sys/math_extras.h>

Add two unsigned 64-bit integers.

Parameters
aFirst operand.
bSecond operand.
resultPointer to the result.
Returns
true if the operation overflowed.

◆ u64_count_leading_zeros()

int u64_count_leading_zeros ( uint64_t x)
static

#include <zephyr/sys/math_extras.h>

Count the number of leading zero bits in a 64-bit integer.

Parameters
xInteger to count leading zeros in.
Returns
Number of leading zero bits in x.

◆ u64_count_trailing_zeros()

int u64_count_trailing_zeros ( uint64_t x)
static

#include <zephyr/sys/math_extras.h>

Count the number of trailing zero bits in a 64-bit integer.

Parameters
xInteger to count trailing zeros in.
Returns
Number of trailing zero bits in x.

◆ u64_mul_overflow()

bool u64_mul_overflow ( uint64_t a,
uint64_t b,
uint64_t * result )
static

#include <zephyr/sys/math_extras.h>

Multiply two unsigned 64-bit integers.

Parameters
aFirst operand.
bSecond operand.
resultPointer to the result.
Returns
true if the operation overflowed.