no-OS
Macros | Functions
util.h File Reference

Implementation of utility functions. More...

#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
Include dependency graph for util.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define BIT(x)   (1 << (x))
 
#define ARRAY_SIZE(x)   (sizeof(x) / sizeof((x)[0]))
 
#define DIV_ROUND_UP(x, y)   (((x) + (y) - 1) / (y))
 
#define DIV_ROUND_CLOSEST(x, y)   (((x) + (y) / 2) / (y))
 
#define DIV_ROUND_CLOSEST_ULL(x, y)   DIV_ROUND_CLOSEST(x, y)
 
#define min(x, y)   (((x) < (y)) ? (x) : (y))
 
#define min_t(type, x, y)   (type)min((type)(x), (type)(y))
 
#define max(x, y)   (((x) > (y)) ? (x) : (y))
 
#define max_t(type, x, y)   (type)max((type)(x), (type)(y))
 
#define clamp(val, min_val, max_val)   (max(min((val), (max_val)), (min_val)))
 
#define clamp_t(type, val, min_val, max_val)   (type)clamp((type)(val), (type)(min_val), (type)(max_val))
 
#define swap(x, y)   {typeof(x) _tmp_ = (x); (x) = (y); (y) = _tmp_;}
 
#define round_up(x, y)   (((x)+(y)-1)/(y))
 
#define BITS_PER_LONG   32
 
#define GENMASK(h, l)
 
#define bswap_constant_32(x)
 
#define bswap_constant_16(x)
 
#define bit_swap_constant_8(x)
 
#define U16_MAX   ((uint16_t)~0U)
 
#define S16_MAX   ((int16_t)(U16_MAX>>1))
 
#define DIV_U64(x, y)   (x / y)
 
#define UNUSED_PARAM(x)   ((void)x)
 
#define shift_right(x, s)   ((x) < 0 ? -(-(x) >> (s)) : (x) >> (s))
 

Functions

uint32_t find_first_set_bit (uint32_t word)
 
uint32_t find_last_set_bit (uint32_t word)
 
uint32_t find_closest (int32_t val, const int32_t *array, uint32_t size)
 
uint32_t field_prep (uint32_t mask, uint32_t val)
 
uint32_t field_get (uint32_t mask, uint32_t word)
 
int32_t log_base_2 (uint32_t x)
 
uint32_t greatest_common_divisor (uint32_t a, uint32_t b)
 
void rational_best_approximation (uint32_t given_numerator, uint32_t given_denominator, uint32_t max_numerator, uint32_t max_denominator, uint32_t *best_numerator, uint32_t *best_denominator)
 
uint32_t hweight8 (uint32_t word)
 
uint64_t do_div (uint64_t *n, uint64_t base)
 
uint64_t div64_u64_rem (uint64_t dividend, uint64_t divisor, uint64_t *remainder)
 
uint64_t div_u64_rem (uint64_t dividend, uint32_t divisor, uint32_t *remainder)
 
int64_t div_s64_rem (int64_t dividend, int32_t divisor, int32_t *remainder)
 
uint64_t div_u64 (uint64_t dividend, uint32_t divisor)
 
int64_t div_s64 (int64_t dividend, int32_t divisor)
 
int32_t str_to_int32 (const char *str)
 
uint32_t srt_to_uint32 (const char *str)
 

Detailed Description

Implementation of utility functions.

Author
DBogdan (drago.nosp@m.s.bo.nosp@m.gdan@.nosp@m.anal.nosp@m.og.co.nosp@m.m)

Copyright 2018(c) Analog Devices, Inc.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Macro Definition Documentation

◆ ARRAY_SIZE

#define ARRAY_SIZE (   x)    (sizeof(x) / sizeof((x)[0]))

◆ BIT

#define BIT (   x)    (1 << (x))

◆ bit_swap_constant_8

#define bit_swap_constant_8 (   x)
Value:
((((x) & 0x80) >> 7) | \
(((x) & 0x40) >> 5) | \
(((x) & 0x20) >> 3) | \
(((x) & 0x10) >> 1) | \
(((x) & 0x08) << 1) | \
(((x) & 0x04) << 3) | \
(((x) & 0x02) << 5) | \
(((x) & 0x01) << 7))

◆ BITS_PER_LONG

#define BITS_PER_LONG   32

◆ bswap_constant_16

#define bswap_constant_16 (   x)
Value:
((((x) & (uint16_t)0xff00) >> 8) | \
(((x) & (uint16_t)0x00ff) << 8))

◆ bswap_constant_32

