📄 lib_def.h
字号:
*
* Description : Validate a value as less than or equal to a specified maximum value.
*
* Argument(s) : val Value to validate.
*
* val_max Maximum value to test.
*
* Return(s) : DEF_OK, Value is less than or equal to maximum value.
*
* DEF_FAIL, otherwise.
*
* Caller(s) : Application.
*
* Note(s) : (1) DEF_CHK_VAL_MAX() avoids directly comparing any two values if only one of the values
* is negative since the negative value might be incorrectly promoted to an arbitrary
* unsigned value if the other value to compare is unsigned.
*
* (2) Validation of values is limited to the range supported by the compiler &/or target
* environment. All other values that underflow/overflow the supported range will
* modulo/wrap into the supported range as arbitrary signed or unsigned values.
*
* Therefore, any values that underflow the most negative signed value or overflow
* the most positive unsigned value supported by the compiler &/or target environment
* cannot be validated :
*
* ( N-1 N ]
* ( -(2 ) , 2 - 1 ]
* ( ]
*
* where
* N Number of data word bits supported by the compiler
* &/or target environment
*
* (a) Note that the most negative value, -2^(N-1), is NOT included in the supported
* range since many compilers do NOT always correctly handle this value.
*
* (3) 'val' and 'val_max' are compared to 1 instead of 0 to avoid warning generated for
* unsigned numbers.
*********************************************************************************************************
*/
#define DEF_CHK_VAL_MAX(val, val_max) (((!(((val_max) >= 1) && ((val) < 1))) && \
((((val) >= 1) && ((val_max) < 1)) || \
((val) > (val_max)))) ? DEF_FAIL : DEF_OK)
/*$PAGE*/
/*
*********************************************************************************************************
* DEF_CHK_VAL()
*
* Description : Validate a value as greater than or equal to a specified minimum value & less than or
* equal to a specified maximum value.
*
* Argument(s) : val Value to validate.
*
* val_min Minimum value to test.
*
* val_max Maximum value to test.
*
* Return(s) : DEF_OK, Value is greater than or equal to minimum value AND
* less than or equal to maximum value.
*
* DEF_FAIL, otherwise.
*
* Caller(s) : Application.
*
* Note(s) : (1) DEF_CHK_VAL() avoids directly comparing any two values if only one of the values
* is negative since the negative value might be incorrectly promoted to an arbitrary
* unsigned value if the other value to compare is unsigned.
*
* (2) Validation of values is limited to the range supported by the compiler &/or target
* environment. All other values that underflow/overflow the supported range will
* modulo/wrap into the supported range as arbitrary signed or unsigned values.
*
* Therefore, any values that underflow the most negative signed value or overflow
* the most positive unsigned value supported by the compiler &/or target environment
* cannot be validated :
*
* ( N-1 N ]
* ( -(2 ) , 2 - 1 ]
* ( ]
*
* where
* N Number of data word bits supported by the compiler
* &/or target environment
*
* (a) Note that the most negative value, -2^(N-1), is NOT included in the supported
* range since many compilers do NOT always correctly handle this value.
*
* (3) DEF_CHK_VAL() does NOT validate that the maximum value ('val_max') is greater than
* or equal to the minimum value ('val_min').
*********************************************************************************************************
*/
#define DEF_CHK_VAL(val, val_min, val_max) (((DEF_CHK_VAL_MIN(val, val_min) == DEF_FAIL) || \
(DEF_CHK_VAL_MAX(val, val_max) == DEF_FAIL)) ? DEF_FAIL : DEF_OK)
/*$PAGE*/
/*
*********************************************************************************************************
* DEF_GET_U_MAX_VAL()
*
* Description : Get the maximum unsigned value that can be represented in an unsigned integer variable
* of the same data type size as an object.
*
* Argument(s) : obj Object or data type to return maximum unsigned value (see Note #1).
*
* Return(s) : Maximum unsigned integer value that can be represented by the object, if NO error(s).
*
* 0, otherwise.
*
* Caller(s) : Application.
*
* Note(s) : (1) 'obj' SHOULD be an integer object or data type but COULD also be a character or
* pointer object or data type.
*********************************************************************************************************
*/
#if (CPU_CFG_DATA_SIZE_MAX == CPU_WORD_SIZE_08)
#define DEF_GET_U_MAX_VAL(obj) ((sizeof(obj) == CPU_WORD_SIZE_08) ? DEF_INT_08U_MAX_VAL : 0)
#elif (CPU_CFG_DATA_SIZE_MAX == CPU_WORD_SIZE_16)
#define DEF_GET_U_MAX_VAL(obj) ((sizeof(obj) == CPU_WORD_SIZE_08) ? DEF_INT_08U_MAX_VAL : \
((sizeof(obj) == CPU_WORD_SIZE_16) ? DEF_INT_16U_MAX_VAL : 0))
#elif (CPU_CFG_DATA_SIZE_MAX == CPU_WORD_SIZE_32)
#define DEF_GET_U_MAX_VAL(obj) ((sizeof(obj) == CPU_WORD_SIZE_08) ? DEF_INT_08U_MAX_VAL : \
((sizeof(obj) == CPU_WORD_SIZE_16) ? DEF_INT_16U_MAX_VAL : \
((sizeof(obj) == CPU_WORD_SIZE_32) ? DEF_INT_32U_MAX_VAL : 0)))
#elif (CPU_CFG_DATA_SIZE_MAX == CPU_WORD_SIZE_64)
#define DEF_GET_U_MAX_VAL(obj) ((sizeof(obj) == CPU_WORD_SIZE_08) ? DEF_INT_08U_MAX_VAL : \
((sizeof(obj) == CPU_WORD_SIZE_16) ? DEF_INT_16U_MAX_VAL : \
((sizeof(obj) == CPU_WORD_SIZE_32) ? DEF_INT_32U_MAX_VAL : \
((sizeof(obj) == CPU_WORD_SIZE_64) ? DEF_INT_64U_MAX_VAL : 0))))
#else
#error "CPU_CFG_DATA_SIZE_MAX illegally #defined in 'cpu.h' "
#error " [See 'cpu.h CONFIGURATION ERRORS']"
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* MATH MACRO'S
*
* Note(s) : (1) Ideally, ALL mathematical macro's & functions SHOULD be defined in the custom mathematics
* library ('lib_math.*'). #### However, to maintain backwards compatibility with previously-
* released modules, mathematical macro & function definitions should only be moved to the
* custom mathematics library once all previously-released modules are updated to include the
* custom mathematics library.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* DEF_MIN()
*
* Description : Determine the minimum of two values.
*
* Argument(s) : a First value.
*
* b Second value.
*
* Return(s) : Minimum of the two values.
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
#define DEF_MIN(a, b) (((a) < (b)) ? (a) : (b))
/*
*********************************************************************************************************
* DEF_MAX()
*
* Description : Determine the maximum of two values.
*
* Argument(s) : a First value.
*
* b Second value.
*
* Return(s) : Maximum of the two values.
*
* Note(s) : none.
*********************************************************************************************************
*/
#define DEF_MAX(a, b) (((a) > (b)) ? (a) : (b))
/*$PAGE*/
/*
*********************************************************************************************************
* DEF_ABS()
*
* Description : Determine the absolute value of a value.
*
* Argument(s) : a Value to calculate absolute value.
*
* Return(s) : Absolute value of the value.
*
* Caller(s) : Application.
*
* Note(s) : none.
*********************************************************************************************************
*/
#define DEF_ABS(a) (((a) < 0) ? (-(a)) : (a))
/*$PAGE*/
/*
*********************************************************************************************************
* FUNCTION PROTOTYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* CONFIGURATION ERRORS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LIBRARY CONFIGURATION ERRORS
*********************************************************************************************************
*/
/* See 'lib_def.h Note #1a'. */
#if (CPU_CORE_VERSION < 12900u)
#error "CPU_CORE_VERSION [SHOULD be >= V1.29.00]"
#endif
/*
*********************************************************************************************************
* MODULE END
*
* Note(s) : (1) See 'lib_def.h MODULE'.
*********************************************************************************************************
*/
#endif /* End of lib def module include. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -