SSCMA-Micro CPP SDK  v2.0.0
SSCMA-Micro is a cross-platform machine learning inference framework designed for embedded devices.
ma_compiler.h
Go to the documentation of this file.
1 #ifndef _MA_COMPILER_H_
2 #define _MA_COMPILER_H_
3 
4 
5 #define MA_STRINGIFY(s) #s
6 #define MA_STRINGIZE(s) MA_STRINGIFY(s)
7 
8 #define MA_CONCAT(a, b) a##b
9 #define MA_CONCAT3(a, b, c) a##b##c
10 
11 #define MA_UNUSED(x) (void)(x)
12 #define MA_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
13 
14 #define MA_MAX(a, b) ((a) > (b) ? (a) : (b))
15 #define MA_MIN(a, b) ((a) < (b) ? (a) : (b))
16 #define MA_CLIP(x, a, b) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
17 
18 #define MA_ABS(a) ((a) < 0 ? -(a) : (a))
19 #define MA_ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
20 #define MA_ALIGN_DOWN(x, a) ((x) & ~((a) - 1))
21 #define MA_ALIGN_UP(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
22 #define MA_IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
23 #define MA_IS_ALIGNED_DOWN(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
24 #define MA_IS_ALIGNED_UP(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
25 
26 #define MA_BIT(n) (1 << (n))
27 #define MA_BIT_MASK(n) (MA_BIT(n) - 1)
28 #define MA_BIT_SET(x, n) ((x) |= MA_BIT(n))
29 #define MA_BIT_CLR(x, n) ((x) &= ~EL_BIT(n))
30 #define MA_BIT_GET(x, n) (((x) >> (n)) & 1)
31 #define MA_BIT_SET_MASK(x, n, m) ((x) |= ((m) << (n)))
32 #define MA_BIT_CLR_MASK(x, n, m) ((x) &= ~((m) << (n)))
33 #define MA_BIT_GET_MASK(x, n, m) (((x) >> (n)) & (m))
34 #define MA_BIT_SET_MASKED(x, n, m) ((x) |= ((m) & (MA_BIT_MASK(n) << (n))))
35 #define MA_BIT_CLR_MASKED(x, n, m) ((x) &= ~((m) & (MA_BIT_MASK(n) << (n))))
36 #define MA_BIT_GET_MASKED(x, n, m) (((x) >> (n)) & (m))
37 
38 
39 #if defined __COUNTER__ && __COUNTER__ != __COUNTER__
40 #define _MA_COUNTER_ __COUNTER__
41 #else
42 #define _MA_COUNTER_ __LINE__
43 #endif
44 
45 // Compile-time Assert
46 #if defined(__cplusplus) && __cplusplus >= 201103L
47 #define MA_STATIC_ASSERT static_assert
48 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
49 #define MA_STATIC_ASSERT _Static_assert
50 #elif defined(__CCRX__)
51 #define MA_STATIC_ASSERT(const_expr, _mess) \
52  typedef char MA_CONCAT(_verify_static_, _MA_COUNTER_)[(const_expr) ? 1 : 0];
53 #else
54 #define MA_STATIC_ASSERT(const_expr, _mess) \
55  enum { MA_CONCAT(_verify_static_, _MA_COUNTER_) = 1 / (!!(const_expr)) }
56 #endif
57 
58 #define MA_LITTLE_ENDIAN (0x12u)
59 #define MA_BIG_ENDIAN (0x21u)
60 
61 #if defined(__GNUC__) || defined(__clang__)
62 #define MA_ATTR_ALIGNED(Bytes) __attribute__((aligned(Bytes)))
63 #define MA_ATTR_SECTION(sec_name) __attribute__((section(#sec_name)))
64 #define MA_ATTR_PACKED __attribute__((packed))
65 #define MA_ATTR_WEAK __attribute__((weak))
66 #define MA_ATTR_ALWAYS_INLINE __attribute__((always_inline))
67 #define MA_ATTR_DEPRECATED(mess) __attribute__((deprecated(mess)))
68 #define MA_ATTR_UNUSED __attribute__((unused))
69 #define MA_ATTR_USED __attribute__((used))
70 
71 #if __has_attribute(__fallthrough__)
72 #define MA_ATTR_FALLTHROUGH __attribute__((fallthrough))
73 #else
74 #define MA_ATTR_FALLTHROUGH \
75  do { \
76  } while (0) /* fallthrough */
77 #endif
78 
79 // Endian conversion use well-known host to network (big endian) naming
80 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
81 #define MA_BYTE_ORDER MA_LITTLE_ENDIAN
82 #else
83 #define MA_BYTE_ORDER MA_BIG_ENDIAN
84 #endif
85 
86 #define MA_BSWAP16(u16) (__builtin_bswap16(u16))
87 #define MA_BSWAP32(u32) (__builtin_bswap32(u32))
88 
89 #elif defined(__ICCARM__) || defined(__IAR_SYSTEMS_ICC__) || defined(__CC_ARM)
90 #include <intrinsics.h>
91 
92 #define MA_ATTR_ALIGNED(Bytes) __attribute__((aligned(Bytes)))
93 #define MA_ATTR_SECTION(sec_name) __attribute__((section(#sec_name)))
94 #define MA_ATTR_PACKED __attribute__((packed))
95 #define MA_ATTR_WEAK __attribute__((weak))
96 #define MA_ATTR_ALWAYS_INLINE __attribute__((always_inline))
97 #define MA_ATTR_DEPRECATED(mess) \
98  __attribute__((deprecated(mess))) // warn if function with this attribute is used
99 #define MA_ATTR_UNUSED __attribute__((unused)) // Function/Variable is meant to be possibly unused
100 #define MA_ATTR_USED __attribute__((used)) // Function/Variable is meant to be used
101 #define MA_ATTR_FALLTHROUGH __attribute__((fallthrough))
102 
103 // Endian conversion use well-known host to network (big endian) naming
104 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
105 #define MA_BYTE_ORDER MA_LITTLE_ENDIAN
106 #else
107 #define MA_BYTE_ORDER MA_BIG_ENDIAN
108 #endif
109 
110 #define MA_BSWAP16(u16) (__iar_builtin_REV16(u16))
111 #define MA_BSWAP32(u32) (__iar_builtin_REV(u32))
112 
113 #else
114 #error "Compiler attribute porting is required"
115 #endif
116 
117 #if (MA_BYTE_ORDER == MA_LITTLE_ENDIAN)
118 
119 #define ma_htons(u16) (MA_BSWAP16(u16))
120 #define ma_ntohs(u16) (MA_BSWAP16(u16))
121 
122 #define ma_htonl(u32) (MA_BSWAP32(u32))
123 #define ma_ntohl(u32) (MA_BSWAP32(u32))
124 
125 #define ma_htole16(u16) (u16)
126 #define ma_le16toh(u16) (u16)
127 
128 #define ma_htole32(u32) (u32)
129 #define ma_le32toh(u32) (u32)
130 
131 #elif (MA_BYTE_ORDER == MA_BIG_ENDIAN)
132 
133 #define ma_htons(u16) (u16)
134 #define ma_ntohs(u16) (u16)
135 
136 #define ma_htonl(u32) (u32)
137 #define ma_ntohl(u32) (u32)
138 
139 #define ma_htole16(u16) (MA_BSWAP16(u16))
140 #define ma_le16toh(u16) (MA_BSWAP16(u16))
141 
142 #define ma_htole32(u32) (MA_BSWAP32(u32))
143 #define ma_le32toh(u32) (MA_BSWAP32(u32))
144 #else
145 #error "Byte order is undefined"
146 #endif
147 
148 #ifdef SWIG
149 #define MA_EXPORT
150 #elif defined(MA_STATIC_LIBRARY_BUILD)
151 #define MA_EXPORT
152 #else // not definded MA_STATIC_LIBRARY_BUILD
153 #if defined(_WIN32)
154 #ifdef MA_COMPILE_LIBRARY
155 #define MA_EXPORT __declspec(dllexport)
156 #else
157 #define MA_EXPORT __declspec(dllimport)
158 #endif // MA_COMPILE_LIBRARY
159 #else
160 #define MA_EXPORT __attribute__((visibility("default")))
161 #endif // _WIN32
162 #endif // SWIG
163 
164 
165 #if defined(__x86_64__) || defined(__ppc64__) || defined(_LP64)
166 #define MA_ARCH_64 1
167 #else
168 #define MA_ARCH_32 1
169 #endif
170 
171 #endif // _MA_COMPILER_H_