sarmfsw: SMFSW Toolbox (for ARM & compatible with Arduino platform) 3.7
SMFSW collection of miscellaneous functions & macros (for ARM & compatible with Arduino platform)
Loading...
Searching...
No Matches
arm_macros.h File Reference

ARM common macros. More...

This graph shows which files directly or indirectly include this file:

Macros

#define Undefined   (-1)
 Undefined value.
 
#define Null   (0)
 Null Value.
 
#define pNull   ((void *) 0)
 Null pointer -> same as NULL in stdlib.h.
 
#define charNull   ('\0')
 Null Char.
 
#define True   true
 True alias for true
 
#define False   false
 False alias for false
 
#define TRUE   true
 TRUE alias for true
 
#define FALSE   false
 FALSE alias for false
 
#define GET_BITS(v, b)   ((v) & (b))
 Get bits from mask b of variable v.
 
#define SET_BITS(v, b)   ((v) |= (b))
 Set bits b of variable v.
 
#define CLR_BITS(v, b)   ((v) &= ~(b))
 Clear bits b of variable v.
 
#define INV_BITS(v, b)   ((v) ^= (b))
 Invert bits b of variable v.
 
#define SET_BITS_VAL(v, m, s)   ((v) = (((v) & ~(m)) | ((s) & (m))))
 Set variable v to set value s with mask m.
 
#define TEST_BITS_VAL(v, b)   (((v) & (b)) == (b))
 Test variable v with bits b.
 
#define TEST_MASK_VAL(v, m, r)   (((v) & (m)) == (r))
 Test variable v with bits mask m is equal to r.
 
#define LSHIFT(v, n)   ((v) << (n))
 Shift v n bits left.
 
#define RSHIFT(v, n)   ((v) >> (n))
 Shift v n bits right.
 
#define LSHIFT8(v, n)   ((BYTE) ((BYTE) (v) << (n)))
 Shift v n bits left (up to 7b)
 
#define RSHIFT8(v, n)   ((BYTE) ((BYTE) (v) >> (n)))
 Shift v n bits right (up to 7b)
 
#define LSHIFT16(v, n)   ((WORD) ((WORD) (v) << (n)))
 Shift v n bits left (up to 15b)
 
#define RSHIFT16(v, n)   ((WORD) ((WORD) (v) >> (n)))
 Shift v n bits right (up to 15b)
 
#define LSHIFT32(v, n)   ((DWORD) ((DWORD) (v) << (n)))
 Shift v n bits left (up to 31b)
 
#define RSHIFT32(v, n)   ((DWORD) ((DWORD) (v) >> (n)))
 Shift v n bits right (up to 31b)
 
#define LSHIFT64(v, n)   ((LWORD) ((LWORD) (v) << (n)))
 Shift v n bits left (up to 63b)
 
#define RSHIFT64(v, n)   ((LWORD) ((LWORD) (v) >> (n)))
 Shift v n bits right (up to 63b)
 
#define MAKEWORD(lsb, msb)   ((WORD) (((BYTE) (lsb)) | LSHIFT(((WORD) (msb)), 8)))
 Make WORD from lsb and msb.
 
#define MAKELONG(lsw, msw)   ((DWORD) (((WORD) (lsw)) | LSHIFT(((DWORD) (msw)), 16)))
 Make LONG from lsw and msw.
 
#define LOBYTE(w)   ((BYTE) (w))
 Get BYTE LSB from WORD w.
 
#define HIBYTE(w)   ((BYTE) RSHIFT((WORD) (w), 8))
 Get BYTE MSB from WORD w.
 
#define LOWORD(l)   ((WORD) (l))
 Get WORD LSW from LONG l.
 
#define HIWORD(l)   ((WORD) RSHIFT((DWORD) (l), 16))
 Get WORD MSW from LONG l.
 
#define SWAP_TYPE(a, b, typ)   ({ typ c = a; a = b; b = c; })
 Swap two variable type typ a & b.
 
#define SWAP_BYTE(a, b)   SWAP_TYPE(a, b, BYTE)
 Swap two BYTE a & b.
 
#define SWAP_WORD(a, b)   SWAP_TYPE(a, b, WORD)
 Swap two WORD a & b.
 
#define SWAP_DWORD(a, b)   SWAP_TYPE(a, b, DWORD)
 Swap two DWORD a & b.
 
#define SWAP_LWORD(a, b)   SWAP_TYPE(a, b, LWORD)
 Swap two LWORD a & b.
 
#define SWAP_FLOAT(a, b)   SWAP_TYPE(a, b, float)
 Swap two float a & b.
 
#define SWAP_DOUBLE(a, b)   SWAP_TYPE(a, b, double)
 Swap two double a & b.
 
#define VAL_AT(addr, typ)   (*(typ *) (addr))
 Get the type typ content of address addr.
 
#define SZ_ARRAY(arr)   ((size_t) (sizeof(arr) / sizeof(arr[0])))
 Computes the number of array elements in arr.
 
#define SZ_OBJ(obj, typ)   ((size_t) (sizeof(obj) / sizeof(typ)))
 Computes the number of elements in obj of type typ.
 
#define SZ_TYP_MBR(typ, mbr)   ((size_t) sizeof(((typ *)0)->mbr))
 Computes the size of member mbr in struct type typ.
 
#define TYP_MBR(typ, mbr)   (((typ *)0)->mbr)
 Get member mbr from a struct type typ.
 
#define TYP_MBR_TYP(typ, mbr)   typeof(TYP_MBR(typ, mbr))
 Get typeof member mbr from a struct type typ.
 
#define OFFSET_OF(typ, mbr)   ((size_t) &(((typ *)0)->mbr))
 Computes the offset member mbr from struct type typ.
 
#define ROOT_OF(ptr, typ, mbr)   ((typ *) (((uint8_t *) (ptr)) - OFFSET_OF(typ, mbr)))
 Computes the address of parent struct typ of ptr from member mbr.
 
#define CAT(a, b)   a##b
 Preprocessor Name catenation of a and b (use of XCAT is highly recommended for any need)
 
#define XCAT(a, b)   CAT(a, b)
 Preprocessor Name catenation of a and b (nesting possibility)
 
#define STR(s)   #s
 Stringify s expression.
 
#define XSTR(s)   STR(s)
 Stringify the result of s expression expansion (stringification of a macro expansion)
 
#define binEval(exp)   ((exp) ? true : false)
 boolean evaluation of expression exp
 
#define nbinEval(exp)   (!binEval(exp))
 complemented boolean evaluation of expression exp
 
#define min   MIN
 min alias for MIN
 
#define MIN(a, b)
 Returns min value between a and b.
 
#define max   MAX
 max alias for MAX
 
#define MAX(a, b)
 Returns max value between a and b.
 
#define min3   MIN3
 min3 alias for MIN3
 
#define MIN3(a, b, c)
 Returns min value between a, b and c.
 
#define max3   MAX3
 max3 alias for MAX3
 
#define MAX3(a, b, c)
 Returns max value between a, b and c.
 
#define clamp   CLAMP
 clamp alias for CLAMP
 
#define CLAMP(v, mn, mx)
 Returns the value between mn and mx from val.
 
#define OVF_DIFF(a, b)
 Returns difference of unsigned a and b (with potential overflow handling)
 
#define SCALE_VAL(v, from_min, from_max, to_min, to_max)
 Scale value v from range from_min:from_max to range to_min:to_max.
 
#define SCALE_VAL_T(typ, v, from_min, from_max, to_min, to_max)
 Scale typed typ value v from range from_min:from_max to range to_min:to_max.
 
#define BYTE_TO_PERC(b)   ((BYTE) ((((b) > 255U ? 255U : (b)) * 100U) / 255U))
 
#define PERC_TO_BYTE(p)   ((BYTE) ((((p) > 100U ? 100U : (p)) * 255U) / 100U))
 Converts a p percentage (0-100) to BYTE (0-255) with max checking.
 
#define M_1_3   (1.0 / 3.0)
 1/3 constant approximation
 
#define M_2_3   (2.0 / 3.0)
 2/3 constant approximation
 
#define malloc_assert(x)   ((x) = malloc(sizeof(*(x))), assert((x)))
 Asserted malloc.
 

Detailed Description

ARM common macros.

Author
SMFSW
Note
If used CPU only handles single shifts, you may define SINGLE_SHIFT_ONLY_OPCODE at project level (see LSHIFT & RSHIFT)
MISRA C:2012 Deviations
Header scope deviation has been granted for following rules:
Rule-1.2 - Advisory: Language extensions (misra-c2012-1.2)
Rule-20.5 - Advisory: #undef (misra-c2012-20.5)
Rule-20.10 - Advisory: # and ## preprocessor operators (misra-c2012-20.10)

