4 * \brief Commonly used includes, types and macros.
\r
6 * Copyright (c) 2010-2012 Atmel Corporation. All rights reserved.
\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
15 * 1. Redistributions of source code must retain the above copyright notice,
\r
16 * this list of conditions and the following disclaimer.
\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
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
25 * 4. This software may only be redistributed and used in connection with an
\r
26 * Atmel microcontroller product.
\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
44 #ifndef UTILS_COMPILER_H
\r
45 #define UTILS_COMPILER_H
\r
48 * \defgroup group_sam_utils Compiler abstraction layer and code utilities
\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
57 #if (defined __ICCARM__)
\r
58 # include <intrinsics.h>
\r
62 #include "preprocessor.h"
\r
66 //_____ D E C L A R A T I O N S ____________________________________________
\r
68 #ifndef __ASSEMBLY__ // Not defined for assembling.
\r
71 #include <stdbool.h>
\r
76 /*! \name Compiler Keywords
\r
78 * Port of some keywords from GCC to IAR Embedded Workbench.
\r
82 #define __inline__ inline
\r
83 #define __volatile__
\r
90 * \brief Marking \a v as a unused parameter or value.
\r
92 #define UNUSED(v) (void)(v)
\r
96 * \brief Marking \a v as a unused parameter or value.
\r
98 #define unused(v) do { (void)(v); } while(0)
\r
102 * \brief Memory barrier
\r
104 #define barrier() __DMB()
\r
107 * \brief Emit the compiler pragma \a arg.
\r
109 * \param arg The pragma directive as it would appear after \e \#pragma
\r
110 * (i.e. not stringified).
\r
112 #define COMPILER_PRAGMA(arg) _Pragma(#arg)
\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
119 #define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment))
\r
122 * \def COMPILER_PACK_RESET()
\r
123 * \brief Set default alignment for subsequent struct and union
\r
126 #define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack())
\r
130 * \brief Set aligned boundary.
\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
139 * \brief Set word-aligned boundary.
\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
148 * \def __always_inline
\r
149 * \brief The function should always be inlined.
\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
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
163 /*! \brief This macro is used to test fatal errors.
\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
170 * \param expr Expression to evaluate and supposed to be nonzero.
\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
177 #undef TEST_SUITE_DEFINE_ASSERT_MACRO
\r
178 # define Assert(expr) \
\r
180 if (!(expr)) while (true);\
\r
184 # define Assert(expr) ((void) 0)
\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
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
205 #include "interrupt.h"
\r
207 /*! \name Usual Types
\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
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
234 /*! \name Status Types
\r
237 typedef bool Status_bool_t; //!< Boolean status.
\r
238 typedef U8 Status_t; //!< 8-bit-coded status.
\r
242 /*! \name Aliasing Aggregate Types
\r
279 //! Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers.
\r
292 //! Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.
\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
305 //! Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.
\r
318 //! Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.
\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
331 //! Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers.
\r
344 //! Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.
\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
357 //! Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.
\r
370 //! Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.
\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
385 #endif // #ifndef __ASSEMBLY__
\r
387 /*! \name Usual Constants
\r
392 #ifndef __cplusplus
\r
393 #if !defined(__bool_true_false_are_defined)
\r
405 #ifndef __ASSEMBLY__ // not for assembling.
\r
407 //! \name Optimization Control
\r
412 * \brief The expression \a exp is likely to be true
\r
415 # define likely(exp) (exp)
\r
419 * \def unlikely(exp)
\r
420 * \brief The expression \a exp is unlikely to be true
\r
423 # define unlikely(exp) (exp)
\r
427 * \def is_constant(exp)
\r
428 * \brief Determine if an expression evaluates to a constant value.
\r
430 * \param exp Any expression
\r
432 * \return true if \a exp is constant, false otherwise.
\r
434 #if (defined __GNUC__) || (defined __CC_ARM)
\r
435 # define is_constant(exp) __builtin_constant_p(exp)
\r
437 # define is_constant(exp) (0)
\r
442 /*! \name Bit-Field Handling
\r
446 /*! \brief Reads the bits of a value specified by a given bit-mask.
\r
448 * \param value Value to read bits from.
\r
449 * \param mask Bit-mask indicating bits to read.
\r
451 * \return Read bits.
\r
453 #define Rd_bits( value, mask) ((value) & (mask))
\r
455 /*! \brief Writes the bits of a C lvalue specified by a given bit-mask.
\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
461 * \return Resulting value with written bits.
\r
463 #define Wr_bits(lvalue, mask, bits) ((lvalue) = ((lvalue) & ~(mask)) |\
\r
464 ((bits ) & (mask)))
\r
466 /*! \brief Tests the bits of a value specified by a given bit-mask.
\r
468 * \param value Value of which to test bits.
\r
469 * \param mask Bit-mask indicating bits to test.
\r
471 * \return \c 1 if at least one of the tested bits is set, else \c 0.
\r
473 #define Tst_bits( value, mask) (Rd_bits(value, mask) != 0)
\r
475 /*! \brief Clears the bits of a C lvalue specified by a given bit-mask.
\r
477 * \param lvalue C lvalue of which to clear bits.
\r
478 * \param mask Bit-mask indicating bits to clear.
\r
480 * \return Resulting value with cleared bits.
\r
482 #define Clr_bits(lvalue, mask) ((lvalue) &= ~(mask))
\r
484 /*! \brief Sets the bits of a C lvalue specified by a given bit-mask.
\r
486 * \param lvalue C lvalue of which to set bits.
\r
487 * \param mask Bit-mask indicating bits to set.
\r
489 * \return Resulting value with set bits.
\r
491 #define Set_bits(lvalue, mask) ((lvalue) |= (mask))
\r
493 /*! \brief Toggles the bits of a C lvalue specified by a given bit-mask.
\r
495 * \param lvalue C lvalue of which to toggle bits.
\r
496 * \param mask Bit-mask indicating bits to toggle.
\r
498 * \return Resulting value with toggled bits.
\r
500 #define Tgl_bits(lvalue, mask) ((lvalue) ^= (mask))
\r
502 /*! \brief Reads the bit-field of a value specified by a given bit-mask.
\r
504 * \param value Value to read a bit-field from.
\r
505 * \param mask Bit-mask indicating the bit-field to read.
\r
507 * \return Read bit-field.
\r
509 #define Rd_bitfield( value, mask) (Rd_bits( value, mask) >> ctz(mask))
\r
511 /*! \brief Writes the bit-field of a C lvalue specified by a given bit-mask.
\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
517 * \return Resulting value with written bit-field.
\r
519 #define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (U32)(bitfield) << ctz(mask)))
\r
524 /*! \name Zero-Bit Counting
\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
538 /*! \brief Counts the leading zero bits of the given value considered as a 32-bit integer.
\r
540 * \param u Value of which to count the leading zero bits.
\r
542 * \return The count of leading zero bits in \a u.
\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
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
584 /*! \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
\r
586 * \param u Value of which to count the trailing zero bits.
\r
588 * \return The count of trailing zero bits in \a u.
\r
590 #if (defined __GNUC__) || (defined __CC_ARM)
\r
591 # define ctz(u) __builtin_ctz(u)
\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
631 /*! \name Bit Reversing
\r
635 /*! \brief Reverses the bits of \a u8.
\r
637 * \param u8 U8 of which to reverse the bits.
\r
639 * \return Value resulting from \a u8 with reversed bits.
\r
641 #define bit_reverse8(u8) ((U8)(bit_reverse32((U8)(u8)) >> 24))
\r
643 /*! \brief Reverses the bits of \a u16.
\r
645 * \param u16 U16 of which to reverse the bits.
\r
647 * \return Value resulting from \a u16 with reversed bits.
\r
649 #define bit_reverse16(u16) ((U16)(bit_reverse32((U16)(u16)) >> 16))
\r
651 /*! \brief Reverses the bits of \a u32.
\r
653 * \param u32 U32 of which to reverse the bits.
\r
655 * \return Value resulting from \a u32 with reversed bits.
\r
657 #define bit_reverse32(u32) __RBIT(u32)
\r
659 /*! \brief Reverses the bits of \a u64.
\r
661 * \param u64 U64 of which to reverse the bits.
\r
663 * \return Value resulting from \a u64 with reversed bits.
\r
665 #define bit_reverse64(u64) ((U64)(((U64)bit_reverse32((U64)(u64) >> 32)) |\
\r
666 ((U64)bit_reverse32((U64)(u64)) << 32)))
\r
671 /*! \name Alignment
\r
675 /*! \brief Tests alignment of the number \a val with the \a n boundary.
\r
677 * \param val Input value.
\r
678 * \param n Boundary.
\r
680 * \return \c 1 if the number \a val is aligned with the \a n boundary, else \c 0.
\r
682 #define Test_align(val, n ) (!Tst_bits( val, (n) - 1 ) )
\r
684 /*! \brief Gets alignment of the number \a val with respect to the \a n boundary.
\r
686 * \param val Input value.
\r
687 * \param n Boundary.
\r
689 * \return Alignment of the number \a val with respect to the \a n boundary.
\r
691 #define Get_align( val, n ) ( Rd_bits( val, (n) - 1 ) )
\r
693 /*! \brief Sets alignment of the lvalue number \a lval to \a alg with respect to the \a n boundary.
\r
695 * \param lval Input/output lvalue.
\r
696 * \param n Boundary.
\r
697 * \param alg Alignment.
\r
699 * \return New value of \a lval resulting from its alignment set to \a alg with respect to the \a n boundary.
\r
701 #define Set_align(lval, n, alg) ( Wr_bits(lval, (n) - 1, alg) )
\r
703 /*! \brief Aligns the number \a val with the upper \a n boundary.
\r
705 * \param val Input value.
\r
706 * \param n Boundary.
\r
708 * \return Value resulting from the number \a val aligned with the upper \a n boundary.
\r
710 #define Align_up( val, n ) (((val) + ((n) - 1)) & ~((n) - 1))
\r
712 /*! \brief Aligns the number \a val with the lower \a n boundary.
\r
714 * \param val Input value.
\r
715 * \param n Boundary.
\r
717 * \return Value resulting from the number \a val aligned with the lower \a n boundary.
\r
719 #define Align_down(val, n ) ( (val) & ~((n) - 1))
\r
724 /*! \name Mathematics
\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
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
737 /*! \brief Takes the absolute value of \a a.
\r
739 * \param a Input value.
\r
741 * \return Absolute value of \a a.
\r
743 * \note More optimized if only used with values known at compile time.
\r
745 #define Abs(a) (((a) < 0 ) ? -(a) : (a))
\r
747 /*! \brief Takes the minimal value of \a a and \a b.
\r
749 * \param a Input value.
\r
750 * \param b Input value.
\r
752 * \return Minimal value of \a a and \a b.
\r
754 * \note More optimized if only used with values known at compile time.
\r
756 #define Min(a, b) (((a) < (b)) ? (a) : (b))
\r
758 /*! \brief Takes the maximal value of \a a and \a b.
\r
760 * \param a Input value.
\r
761 * \param b Input value.
\r
763 * \return Maximal value of \a a and \a b.
\r
765 * \note More optimized if only used with values known at compile time.
\r
767 #define Max(a, b) (((a) > (b)) ? (a) : (b))
\r
769 // abs() is already defined by stdlib.h
\r
771 /*! \brief Takes the minimal value of \a a and \a b.
\r
773 * \param a Input value.
\r
774 * \param b Input value.
\r
776 * \return Minimal value of \a a and \a b.
\r
778 * \note More optimized if only used with values unknown at compile time.
\r
780 #define min(a, b) Min(a, b)
\r
782 /*! \brief Takes the maximal value of \a a and \a b.
\r
784 * \param a Input value.
\r
785 * \param b Input value.
\r
787 * \return Maximal value of \a a and \a b.
\r
789 * \note More optimized if only used with values unknown at compile time.
\r
791 #define max(a, b) Max(a, b)
\r
796 /*! \brief Calls the routine at address \a addr.
\r
798 * It generates a long call opcode.
\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
803 * \param addr Address of the routine to call.
\r
805 * \note It may be used as a long jump opcode in some special cases.
\r
807 #define Long_call(addr) ((*(void (*)(void))(addr))())
\r
810 /*! \name MCU Endianism Handling
\r
811 * ARM is MCU little endianism.
\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
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
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
855 #define BE16(x) Swap16(x)
\r
856 #define LE16(x) (x)
\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
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
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
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
880 /*! \name Endianism Conversion
\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
892 /*! \brief Toggles the endianism of \a u16 (by swapping its bytes).
\r
894 * \param u16 U16 of which to toggle the endianism.
\r
896 * \return Value resulting from \a u16 with toggled endianism.
\r
898 * \note More optimized if only used with values known at compile time.
\r
900 #define Swap16(u16) ((U16)(((U16)(u16) >> 8) |\
\r
901 ((U16)(u16) << 8)))
\r
903 /*! \brief Toggles the endianism of \a u32 (by swapping its bytes).
\r
905 * \param u32 U32 of which to toggle the endianism.
\r
907 * \return Value resulting from \a u32 with toggled endianism.
\r
909 * \note More optimized if only used with values known at compile time.
\r
911 #define Swap32(u32) ((U32)(((U32)Swap16((U32)(u32) >> 16)) |\
\r
912 ((U32)Swap16((U32)(u32)) << 16)))
\r
914 /*! \brief Toggles the endianism of \a u64 (by swapping its bytes).
\r
916 * \param u64 U64 of which to toggle the endianism.
\r
918 * \return Value resulting from \a u64 with toggled endianism.
\r
920 * \note More optimized if only used with values known at compile time.
\r
922 #define Swap64(u64) ((U64)(((U64)Swap32((U64)(u64) >> 32)) |\
\r
923 ((U64)Swap32((U64)(u64)) << 32)))
\r
925 /*! \brief Toggles the endianism of \a u16 (by swapping its bytes).
\r
927 * \param u16 U16 of which to toggle the endianism.
\r
929 * \return Value resulting from \a u16 with toggled endianism.
\r
931 * \note More optimized if only used with values unknown at compile time.
\r
933 #define swap16(u16) Swap16(u16)
\r
935 /*! \brief Toggles the endianism of \a u32 (by swapping its bytes).
\r
937 * \param u32 U32 of which to toggle the endianism.
\r
939 * \return Value resulting from \a u32 with toggled endianism.
\r
941 * \note More optimized if only used with values unknown at compile time.
\r
943 #if (defined __GNUC__)
\r
944 # define swap32(u32) ((U32)__builtin_bswap32((U32)(u32)))
\r
946 # define swap32(u32) Swap32(u32)
\r
949 /*! \brief Toggles the endianism of \a u64 (by swapping its bytes).
\r
951 * \param u64 U64 of which to toggle the endianism.
\r
953 * \return Value resulting from \a u64 with toggled endianism.
\r
955 * \note More optimized if only used with values unknown at compile time.
\r
957 #if (defined __GNUC__)
\r
958 # define swap64(u64) ((U64)__builtin_bswap64((U64)(u64)))
\r
960 # define swap64(u64) ((U64)(((U64)swap32((U64)(u64) >> 32)) |\
\r
961 ((U64)swap32((U64)(u64)) << 32)))
\r
967 /*! \name Target Abstraction
\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
977 typedef U8 Byte; //!< 8-bit unsigned integer.
\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
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
996 * \brief Calculate \f$ \left\lceil \frac{a}{b} \right\rceil \f$ using
\r
997 * integer arithmetic.
\r
999 * \param a An integer
\r
1000 * \param b Another integer
\r
1002 * \return (\a a / \a b) rounded up to the nearest integer.
\r
1004 #define div_ceil(a, b) (((a) + (b) - 1) / (b))
\r
1006 #endif // #ifndef __ASSEMBLY__
\r
1012 #endif /* UTILS_COMPILER_H */
\r