ic-macros  0.1.5
integer_cast.h File Reference
#include <stdint.h>
#include "ic_macros/ic_assert.h"
#include "zephyr/util.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define IC_G_CAST_NUMBER(type, min, max, expr)    (type)(expr) + IC_ZERO_OR_COMPILE_ERROR((expr) >= min && (expr) <= max)
 Compile-time constant safe cast from one number type to another. More...
 
#define IC_CAST_NUMBER(type, min, max, expr)
 Safe cast from one number type to another. More...
 
#define IC_CAST_UNSIGNED(type, _, max, expr)
 Safe cast from one number type to another. More...
 
#define IC_UINT8(expr)   IC_CAST_NUMBER(uint8_t, 0, UINT8_MAX, expr)
 Cast the expression after asserting that it is in range. More...
 
#define IC_UINT16(expr)   IC_CAST_NUMBER(uint16_t, 0, UINT16_MAX, expr)
 Cast the expression after asserting that it is in range. More...
 
#define IC_UINT32(expr)   IC_CAST_NUMBER(uint32_t, 0, UINT32_MAX, expr)
 Cast the expression after asserting that it is in range. More...
 
#define IC_UINT64(expr)   IC_CAST_NUMBER(uint64_t, 0, UINT64_MAX, expr)
 Cast the expression after asserting that it is in range. More...
 
#define IC_INT8(expr)   IC_CAST_NUMBER(int8_t, INT8_MIN, INT8_MAX, expr)
 Cast the expression after asserting that it is in range. More...
 
#define IC_INT16(expr)   IC_CAST_NUMBER(int16_t, INT16_MIN, INT16_MAX, expr)
 Cast the expression after asserting that it is in range. More...
 
#define IC_INT32(expr)   IC_CAST_NUMBER(int32_t, INT32_MIN, INT32_MAX, expr)
 Cast the expression after asserting that it is in range. More...
 
#define IC_INT64(expr)   IC_CAST_NUMBER(int64_t, INT64_MIN, INT64_MAX, expr)
 Cast the expression after asserting that it is in range. More...
 
#define IC_G_UINT8(expr)   IC_G_CAST_NUMBER(uint8_t, 0, UINT8_MAX, expr)
 Global scope only; cast the expression after asserting that it is in range. More...
 
#define IC_G_UINT16(expr)   IC_G_CAST_NUMBER(uint16_t, 0, UINT16_MAX, expr)
 Global scope only; cast the expression after asserting that it is in range. More...
 
#define IC_G_UINT32(expr)   IC_G_CAST_NUMBER(uint32_t, 0, UINT32_MAX, expr)
 Global scope only; cast the expression after asserting that it is in range. More...
 
#define IC_G_UINT64(expr)   IC_G_CAST_NUMBER(uint64_t, 0, UINT64_MAX, expr)
 Global scope only; cast the expression after asserting that it is in range. More...
 
#define IC_G_INT8(expr)   IC_G_CAST_NUMBER(int8_t, INT8_MIN, INT8_MAX, expr)
 Global scope only; cast the expression after asserting that it is in range. More...
 
#define IC_G_INT16(expr)   IC_G_CAST_NUMBER(int16_t, INT16_MIN, INT16_MAX, expr)
 Global scope only; cast the expression after asserting that it is in range. More...
 
#define IC_G_INT32(expr)   IC_G_CAST_NUMBER(int32_t, INT32_MIN, INT32_MAX, expr)
 Global scope only; cast the expression after asserting that it is in range. More...
 
#define IC_G_INT64(expr)   IC_G_CAST_NUMBER(int64_t, INT64_MIN, INT64_MAX, expr)
 Global scope only; cast the expression after asserting that it is in range. More...
 

Macro Definition Documentation

◆ IC_CAST_NUMBER

#define IC_CAST_NUMBER (   type,
  min,
  max,
  expr 
)
Value:
({ \
__auto_type const _IC_CAST_NUMBER_expr_ = (expr); \
IC_ASSERT(IC_G_IN_RANGE(_IC_CAST_NUMBER_expr_, min, max)); \
(type) _IC_CAST_NUMBER_expr_; \
})
#define IC_G_IN_RANGE(val, min, max)
Only use in global scope; checks if a value is within range.
Definition: util.h:548

Safe cast from one number type to another.

Note
The expression will be evaluated once.
Parameters
typeThe integer type to cast to.
minThe minimum value of type, e.g. 0 or INT<>_MIN.
maxThe maximum value of type, e.g. INT<>_MAX.
exprThe expression to cast.
Returns
The expr casted to type: (type) (expr)

Definition at line 34 of file integer_cast.h.

◆ IC_CAST_UNSIGNED

#define IC_CAST_UNSIGNED (   type,
  _,
  max,
  expr 
)
Value:
({ \
__auto_type const _IC_CAST_NUMBER_expr_ = (expr); \
IC_ASSERT(_IC_CAST_NUMBER_expr_ <= max); \
(type) _IC_CAST_NUMBER_expr_; \
})

Safe cast from one number type to another.

Note
The expression will be evaluated once.
Parameters
typeThe integer type to cast to.
minThe minimum value of type, e.g. 0 or INT<>_MIN.
maxThe maximum value of type, e.g. INT<>_MAX.
exprThe expression to cast.
Returns
The expr casted to type: (type) (expr)

Definition at line 50 of file integer_cast.h.

◆ IC_G_CAST_NUMBER

#define IC_G_CAST_NUMBER (   type,
  min,
  max,
  expr 
)     (type)(expr) + IC_ZERO_OR_COMPILE_ERROR((expr) >= min && (expr) <= max)

Compile-time constant safe cast from one number type to another.

Note
Causes a compile error if the expr is out of range.
Parameters
typeThe integer type to cast to.
minThe minimum value of type, e.g. 0 or INT<>_MIN.
maxThe maximum value of type, e.g. INT<>_MAX.
exprThe expression to cast.
Returns
The expr casted to type: (type) (expr)

Definition at line 21 of file integer_cast.h.

◆ IC_G_INT16

#define IC_G_INT16 (   expr)    IC_G_CAST_NUMBER(int16_t, INT16_MIN, INT16_MAX, expr)

Global scope only; cast the expression after asserting that it is in range.

Parameters
exprThe expression to cast.
Returns
The expr casted to type: (int16_t) (expr)

Definition at line 175 of file integer_cast.h.

◆ IC_G_INT32

#define IC_G_INT32 (   expr)    IC_G_CAST_NUMBER(int32_t, INT32_MIN, INT32_MAX, expr)

Global scope only; cast the expression after asserting that it is in range.

Parameters
exprThe expression to cast.
Returns
The expr casted to type: (int32_t) (expr)

Definition at line 182 of file integer_cast.h.

◆ IC_G_INT64

#define IC_G_INT64 (   expr)    IC_G_CAST_NUMBER(int64_t, INT64_MIN, INT64_MAX, expr)

Global scope only; cast the expression after asserting that it is in range.

Parameters
exprThe expression to cast.
Returns
The expr casted to type: (int64_t) (expr)

Definition at line 189 of file integer_cast.h.

◆ IC_G_INT8

#define IC_G_INT8 (   expr)    IC_G_CAST_NUMBER(int8_t, INT8_MIN, INT8_MAX, expr)

Global scope only; cast the expression after asserting that it is in range.

Parameters
exprThe expression to cast.
Returns
The expr casted to type: (int8_t) (expr)

Definition at line 168 of file integer_cast.h.

◆ IC_G_UINT16

#define IC_G_UINT16 (   expr)    IC_G_CAST_NUMBER(uint16_t, 0, UINT16_MAX, expr)

Global scope only; cast the expression after asserting that it is in range.

Parameters
exprThe expression to cast.
Returns
The expr casted to type: (uint16_t) (expr)

Definition at line 147 of file integer_cast.h.

◆ IC_G_UINT32

#define IC_G_UINT32 (   expr)    IC_G_CAST_NUMBER(uint32_t, 0, UINT32_MAX, expr)

Global scope only; cast the expression after asserting that it is in range.

Parameters
exprThe expression to cast.
Returns
The expr casted to type: (uint32_t) (expr)

Definition at line 154 of file integer_cast.h.

◆ IC_G_UINT64

#define IC_G_UINT64 (   expr)    IC_G_CAST_NUMBER(uint64_t, 0, UINT64_MAX, expr)

Global scope only; cast the expression after asserting that it is in range.

Parameters
exprThe expression to cast.
Returns
The expr casted to type: (uint64_t) (expr)

Definition at line 161 of file integer_cast.h.

◆ IC_G_UINT8

#define IC_G_UINT8 (   expr)    IC_G_CAST_NUMBER(uint8_t, 0, UINT8_MAX, expr)

Global scope only; cast the expression after asserting that it is in range.

Parameters
exprThe expression to cast.
Returns
The expr casted to type: (uint8_t) (expr)

Definition at line 140 of file integer_cast.h.

◆ IC_INT16

#define IC_INT16 (   expr)    IC_CAST_NUMBER(int16_t, INT16_MIN, INT16_MAX, expr)

Cast the expression after asserting that it is in range.

Note
The expression will be evaluated once.
Parameters
exprThe expression to cast.
Returns
The expr casted to type: (int16_t) (expr)

Definition at line 117 of file integer_cast.h.

◆ IC_INT32

#define IC_INT32 (   expr)    IC_CAST_NUMBER(int32_t, INT32_MIN, INT32_MAX, expr)

Cast the expression after asserting that it is in range.

Note
The expression will be evaluated once.
Parameters
exprThe expression to cast.
Returns
The expr casted to type: (int32_t) (expr)

Definition at line 125 of file integer_cast.h.

◆ IC_INT64

#define IC_INT64 (   expr)    IC_CAST_NUMBER(int64_t, INT64_MIN, INT64_MAX, expr)

Cast the expression after asserting that it is in range.

Note
The expression will be evaluated once.
Parameters
exprThe expression to cast.
Returns
The expr casted to type: (int64_t) (expr)

Definition at line 133 of file integer_cast.h.

◆ IC_INT8

#define IC_INT8 (   expr)    IC_CAST_NUMBER(int8_t, INT8_MIN, INT8_MAX, expr)

Cast the expression after asserting that it is in range.

Note
The expression will be evaluated once.
Parameters
exprThe expression to cast.
Returns
The expr casted to type: (int8_t) (expr)

Definition at line 109 of file integer_cast.h.

◆ IC_UINT16

#define IC_UINT16 (   expr)    IC_CAST_NUMBER(uint16_t, 0, UINT16_MAX, expr)

Cast the expression after asserting that it is in range.

Note
The expression will be evaluated once.
Parameters
exprThe expression to cast.
Returns
The expr casted to type: (uint16_t) (expr)

Definition at line 85 of file integer_cast.h.

◆ IC_UINT32

#define IC_UINT32 (   expr)    IC_CAST_NUMBER(uint32_t, 0, UINT32_MAX, expr)

Cast the expression after asserting that it is in range.

Note
The expression will be evaluated once.
Parameters
exprThe expression to cast.
Returns
The expr casted to type: (uint32_t) (expr)

Definition at line 93 of file integer_cast.h.

◆ IC_UINT64

#define IC_UINT64 (   expr)    IC_CAST_NUMBER(uint64_t, 0, UINT64_MAX, expr)

Cast the expression after asserting that it is in range.

Note
The expression will be evaluated once.
Parameters
exprThe expression to cast.
Returns
The expr casted to type: (uint64_t) (expr)

Definition at line 101 of file integer_cast.h.

◆ IC_UINT8

#define IC_UINT8 (   expr)    IC_CAST_NUMBER(uint8_t, 0, UINT8_MAX, expr)

Cast the expression after asserting that it is in range.

Note
The expression will be evaluated once.
Parameters
exprThe expression to cast.
Returns
The expr casted to type: (uint8_t) (expr)

Definition at line 77 of file integer_cast.h.