libc: minimal: add INTn_C macros to stdint.h
Macros like INT64_C(x) convert x to a constant integral expression, i.e. one that can be used in preprocessor code. Implement wrappers that use the GNUC intrinsics to perform the translation. Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
This commit is contained in:
parent
5fd15c3038
commit
ee7c9f7fe8
2 changed files with 32 additions and 0 deletions
|
@ -74,6 +74,24 @@ typedef __UINT_LEAST64_TYPE__ uint_least64_t;
|
|||
typedef __INTPTR_TYPE__ intptr_t;
|
||||
typedef __UINTPTR_TYPE__ uintptr_t;
|
||||
|
||||
#ifdef __GNUC__
|
||||
/* These macros must produce constant integer expressions, which can't
|
||||
* be done in the preprocessor (casts aren't allowed). Defer to the
|
||||
* GCC internal functions where they're available.
|
||||
*/
|
||||
#define INT8_C(_v) __INT8_C(_v)
|
||||
#define INT16_C(_v) __INT16_C(_v)
|
||||
#define INT32_C(_v) __INT32_C(_v)
|
||||
#define INT64_C(_v) __INT64_C(_v)
|
||||
#define INTMAX_C(_v) __INTMAX_C(_v)
|
||||
|
||||
#define UINT8_C(_v) __UINT8_C(_v)
|
||||
#define UINT16_C(_v) __UINT16_C(_v)
|
||||
#define UINT32_C(_v) __UINT32_C(_v)
|
||||
#define UINT64_C(_v) __UINT64_C(_v)
|
||||
#define UINTMAX_C(_v) __UINTMAX_C(_v)
|
||||
#endif /* __GNUC__ */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -123,6 +123,20 @@ void test_stdint(void)
|
|||
{
|
||||
zassert_true((unsigned_int + unsigned_byte + 1u == 0U), NULL);
|
||||
|
||||
#if (UINT8_C(1) == 1) \
|
||||
&& (INT8_C(-1) == -1) \
|
||||
&& (UINT16_C(2) == 2) \
|
||||
&& (INT16_C(-2) == -2) \
|
||||
&& (UINT32_C(4) == 4) \
|
||||
&& (INT32_C(-4) == -4) \
|
||||
&& (UINT64_C(8) == 8) \
|
||||
&& (INT64_C(-8) == -8) \
|
||||
&& (UINTMAX_C(11) == 11) \
|
||||
&& (INTMAX_C(-11) == -11)
|
||||
zassert_true(true, NULL);
|
||||
#else
|
||||
zassert_true(false, "const int expr values ...");
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue