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