Validates a value as less than or equal to a specified maximum value.
Files
lib_def.h
Prototype
DEF_CHK_VAL_MAX(val, val_max);
Arguments
val
Value to validate.
val_max
Maximum value to test.
Returned Value
DEF_OK
Value is less than or equal to maximum value;
DEF_FAIL
otherwise.
Required Configuration
None.
Notes / Warnings
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.
Validation of values is limited to the range supported by the compiler and/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 and/or target environment cannot be validated:
( N-1 N ]
( -(2 ) , 2 - 1 ]
( ]
where N
is the number of data word bits supported by the compiler and/or target environment. 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.
Example Usage
#define CFG_VAL -1 #if (DEF_CHK_VAL_MAX(CFG_VAL, 1000u) != DEF_OK) /* Signed CFG_VAL NOT promoted to unsigned. */ #error "CFG_VAL must be <= 100" #endif