]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4L_Atmel_Studio/src/asf/sam/utils/compiler.h
5f1e1d239b8d4c06904048fed5344b1440522b84
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4L_Atmel_Studio / src / asf / sam / utils / compiler.h
1 /**\r
2  * \file\r
3  *\r
4  * \brief Commonly used includes, types and macros.\r
5  *\r
6  * Copyright (c) 2010-2013 Atmel Corporation. All rights reserved.\r
7  *\r
8  * \asf_license_start\r
9  *\r
10  * \page License\r
11  *\r
12  * Redistribution and use in source and binary forms, with or without\r
13  * modification, are permitted provided that the following conditions are met:\r
14  *\r
15  * 1. Redistributions of source code must retain the above copyright notice,\r
16  *    this list of conditions and the following disclaimer.\r
17  *\r
18  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
19  *    this list of conditions and the following disclaimer in the documentation\r
20  *    and/or other materials provided with the distribution.\r
21  *\r
22  * 3. The name of Atmel may not be used to endorse or promote products derived\r
23  *    from this software without specific prior written permission.\r
24  *\r
25  * 4. This software may only be redistributed and used in connection with an\r
26  *    Atmel microcontroller product.\r
27  *\r
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED\r
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR\r
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
38  * POSSIBILITY OF SUCH DAMAGE.\r
39  *\r
40  * \asf_license_stop\r
41  *\r
42  */\r
43 \r
44 #ifndef UTILS_COMPILER_H\r
45 #define UTILS_COMPILER_H\r
46 \r
47 /**\r
48  * \defgroup group_sam_utils Compiler abstraction layer and code utilities\r
49  *\r
50  * Compiler abstraction layer and code utilities for AT91SAM.\r
51  * This module provides various abstraction layers and utilities to make code compatible between different compilers.\r
52  *\r
53  * \{\r
54  */\r
55 #include <stddef.h>\r
56 \r
57 #if (defined __ICCARM__)\r
58 #  include <intrinsics.h>\r
59 #endif\r
60 \r
61 #include <parts.h>\r
62 #include "preprocessor.h"\r
63 \r
64 #include <io.h>\r
65 \r
66 //_____ D E C L A R A T I O N S ____________________________________________\r
67 \r
68 #ifndef __ASSEMBLY__ // Not defined for assembling.\r
69 \r
70 #include <stdio.h>\r
71 #include <stdbool.h>\r
72 #include <stdint.h>\r
73 #include <stdlib.h>\r
74 \r
75 #ifdef __ICCARM__\r
76 /*! \name Compiler Keywords\r
77  *\r
78  * Port of some keywords from GCC to IAR Embedded Workbench.\r
79  */\r
80 //! @{\r
81 #define __asm__             asm\r
82 #define __inline__          inline\r
83 #define __volatile__\r
84 //! @}\r
85 \r
86 #endif\r
87 \r
88 #define FUNC_PTR                            void *\r
89 /**\r
90  * \def UNUSED\r
91  * \brief Marking \a v as a unused parameter or value.\r
92  */\r
93 #define UNUSED(v)          (void)(v)\r
94 \r
95 /**\r
96  * \def unused\r
97  * \brief Marking \a v as a unused parameter or value.\r
98  */\r
99 #define unused(v)          do { (void)(v); } while(0)\r
100 \r
101 /**\r
102  * \def barrier\r
103  * \brief Memory barrier\r
104  */\r
105 #define barrier()          __DMB()\r
106 \r
107 /**\r
108  * \brief Emit the compiler pragma \a arg.\r
109  *\r
110  * \param arg The pragma directive as it would appear after \e \#pragma\r
111  * (i.e. not stringified).\r
112  */\r
113 #define COMPILER_PRAGMA(arg)            _Pragma(#arg)\r
114 \r
115 /**\r
116  * \def COMPILER_PACK_SET(alignment)\r
117  * \brief Set maximum alignment for subsequent struct and union\r
118  * definitions to \a alignment.\r
119  */\r
120 #define COMPILER_PACK_SET(alignment)   COMPILER_PRAGMA(pack(alignment))\r
121 \r
122 /**\r
123  * \def COMPILER_PACK_RESET()\r
124  * \brief Set default alignment for subsequent struct and union\r
125  * definitions.\r
126  */\r
127 #define COMPILER_PACK_RESET()          COMPILER_PRAGMA(pack())\r
128 \r
129 \r
130 /**\r
131  * \brief Set aligned boundary.\r
132  */\r
133 #if (defined __GNUC__) || (defined __CC_ARM)\r
134 #   define COMPILER_ALIGNED(a)    __attribute__((__aligned__(a)))\r
135 #elif (defined __ICCARM__)\r
136 #   define COMPILER_ALIGNED(a)    COMPILER_PRAGMA(data_alignment = a)\r
137 #endif\r
138 \r
139 /**\r
140  * \brief Set word-aligned boundary.\r
141  */\r
142 #if (defined __GNUC__) || defined(__CC_ARM)\r
143 #define COMPILER_WORD_ALIGNED    __attribute__((__aligned__(4)))\r
144 #elif (defined __ICCARM__)\r
145 #define COMPILER_WORD_ALIGNED    COMPILER_PRAGMA(data_alignment = 4)\r
146 #endif\r
147 \r
148 /**\r
149  * \def __always_inline\r
150  * \brief The function should always be inlined.\r
151  *\r
152  * This annotation instructs the compiler to ignore its inlining\r
153  * heuristics and inline the function no matter how big it thinks it\r
154  * becomes.\r
155  */\r
156 #if defined(__CC_ARM)\r
157 #   define __always_inline   __forceinline\r
158 #elif (defined __GNUC__)\r
159 #       define __always_inline   inline __attribute__((__always_inline__))\r
160 #elif (defined __ICCARM__)\r
161 #       define __always_inline   _Pragma("inline=forced")\r
162 #endif\r
163 \r
164 /*! \brief This macro is used to test fatal errors.\r
165  *\r
166  * The macro tests if the expression is false. If it is, a fatal error is\r
167  * detected and the application hangs up. If TEST_SUITE_DEFINE_ASSERT_MACRO\r
168  * is defined, a unit test version of the macro is used, to allow execution\r
169  * of further tests after a false expression.\r
170  *\r
171  * \param expr  Expression to evaluate and supposed to be nonzero.\r
172  */\r
173 #if defined(_ASSERT_ENABLE_)\r
174 #  if defined(TEST_SUITE_DEFINE_ASSERT_MACRO)\r
175      // Assert() is defined in unit_test/suite.h\r
176 #    include "unit_test/suite.h"\r
177 #  else\r
178 #undef TEST_SUITE_DEFINE_ASSERT_MACRO\r
179 #    define Assert(expr) \\r
180         {\\r
181                 if (!(expr)) while (true);\\r
182         }\r
183 #  endif\r
184 #else\r
185 #  define Assert(expr) ((void) 0)\r
186 #endif\r
187 \r
188 /* Define WEAK attribute */\r
189 #if defined   ( __CC_ARM   ) /* Keil µVision 4 */\r
190 #   define WEAK __attribute__ ((weak))\r
191 #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */\r
192 #   define WEAK __weak\r
193 #elif defined (  __GNUC__  ) /* GCC CS3 2009q3-68 */\r
194 #   define WEAK __attribute__ ((weak))\r
195 #endif\r
196 \r
197 /* Define NO_INIT attribute */\r
198 #if defined   ( __CC_ARM   )\r
199 #   define NO_INIT __attribute__((zero_init))\r
200 #elif defined ( __ICCARM__ )\r
201 #   define NO_INIT __no_init\r
202 #elif defined (  __GNUC__  )\r
203 #   define NO_INIT __attribute__((section(".no_init")))\r
204 #endif\r
205 \r
206 /* Define RAMFUNC attribute */\r
207 #if defined   ( __CC_ARM   ) /* Keil µVision 4 */\r
208 #   define RAMFUNC __attribute__ ((section(".ramfunc")))\r
209 #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */\r
210 #   define RAMFUNC __ramfunc\r
211 #elif defined (  __GNUC__  ) /* GCC CS3 2009q3-68 */\r
212 #   define RAMFUNC __attribute__ ((section(".ramfunc")))\r
213 #endif\r
214 \r
215 /* Define OPTIMIZE_HIGH attribute */\r
216 #if defined   ( __CC_ARM   ) /* Keil µVision 4 */\r
217 #   define OPTIMIZE_HIGH _Pragma("O3") \r
218 #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */\r
219 #   define OPTIMIZE_HIGH _Pragma("optimize=high")\r
220 #elif defined (  __GNUC__  ) /* GCC CS3 2009q3-68 */\r
221 #   define OPTIMIZE_HIGH __attribute__((optimize(s)))\r
222 #endif\r
223 \r
224 #include "interrupt.h"\r
225 \r
226 /*! \name Usual Types\r
227  */\r
228 //! @{\r
229 typedef unsigned char           Bool; //!< Boolean.\r
230 #ifndef __cplusplus\r
231 #if !defined(__bool_true_false_are_defined)\r
232 typedef unsigned char           bool; //!< Boolean.\r
233 #endif\r
234 #endif\r
235 typedef int8_t                  S8 ;  //!< 8-bit signed integer.\r
236 typedef uint8_t                 U8 ;  //!< 8-bit unsigned integer.\r
237 typedef int16_t                 S16;  //!< 16-bit signed integer.\r
238 typedef uint16_t                U16;  //!< 16-bit unsigned integer.\r
239 typedef uint16_t                le16_t;\r
240 typedef uint16_t                be16_t;\r
241 typedef int32_t                 S32;  //!< 32-bit signed integer.\r
242 typedef uint32_t                U32;  //!< 32-bit unsigned integer.\r
243 typedef uint32_t                le32_t;\r
244 typedef uint32_t                be32_t;\r
245 typedef int64_t                 S64;  //!< 64-bit signed integer.\r
246 typedef uint64_t                U64;  //!< 64-bit unsigned integer.\r
247 typedef float                   F32;  //!< 32-bit floating-point number.\r
248 typedef double                  F64;  //!< 64-bit floating-point number.\r
249 typedef uint32_t                iram_size_t;\r
250 //! @}\r
251 \r
252 \r
253 /*! \name Status Types\r
254  */\r
255 //! @{\r
256 typedef bool                Status_bool_t;  //!< Boolean status.\r
257 typedef U8                  Status_t;       //!< 8-bit-coded status.\r
258 //! @}\r
259 \r
260 \r
261 /*! \name Aliasing Aggregate Types\r
262  */\r
263 //! @{\r
264 \r
265 //! 16-bit union.\r
266 typedef union\r
267 {\r
268   S16 s16   ;\r
269   U16 u16   ;\r
270   S8  s8 [2];\r
271   U8  u8 [2];\r
272 } Union16;\r
273 \r
274 //! 32-bit union.\r
275 typedef union\r
276 {\r
277   S32 s32   ;\r
278   U32 u32   ;\r
279   S16 s16[2];\r
280   U16 u16[2];\r
281   S8  s8 [4];\r
282   U8  u8 [4];\r
283 } Union32;\r
284 \r
285 //! 64-bit union.\r
286 typedef union\r
287 {\r
288   S64 s64   ;\r
289   U64 u64   ;\r
290   S32 s32[2];\r
291   U32 u32[2];\r
292   S16 s16[4];\r
293   U16 u16[4];\r
294   S8  s8 [8];\r
295   U8  u8 [8];\r
296 } Union64;\r
297 \r
298 //! Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers.\r
299 typedef union\r
300 {\r
301   S64 *s64ptr;\r
302   U64 *u64ptr;\r
303   S32 *s32ptr;\r
304   U32 *u32ptr;\r
305   S16 *s16ptr;\r
306   U16 *u16ptr;\r
307   S8  *s8ptr ;\r
308   U8  *u8ptr ;\r
309 } UnionPtr;\r
310 \r
311 //! Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.\r
312 typedef union\r
313 {\r
314   volatile S64 *s64ptr;\r
315   volatile U64 *u64ptr;\r
316   volatile S32 *s32ptr;\r
317   volatile U32 *u32ptr;\r
318   volatile S16 *s16ptr;\r
319   volatile U16 *u16ptr;\r
320   volatile S8  *s8ptr ;\r
321   volatile U8  *u8ptr ;\r
322 } UnionVPtr;\r
323 \r
324 //! Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.\r
325 typedef union\r
326 {\r
327   const S64 *s64ptr;\r
328   const U64 *u64ptr;\r
329   const S32 *s32ptr;\r
330   const U32 *u32ptr;\r
331   const S16 *s16ptr;\r
332   const U16 *u16ptr;\r
333   const S8  *s8ptr ;\r
334   const U8  *u8ptr ;\r
335 } UnionCPtr;\r
336 \r
337 //! Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.\r
338 typedef union\r
339 {\r
340   const volatile S64 *s64ptr;\r
341   const volatile U64 *u64ptr;\r
342   const volatile S32 *s32ptr;\r
343   const volatile U32 *u32ptr;\r
344   const volatile S16 *s16ptr;\r
345   const volatile U16 *u16ptr;\r
346   const volatile S8  *s8ptr ;\r
347   const volatile U8  *u8ptr ;\r
348 } UnionCVPtr;\r
349 \r
350 //! Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers.\r
351 typedef struct\r
352 {\r
353   S64 *s64ptr;\r
354   U64 *u64ptr;\r
355   S32 *s32ptr;\r
356   U32 *u32ptr;\r
357   S16 *s16ptr;\r
358   U16 *u16ptr;\r
359   S8  *s8ptr ;\r
360   U8  *u8ptr ;\r
361 } StructPtr;\r
362 \r
363 //! Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.\r
364 typedef struct\r
365 {\r
366   volatile S64 *s64ptr;\r
367   volatile U64 *u64ptr;\r
368   volatile S32 *s32ptr;\r
369   volatile U32 *u32ptr;\r
370   volatile S16 *s16ptr;\r
371   volatile U16 *u16ptr;\r
372   volatile S8  *s8ptr ;\r
373   volatile U8  *u8ptr ;\r
374 } StructVPtr;\r
375 \r
376 //! Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.\r
377 typedef struct\r
378 {\r
379   const S64 *s64ptr;\r
380   const U64 *u64ptr;\r
381   const S32 *s32ptr;\r
382   const U32 *u32ptr;\r
383   const S16 *s16ptr;\r
384   const U16 *u16ptr;\r
385   const S8  *s8ptr ;\r
386   const U8  *u8ptr ;\r
387 } StructCPtr;\r
388 \r
389 //! Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.\r
390 typedef struct\r
391 {\r
392   const volatile S64 *s64ptr;\r
393   const volatile U64 *u64ptr;\r
394   const volatile S32 *s32ptr;\r
395   const volatile U32 *u32ptr;\r
396   const volatile S16 *s16ptr;\r
397   const volatile U16 *u16ptr;\r
398   const volatile S8  *s8ptr ;\r
399   const volatile U8  *u8ptr ;\r
400 } StructCVPtr;\r
401 \r
402 //! @}\r
403 \r
404 #endif  // #ifndef __ASSEMBLY__\r
405 \r
406 /*! \name Usual Constants\r
407  */\r
408 //! @{\r
409 #define DISABLE   0\r
410 #define ENABLE    1\r
411 #ifndef __cplusplus\r
412 #if !defined(__bool_true_false_are_defined)\r
413 #define false     0\r
414 #define true      1\r
415 #endif\r
416 #endif\r
417 #define PASS      0\r
418 #define FAIL      1\r
419 #define LOW       0\r
420 #define HIGH      1\r
421 //! @}\r
422 \r
423 \r
424 #ifndef __ASSEMBLY__ // not for assembling.\r
425 \r
426 //! \name Optimization Control\r
427 //@{\r
428 \r
429 /**\r
430  * \def likely(exp)\r
431  * \brief The expression \a exp is likely to be true\r
432  */\r
433 #ifndef likely\r
434 #   define likely(exp)    (exp)\r
435 #endif\r
436 \r
437 /**\r
438  * \def unlikely(exp)\r
439  * \brief The expression \a exp is unlikely to be true\r
440  */\r
441 #ifndef unlikely\r
442 #   define unlikely(exp)  (exp)\r
443 #endif\r
444 \r
445 /**\r
446  * \def is_constant(exp)\r
447  * \brief Determine if an expression evaluates to a constant value.\r
448  *\r
449  * \param exp Any expression\r
450  *\r
451  * \return true if \a exp is constant, false otherwise.\r
452  */\r
453 #if (defined __GNUC__) || (defined __CC_ARM)\r
454 #   define is_constant(exp)       __builtin_constant_p(exp)\r
455 #else\r
456 #   define is_constant(exp)       (0)\r
457 #endif\r
458 \r
459 //! @}\r
460 \r
461 /*! \name Bit-Field Handling\r
462  */\r
463 //! @{\r
464 \r
465 /*! \brief Reads the bits of a value specified by a given bit-mask.\r
466  *\r
467  * \param value Value to read bits from.\r
468  * \param mask  Bit-mask indicating bits to read.\r
469  *\r
470  * \return Read bits.\r
471  */\r
472 #define Rd_bits( value, mask)        ((value) & (mask))\r
473 \r
474 /*! \brief Writes the bits of a C lvalue specified by a given bit-mask.\r
475  *\r
476  * \param lvalue  C lvalue to write bits to.\r
477  * \param mask    Bit-mask indicating bits to write.\r
478  * \param bits    Bits to write.\r
479  *\r
480  * \return Resulting value with written bits.\r
481  */\r
482 #define Wr_bits(lvalue, mask, bits)  ((lvalue) = ((lvalue) & ~(mask)) |\\r
483                                                  ((bits  ) &  (mask)))\r
484 \r
485 /*! \brief Tests the bits of a value specified by a given bit-mask.\r
486  *\r
487  * \param value Value of which to test bits.\r
488  * \param mask  Bit-mask indicating bits to test.\r
489  *\r
490  * \return \c 1 if at least one of the tested bits is set, else \c 0.\r
491  */\r
492 #define Tst_bits( value, mask)  (Rd_bits(value, mask) != 0)\r
493 \r
494 /*! \brief Clears the bits of a C lvalue specified by a given bit-mask.\r
495  *\r
496  * \param lvalue  C lvalue of which to clear bits.\r
497  * \param mask    Bit-mask indicating bits to clear.\r
498  *\r
499  * \return Resulting value with cleared bits.\r
500  */\r
501 #define Clr_bits(lvalue, mask)  ((lvalue) &= ~(mask))\r
502 \r
503 /*! \brief Sets the bits of a C lvalue specified by a given bit-mask.\r
504  *\r
505  * \param lvalue  C lvalue of which to set bits.\r
506  * \param mask    Bit-mask indicating bits to set.\r
507  *\r
508  * \return Resulting value with set bits.\r
509  */\r
510 #define Set_bits(lvalue, mask)  ((lvalue) |=  (mask))\r
511 \r
512 /*! \brief Toggles the bits of a C lvalue specified by a given bit-mask.\r
513  *\r
514  * \param lvalue  C lvalue of which to toggle bits.\r
515  * \param mask    Bit-mask indicating bits to toggle.\r
516  *\r
517  * \return Resulting value with toggled bits.\r
518  */\r
519 #define Tgl_bits(lvalue, mask)  ((lvalue) ^=  (mask))\r
520 \r
521 /*! \brief Reads the bit-field of a value specified by a given bit-mask.\r
522  *\r
523  * \param value Value to read a bit-field from.\r
524  * \param mask  Bit-mask indicating the bit-field to read.\r
525  *\r
526  * \return Read bit-field.\r
527  */\r
528 #define Rd_bitfield( value, mask)           (Rd_bits( value, mask) >> ctz(mask))\r
529 \r
530 /*! \brief Writes the bit-field of a C lvalue specified by a given bit-mask.\r
531  *\r
532  * \param lvalue    C lvalue to write a bit-field to.\r
533  * \param mask      Bit-mask indicating the bit-field to write.\r
534  * \param bitfield  Bit-field to write.\r
535  *\r
536  * \return Resulting value with written bit-field.\r
537  */\r
538 #define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (U32)(bitfield) << ctz(mask)))\r
539 \r
540 //! @}\r
541 \r
542 \r
543 /*! \name Zero-Bit Counting\r
544  *\r
545  * Under GCC, __builtin_clz and __builtin_ctz behave like macros when\r
546  * applied to constant expressions (values known at compile time), so they are\r
547  * more optimized than the use of the corresponding assembly instructions and\r
548  * they can be used as constant expressions e.g. to initialize objects having\r
549  * static storage duration, and like the corresponding assembly instructions\r
550  * when applied to non-constant expressions (values unknown at compile time), so\r
551  * they are more optimized than an assembly periphrasis. Hence, clz and ctz\r
552  * ensure a possible and optimized behavior for both constant and non-constant\r
553  * expressions.\r
554  */\r
555 //! @{\r
556 \r
557 /*! \brief Counts the leading zero bits of the given value considered as a 32-bit integer.\r
558  *\r
559  * \param u Value of which to count the leading zero bits.\r
560  *\r
561  * \return The count of leading zero bits in \a u.\r
562  */\r
563 #if (defined __GNUC__) || (defined __CC_ARM)\r
564 #   define clz(u)              __builtin_clz(u)\r
565 #elif (defined __ICCARM__)\r
566 #   define clz(u)              __CLZ(u)\r
567 #else\r
568 #   define clz(u)              (((u) == 0)          ? 32 : \\r
569                                 ((u) & (1ul << 31)) ?  0 : \\r
570                                 ((u) & (1ul << 30)) ?  1 : \\r
571                                 ((u) & (1ul << 29)) ?  2 : \\r
572                                 ((u) & (1ul << 28)) ?  3 : \\r
573                                 ((u) & (1ul << 27)) ?  4 : \\r
574                                 ((u) & (1ul << 26)) ?  5 : \\r
575                                 ((u) & (1ul << 25)) ?  6 : \\r
576                                 ((u) & (1ul << 24)) ?  7 : \\r
577                                 ((u) & (1ul << 23)) ?  8 : \\r
578                                 ((u) & (1ul << 22)) ?  9 : \\r
579                                 ((u) & (1ul << 21)) ? 10 : \\r
580                                 ((u) & (1ul << 20)) ? 11 : \\r
581                                 ((u) & (1ul << 19)) ? 12 : \\r
582                                 ((u) & (1ul << 18)) ? 13 : \\r
583                                 ((u) & (1ul << 17)) ? 14 : \\r
584                                 ((u) & (1ul << 16)) ? 15 : \\r
585                                 ((u) & (1ul << 15)) ? 16 : \\r
586                                 ((u) & (1ul << 14)) ? 17 : \\r
587                                 ((u) & (1ul << 13)) ? 18 : \\r
588                                 ((u) & (1ul << 12)) ? 19 : \\r
589                                 ((u) & (1ul << 11)) ? 20 : \\r
590                                 ((u) & (1ul << 10)) ? 21 : \\r
591                                 ((u) & (1ul <<  9)) ? 22 : \\r
592                                 ((u) & (1ul <<  8)) ? 23 : \\r
593                                 ((u) & (1ul <<  7)) ? 24 : \\r
594                                 ((u) & (1ul <<  6)) ? 25 : \\r
595                                 ((u) & (1ul <<  5)) ? 26 : \\r
596                                 ((u) & (1ul <<  4)) ? 27 : \\r
597                                 ((u) & (1ul <<  3)) ? 28 : \\r
598                                 ((u) & (1ul <<  2)) ? 29 : \\r
599                                 ((u) & (1ul <<  1)) ? 30 : \\r
600                                 31)\r
601 #endif\r
602 \r
603 /*! \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.\r
604  *\r
605  * \param u Value of which to count the trailing zero bits.\r
606  *\r
607  * \return The count of trailing zero bits in \a u.\r
608  */\r
609 #if (defined __GNUC__) || (defined __CC_ARM)\r
610 #   define ctz(u)              __builtin_ctz(u)\r
611 #else\r
612 #   define ctz(u)              ((u) & (1ul <<  0) ?  0 : \\r
613                                 (u) & (1ul <<  1) ?  1 : \\r
614                                 (u) & (1ul <<  2) ?  2 : \\r
615                                 (u) & (1ul <<  3) ?  3 : \\r
616                                 (u) & (1ul <<  4) ?  4 : \\r
617                                 (u) & (1ul <<  5) ?  5 : \\r
618                                 (u) & (1ul <<  6) ?  6 : \\r
619                                 (u) & (1ul <<  7) ?  7 : \\r
620                                 (u) & (1ul <<  8) ?  8 : \\r
621                                 (u) & (1ul <<  9) ?  9 : \\r
622                                 (u) & (1ul << 10) ? 10 : \\r
623                                 (u) & (1ul << 11) ? 11 : \\r
624                                 (u) & (1ul << 12) ? 12 : \\r
625                                 (u) & (1ul << 13) ? 13 : \\r
626                                 (u) & (1ul << 14) ? 14 : \\r
627                                 (u) & (1ul << 15) ? 15 : \\r
628                                 (u) & (1ul << 16) ? 16 : \\r
629                                 (u) & (1ul << 17) ? 17 : \\r
630                                 (u) & (1ul << 18) ? 18 : \\r
631                                 (u) & (1ul << 19) ? 19 : \\r
632                                 (u) & (1ul << 20) ? 20 : \\r
633                                 (u) & (1ul << 21) ? 21 : \\r
634                                 (u) & (1ul << 22) ? 22 : \\r
635                                 (u) & (1ul << 23) ? 23 : \\r
636                                 (u) & (1ul << 24) ? 24 : \\r
637                                 (u) & (1ul << 25) ? 25 : \\r
638                                 (u) & (1ul << 26) ? 26 : \\r
639                                 (u) & (1ul << 27) ? 27 : \\r
640                                 (u) & (1ul << 28) ? 28 : \\r
641                                 (u) & (1ul << 29) ? 29 : \\r
642                                 (u) & (1ul << 30) ? 30 : \\r
643                                 (u) & (1ul << 31) ? 31 : \\r
644                                 32)\r
645 #endif\r
646 \r
647 //! @}\r
648 \r
649 \r
650 /*! \name Bit Reversing\r
651  */\r
652 //! @{\r
653 \r
654 /*! \brief Reverses the bits of \a u8.\r
655  *\r
656  * \param u8  U8 of which to reverse the bits.\r
657  *\r
658  * \return Value resulting from \a u8 with reversed bits.\r
659  */\r
660 #define bit_reverse8(u8)    ((U8)(bit_reverse32((U8)(u8)) >> 24))\r
661 \r
662 /*! \brief Reverses the bits of \a u16.\r
663  *\r
664  * \param u16 U16 of which to reverse the bits.\r
665  *\r
666  * \return Value resulting from \a u16 with reversed bits.\r
667  */\r
668 #define bit_reverse16(u16)  ((U16)(bit_reverse32((U16)(u16)) >> 16))\r
669 \r
670 /*! \brief Reverses the bits of \a u32.\r
671  *\r
672  * \param u32 U32 of which to reverse the bits.\r
673  *\r
674  * \return Value resulting from \a u32 with reversed bits.\r
675  */\r
676 #define bit_reverse32(u32)   __RBIT(u32)\r
677 \r
678 /*! \brief Reverses the bits of \a u64.\r
679  *\r
680  * \param u64 U64 of which to reverse the bits.\r
681  *\r
682  * \return Value resulting from \a u64 with reversed bits.\r
683  */\r
684 #define bit_reverse64(u64)  ((U64)(((U64)bit_reverse32((U64)(u64) >> 32)) |\\r
685                                    ((U64)bit_reverse32((U64)(u64)) << 32)))\r
686 \r
687 //! @}\r
688 \r
689 \r
690 /*! \name Alignment\r
691  */\r
692 //! @{\r
693 \r
694 /*! \brief Tests alignment of the number \a val with the \a n boundary.\r
695  *\r
696  * \param val Input value.\r
697  * \param n   Boundary.\r
698  *\r
699  * \return \c 1 if the number \a val is aligned with the \a n boundary, else \c 0.\r
700  */\r
701 #define Test_align(val, n     ) (!Tst_bits( val, (n) - 1     )   )\r
702 \r
703 /*! \brief Gets alignment of the number \a val with respect to the \a n boundary.\r
704  *\r
705  * \param val Input value.\r
706  * \param n   Boundary.\r
707  *\r
708  * \return Alignment of the number \a val with respect to the \a n boundary.\r
709  */\r
710 #define Get_align( val, n     ) (  Rd_bits( val, (n) - 1     )   )\r
711 \r
712 /*! \brief Sets alignment of the lvalue number \a lval to \a alg with respect to the \a n boundary.\r
713  *\r
714  * \param lval  Input/output lvalue.\r
715  * \param n     Boundary.\r
716  * \param alg   Alignment.\r
717  *\r
718  * \return New value of \a lval resulting from its alignment set to \a alg with respect to the \a n boundary.\r
719  */\r
720 #define Set_align(lval, n, alg) (  Wr_bits(lval, (n) - 1, alg)   )\r
721 \r
722 /*! \brief Aligns the number \a val with the upper \a n boundary.\r
723  *\r
724  * \param val Input value.\r
725  * \param n   Boundary.\r
726  *\r
727  * \return Value resulting from the number \a val aligned with the upper \a n boundary.\r
728  */\r
729 #define Align_up(  val, n     ) (((val) + ((n) - 1)) & ~((n) - 1))\r
730 \r
731 /*! \brief Aligns the number \a val with the lower \a n boundary.\r
732  *\r
733  * \param val Input value.\r
734  * \param n   Boundary.\r
735  *\r
736  * \return Value resulting from the number \a val aligned with the lower \a n boundary.\r
737  */\r
738 #define Align_down(val, n     ) ( (val)              & ~((n) - 1))\r
739 \r
740 //! @}\r
741 \r
742 \r
743 /*! \name Mathematics\r
744  *\r
745  * The same considerations as for clz and ctz apply here but GCC does not\r
746  * provide built-in functions to access the assembly instructions abs, min and\r
747  * max and it does not produce them by itself in most cases, so two sets of\r
748  * macros are defined here:\r
749  *   - Abs, Min and Max to apply to constant expressions (values known at\r
750  *     compile time);\r
751  *   - abs, min and max to apply to non-constant expressions (values unknown at\r
752  *     compile time), abs is found in stdlib.h.\r
753  */\r
754 //! @{\r
755 \r
756 /*! \brief Takes the absolute value of \a a.\r
757  *\r
758  * \param a Input value.\r
759  *\r
760  * \return Absolute value of \a a.\r
761  *\r
762  * \note More optimized if only used with values known at compile time.\r
763  */\r
764 #define Abs(a)              (((a) <  0 ) ? -(a) : (a))\r
765 \r
766 /*! \brief Takes the minimal value of \a a and \a b.\r
767  *\r
768  * \param a Input value.\r
769  * \param b Input value.\r
770  *\r
771  * \return Minimal value of \a a and \a b.\r
772  *\r
773  * \note More optimized if only used with values known at compile time.\r
774  */\r
775 #define Min(a, b)           (((a) < (b)) ?  (a) : (b))\r
776 \r
777 /*! \brief Takes the maximal value of \a a and \a b.\r
778  *\r
779  * \param a Input value.\r
780  * \param b Input value.\r
781  *\r
782  * \return Maximal value of \a a and \a b.\r
783  *\r
784  * \note More optimized if only used with values known at compile time.\r
785  */\r
786 #define Max(a, b)           (((a) > (b)) ?  (a) : (b))\r
787 \r
788 // abs() is already defined by stdlib.h\r
789 \r
790 /*! \brief Takes the minimal value of \a a and \a b.\r
791  *\r
792  * \param a Input value.\r
793  * \param b Input value.\r
794  *\r
795  * \return Minimal value of \a a and \a b.\r
796  *\r
797  * \note More optimized if only used with values unknown at compile time.\r
798  */\r
799 #define min(a, b)   Min(a, b)\r
800 \r
801 /*! \brief Takes the maximal value of \a a and \a b.\r
802  *\r
803  * \param a Input value.\r
804  * \param b Input value.\r
805  *\r
806  * \return Maximal value of \a a and \a b.\r
807  *\r
808  * \note More optimized if only used with values unknown at compile time.\r
809  */\r
810 #define max(a, b)   Max(a, b)\r
811 \r
812 //! @}\r
813 \r
814 \r
815 /*! \brief Calls the routine at address \a addr.\r
816  *\r
817  * It generates a long call opcode.\r
818  *\r
819  * For example, `Long_call(0x80000000)' generates a software reset on a UC3 if\r
820  * it is invoked from the CPU supervisor mode.\r
821  *\r
822  * \param addr  Address of the routine to call.\r
823  *\r
824  * \note It may be used as a long jump opcode in some special cases.\r
825  */\r
826 #define Long_call(addr)                   ((*(void (*)(void))(addr))())\r
827 \r
828 \r
829 /*! \name MCU Endianism Handling\r
830  * ARM is MCU little endianism.\r
831  */\r
832 //! @{\r
833 #define  MSB(u16)       (((U8  *)&(u16))[1]) //!< Most significant byte of \a u16.\r
834 #define  LSB(u16)       (((U8  *)&(u16))[0]) //!< Least significant byte of \a u16.\r
835 \r
836 #define  MSH(u32)       (((U16 *)&(u32))[1]) //!< Most significant half-word of \a u32.\r
837 #define  LSH(u32)       (((U16 *)&(u32))[0]) //!< Least significant half-word of \a u32.\r
838 #define  MSB0W(u32)     (((U8  *)&(u32))[3]) //!< Most significant byte of 1st rank of \a u32.\r
839 #define  MSB1W(u32)     (((U8  *)&(u32))[2]) //!< Most significant byte of 2nd rank of \a u32.\r
840 #define  MSB2W(u32)     (((U8  *)&(u32))[1]) //!< Most significant byte of 3rd rank of \a u32.\r
841 #define  MSB3W(u32)     (((U8  *)&(u32))[0]) //!< Most significant byte of 4th rank of \a u32.\r
842 #define  LSB3W(u32)     MSB0W(u32)           //!< Least significant byte of 4th rank of \a u32.\r
843 #define  LSB2W(u32)     MSB1W(u32)           //!< Least significant byte of 3rd rank of \a u32.\r
844 #define  LSB1W(u32)     MSB2W(u32)           //!< Least significant byte of 2nd rank of \a u32.\r
845 #define  LSB0W(u32)     MSB3W(u32)           //!< Least significant byte of 1st rank of \a u32.\r
846         \r
847 #define  MSW(u64)       (((U32 *)&(u64))[1]) //!< Most significant word of \a u64.\r
848 #define  LSW(u64)       (((U32 *)&(u64))[0]) //!< Least significant word of \a u64.\r
849 #define  MSH0(u64)      (((U16 *)&(u64))[3]) //!< Most significant half-word of 1st rank of \a u64.\r
850 #define  MSH1(u64)      (((U16 *)&(u64))[2]) //!< Most significant half-word of 2nd rank of \a u64.\r
851 #define  MSH2(u64)      (((U16 *)&(u64))[1]) //!< Most significant half-word of 3rd rank of \a u64.\r
852 #define  MSH3(u64)      (((U16 *)&(u64))[0]) //!< Most significant half-word of 4th rank of \a u64.\r
853 #define  LSH3(u64)      MSH0(u64)            //!< Least significant half-word of 4th rank of \a u64.\r
854 #define  LSH2(u64)      MSH1(u64)            //!< Least significant half-word of 3rd rank of \a u64.\r
855 #define  LSH1(u64)      MSH2(u64)            //!< Least significant half-word of 2nd rank of \a u64.\r
856 #define  LSH0(u64)      MSH3(u64)            //!< Least significant half-word of 1st rank of \a u64.\r
857 #define  MSB0D(u64)     (((U8  *)&(u64))[7]) //!< Most significant byte of 1st rank of \a u64.\r
858 #define  MSB1D(u64)     (((U8  *)&(u64))[6]) //!< Most significant byte of 2nd rank of \a u64.\r
859 #define  MSB2D(u64)     (((U8  *)&(u64))[5]) //!< Most significant byte of 3rd rank of \a u64.\r
860 #define  MSB3D(u64)     (((U8  *)&(u64))[4]) //!< Most significant byte of 4th rank of \a u64.\r
861 #define  MSB4D(u64)     (((U8  *)&(u64))[3]) //!< Most significant byte of 5th rank of \a u64.\r
862 #define  MSB5D(u64)     (((U8  *)&(u64))[2]) //!< Most significant byte of 6th rank of \a u64.\r
863 #define  MSB6D(u64)     (((U8  *)&(u64))[1]) //!< Most significant byte of 7th rank of \a u64.\r
864 #define  MSB7D(u64)     (((U8  *)&(u64))[0]) //!< Most significant byte of 8th rank of \a u64.\r
865 #define  LSB7D(u64)     MSB0D(u64)           //!< Least significant byte of 8th rank of \a u64.\r
866 #define  LSB6D(u64)     MSB1D(u64)           //!< Least significant byte of 7th rank of \a u64.\r
867 #define  LSB5D(u64)     MSB2D(u64)           //!< Least significant byte of 6th rank of \a u64.\r
868 #define  LSB4D(u64)     MSB3D(u64)           //!< Least significant byte of 5th rank of \a u64.\r
869 #define  LSB3D(u64)     MSB4D(u64)           //!< Least significant byte of 4th rank of \a u64.\r
870 #define  LSB2D(u64)     MSB5D(u64)           //!< Least significant byte of 3rd rank of \a u64.\r
871 #define  LSB1D(u64)     MSB6D(u64)           //!< Least significant byte of 2nd rank of \a u64.\r
872 #define  LSB0D(u64)     MSB7D(u64)           //!< Least significant byte of 1st rank of \a u64.\r
873 \r
874 #define  BE16(x)        Swap16(x)\r
875 #define  LE16(x)        (x)\r
876 \r
877 #define  le16_to_cpu(x) (x)\r
878 #define  cpu_to_le16(x) (x)\r
879 #define  LE16_TO_CPU(x) (x)\r
880 #define  CPU_TO_LE16(x) (x)\r
881 \r
882 #define  be16_to_cpu(x) Swap16(x)\r
883 #define  cpu_to_be16(x) Swap16(x)\r
884 #define  BE16_TO_CPU(x) Swap16(x)\r
885 #define  CPU_TO_BE16(x) Swap16(x)\r
886 \r
887 #define  le32_to_cpu(x) (x)\r
888 #define  cpu_to_le32(x) (x)\r
889 #define  LE32_TO_CPU(x) (x)\r
890 #define  CPU_TO_LE32(x) (x)\r
891 \r
892 #define  be32_to_cpu(x) swap32(x)\r
893 #define  cpu_to_be32(x) swap32(x)\r
894 #define  BE32_TO_CPU(x) swap32(x)\r
895 #define  CPU_TO_BE32(x) swap32(x)\r
896 //! @}\r
897 \r
898 \r
899 /*! \name Endianism Conversion\r
900  *\r
901  * The same considerations as for clz and ctz apply here but GCC's\r
902  * __builtin_bswap_32 and __builtin_bswap_64 do not behave like macros when\r
903  * applied to constant expressions, so two sets of macros are defined here:\r
904  *   - Swap16, Swap32 and Swap64 to apply to constant expressions (values known\r
905  *     at compile time);\r
906  *   - swap16, swap32 and swap64 to apply to non-constant expressions (values\r
907  *     unknown at compile time).\r
908  */\r
909 //! @{\r
910 \r
911 /*! \brief Toggles the endianism of \a u16 (by swapping its bytes).\r
912  *\r
913  * \param u16 U16 of which to toggle the endianism.\r
914  *\r
915  * \return Value resulting from \a u16 with toggled endianism.\r
916  *\r
917  * \note More optimized if only used with values known at compile time.\r
918  */\r
919 #define Swap16(u16) ((U16)(((U16)(u16) >> 8) |\\r
920                            ((U16)(u16) << 8)))\r
921 \r
922 /*! \brief Toggles the endianism of \a u32 (by swapping its bytes).\r
923  *\r
924  * \param u32 U32 of which to toggle the endianism.\r
925  *\r
926  * \return Value resulting from \a u32 with toggled endianism.\r
927  *\r
928  * \note More optimized if only used with values known at compile time.\r
929  */\r
930 #define Swap32(u32) ((U32)(((U32)Swap16((U32)(u32) >> 16)) |\\r
931                            ((U32)Swap16((U32)(u32)) << 16)))\r
932 \r
933 /*! \brief Toggles the endianism of \a u64 (by swapping its bytes).\r
934  *\r
935  * \param u64 U64 of which to toggle the endianism.\r
936  *\r
937  * \return Value resulting from \a u64 with toggled endianism.\r
938  *\r
939  * \note More optimized if only used with values known at compile time.\r
940  */\r
941 #define Swap64(u64) ((U64)(((U64)Swap32((U64)(u64) >> 32)) |\\r
942                            ((U64)Swap32((U64)(u64)) << 32)))\r
943 \r
944 /*! \brief Toggles the endianism of \a u16 (by swapping its bytes).\r
945  *\r
946  * \param u16 U16 of which to toggle the endianism.\r
947  *\r
948  * \return Value resulting from \a u16 with toggled endianism.\r
949  *\r
950  * \note More optimized if only used with values unknown at compile time.\r
951  */\r
952 #define swap16(u16) Swap16(u16)\r
953 \r
954 /*! \brief Toggles the endianism of \a u32 (by swapping its bytes).\r
955  *\r
956  * \param u32 U32 of which to toggle the endianism.\r
957  *\r
958  * \return Value resulting from \a u32 with toggled endianism.\r
959  *\r
960  * \note More optimized if only used with values unknown at compile time.\r
961  */\r
962 #if (defined __GNUC__)\r
963 #   define swap32(u32) ((U32)__builtin_bswap32((U32)(u32)))\r
964 #else\r
965 #   define swap32(u32) Swap32(u32)\r
966 #endif\r
967 \r
968 /*! \brief Toggles the endianism of \a u64 (by swapping its bytes).\r
969  *\r
970  * \param u64 U64 of which to toggle the endianism.\r
971  *\r
972  * \return Value resulting from \a u64 with toggled endianism.\r
973  *\r
974  * \note More optimized if only used with values unknown at compile time.\r
975  */\r
976 #if (defined __GNUC__)\r
977 #   define swap64(u64) ((U64)__builtin_bswap64((U64)(u64)))\r
978 #else\r
979 #   define swap64(u64) ((U64)(((U64)swap32((U64)(u64) >> 32)) |\\r
980                            ((U64)swap32((U64)(u64)) << 32)))\r
981 #endif\r
982 \r
983 //! @}\r
984 \r
985 \r
986 /*! \name Target Abstraction\r
987  */\r
988 //! @{\r
989 \r
990 #define _GLOBEXT_           extern      //!< extern storage-class specifier.\r
991 #define _CONST_TYPE_        const       //!< const type qualifier.\r
992 #define _MEM_TYPE_SLOW_                 //!< Slow memory type.\r
993 #define _MEM_TYPE_MEDFAST_              //!< Fairly fast memory type.\r
994 #define _MEM_TYPE_FAST_                 //!< Fast memory type.\r
995 \r
996 typedef U8                  Byte;       //!< 8-bit unsigned integer.\r
997 \r
998 #define memcmp_ram2ram      memcmp      //!< Target-specific memcmp of RAM to RAM.\r
999 #define memcmp_code2ram     memcmp      //!< Target-specific memcmp of RAM to NVRAM.\r
1000 #define memcpy_ram2ram      memcpy      //!< Target-specific memcpy from RAM to RAM.\r
1001 #define memcpy_code2ram     memcpy      //!< Target-specific memcpy from NVRAM to RAM.\r
1002 \r
1003 #define LSB0(u32)           LSB0W(u32)  //!< Least significant byte of 1st rank of \a u32.\r
1004 #define LSB1(u32)           LSB1W(u32)  //!< Least significant byte of 2nd rank of \a u32.\r
1005 #define LSB2(u32)           LSB2W(u32)  //!< Least significant byte of 3rd rank of \a u32.\r
1006 #define LSB3(u32)           LSB3W(u32)  //!< Least significant byte of 4th rank of \a u32.\r
1007 #define MSB3(u32)           MSB3W(u32)  //!< Most significant byte of 4th rank of \a u32.\r
1008 #define MSB2(u32)           MSB2W(u32)  //!< Most significant byte of 3rd rank of \a u32.\r
1009 #define MSB1(u32)           MSB1W(u32)  //!< Most significant byte of 2nd rank of \a u32.\r
1010 #define MSB0(u32)           MSB0W(u32)  //!< Most significant byte of 1st rank of \a u32.\r
1011 \r
1012 //! @}\r
1013 \r
1014 /**\r
1015  * \brief Calculate \f$ \left\lceil \frac{a}{b} \right\rceil \f$ using\r
1016  * integer arithmetic.\r
1017  *\r
1018  * \param a An integer\r
1019  * \param b Another integer\r
1020  *\r
1021  * \return (\a a / \a b) rounded up to the nearest integer.\r
1022  */\r
1023 #define div_ceil(a, b)      (((a) + (b) - 1) / (b))\r
1024 \r
1025 #endif  // #ifndef __ASSEMBLY__\r
1026 \r
1027 \r
1028 #if defined(__ICCARM__)\r
1029 #define SHORTENUM           __packed\r
1030 #elif defined(__GNUC__)\r
1031 #define SHORTENUM           __attribute__((packed))\r
1032 #endif\r
1033 \r
1034 #define FLASH_DECLARE(x)  const x\r
1035 #define FLASH_EXTERN(x) extern const x\r
1036 #define PGM_READ_BYTE(x) *(x)\r
1037 #define PGM_READ_WORD(x) *(x)\r
1038 #define MEMCPY_ENDIAN memcpy\r
1039 #define PGM_READ_BLOCK(dst, src, len) memcpy((dst), (src), (len))\r
1040 \r
1041 /* Converting of values from CPU endian to little endian. */\r
1042 #define CPU_ENDIAN_TO_LE16(x)   (x)\r
1043 #define CPU_ENDIAN_TO_LE32(x)   (x)\r
1044 #define CPU_ENDIAN_TO_LE64(x)   (x)\r
1045 \r
1046 /* Converting of values from little endian to CPU endian. */\r
1047 #define LE16_TO_CPU_ENDIAN(x)   (x)\r
1048 #define LE32_TO_CPU_ENDIAN(x)   (x)\r
1049 #define LE64_TO_CPU_ENDIAN(x)   (x)\r
1050 \r
1051 /* Converting of constants from CPU endian to little endian. */\r
1052 #define CCPU_ENDIAN_TO_LE16(x)  (x)\r
1053 #define CCPU_ENDIAN_TO_LE32(x)  (x)\r
1054 #define CCPU_ENDIAN_TO_LE64(x)  (x)\r
1055 \r
1056 #define ADDR_COPY_DST_SRC_16(dst, src)  ((dst) = (src))\r
1057 #define ADDR_COPY_DST_SRC_64(dst, src)  ((dst) = (src))\r
1058 \r
1059 /**\r
1060  * \}\r
1061  */\r
1062 \r
1063 #endif /* UTILS_COMPILER_H */\r