4 * \brief Commonly used includes, types and macros.
\r
6 * Copyright (c) 2010-2012 Atmel Corporation. All rights reserved.
\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
13 * 1. Redistributions of source code must retain the above copyright notice,
\r
14 * this list of conditions and the following disclaimer.
\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
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
23 * 4. This software may only be redistributed and used in connection with an
\r
24 * Atmel microcontroller product.
\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
42 #ifndef UTILS_COMPILER_H
\r
43 #define UTILS_COMPILER_H
\r
46 * \defgroup group_sam_utils Compiler abstraction layer and code utilities
\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
55 #if (defined __ICCARM__)
\r
56 # include <intrinsics.h>
\r
60 #include "preprocessor.h"
\r
63 //_____ D E C L A R A T I O N S ____________________________________________
\r
65 #ifndef __ASSEMBLY__ // Not defined for assembling.
\r
68 #include <stdbool.h>
\r
73 /*! \name Compiler Keywords
\r
75 * Port of some keywords from GCC to IAR Embedded Workbench.
\r
79 #define __inline__ inline
\r
80 #define __volatile__
\r
87 * \brief Memory barrier
\r
89 #define barrier() __DMB()
\r
92 * \brief Emit the compiler pragma \a arg.
\r
94 * \param arg The pragma directive as it would appear after \e \#pragma
\r
95 * (i.e. not stringified).
\r
97 #define COMPILER_PRAGMA(arg) _Pragma(#arg)
\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
104 #define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment))
\r
107 * \def COMPILER_PACK_RESET()
\r
108 * \brief Set default alignment for subsequent struct and union
\r
111 #define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack())
\r
115 * \brief Set aligned boundary.
\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
124 * \brief Set word-aligned boundary.
\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
133 * \def __always_inline
\r
134 * \brief The function should always be inlined.
\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
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
148 /*! \brief This macro is used to test fatal errors.
\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
155 * \param expr Expression to evaluate and supposed to be nonzero.
\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
162 #undef TEST_SUITE_DEFINE_ASSERT_MACRO
\r
163 # define Assert(expr) \
\r
165 if (!(expr)) while (true);\
\r
169 # define Assert(expr) ((void) 0)
\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
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
190 #include "interrupt.h"
\r
192 /*! \name Usual Types
\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
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
219 /*! \name Status Types
\r
222 typedef bool Status_bool_t; //!< Boolean status.
\r
223 typedef U8 Status_t; //!< 8-bit-coded status.
\r
227 /*! \name Aliasing Aggregate Types
\r
264 //! Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers.
\r
277 //! Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.
\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
290 //! Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.
\r
303 //! Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.
\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
316 //! Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers.
\r
329 //! Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.
\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
342 //! Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.
\r
355 //! Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.
\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
370 #endif // #ifndef __ASSEMBLY__
\r
372 /*! \name Usual Constants
\r
377 #ifndef __cplusplus
\r
378 #if !defined(__bool_true_false_are_defined)
\r
390 #ifndef __ASSEMBLY__ // not for assembling.
\r
392 //! \name Optimization Control
\r
397 * \brief The expression \a exp is likely to be true
\r
400 # define likely(exp) (exp)
\r
404 * \def unlikely(exp)
\r
405 * \brief The expression \a exp is unlikely to be true
\r
408 # define unlikely(exp) (exp)
\r
412 * \def is_constant(exp)
\r
413 * \brief Determine if an expression evaluates to a constant value.
\r
415 * \param exp Any expression
\r
417 * \return true if \a exp is constant, false otherwise.
\r
419 #if (defined __GNUC__) || (defined __CC_ARM)
\r
420 # define is_constant(exp) __builtin_constant_p(exp)
\r
422 # define is_constant(exp) (0)
\r
427 /*! \name Bit-Field Handling
\r
431 /*! \brief Reads the bits of a value specified by a given bit-mask.
\r
433 * \param value Value to read bits from.
\r
434 * \param mask Bit-mask indicating bits to read.
\r
436 * \return Read bits.
\r
438 #define Rd_bits( value, mask) ((value) & (mask))
\r
440 /*! \brief Writes the bits of a C lvalue specified by a given bit-mask.
\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
446 * \return Resulting value with written bits.
\r
448 #define Wr_bits(lvalue, mask, bits) ((lvalue) = ((lvalue) & ~(mask)) |\
\r
449 ((bits ) & (mask)))
\r
451 /*! \brief Tests the bits of a value specified by a given bit-mask.
\r
453 * \param value Value of which to test bits.
\r
454 * \param mask Bit-mask indicating bits to test.
\r
456 * \return \c 1 if at least one of the tested bits is set, else \c 0.
\r
458 #define Tst_bits( value, mask) (Rd_bits(value, mask) != 0)
\r
460 /*! \brief Clears the bits of a C lvalue specified by a given bit-mask.
\r
462 * \param lvalue C lvalue of which to clear bits.
\r
463 * \param mask Bit-mask indicating bits to clear.
\r
465 * \return Resulting value with cleared bits.
\r
467 #define Clr_bits(lvalue, mask) ((lvalue) &= ~(mask))
\r
469 /*! \brief Sets the bits of a C lvalue specified by a given bit-mask.
\r
471 * \param lvalue C lvalue of which to set bits.
\r
472 * \param mask Bit-mask indicating bits to set.
\r
474 * \return Resulting value with set bits.
\r
476 #define Set_bits(lvalue, mask) ((lvalue) |= (mask))
\r
478 /*! \brief Toggles the bits of a C lvalue specified by a given bit-mask.
\r
480 * \param lvalue C lvalue of which to toggle bits.
\r
481 * \param mask Bit-mask indicating bits to toggle.
\r
483 * \return Resulting value with toggled bits.
\r
485 #define Tgl_bits(lvalue, mask) ((lvalue) ^= (mask))
\r
487 /*! \brief Reads the bit-field of a value specified by a given bit-mask.
\r
489 * \param value Value to read a bit-field from.
\r
490 * \param mask Bit-mask indicating the bit-field to read.
\r
492 * \return Read bit-field.
\r
494 #define Rd_bitfield( value, mask) (Rd_bits( value, mask) >> ctz(mask))
\r
496 /*! \brief Writes the bit-field of a C lvalue specified by a given bit-mask.
\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
502 * \return Resulting value with written bit-field.
\r
504 #define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (U32)(bitfield) << ctz(mask)))
\r
509 /*! \name Zero-Bit Counting
\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
523 /*! \brief Counts the leading zero bits of the given value considered as a 32-bit integer.
\r
525 * \param u Value of which to count the leading zero bits.
\r
527 * \return The count of leading zero bits in \a u.
\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
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
569 /*! \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
\r
571 * \param u Value of which to count the trailing zero bits.
\r
573 * \return The count of trailing zero bits in \a u.
\r
575 #if (defined __GNUC__) || (defined __CC_ARM)
\r
576 # define ctz(u) __builtin_ctz(u)
\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
616 /*! \name Bit Reversing
\r
620 /*! \brief Reverses the bits of \a u8.
\r
622 * \param u8 U8 of which to reverse the bits.
\r
624 * \return Value resulting from \a u8 with reversed bits.
\r
626 #define bit_reverse8(u8) ((U8)(bit_reverse32((U8)(u8)) >> 24))
\r
628 /*! \brief Reverses the bits of \a u16.
\r
630 * \param u16 U16 of which to reverse the bits.
\r
632 * \return Value resulting from \a u16 with reversed bits.
\r
634 #define bit_reverse16(u16) ((U16)(bit_reverse32((U16)(u16)) >> 16))
\r
636 /*! \brief Reverses the bits of \a u32.
\r
638 * \param u32 U32 of which to reverse the bits.
\r
640 * \return Value resulting from \a u32 with reversed bits.
\r
642 #define bit_reverse32(u32) __RBIT(u32)
\r
644 /*! \brief Reverses the bits of \a u64.
\r
646 * \param u64 U64 of which to reverse the bits.
\r
648 * \return Value resulting from \a u64 with reversed bits.
\r
650 #define bit_reverse64(u64) ((U64)(((U64)bit_reverse32((U64)(u64) >> 32)) |\
\r
651 ((U64)bit_reverse32((U64)(u64)) << 32)))
\r
656 /*! \name Alignment
\r
660 /*! \brief Tests alignment of the number \a val with the \a n boundary.
\r
662 * \param val Input value.
\r
663 * \param n Boundary.
\r
665 * \return \c 1 if the number \a val is aligned with the \a n boundary, else \c 0.
\r
667 #define Test_align(val, n ) (!Tst_bits( val, (n) - 1 ) )
\r
669 /*! \brief Gets alignment of the number \a val with respect to the \a n boundary.
\r
671 * \param val Input value.
\r
672 * \param n Boundary.
\r
674 * \return Alignment of the number \a val with respect to the \a n boundary.
\r
676 #define Get_align( val, n ) ( Rd_bits( val, (n) - 1 ) )
\r
678 /*! \brief Sets alignment of the lvalue number \a lval to \a alg with respect to the \a n boundary.
\r
680 * \param lval Input/output lvalue.
\r
681 * \param n Boundary.
\r
682 * \param alg Alignment.
\r
684 * \return New value of \a lval resulting from its alignment set to \a alg with respect to the \a n boundary.
\r
686 #define Set_align(lval, n, alg) ( Wr_bits(lval, (n) - 1, alg) )
\r
688 /*! \brief Aligns the number \a val with the upper \a n boundary.
\r
690 * \param val Input value.
\r
691 * \param n Boundary.
\r
693 * \return Value resulting from the number \a val aligned with the upper \a n boundary.
\r
695 #define Align_up( val, n ) (((val) + ((n) - 1)) & ~((n) - 1))
\r
697 /*! \brief Aligns the number \a val with the lower \a n boundary.
\r
699 * \param val Input value.
\r
700 * \param n Boundary.
\r
702 * \return Value resulting from the number \a val aligned with the lower \a n boundary.
\r
704 #define Align_down(val, n ) ( (val) & ~((n) - 1))
\r
709 /*! \name Mathematics
\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
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
722 /*! \brief Takes the absolute value of \a a.
\r
724 * \param a Input value.
\r
726 * \return Absolute value of \a a.
\r
728 * \note More optimized if only used with values known at compile time.
\r
730 #define Abs(a) (((a) < 0 ) ? -(a) : (a))
\r
732 /*! \brief Takes the minimal value of \a a and \a b.
\r
734 * \param a Input value.
\r
735 * \param b Input value.
\r
737 * \return Minimal value of \a a and \a b.
\r
739 * \note More optimized if only used with values known at compile time.
\r
741 #define Min(a, b) (((a) < (b)) ? (a) : (b))
\r
743 /*! \brief Takes the maximal value of \a a and \a b.
\r
745 * \param a Input value.
\r
746 * \param b Input value.
\r
748 * \return Maximal value of \a a and \a b.
\r
750 * \note More optimized if only used with values known at compile time.
\r
752 #define Max(a, b) (((a) > (b)) ? (a) : (b))
\r
754 // abs() is already defined by stdlib.h
\r
756 /*! \brief Takes the minimal value of \a a and \a b.
\r
758 * \param a Input value.
\r
759 * \param b Input value.
\r
761 * \return Minimal value of \a a and \a b.
\r
763 * \note More optimized if only used with values unknown at compile time.
\r
765 #define min(a, b) Min(a, b)
\r
767 /*! \brief Takes the maximal value of \a a and \a b.
\r
769 * \param a Input value.
\r
770 * \param b Input value.
\r
772 * \return Maximal value of \a a and \a b.
\r
774 * \note More optimized if only used with values unknown at compile time.
\r
776 #define max(a, b) Max(a, b)
\r
781 /*! \brief Calls the routine at address \a addr.
\r
783 * It generates a long call opcode.
\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
788 * \param addr Address of the routine to call.
\r
790 * \note It may be used as a long jump opcode in some special cases.
\r
792 #define Long_call(addr) ((*(void (*)(void))(addr))())
\r
795 /*! \name MCU Endianism Handling
\r
796 * ARM is MCU little endianism.
\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
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
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
840 #define BE16(x) Swap16(x)
\r
841 #define LE16(x) (x)
\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
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
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
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
865 /*! \name Endianism Conversion
\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
877 /*! \brief Toggles the endianism of \a u16 (by swapping its bytes).
\r
879 * \param u16 U16 of which to toggle the endianism.
\r
881 * \return Value resulting from \a u16 with toggled endianism.
\r
883 * \note More optimized if only used with values known at compile time.
\r
885 #define Swap16(u16) ((U16)(((U16)(u16) >> 8) |\
\r
886 ((U16)(u16) << 8)))
\r
888 /*! \brief Toggles the endianism of \a u32 (by swapping its bytes).
\r
890 * \param u32 U32 of which to toggle the endianism.
\r
892 * \return Value resulting from \a u32 with toggled endianism.
\r
894 * \note More optimized if only used with values known at compile time.
\r
896 #define Swap32(u32) ((U32)(((U32)Swap16((U32)(u32) >> 16)) |\
\r
897 ((U32)Swap16((U32)(u32)) << 16)))
\r
899 /*! \brief Toggles the endianism of \a u64 (by swapping its bytes).
\r
901 * \param u64 U64 of which to toggle the endianism.
\r
903 * \return Value resulting from \a u64 with toggled endianism.
\r
905 * \note More optimized if only used with values known at compile time.
\r
907 #define Swap64(u64) ((U64)(((U64)Swap32((U64)(u64) >> 32)) |\
\r
908 ((U64)Swap32((U64)(u64)) << 32)))
\r
910 /*! \brief Toggles the endianism of \a u16 (by swapping its bytes).
\r
912 * \param u16 U16 of which to toggle the endianism.
\r
914 * \return Value resulting from \a u16 with toggled endianism.
\r
916 * \note More optimized if only used with values unknown at compile time.
\r
918 #define swap16(u16) Swap16(u16)
\r
920 /*! \brief Toggles the endianism of \a u32 (by swapping its bytes).
\r
922 * \param u32 U32 of which to toggle the endianism.
\r
924 * \return Value resulting from \a u32 with toggled endianism.
\r
926 * \note More optimized if only used with values unknown at compile time.
\r
928 #if (defined __GNUC__)
\r
929 # define swap32(u32) ((U32)__builtin_bswap32((U32)(u32)))
\r
931 # define swap32(u32) Swap32(u32)
\r
934 /*! \brief Toggles the endianism of \a u64 (by swapping its bytes).
\r
936 * \param u64 U64 of which to toggle the endianism.
\r
938 * \return Value resulting from \a u64 with toggled endianism.
\r
940 * \note More optimized if only used with values unknown at compile time.
\r
942 #if (defined __GNUC__)
\r
943 # define swap64(u64) ((U64)__builtin_bswap64((U64)(u64)))
\r
945 # define swap64(u64) ((U64)(((U64)swap32((U64)(u64) >> 32)) |\
\r
946 ((U64)swap32((U64)(u64)) << 32)))
\r
952 /*! \name Target Abstraction
\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
962 typedef U8 Byte; //!< 8-bit unsigned integer.
\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
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
981 * \brief Calculate \f$ \left\lceil \frac{a}{b} \right\rceil \f$ using
\r
982 * integer arithmetic.
\r
984 * \param a An integer
\r
985 * \param b Another integer
\r
987 * \return (\a a / \a b) rounded up to the nearest integer.
\r
989 #define div_ceil(a, b) (((a) + (b) - 1) / (b))
\r
991 #endif // #ifndef __ASSEMBLY__
\r
997 #endif /* UTILS_COMPILER_H */
\r