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