#define bswap_constant_32 (   x)
Value:
((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
(((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))

◆ clamp

#define clamp (   val,
  min_val,
  max_val 
)    (max(min((val), (max_val)), (min_val)))

◆ clamp_t

#define clamp_t (   type,
  val,
  min_val,
  max_val 
)    (type)clamp((type)(val), (type)(min_val), (type)(max_val))

◆ DIV_ROUND_CLOSEST

#define DIV_ROUND_CLOSEST (   x,
 
)    (((x) + (y) / 2) / (y))

◆ DIV_ROUND_CLOSEST_ULL

#define DIV_ROUND_CLOSEST_ULL (   x,
 
)    DIV_ROUND_CLOSEST(x, y)

◆ DIV_ROUND_UP

#define DIV_ROUND_UP (   x,
 
)    (((x) + (y) - 1) / (y))

◆ DIV_U64

#define DIV_U64 (   x,
 
)    (x / y)

◆ GENMASK

#define GENMASK (   h,
 
)
Value:
({ \
uint32_t t = (uint32_t)(~0UL); \
t = t << (BITS_PER_LONG - (h - l + 1)); \
t = t >> (BITS_PER_LONG - (h + 1)); \
t; \
})

◆ max

#define max (   x,
 
)    (((x) > (y)) ? (x) : (y))

◆ max_t

#define max_t (   type,
  x,
 
)    (type)max((type)(x), (type)(y))

◆ min

#define min (   x,
 
)    (((x) < (y)) ? (x) : (y))

◆ min_t

#define min_t (   type,
  x,
 
)    (type)min((type)(x), (type)(y))

◆ round_up

#define round_up (   x,
 
)    (((x)+(y)-1)/(y))

◆ S16_MAX

#define S16_MAX   ((int16_t)(U16_MAX>>1))

◆ shift_right

#define shift_right (   x,
 
)    ((x) < 0 ? -(-(x) >> (s)) : (x) >> (s))

◆ swap

#define swap (   x,
 
)    {typeof(x) _tmp_ = (x); (x) = (y); (y) = _tmp_;}

◆ U16_MAX

#define U16_MAX   ((uint16_t)~0U)

◆ UNUSED_PARAM

#define UNUSED_PARAM (   x)    ((void)x)

Function Documentation

◆ div64_u64_rem()

uint64_t div64_u64_rem ( uint64_t  dividend,
uint64_t  divisor,
uint64_t *  remainder 
)

◆ div_s64()

int64_t div_s64 ( int64_t  dividend,
int32_t  divisor 
)
Here is the caller graph for this function:

◆ div_s64_rem()

int64_t div_s64_rem ( int64_t  dividend,
int32_t  divisor,
int32_t *  remainder 
)
Here is the caller graph for this function:

◆ div_u64()

uint64_t div_u64 ( uint64_t  dividend,
uint32_t  divisor 
)

◆ div_u64_rem()

uint64_t div_u64_rem ( uint64_t  dividend,
uint32_t  divisor,
uint32_t *  remainder 
)

◆ do_div()

uint64_t do_div ( uint64_t *  n,
uint64_t  base 
)
Here is the caller graph for this function:

◆ field_get()

uint32_t field_get ( uint32_t  mask,
uint32_t  word 
)
Here is the caller graph for this function:

◆ field_prep()

uint32_t field_prep ( uint32_t  mask,
uint32_t  val 
)
Here is the caller graph for this function:

◆ find_closest()

uint32_t find_closest ( int32_t  val,
const int32_t *  array,
uint32_t  size 
)
Here is the caller graph for this function:

◆ find_first_set_bit()

uint32_t find_first_set_bit ( uint32_t  word)
Here is the caller graph for this function:

◆ find_last_set_bit()

uint32_t find_last_set_bit ( uint32_t  word)

◆ greatest_common_divisor()

uint32_t greatest_common_divisor ( uint32_t  a,
uint32_t  b 
)

◆ hweight8()

uint32_t hweight8 ( uint32_t  word)
Here is the caller graph for this function:

◆ log_base_2()

int32_t log_base_2 ( uint32_t  x)

◆ rational_best_approximation()

void rational_best_approximation ( uint32_t  given_numerator,
uint32_t  given_denominator,
uint32_t  max_numerator,
uint32_t  max_denominator,
uint32_t *  best_numerator,
uint32_t *  best_denominator 
)

◆ srt_to_uint32()

uint32_t srt_to_uint32 ( const char *  str)
Here is the caller graph for this function:

◆ str_to_int32()

int32_t str_to_int32 ( const char *  str)
BITS_PER_LONG
#define BITS_PER_LONG
Definition: util.h:84