Utility Library
Modules |
| | Assembler Support |
| | Assertions |
| | Atomic Operations |
| | Bit Operations |
| | Byte Order Conversion |
| | Compiler Support |
| | Data in Program Space |
| | Standard integer types |
| | String Operations |
| | Unaligned Access Helpers |
Defines |
| #define | xstr(s) str(s) |
| | Stringify the result after expansion of a macro argument.
|
| #define | str(s) #s |
| | Stringify a macro argument without expansion.
|
| #define | ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) |
| | Get the number of elements in array a.
|
| #define | container_of(ptr, type, member) ((type *)((uintptr_t)(ptr) - offsetof(type, member))) |
| | Get the containing object.
|
| #define | round_down(x, order) |
| | Round down to the nearest power of two boundary.
|
| #define | round_up(x, order) |
| | Round up to the nearest power of two boundary.
|
| #define | div_ceil(a, b) (((a) + (b) - 1) / (b)) |
| | Calculate using integer arithmetic.
|
Functions |
| static int | isdigit (int c) |
| | Determine whether or not the character c is a digit.
|
| static int | iscntrl (int c) |
| | Determine whether or not the character c is a control character.
|
| static int | isspace (int c) |
| | Determine whether or not the character c is a space.
|
| int_fast8_t | ilog2_undefined (void) |
| static __always_inline int_fast8_t | ilog2 (uint32_t x) |
| | Calculate the base-2 logarithm of a number rounded down to the nearest integer.
|
| static __always_inline bool | is_power_of_two (unsigned long x) |
| | Test if a given value is a power of two.
|
| static unsigned long | word_align (unsigned long x) |
| | Round up to the nearest word-aligned boundary.
|
Minimum and Maximum |
|
| #define | min_s(a, b) |
| | Get the lowest of two signed values.
|
| #define | min_u(a, b) |
| | Get the lowest of two unsigned values.
|
| #define | max_s(a, b) |
| | Get the highest of two signed values.
|
| #define | max_u(a, b) |
| | Get the highest of two unsigned values.
|
Detailed Description
This is a collection of utility functions and macros which may be useful when dealing with certain common problems, e.g. accessing data from a byte stream, simple mathematical operations, etc.
Define Documentation
| #define ARRAY_LEN |
( |
a |
|
) |
(sizeof(a) / sizeof((a)[0])) |
Get the number of elements in array a.
Definition at line 68 of file util.h.
| #define container_of |
( |
ptr, |
|
|
type, |
|
|
member |
|
) |
((type *)((uintptr_t)(ptr) - offsetof(type, member))) |
Get the containing object.
- Parameters:
-
| ptr | Pointer to the contained object. |
| type | Type of the containing object. |
| member | Member name of the contained object inside the containing object. |
- Returns:
- Pointer to the containing object.
Definition at line 120 of file util.h.
| #define div_ceil |
( |
a, |
|
|
b |
|
) |
(((a) + (b) - 1) / (b)) |
Calculate
using integer arithmetic.
- Parameters:
-
| a | An integer |
| b | Another integer |
- Returns:
- (a / b) rounded up to the nearest integer.
Definition at line 335 of file util.h.
Referenced by bit_array_find_first_one_bit().
Value:((sizeof(a) == 1) && (sizeof(b) == 1) ? compiler_max_s8(a, b) \
: (sizeof(a) <= 2) && (sizeof(b) <= 2) ? compiler_max_s16(a, b) \
: (sizeof(a) <= 4) && (sizeof(b) <= 4) ? compiler_max_s32(a, b) \
: compiler_max_s64(a, b))
Get the highest of two signed values.
- Parameters:
-
| a | A signed integer |
| b | Another signed integer |
- Returns:
- The numerically highest value of a and b.
Definition at line 156 of file util.h.
Value:((sizeof(a) == 1) && (sizeof(b) == 1) ? compiler_max_u8(a, b) \
: (sizeof(a) <= 2) && (sizeof(b) <= 2) ? compiler_max_u16(a, b) \
: (sizeof(a) <= 4) && (sizeof(b) <= 4) ? compiler_max_u32(a, b) \
: compiler_max_u64(a, b))
Get the highest of two unsigned values.
- Parameters:
-
| a | An unsigned integer |
| b | Another unsigned integer |
- Returns:
- The numerically highest value of a and b.
Definition at line 168 of file util.h.
Value:((sizeof(a) == 1) && (sizeof(b) == 1) ? compiler_min_s8(a, b) \
: (sizeof(a) <= 2) && (sizeof(b) <= 2) ? compiler_min_s16(a, b) \
: (sizeof(a) <= 4) && (sizeof(b) <= 4) ? compiler_min_s32(a, b) \
: compiler_min_s64(a, b))
Get the lowest of two signed values.
- Parameters:
-
| a | A signed integer |
| b | Another signed integer |
- Returns:
- The numerically lowest value of a and b.
Definition at line 132 of file util.h.
Value:((sizeof(a) == 1) && (sizeof(b) == 1) ? compiler_min_u8(a, b) \
: (sizeof(a) <= 2) && (sizeof(b) <= 2) ? compiler_min_u16(a, b) \
: (sizeof(a) <= 4) && (sizeof(b) <= 4) ? compiler_min_u32(a, b) \
: compiler_min_u64(a, b))
Get the lowest of two unsigned values.
- Parameters:
-
| a | An unsigned integer |
| b | Another unsigned integer |
- Returns:
- The numerically lowest value of a and b.
Definition at line 144 of file util.h.
| #define round_down |
( |
x, |
|
|
order |
|
) |
|
Value:(sizeof(x) == 4 ? round_down32((uint32_t)(x), (order)) : \
sizeof(x) == 2 ? round_down16((uint16_t)(x), (order)) : \
sizeof(x) == 1 ? round_down8 (( uint8_t)(x), (order)) : \
(priv_round_down_bad_type(),1))
Round down to the nearest power of two boundary.
- Parameters:
-
| x | A positive integer |
| order | log2 of the required boundary. |
- Returns:
- x rounded down to the nearest multiple of (1 << order)
Definition at line 250 of file util.h.
| #define round_up |
( |
x, |
|
|
order |
|
) |
|
Value:(sizeof(x) == 4 ? round_up32((uint32_t)(x), (order)) : \
sizeof(x) == 2 ? round_up16((uint16_t)(x), (order)) : \
sizeof(x) == 1 ? round_up8 (( uint8_t)(x), (order)) : \
(priv_round_up_bad_type(),1))
Round up to the nearest power of two boundary.
- Parameters:
-
| x | A positive integer |
| order | log2 of the required boundary. |
- Returns:
- x rounded up to the next multiple of (1 << order)
Definition at line 280 of file util.h.
Referenced by word_align().
Stringify a macro argument without expansion.
Definition at line 63 of file util.h.
| #define xstr |
( |
s |
|
) |
str(s) |
Stringify the result after expansion of a macro argument.
Definition at line 58 of file util.h.
Function Documentation
For internal use only.
Undefined function. Will cause a link failure if ilog2() is called with an invalid constant value.
Referenced by ilog2().
| static __always_inline bool is_power_of_two |
( |
unsigned long |
x |
) |
[static] |
Test if a given value is a power of two.
- Parameters:
-
- Returns:
- true if x is a power of two, false otherwise
Definition at line 236 of file util.h.
| static int iscntrl |
( |
int |
c |
) |
[inline, static] |
Determine whether or not the character c is a control character.
- Parameters:
-
| c | The character to consider. |
- Return values:
-
| 0 | The character c is not a control character. |
| 1 | The character c is a control character. |
Definition at line 90 of file util.h.
| static int isdigit |
( |
int |
c |
) |
[inline, static] |
Determine whether or not the character c is a digit.
- Parameters:
-
| c | The character to consider. |
- Return values:
-
| 0 | The character c is not a digit. |
| 1 | The character c is a digit. |
Definition at line 77 of file util.h.
| static int isspace |
( |
int |
c |
) |
[inline, static] |
Determine whether or not the character c is a space.
- Note:
- This implementation is very limited in that it doesn't consider a bunch of control characters that probably should be interpreted as space.
- Parameters:
-
| c | The character to consider. |
- Return values:
-
| 0 | The character c is not a space. |
| 1 | The character c is a space. |
Definition at line 106 of file util.h.
| static unsigned long word_align |
( |
unsigned long |
x |
) |
[inline, static] |
Round up to the nearest word-aligned boundary.
- Parameters:
-
| x | Address or offset to be word-aligned |
- Returns:
- The smallest number y where y >= x and y & 3 == 0.
Definition at line 307 of file util.h.
References round_up.