]> begriffs open source - cmsis/blob - CMSIS/Core/Include/cmsis_compiler.h
CMSIS-Core: Changed IAR __CLZ workaround to respect a new guard signaling availabilit...
[cmsis] / CMSIS / Core / Include / cmsis_compiler.h
1 /**************************************************************************//**
2  * @file     cmsis_compiler.h
3  * @brief    CMSIS compiler generic header file
4  * @version  V5.0.2
5  * @date     13. February 2017
6  ******************************************************************************/
7 /*
8  * Copyright (c) 2009-2017 ARM Limited. All rights reserved.
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  *
12  * Licensed under the Apache License, Version 2.0 (the License); you may
13  * not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  * www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
20  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */
24
25 #ifndef __CMSIS_COMPILER_H
26 #define __CMSIS_COMPILER_H
27
28 #include <stdint.h>
29
30 /*
31  * ARM Compiler 4/5
32  */
33 #if   defined ( __CC_ARM )
34   #include "cmsis_armcc.h"
35
36
37 /*
38  * ARM Compiler 6 (armclang)
39  */
40 #elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
41   #include "cmsis_armclang.h"
42
43
44 /*
45  * GNU Compiler
46  */
47 #elif defined ( __GNUC__ )
48   #include "cmsis_gcc.h"
49
50
51 /*
52  * IAR Compiler
53  */
54 #elif defined ( __ICCARM__ )
55
56
57   #ifndef   __ASM
58     #define __ASM                                  __asm
59   #endif
60   #ifndef   __INLINE
61     #define __INLINE                               inline
62   #endif
63   #ifndef   __STATIC_INLINE
64     #define __STATIC_INLINE                        static inline
65   #endif
66
67   #include <cmsis_iar.h>
68
69   /* CMSIS compiler control architecture macros */
70   #if (__CORE__ == __ARM6M__) || (__CORE__ == __ARM6SM__)
71     #ifndef __ARM_ARCH_6M__
72       #define __ARM_ARCH_6M__                      1
73     #endif
74   #elif (__CORE__ == __ARM7M__)
75     #ifndef __ARM_ARCH_7M__
76       #define __ARM_ARCH_7M__                      1
77     #endif
78   #elif (__CORE__ == __ARM7EM__)
79     #ifndef __ARM_ARCH_7EM__
80       #define __ARM_ARCH_7EM__                     1
81     #endif
82   #endif
83
84   #ifndef   __NO_RETURN
85     #define __NO_RETURN                            __noreturn
86   #endif
87   #ifndef   __USED
88     #define __USED                                 __root
89   #endif
90   #ifndef   __WEAK
91     #define __WEAK                                 __weak
92   #endif
93   #ifndef   __PACKED
94     #define __PACKED                               __packed
95   #endif
96   #ifndef   __PACKED_STRUCT
97     #define __PACKED_STRUCT                        __packed struct
98   #endif
99   #ifndef   __UNALIGNED_UINT32        /* deprecated */
100     __packed struct T_UINT32 { uint32_t v; };
101     #define __UNALIGNED_UINT32(x)                  (((struct T_UINT32 *)(x))->v)
102   #endif
103   #ifndef   __UNALIGNED_UINT16_WRITE
104     __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
105     #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
106   #endif
107   #ifndef   __UNALIGNED_UINT16_READ
108     __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
109     #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
110   #endif
111   #ifndef   __UNALIGNED_UINT32_WRITE
112     __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
113     #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
114   #endif
115   #ifndef   __UNALIGNED_UINT32_READ
116     __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
117     #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
118   #endif
119   #ifndef   __ALIGNED
120     #warning No compiler specific solution for __ALIGNED. __ALIGNED is ignored.
121     #define __ALIGNED(x)
122   #endif
123
124   // Workaround for missing __CLZ intrinsic in
125   // various versions of the IAR compilers.
126   // __IAR_FEATURE_CLZ__ should be defined by
127   // the compiler that supports __CLZ internally.
128   #if __ARM_ARCH_6M__ && !__IAR_FEATURE_CLZ__
129     __STATIC_INLINE uint32_t __CLZ(uint32_t data)
130     {
131       if (data == 0u) { return 32u; }
132       
133       uint32_t count = 0;
134       uint32_t mask = 0x80000000;
135       
136       while ((data & mask) == 0)
137       {
138         count += 1u;
139         mask = mask >> 1u;
140       }
141       
142       return (count);
143     }
144   #endif
145
146
147 /*
148  * TI ARM Compiler
149  */
150 #elif defined ( __TI_ARM__ )
151   #include <cmsis_ccs.h>
152
153   #ifndef   __ASM
154     #define __ASM                                  __asm
155   #endif
156   #ifndef   __INLINE
157     #define __INLINE                               inline
158   #endif
159   #ifndef   __STATIC_INLINE
160     #define __STATIC_INLINE                        static inline
161   #endif
162   #ifndef   __NO_RETURN
163     #define __NO_RETURN                            __attribute__((noreturn))
164   #endif
165   #ifndef   __USED
166     #define __USED                                 __attribute__((used))
167   #endif
168   #ifndef   __WEAK
169     #define __WEAK                                 __attribute__((weak))
170   #endif
171   #ifndef   __PACKED
172     #define __PACKED                               __attribute__((packed))
173   #endif
174   #ifndef   __PACKED_STRUCT
175     #define __PACKED_STRUCT                        struct __attribute__((packed))
176   #endif
177   #ifndef   __UNALIGNED_UINT32        /* deprecated */
178     struct __attribute__((packed)) T_UINT32 { uint32_t v; };
179     #define __UNALIGNED_UINT32(x)                  (((struct T_UINT32 *)(x))->v)
180   #endif
181   #ifndef   __UNALIGNED_UINT16_WRITE
182     __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
183     #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void*)(addr))->v) = (val))
184   #endif
185   #ifndef   __UNALIGNED_UINT16_READ
186     __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
187     #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
188   #endif
189   #ifndef   __UNALIGNED_UINT32_WRITE
190     __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
191     #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
192   #endif
193   #ifndef   __UNALIGNED_UINT32_READ
194     __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
195     #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
196   #endif
197   #ifndef   __ALIGNED
198     #define __ALIGNED(x)                           __attribute__((aligned(x)))
199   #endif
200
201
202 /*
203  * TASKING Compiler
204  */
205 #elif defined ( __TASKING__ )
206   /*
207    * The CMSIS functions have been implemented as intrinsics in the compiler.
208    * Please use "carm -?i" to get an up to date list of all intrinsics,
209    * Including the CMSIS ones.
210    */
211
212   #ifndef   __ASM
213     #define __ASM                                  __asm
214   #endif
215   #ifndef   __INLINE
216     #define __INLINE                               inline
217   #endif
218   #ifndef   __STATIC_INLINE
219     #define __STATIC_INLINE                        static inline
220   #endif
221   #ifndef   __NO_RETURN
222     #define __NO_RETURN                            __attribute__((noreturn))
223   #endif
224   #ifndef   __USED
225     #define __USED                                 __attribute__((used))
226   #endif
227   #ifndef   __WEAK
228     #define __WEAK                                 __attribute__((weak))
229   #endif
230   #ifndef   __PACKED
231     #define __PACKED                               __packed__
232   #endif
233   #ifndef   __PACKED_STRUCT
234     #define __PACKED_STRUCT                        struct __packed__
235   #endif
236   #ifndef   __UNALIGNED_UINT32        /* deprecated */
237     struct __packed__ T_UINT32 { uint32_t v; };
238     #define __UNALIGNED_UINT32(x)                  (((struct T_UINT32 *)(x))->v)
239   #endif
240   #ifndef   __UNALIGNED_UINT16_WRITE
241     __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
242     #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
243   #endif
244   #ifndef   __UNALIGNED_UINT16_READ
245     __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
246     #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
247   #endif
248   #ifndef   __UNALIGNED_UINT32_WRITE
249     __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
250     #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
251   #endif
252   #ifndef   __UNALIGNED_UINT32_READ
253     __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
254     #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
255   #endif
256   #ifndef   __ALIGNED
257     #define __ALIGNED(x)              __align(x)
258   #endif
259
260
261 /*
262  * COSMIC Compiler
263  */
264 #elif defined ( __CSMC__ )
265    #include <cmsis_csm.h>
266
267  #ifndef   __ASM
268     #define __ASM                                  _asm
269   #endif
270   #ifndef   __INLINE
271     #define __INLINE                               inline
272   #endif
273   #ifndef   __STATIC_INLINE
274     #define __STATIC_INLINE                        static inline
275   #endif
276   #ifndef   __NO_RETURN
277     // NO RETURN is automatically detected hence no warning here
278     #define __NO_RETURN
279   #endif
280   #ifndef   __USED
281     #warning No compiler specific solution for __USED. __USED is ignored.
282     #define __USED
283   #endif
284   #ifndef   __WEAK
285     #define __WEAK                                 __weak
286   #endif
287   #ifndef   __PACKED
288     #define __PACKED                               @packed
289   #endif
290   #ifndef   __PACKED_STRUCT
291     #define __PACKED_STRUCT                        @packed struct
292   #endif
293   #ifndef   __UNALIGNED_UINT32        /* deprecated */
294     @packed struct T_UINT32 { uint32_t v; };
295     #define __UNALIGNED_UINT32(x)                  (((struct T_UINT32 *)(x))->v)
296   #endif
297   #ifndef   __UNALIGNED_UINT16_WRITE
298     __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
299     #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
300   #endif
301   #ifndef   __UNALIGNED_UINT16_READ
302     __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
303     #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
304   #endif
305   #ifndef   __UNALIGNED_UINT32_WRITE
306     __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
307     #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
308   #endif
309   #ifndef   __UNALIGNED_UINT32_READ
310     __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
311     #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
312   #endif
313   #ifndef   __ALIGNED
314     #warning No compiler specific solution for __ALIGNED. __ALIGNED is ignored.
315     #define __ALIGNED(x)
316   #endif
317
318
319 #else
320   #error Unknown compiler.
321 #endif
322
323
324 #endif /* __CMSIS_COMPILER_H */
325