1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun * 3*4882a593Smuzhiyun * (C) COPYRIGHT 2014-2015 ARM Limited. All rights reserved. 4*4882a593Smuzhiyun * 5*4882a593Smuzhiyun * This program is free software and is provided to you under the terms of the 6*4882a593Smuzhiyun * GNU General Public License version 2 as published by the Free Software 7*4882a593Smuzhiyun * Foundation, and any use by you of this program is subject to the terms 8*4882a593Smuzhiyun * of such GNU licence. 9*4882a593Smuzhiyun * 10*4882a593Smuzhiyun * A copy of the licence is included with the program, and can also be obtained 11*4882a593Smuzhiyun * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 12*4882a593Smuzhiyun * Boston, MA 02110-1301, USA. 13*4882a593Smuzhiyun * 14*4882a593Smuzhiyun */ 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun 18*4882a593Smuzhiyun /** 19*4882a593Smuzhiyun * Kernel-wide include for common macros and types. 20*4882a593Smuzhiyun */ 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun #ifndef _MALISW_H_ 23*4882a593Smuzhiyun #define _MALISW_H_ 24*4882a593Smuzhiyun 25*4882a593Smuzhiyun #include <linux/version.h> 26*4882a593Smuzhiyun #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0) 27*4882a593Smuzhiyun #define U8_MAX ((u8)~0U) 28*4882a593Smuzhiyun #define S8_MAX ((s8)(U8_MAX>>1)) 29*4882a593Smuzhiyun #define S8_MIN ((s8)(-S8_MAX - 1)) 30*4882a593Smuzhiyun #define U16_MAX ((u16)~0U) 31*4882a593Smuzhiyun #define S16_MAX ((s16)(U16_MAX>>1)) 32*4882a593Smuzhiyun #define S16_MIN ((s16)(-S16_MAX - 1)) 33*4882a593Smuzhiyun #define U32_MAX ((u32)~0U) 34*4882a593Smuzhiyun #define S32_MAX ((s32)(U32_MAX>>1)) 35*4882a593Smuzhiyun #define S32_MIN ((s32)(-S32_MAX - 1)) 36*4882a593Smuzhiyun #define U64_MAX ((u64)~0ULL) 37*4882a593Smuzhiyun #define S64_MAX ((s64)(U64_MAX>>1)) 38*4882a593Smuzhiyun #define S64_MIN ((s64)(-S64_MAX - 1)) 39*4882a593Smuzhiyun #endif /* LINUX_VERSION_CODE */ 40*4882a593Smuzhiyun #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 5, 0) 41*4882a593Smuzhiyun #define SIZE_MAX (~(size_t)0) 42*4882a593Smuzhiyun #endif /* LINUX_VERSION_CODE */ 43*4882a593Smuzhiyun 44*4882a593Smuzhiyun /** 45*4882a593Smuzhiyun * MIN - Return the lesser of two values. 46*4882a593Smuzhiyun * 47*4882a593Smuzhiyun * As a macro it may evaluate its arguments more than once. 48*4882a593Smuzhiyun * Refer to MAX macro for more details 49*4882a593Smuzhiyun */ 50*4882a593Smuzhiyun #define MIN(x, y) ((x) < (y) ? (x) : (y)) 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun /** 53*4882a593Smuzhiyun * MAX - Return the greater of two values. 54*4882a593Smuzhiyun * 55*4882a593Smuzhiyun * As a macro it may evaluate its arguments more than once. 56*4882a593Smuzhiyun * If called on the same two arguments as MIN it is guaranteed to return 57*4882a593Smuzhiyun * the one that MIN didn't return. This is significant for types where not 58*4882a593Smuzhiyun * all values are comparable e.g. NaNs in floating-point types. But if you want 59*4882a593Smuzhiyun * to retrieve the min and max of two values, consider using a conditional swap 60*4882a593Smuzhiyun * instead. 61*4882a593Smuzhiyun */ 62*4882a593Smuzhiyun #define MAX(x, y) ((x) < (y) ? (y) : (x)) 63*4882a593Smuzhiyun 64*4882a593Smuzhiyun /** 65*4882a593Smuzhiyun * @hideinitializer 66*4882a593Smuzhiyun * Function-like macro for suppressing unused variable warnings. Where possible 67*4882a593Smuzhiyun * such variables should be removed; this macro is present for cases where we 68*4882a593Smuzhiyun * much support API backwards compatibility. 69*4882a593Smuzhiyun */ 70*4882a593Smuzhiyun #define CSTD_UNUSED(x) ((void)(x)) 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun /** 73*4882a593Smuzhiyun * @hideinitializer 74*4882a593Smuzhiyun * Function-like macro for use where "no behavior" is desired. This is useful 75*4882a593Smuzhiyun * when compile time macros turn a function-like macro in to a no-op, but 76*4882a593Smuzhiyun * where having no statement is otherwise invalid. 77*4882a593Smuzhiyun */ 78*4882a593Smuzhiyun #define CSTD_NOP(...) ((void)#__VA_ARGS__) 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun /** 81*4882a593Smuzhiyun * Function-like macro for converting a pointer in to a u64 for storing into 82*4882a593Smuzhiyun * an external data structure. This is commonly used when pairing a 32-bit 83*4882a593Smuzhiyun * CPU with a 64-bit peripheral, such as a Midgard GPU. C's type promotion 84*4882a593Smuzhiyun * is complex and a straight cast does not work reliably as pointers are 85*4882a593Smuzhiyun * often considered as signed. 86*4882a593Smuzhiyun */ 87*4882a593Smuzhiyun #define PTR_TO_U64(x) ((uint64_t)((uintptr_t)(x))) 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun /** 90*4882a593Smuzhiyun * @hideinitializer 91*4882a593Smuzhiyun * Function-like macro for stringizing a single level macro. 92*4882a593Smuzhiyun * @code 93*4882a593Smuzhiyun * #define MY_MACRO 32 94*4882a593Smuzhiyun * CSTD_STR1( MY_MACRO ) 95*4882a593Smuzhiyun * > "MY_MACRO" 96*4882a593Smuzhiyun * @endcode 97*4882a593Smuzhiyun */ 98*4882a593Smuzhiyun #define CSTD_STR1(x) #x 99*4882a593Smuzhiyun 100*4882a593Smuzhiyun /** 101*4882a593Smuzhiyun * @hideinitializer 102*4882a593Smuzhiyun * Function-like macro for stringizing a macro's value. This should not be used 103*4882a593Smuzhiyun * if the macro is defined in a way which may have no value; use the 104*4882a593Smuzhiyun * alternative @c CSTD_STR2N macro should be used instead. 105*4882a593Smuzhiyun * @code 106*4882a593Smuzhiyun * #define MY_MACRO 32 107*4882a593Smuzhiyun * CSTD_STR2( MY_MACRO ) 108*4882a593Smuzhiyun * > "32" 109*4882a593Smuzhiyun * @endcode 110*4882a593Smuzhiyun */ 111*4882a593Smuzhiyun #define CSTD_STR2(x) CSTD_STR1(x) 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun /** 114*4882a593Smuzhiyun * Specify an assertion value which is evaluated at compile time. Recommended 115*4882a593Smuzhiyun * usage is specification of a @c static @c INLINE function containing all of 116*4882a593Smuzhiyun * the assertions thus: 117*4882a593Smuzhiyun * 118*4882a593Smuzhiyun * @code 119*4882a593Smuzhiyun * static INLINE [module]_compile_time_assertions( void ) 120*4882a593Smuzhiyun * { 121*4882a593Smuzhiyun * COMPILE_TIME_ASSERT( sizeof(uintptr_t) == sizeof(intptr_t) ); 122*4882a593Smuzhiyun * } 123*4882a593Smuzhiyun * @endcode 124*4882a593Smuzhiyun * 125*4882a593Smuzhiyun * @note Use @c static not @c STATIC. We never want to turn off this @c static 126*4882a593Smuzhiyun * specification for testing purposes. 127*4882a593Smuzhiyun */ 128*4882a593Smuzhiyun #define CSTD_COMPILE_TIME_ASSERT(expr) \ 129*4882a593Smuzhiyun do { switch (0) { case 0: case (expr):; } } while (false) 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun #endif /* _MALISW_H_ */ 132