Macro Definition Documentation

◆ binEval

#define binEval ( exp)    ((exp) ? true : false)

boolean evaluation of expression exp

◆ BYTE_TO_PERC

#define BYTE_TO_PERC ( b)    ((BYTE) ((((b) > 255U ? 255U : (b)) * 100U) / 255U))
Warning
This macro doesn't prevent from double evaluation; otherwise it couldn't be used as pre-processing initializer macro Converts a BYTE b (0-255) to percent (0-100)

◆ CAT

#define CAT ( a,
b )   a##b

Preprocessor Name catenation of a and b (use of XCAT is highly recommended for any need)

Warning
No nesting possible, use XCAT instead (unless there is a good reason not to use XCAT)

◆ charNull

#define charNull   ('\0')

Null Char.

◆ CLAMP

#define CLAMP ( v,
mn,
mx )
Value:
({ __TYPEOF(v) _v = (v); \
__TYPEOF(mn) _mn = (mn); \
__TYPEOF(mx) _mx = (mx); \
(_v < _mn) ? _mn : ((_v > _mx) ? _mx : _v); })
#define __TYPEOF
typeof keyword alias (
Definition arm_attributes.h:255

Returns the value between mn and mx from val.

◆ clamp

#define clamp   CLAMP

clamp alias for CLAMP

◆ CLR_BITS

#define CLR_BITS ( v,
b )   ((v) &= ~(b))

Clear bits b of variable v.

◆ FALSE

#define FALSE   false

FALSE alias for false

◆ False

#define False   false

False alias for false

◆ GET_BITS

#define GET_BITS ( v,
b )   ((v) & (b))

Get bits from mask b of variable v.

◆ HIBYTE

#define HIBYTE ( w)    ((BYTE) RSHIFT((WORD) (w), 8))

Get BYTE MSB from WORD w.

◆ HIWORD

#define HIWORD ( l)    ((WORD) RSHIFT((DWORD) (l), 16))

Get WORD MSW from LONG l.

◆ INV_BITS

#define INV_BITS ( v,
b )   ((v) ^= (b))

Invert bits b of variable v.

◆ LOBYTE

#define LOBYTE ( w)    ((BYTE) (w))

Get BYTE LSB from WORD w.

◆ LOWORD

#define LOWORD ( l)    ((WORD) (l))

Get WORD LSW from LONG l.

◆ LSHIFT

#define LSHIFT ( v,
n )   ((v) << (n))

Shift v n bits left.

◆ LSHIFT16

#define LSHIFT16 ( v,
n )   ((WORD) ((WORD) (v) << (n)))

Shift v n bits left (up to 15b)

◆ LSHIFT32

#define LSHIFT32 ( v,
n )   ((DWORD) ((DWORD) (v) << (n)))

Shift v n bits left (up to 31b)

◆ LSHIFT64

#define LSHIFT64 ( v,
n )   ((LWORD) ((LWORD) (v) << (n)))

Shift v n bits left (up to 63b)

◆ LSHIFT8

#define LSHIFT8 ( v,
n )   ((BYTE) ((BYTE) (v) << (n)))

Shift v n bits left (up to 7b)

◆ M_1_3

#define M_1_3   (1.0 / 3.0)

1/3 constant approximation

◆ M_2_3

#define M_2_3   (2.0 / 3.0)

2/3 constant approximation

◆ MAKELONG

#define MAKELONG ( lsw,
msw )   ((DWORD) (((WORD) (lsw)) | LSHIFT(((DWORD) (msw)), 16)))

Make LONG from lsw and msw.

◆ MAKEWORD

#define MAKEWORD ( lsb,
msb )   ((WORD) (((BYTE) (lsb)) | LSHIFT(((WORD) (msb)), 8)))

Make WORD from lsb and msb.

◆ malloc_assert

#define malloc_assert ( x)    ((x) = malloc(sizeof(*(x))), assert((x)))

Asserted malloc.

◆ MAX

#define MAX ( a,
b )
Value:
({ __TYPEOF(a) _a = (a); \
__TYPEOF(b) _b = (b); \
(_a >= _b) ? _a : _b; })

Returns max value between a and b.

◆ max

#define max   MAX

max alias for MAX

◆ MAX3

#define MAX3 ( a,
b,
c )
Value:
({ __TYPEOF(a) _a = (a); \
__TYPEOF(b) _b = (b); \
__TYPEOF(c) _c = (c); \
(_b >= _c) ? ((_a >= _b) ? _a : _b) : ((_a >= _c) ? _a : _c); })

Returns max value between a, b and c.

◆ max3

#define max3   MAX3

max3 alias for MAX3

◆ MIN

#define MIN ( a,
b )
Value:
({ __TYPEOF(a) _a = (a); \
__TYPEOF(b) _b = (b); \
(_a <= _b) ? _a : _b; })

Returns min value between a and b.

◆ min

#define min   MIN

min alias for MIN

◆ MIN3

#define MIN3 ( a,
b,
c )
Value:
({ __TYPEOF(a) _a = (a); \
__TYPEOF(b) _b = (b); \
__TYPEOF(c) _c = (c); \
(_b <= _c) ? ((_a <= _b) ? _a : _b) : ((_a <= _c) ? _a : _c); })

Returns min value between a, b and c.

◆ min3

#define min3   MIN3

min3 alias for MIN3

◆ nbinEval

#define nbinEval ( exp)    (!binEval(exp))

complemented boolean evaluation of expression exp

◆ Null

#define Null   (0)

Null Value.

◆ OFFSET_OF

#define OFFSET_OF ( typ,
mbr )   ((size_t) &(((typ *)0)->mbr))

Computes the offset member mbr from struct type typ.

MISRA C:2012 Deviations
Local legitimate use derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: typ parameter would be cast instead of type.
Justification: mbr parameter shall be a structure member; will in any way raise an error in case not.

◆ OVF_DIFF

#define OVF_DIFF ( a,
b )
Value:
({ __TYPEOF(a) _a = (a); \
__TYPEOF(b) _b = (b); \
__TYPEOF(a) _max = -1; \
(_a >= _b) ? (_a - _b) : (_max - _b) + _a + 1; })

Returns difference of unsigned a and b (with potential overflow handling)

Warning
OVF_DIFF only works with unsigned integers

◆ PERC_TO_BYTE

#define PERC_TO_BYTE ( p)    ((BYTE) ((((p) > 100U ? 100U : (p)) * 255U) / 100U))

Converts a p percentage (0-100) to BYTE (0-255) with max checking.

Warning
This macro doesn't prevent from double evaluation; otherwise it couldn't be used as pre-processing initializer macro

◆ pNull

#define pNull   ((void *) 0)

Null pointer -> same as NULL in stdlib.h.

◆ ROOT_OF

#define ROOT_OF ( ptr,
typ,
mbr )   ((typ *) (((uint8_t *) (ptr)) - OFFSET_OF(typ, mbr)))

Computes the address of parent struct typ of ptr from member mbr.

MISRA C:2012 Deviations
Local legitimate use derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: mbr parameter shall be a structure member; will in any way raise an error in case not.

◆ RSHIFT

#define RSHIFT ( v,
n )   ((v) >> (n))

Shift v n bits right.

◆ RSHIFT16

#define RSHIFT16 ( v,
n )   ((WORD) ((WORD) (v) >> (n)))

Shift v n bits right (up to 15b)

◆ RSHIFT32

#define RSHIFT32 ( v,
n )   ((DWORD) ((DWORD) (v) >> (n)))

Shift v n bits right (up to 31b)

◆ RSHIFT64

#define RSHIFT64 ( v,
n )   ((LWORD) ((LWORD) (v) >> (n)))

Shift v n bits right (up to 63b)

◆ RSHIFT8

#define RSHIFT8 ( v,
n )   ((BYTE) ((BYTE) (v) >> (n)))

Shift v n bits right (up to 7b)

◆ SCALE_VAL

#define SCALE_VAL ( v,
from_min,
from_max,
to_min,
to_max )
Value:
(((((v) - (from_min)) * ((to_max) - (to_min))) / \
((from_max) - (from_min))) + (to_min))

Scale value v from range from_min:from_max to range to_min:to_max.

Warning
SCALE_VAL does not check types and is limited to MCU register size computation, for larger scales, use SCALE_VAL_T or scaleValue instead

◆ SCALE_VAL_T

#define SCALE_VAL_T ( typ,
v,
from_min,
from_max,
to_min,
to_max )
Value:
(((((typ) (v) - (typ) (from_min)) * ((typ) (to_max) - (typ) (to_min))) / \
((typ) (from_max) - (typ) (from_min))) + (typ) (to_min))

Scale typed typ value v from range from_min:from_max to range to_min:to_max.

◆ SET_BITS

#define SET_BITS ( v,
b )   ((v) |= (b))

Set bits b of variable v.

◆ SET_BITS_VAL

#define SET_BITS_VAL ( v,
m,
s )   ((v) = (((v) & ~(m)) | ((s) & (m))))

Set variable v to set value s with mask m.

◆ STR

#define STR ( s)    #s

Stringify s expression.

◆ SWAP_BYTE

#define SWAP_BYTE ( a,
b )   SWAP_TYPE(a, b, BYTE)

Swap two BYTE a & b.

◆ SWAP_DOUBLE

#define SWAP_DOUBLE ( a,
b )   SWAP_TYPE(a, b, double)

Swap two double a & b.

◆ SWAP_DWORD

#define SWAP_DWORD ( a,
b )   SWAP_TYPE(a, b, DWORD)

Swap two DWORD a & b.

◆ SWAP_FLOAT

#define SWAP_FLOAT ( a,
b )   SWAP_TYPE(a, b, float)

Swap two float a & b.

◆ SWAP_LWORD

#define SWAP_LWORD ( a,
b )   SWAP_TYPE(a, b, LWORD)

Swap two LWORD a & b.

◆ SWAP_TYPE

#define SWAP_TYPE ( a,
b,
typ )   ({ typ c = a; a = b; b = c; })

Swap two variable type typ a & b.

MISRA C:2012 Deviations
Local legitimate use derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: typ parameter would be cast instead of type declaration (thus raising error); other parameters shall be variable names, raising error otherwise is intended.

◆ SWAP_WORD

#define SWAP_WORD ( a,
b )   SWAP_TYPE(a, b, WORD)

Swap two WORD a & b.

◆ SZ_ARRAY

#define SZ_ARRAY ( arr)    ((size_t) (sizeof(arr) / sizeof(arr[0])))

Computes the number of array elements in arr.

MISRA C:2012 Deviations
local derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: Parenthesis doesn't make sense as parameter arr shall be an instance of type; raising error otherwise is intended.

◆ SZ_OBJ

#define SZ_OBJ ( obj,
typ )   ((size_t) (sizeof(obj) / sizeof(typ)))

Computes the number of elements in obj of type typ.

MISRA C:2012 Deviations
Local legitimate use derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: obj parameter can be a type instead of an istance of type.
Justification: typ parameter would be cast instead of type.

◆ SZ_TYP_MBR

#define SZ_TYP_MBR ( typ,
mbr )   ((size_t) sizeof(((typ *)0)->mbr))

Computes the size of member mbr in struct type typ.

MISRA C:2012 Deviations
Local legitimate use derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: typ parameter would be cast instead of type.
Justification: mbr parameter shall be a structure member; will in any way raise an error in case not.

◆ TEST_BITS_VAL

#define TEST_BITS_VAL ( v,
b )   (((v) & (b)) == (b))

Test variable v with bits b.

◆ TEST_MASK_VAL

#define TEST_MASK_VAL ( v,
m,
r )   (((v) & (m)) == (r))

Test variable v with bits mask m is equal to r.

◆ TRUE

#define TRUE   true

TRUE alias for true

◆ True

#define True   true

True alias for true

◆ TYP_MBR

#define TYP_MBR ( typ,
mbr )   (((typ *)0)->mbr)

Get member mbr from a struct type typ.

MISRA C:2012 Deviations
Local legitimate use derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: typ parameter would be cast instead of type.
Justification: mbr parameter shall be a structure member; will in any way raise an error in case not.

◆ TYP_MBR_TYP

#define TYP_MBR_TYP ( typ,
mbr )   typeof(TYP_MBR(typ, mbr))

Get typeof member mbr from a struct type typ.

Warning
use with caution, C language typeof extension works well with native types, yet can be the cause of big (experienced) issues with more complex ones
Note
typeof C language extension should only be used to get a compiler native known type

◆ Undefined

#define Undefined   (-1)

Undefined value.

◆ VAL_AT

#define VAL_AT ( addr,
typ )   (*(typ *) (addr))

Get the type typ content of address addr.

MISRA C:2012 Deviations
Local legitimate use derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: typ parameter would be cast instead of type.

◆ XCAT

#define XCAT ( a,
b )   CAT(a, b)

Preprocessor Name catenation of a and b (nesting possibility)

◆ XSTR

#define XSTR ( s)    STR(s)

Stringify the result of s expression expansion (stringification of a macro expansion)