3 * (C) COPYRIGHT ARM Limited. All rights reserved.
5 * This program is free software and is provided to you under the terms of the
6 * GNU General Public License version 2 as published by the Free Software
7 * Foundation, and any use by you of this program is subject to the terms
10 * A copy of the licence is included with the program, and can also be obtained
11 * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12 * Boston, MA 02110-1301, USA.
25 /* ============================================================================
27 ============================================================================ */
29 * @defgroup arm_cstd_coding_standard ARM C standard types and constants
30 * The common files are a set of standard headers which are used by all parts
31 * of this development, describing types, and generic constants.
35 * - arm_cstd_compilers.h
37 * - arm_cstd_types_rvct.h
38 * - arm_cstd_types_gcc.h
39 * - arm_cstd_types_msvc.h
40 * - arm_cstd_pack_push.h
41 * - arm_cstd_pack_pop.h
45 * @addtogroup arm_cstd_coding_standard
52 /* ============================================================================
53 Import standard C99 types
54 ============================================================================ */
55 #include "arm_cstd_compilers.h"
56 #include "arm_cstd_types.h"
58 /* ============================================================================
60 ============================================================================ */
61 #if !defined(INT8_MAX)
62 #define INT8_MAX ((int8_t) 0x7F)
64 #if !defined(INT8_MIN)
65 #define INT8_MIN (-INT8_MAX - 1)
68 #if !defined(INT16_MAX)
69 #define INT16_MAX ((int16_t)0x7FFF)
71 #if !defined(INT16_MIN)
72 #define INT16_MIN (-INT16_MAX - 1)
75 #if !defined(INT32_MAX)
76 #define INT32_MAX ((int32_t)0x7FFFFFFF)
78 #if !defined(INT32_MIN)
79 #define INT32_MIN (-INT32_MAX - 1)
82 #if !defined(INT64_MAX)
83 #define INT64_MAX ((int64_t)0x7FFFFFFFFFFFFFFFLL)
85 #if !defined(INT64_MIN)
86 #define INT64_MIN (-INT64_MAX - 1)
89 #if !defined(UINT8_MAX)
90 #define UINT8_MAX ((uint8_t) 0xFF)
93 #if !defined(UINT16_MAX)
94 #define UINT16_MAX ((uint16_t)0xFFFF)
97 #if !defined(UINT32_MAX)
98 #define UINT32_MAX ((uint32_t)0xFFFFFFFF)
101 #if !defined(UINT64_MAX)
102 #define UINT64_MAX ((uint64_t)0xFFFFFFFFFFFFFFFFULL)
105 /* fallbacks if limits.h wasn't available */
106 #if !defined(UCHAR_MAX)
107 #define UCHAR_MAX ((unsigned char)~0U)
110 #if !defined(SCHAR_MAX)
111 #define SCHAR_MAX ((signed char)(UCHAR_MAX >> 1))
113 #if !defined(SCHAR_MIN)
114 #define SCHAR_MIN ((signed char)(-SCHAR_MAX - 1))
117 #if !defined(USHRT_MAX)
118 #define USHRT_MAX ((unsigned short)~0U)
121 #if !defined(SHRT_MAX)
122 #define SHRT_MAX ((signed short)(USHRT_MAX >> 1))
124 #if !defined(SHRT_MIN)
125 #define SHRT_MIN ((signed short)(-SHRT_MAX - 1))
128 #if !defined(UINT_MAX)
129 #define UINT_MAX ((unsigned int)~0U)
132 #if !defined(INT_MAX)
133 #define INT_MAX ((signed int)(UINT_MAX >> 1))
135 #if !defined(INT_MIN)
136 #define INT_MIN ((signed int)(-INT_MAX - 1))
139 #if !defined(ULONG_MAX)
140 #define ULONG_MAX ((unsigned long)~0UL)
143 #if !defined(LONG_MAX)
144 #define LONG_MAX ((signed long)(ULONG_MAX >> 1))
146 #if !defined(LONG_MIN)
147 #define LONG_MIN ((signed long)(-LONG_MAX - 1))
150 #if !defined(ULLONG_MAX)
151 #define ULLONG_MAX ((unsigned long long)~0ULL)
154 #if !defined(LLONG_MAX)
155 #define LLONG_MAX ((signed long long)(ULLONG_MAX >> 1))
157 #if !defined(LLONG_MIN)
158 #define LLONG_MIN ((signed long long)(-LLONG_MAX - 1))
161 #if !defined(SIZE_MAX)
162 #if 1 == CSTD_CPU_32BIT
163 #define SIZE_MAX UINT32_MAX
164 #elif 1 == CSTD_CPU_64BIT
165 #define SIZE_MAX UINT64_MAX
169 /* ============================================================================
171 ============================================================================ */
172 /* Portable keywords. */
177 * Variable is a C @c const, which can be made non-const for testing purposes.
185 * Variable is a C @c static, which can be made non-static for testing
188 #define STATIC static
192 * Specifies a function as being exported outside of a logical module.
198 * Specifies a a function which is internal to an logical module, but which
199 * should not be used outside of that module. This cannot be enforced by the
200 * compiler, as a module is typically more than one translation unit.
205 * Specifies a function as being internal to a translation unit. Private
206 * functions would typically be declared as STATIC, unless they are being
207 * exported for unit test purposes.
209 #define PRIVATE STATIC
212 * Specify an assertion value which is evaluated at compile time. Recommended
213 * usage is specification of a @c static @c INLINE function containing all of
214 * the assertions thus:
217 * static INLINE [module]_compile_time_assertions( void )
219 * COMPILE_TIME_ASSERT( sizeof(uintptr_t) == sizeof(intptr_t) );
223 * @note Use @c static not @c STATIC. We never want to turn off this @c static
224 * specification for testing purposes.
226 #define CSTD_COMPILE_TIME_ASSERT( expr ) \
227 do { switch(0){case 0: case (expr):;} } while( FALSE )
231 * @deprecated Prefered form is @c CSTD_UNUSED
232 * Function-like macro for suppressing unused variable warnings. Where possible
233 * such variables should be removed; this macro is present for cases where we
234 * much support API backwards compatibility.
236 #define UNUSED( x ) ((void)(x))
240 * Function-like macro for suppressing unused variable warnings. Where possible
241 * such variables should be removed; this macro is present for cases where we
242 * much support API backwards compatibility.
244 #define CSTD_UNUSED( x ) ((void)(x))
248 * Function-like macro for use where "no behavior" is desired. This is useful
249 * when compile time macros turn a function-like macro in to a no-op, but
250 * where having no statement is otherwise invalid.
252 #define CSTD_NOP( ... ) ((void)#__VA_ARGS__)
256 * Function-like macro for converting a pointer in to a u64 for storing into
257 * an external data structure. This is commonly used when pairing a 32-bit
258 * CPU with a 64-bit peripheral, such as a Midgard GPU. C's type promotion
259 * is complex and a straight cast does not work reliably as pointers are
260 * often considered as signed.
262 #define CSTD_PTR_TO_U64( x ) ((uint64_t)((uintptr_t)(x)))
266 * Function-like macro for stringizing a single level macro.
268 * #define MY_MACRO 32
269 * CSTD_STR1( MY_MACRO )
273 #define CSTD_STR1( x ) #x
277 * Function-like macro for stringizing a macro's value. This should not be used
278 * if the macro is defined in a way which may have no value; use the
279 * alternative @c CSTD_STR2N macro should be used instead.
281 * #define MY_MACRO 32
282 * CSTD_STR2( MY_MACRO )
286 #define CSTD_STR2( x ) CSTD_STR1( x )
290 * Utility function for stripping the first character off a string.
292 static INLINE char* arm_cstd_strstrip( char * string )
299 * Function-like macro for stringizing a single level macro where the macro
300 * itself may not have a value. Parameter @c a should be set to any single
301 * character which is then stripped by the macro via an inline function. This
302 * should only be used via the @c CSTD_STR2N macro; for printing a single
303 * macro only the @c CSTD_STR1 macro is a better alternative.
305 * This macro requires run-time code to handle the case where the macro has
306 * no value (you can't concat empty strings in the preprocessor).
308 #define CSTD_STR1N( a, x ) arm_cstd_strstrip( CSTD_STR1( a##x ) )
312 * Function-like macro for stringizing a two level macro where the macro itself
313 * may not have a value.
315 * #define MY_MACRO 32
316 * CSTD_STR2N( MY_MACRO )
319 * #define MY_MACRO 32
320 * CSTD_STR2N( MY_MACRO )
324 #define CSTD_STR2N( x ) CSTD_STR1N( _, x )
326 /* ============================================================================
327 Validate portability constructs
328 ============================================================================ */
329 static INLINE void arm_cstd_compile_time_assertions( void )
331 CSTD_COMPILE_TIME_ASSERT( sizeof(uint8_t) == 1 );
332 CSTD_COMPILE_TIME_ASSERT( sizeof(int8_t) == 1 );
333 CSTD_COMPILE_TIME_ASSERT( sizeof(uint16_t) == 2 );
334 CSTD_COMPILE_TIME_ASSERT( sizeof(int16_t) == 2 );
335 CSTD_COMPILE_TIME_ASSERT( sizeof(uint32_t) == 4 );
336 CSTD_COMPILE_TIME_ASSERT( sizeof(int32_t) == 4 );
337 CSTD_COMPILE_TIME_ASSERT( sizeof(uint64_t) == 8 );
338 CSTD_COMPILE_TIME_ASSERT( sizeof(int64_t) == 8 );
339 CSTD_COMPILE_TIME_ASSERT( sizeof(intptr_t) == sizeof(uintptr_t) );
341 CSTD_COMPILE_TIME_ASSERT( 1 == TRUE );
342 CSTD_COMPILE_TIME_ASSERT( 0 == FALSE );
344 #if 1 == CSTD_CPU_32BIT
345 CSTD_COMPILE_TIME_ASSERT( sizeof(uintptr_t) == 4 );
346 #elif 1 == CSTD_CPU_64BIT
347 CSTD_COMPILE_TIME_ASSERT( sizeof(uintptr_t) == 8 );
352 /* ============================================================================
353 Useful function-like macro
354 ============================================================================ */
356 * @brief Return the lesser of two values.
357 * As a macro it may evaluate its arguments more than once.
360 #define CSTD_MIN( x, y ) ((x) < (y) ? (x) : (y))
363 * @brief Return the greater of two values.
364 * As a macro it may evaluate its arguments more than once.
365 * If called on the same two arguments as CSTD_MIN it is guaranteed to return
366 * the one that CSTD_MIN didn't return. This is significant for types where not
367 * all values are comparable e.g. NaNs in floating-point types. But if you want
368 * to retrieve the min and max of two values, consider using a conditional swap
371 #define CSTD_MAX( x, y ) ((x) < (y) ? (y) : (x))
374 * @brief Clamp value @c x to within @c min and @c max inclusive.
376 #define CSTD_CLAMP( x, min, max ) ((x)<(min) ? (min):((x)>(max) ? (max):(x)))
379 * Flag a cast as a reinterpretation, usually of a pointer type.
381 #define CSTD_REINTERPRET_CAST(type) (type)
384 * Flag a cast as casting away const, usually of a pointer type.
386 #define CSTD_CONST_CAST(type) (type)
389 * Flag a cast as a (potentially complex) value conversion, usually of a
392 #define CSTD_STATIC_CAST(type) (type)
394 /* ============================================================================
396 ============================================================================ */
398 * @cond arm_cstd_utilities
401 /* Common bit constant values, useful in embedded programming. */
402 #define F_BIT_0 ((uint32_t)0x00000001)
403 #define F_BIT_1 ((uint32_t)0x00000002)
404 #define F_BIT_2 ((uint32_t)0x00000004)
405 #define F_BIT_3 ((uint32_t)0x00000008)
406 #define F_BIT_4 ((uint32_t)0x00000010)
407 #define F_BIT_5 ((uint32_t)0x00000020)
408 #define F_BIT_6 ((uint32_t)0x00000040)
409 #define F_BIT_7 ((uint32_t)0x00000080)
410 #define F_BIT_8 ((uint32_t)0x00000100)
411 #define F_BIT_9 ((uint32_t)0x00000200)
412 #define F_BIT_10 ((uint32_t)0x00000400)
413 #define F_BIT_11 ((uint32_t)0x00000800)
414 #define F_BIT_12 ((uint32_t)0x00001000)
415 #define F_BIT_13 ((uint32_t)0x00002000)
416 #define F_BIT_14 ((uint32_t)0x00004000)
417 #define F_BIT_15 ((uint32_t)0x00008000)
418 #define F_BIT_16 ((uint32_t)0x00010000)
419 #define F_BIT_17 ((uint32_t)0x00020000)
420 #define F_BIT_18 ((uint32_t)0x00040000)
421 #define F_BIT_19 ((uint32_t)0x00080000)
422 #define F_BIT_20 ((uint32_t)0x00100000)
423 #define F_BIT_21 ((uint32_t)0x00200000)
424 #define F_BIT_22 ((uint32_t)0x00400000)
425 #define F_BIT_23 ((uint32_t)0x00800000)
426 #define F_BIT_24 ((uint32_t)0x01000000)
427 #define F_BIT_25 ((uint32_t)0x02000000)
428 #define F_BIT_26 ((uint32_t)0x04000000)
429 #define F_BIT_27 ((uint32_t)0x08000000)
430 #define F_BIT_28 ((uint32_t)0x10000000)
431 #define F_BIT_29 ((uint32_t)0x20000000)
432 #define F_BIT_30 ((uint32_t)0x40000000)
433 #define F_BIT_31 ((uint32_t)0x80000000)
435 /* Common 2^n size values, useful in embedded programming. */
436 #define C_SIZE_1B ((uint32_t)0x00000001)
437 #define C_SIZE_2B ((uint32_t)0x00000002)
438 #define C_SIZE_4B ((uint32_t)0x00000004)
439 #define C_SIZE_8B ((uint32_t)0x00000008)
440 #define C_SIZE_16B ((uint32_t)0x00000010)
441 #define C_SIZE_32B ((uint32_t)0x00000020)
442 #define C_SIZE_64B ((uint32_t)0x00000040)
443 #define C_SIZE_128B ((uint32_t)0x00000080)
444 #define C_SIZE_256B ((uint32_t)0x00000100)
445 #define C_SIZE_512B ((uint32_t)0x00000200)
446 #define C_SIZE_1KB ((uint32_t)0x00000400)
447 #define C_SIZE_2KB ((uint32_t)0x00000800)
448 #define C_SIZE_4KB ((uint32_t)0x00001000)
449 #define C_SIZE_8KB ((uint32_t)0x00002000)
450 #define C_SIZE_16KB ((uint32_t)0x00004000)
451 #define C_SIZE_32KB ((uint32_t)0x00008000)
452 #define C_SIZE_64KB ((uint32_t)0x00010000)
453 #define C_SIZE_128KB ((uint32_t)0x00020000)
454 #define C_SIZE_256KB ((uint32_t)0x00040000)
455 #define C_SIZE_512KB ((uint32_t)0x00080000)
456 #define C_SIZE_1MB ((uint32_t)0x00100000)
457 #define C_SIZE_2MB ((uint32_t)0x00200000)
458 #define C_SIZE_4MB ((uint32_t)0x00400000)
459 #define C_SIZE_8MB ((uint32_t)0x00800000)
460 #define C_SIZE_16MB ((uint32_t)0x01000000)
461 #define C_SIZE_32MB ((uint32_t)0x02000000)
462 #define C_SIZE_64MB ((uint32_t)0x04000000)
463 #define C_SIZE_128MB ((uint32_t)0x08000000)
464 #define C_SIZE_256MB ((uint32_t)0x10000000)
465 #define C_SIZE_512MB ((uint32_t)0x20000000)
466 #define C_SIZE_1GB ((uint32_t)0x40000000)
467 #define C_SIZE_2GB ((uint32_t)0x80000000)
481 #endif /* End (_ARM_CSTD_) */