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