compiler.h
Go to the documentation of this file.00001
00038 #ifndef UTILS_COMPILER_H
00039 #define UTILS_COMPILER_H
00040
00041 #if defined(__GNUC__)
00042 # include <avr/io.h>
00043 #elif defined(__ICCAVR__)
00044 # include <ioavr.h>
00045 # include <intrinsics.h>
00046 #else
00047 # error Unsupported compiler.
00048 #endif
00049
00050 #include <stdbool.h>
00051 #include <stdint.h>
00052 #include <stddef.h>
00053 #include <stdlib.h>
00054
00055
00056
00057 #ifdef __ICCAVR__
00058
00062
00063 #define __asm__ asm
00064 #define __inline__ inline
00065 #define __volatile__
00066
00067 #endif
00068
00073 #ifdef __GNUC__
00074 # define barrier() asm volatile("" ::: "memory")
00075 #else
00076 # define barrier() asm ("")
00077 #endif
00078
00085 #define COMPILER_PRAGMA(arg) _Pragma(#arg)
00086
00087
00088
00089
00090 #define COMPILER_PACK_RESET(alignment)
00091 #define COMPILER_PACK_SET(alignment)
00092
00096 #if (defined __GNUC__)
00097 #define COMPILER_WORD_ALIGNED __attribute__((__aligned__(2)))
00098 #elif (defined __ICCAVR__)
00099 #define COMPILER_WORD_ALIGNED COMPILER_PRAGMA(data_alignment = 2)
00100 #endif
00101
00102
00103
00106
00107 typedef unsigned char Bool;
00108 #ifndef __cplusplus
00109 #if !defined(__bool_true_false_are_defined)
00110 typedef unsigned char bool;
00111 #endif
00112 #endif
00113 typedef signed char S8 ;
00114 typedef unsigned char U8 ;
00115 typedef signed short int S16;
00116 typedef unsigned short int U16;
00117 typedef unsigned short int le16_t;
00118 typedef unsigned short int be16_t;
00119 typedef signed long int S32;
00120 typedef unsigned long int U32;
00121 typedef uint32_t le32_t;
00122 typedef uint32_t be32_t;
00123 typedef signed long long int S64;
00124 typedef unsigned long long int U64;
00125 typedef float F32;
00126 typedef double F64;
00127 typedef uint16_t iram_size_t;
00129
00130
00133
00134 typedef Bool Status_bool_t;
00135 typedef U8 Status_t;
00136
00137
00138
00141
00142
00144 typedef union
00145 {
00146 S16 s16 ;
00147 U16 u16 ;
00148 S8 s8 [2];
00149 U8 u8 [2];
00150 } Union16;
00151
00153 typedef union
00154 {
00155 S32 s32 ;
00156 U32 u32 ;
00157 S16 s16[2];
00158 U16 u16[2];
00159 S8 s8 [4];
00160 U8 u8 [4];
00161 } Union32;
00162
00164 typedef union
00165 {
00166 S64 s64 ;
00167 U64 u64 ;
00168 S32 s32[2];
00169 U32 u32[2];
00170 S16 s16[4];
00171 U16 u16[4];
00172 S8 s8 [8];
00173 U8 u8 [8];
00174 } Union64;
00175
00177 typedef union
00178 {
00179 S64 *s64ptr;
00180 U64 *u64ptr;
00181 S32 *s32ptr;
00182 U32 *u32ptr;
00183 S16 *s16ptr;
00184 U16 *u16ptr;
00185 S8 *s8ptr ;
00186 U8 *u8ptr ;
00187 } UnionPtr;
00188
00190 typedef union
00191 {
00192 volatile S64 *s64ptr;
00193 volatile U64 *u64ptr;
00194 volatile S32 *s32ptr;
00195 volatile U32 *u32ptr;
00196 volatile S16 *s16ptr;
00197 volatile U16 *u16ptr;
00198 volatile S8 *s8ptr ;
00199 volatile U8 *u8ptr ;
00200 } UnionVPtr;
00201
00203 typedef union
00204 {
00205 const S64 *s64ptr;
00206 const U64 *u64ptr;
00207 const S32 *s32ptr;
00208 const U32 *u32ptr;
00209 const S16 *s16ptr;
00210 const U16 *u16ptr;
00211 const S8 *s8ptr ;
00212 const U8 *u8ptr ;
00213 } UnionCPtr;
00214
00216 typedef union
00217 {
00218 const volatile S64 *s64ptr;
00219 const volatile U64 *u64ptr;
00220 const volatile S32 *s32ptr;
00221 const volatile U32 *u32ptr;
00222 const volatile S16 *s16ptr;
00223 const volatile U16 *u16ptr;
00224 const volatile S8 *s8ptr ;
00225 const volatile U8 *u8ptr ;
00226 } UnionCVPtr;
00227
00229 typedef struct
00230 {
00231 S64 *s64ptr;
00232 U64 *u64ptr;
00233 S32 *s32ptr;
00234 U32 *u32ptr;
00235 S16 *s16ptr;
00236 U16 *u16ptr;
00237 S8 *s8ptr ;
00238 U8 *u8ptr ;
00239 } StructPtr;
00240
00242 typedef struct
00243 {
00244 volatile S64 *s64ptr;
00245 volatile U64 *u64ptr;
00246 volatile S32 *s32ptr;
00247 volatile U32 *u32ptr;
00248 volatile S16 *s16ptr;
00249 volatile U16 *u16ptr;
00250 volatile S8 *s8ptr ;
00251 volatile U8 *u8ptr ;
00252 } StructVPtr;
00253
00255 typedef struct
00256 {
00257 const S64 *s64ptr;
00258 const U64 *u64ptr;
00259 const S32 *s32ptr;
00260 const U32 *u32ptr;
00261 const S16 *s16ptr;
00262 const U16 *u16ptr;
00263 const S8 *s8ptr ;
00264 const U8 *u8ptr ;
00265 } StructCPtr;
00266
00268 typedef struct
00269 {
00270 const volatile S64 *s64ptr;
00271 const volatile U64 *u64ptr;
00272 const volatile S32 *s32ptr;
00273 const volatile U32 *u32ptr;
00274 const volatile S16 *s16ptr;
00275 const volatile U16 *u16ptr;
00276 const volatile S8 *s8ptr ;
00277 const volatile U8 *u8ptr ;
00278 } StructCVPtr;
00279
00281
00282
00283
00284
00287
00288 #define DISABLE 0
00289 #define ENABLE 1
00290 #define DISABLED 0
00291 #define ENABLED 1
00292 #define OFF 0
00293 #define ON 1
00294 #define FALSE 0
00295 #define TRUE 1
00296 #ifndef __cplusplus
00297 #if !defined(__bool_true_false_are_defined)
00298 #define false FALSE
00299 #define true TRUE
00300 #endif
00301 #endif
00302 #define KO 0
00303 #define OK 1
00304 #define PASS 0
00305 #define FAIL 1
00306 #define LOW 0
00307 #define HIGH 1
00308 #define CLR 0
00309 #define SET 1
00310
00311
00312
00314
00315
00328 #ifndef ERROR_FUNC
00329 # define ERROR_FUNC(name, msg) \
00330 extern int name(void)
00331 #endif
00332
00334
00336
00337
00339 ERROR_FUNC(compiler_demux_bad_size, "Invalid parameter size");
00340
00362 #define compiler_demux_size(size, func, ...) \
00363 (((size) == 1) ? func##8(__VA_ARGS__) : \
00364 ((size) == 2) ? func##16(__VA_ARGS__) : \
00365 ((size) == 4) ? func##32(__VA_ARGS__) : \
00366 compiler_demux_bad_size())
00367
00369
00378 #if (defined __GNUC__)
00379 #define __always_inline inline __attribute__((__always_inline__))
00380 #elif (defined __ICCAVR__)
00381 #define __always_inline _Pragma("inline=forced")
00382 #endif
00383
00385
00386
00391 #ifndef likely
00392 # define likely(exp) (exp)
00393 #endif
00394
00399 #ifndef unlikely
00400 # define unlikely(exp) (exp)
00401 #endif
00402
00411 #ifdef __GNUC__
00412 # define is_constant(exp) __builtin_constant_p(exp)
00413 #else
00414 # define is_constant(exp) (0)
00415 #endif
00416
00418
00421 #include "bit_handling/clz_ctz.h"
00423
00431 #define Rd_bits( value, mask) ((value)&(mask))
00432
00441 #define Wr_bits(lvalue, mask, bits) ((lvalue) = ((lvalue) & ~(mask)) |\
00442 ((bits ) & (mask)))
00443
00451 #define Tst_bits( value, mask) (Rd_bits(value, mask) != 0)
00452
00460 #define Clr_bits(lvalue, mask) ((lvalue) &= ~(mask))
00461
00469 #define Set_bits(lvalue, mask) ((lvalue) |= (mask))
00470
00478 #define Tgl_bits(lvalue, mask) ((lvalue) ^= (mask))
00479
00487 #define Rd_bitfield( value,mask) (Rd_bits( value, (uint32_t)mask) >> ctz(mask))
00488
00497 #define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (uint32_t)(bitfield) << ctz(mask)))
00498
00500
00501
00509 #ifdef _ASSERT_ENABLE_
00510 #define Assert(expr) \
00511 {\
00512 if (!(expr)) while (TRUE);\
00513 }
00514 #else
00515 #define Assert(expr)
00516 #endif
00517
00520
00521
00528 #define bit_reverse8(u8) ((U8)(bit_reverse32((U8)(u8)) >> 24))
00529
00536 #define bit_reverse16(u16) ((U16)(bit_reverse32((U16)(u16)) >> 16))
00537
00544 #if (defined __GNUC__)
00545 #define bit_reverse32(u32) \
00546 (\
00547 {\
00548 unsigned int __value = (U32)(u32);\
00549 __asm__ ("brev\t%0" : "+r" (__value) : : "cc");\
00550 (U32)__value;\
00551 }\
00552 )
00553 #elif (defined __ICCAVR__)
00554 #define bit_reverse32(u32) ((U32)__bit_reverse((U32)(u32)))
00555 #endif
00556
00563 #define bit_reverse64(u64) ((U64)(((U64)bit_reverse32((U64)(u64) >> 32)) |\
00564 ((U64)bit_reverse32((U64)(u64)) << 32)))
00565
00567
00570
00576 int_fast8_t ilog2_undefined(void);
00577
00585 __always_inline static inline int_fast8_t ilog2(uint32_t x)
00586 {
00587 if (is_constant(x))
00588 return ((x) & (1ULL << 31) ? 31 :
00589 (x) & (1ULL << 30) ? 30 :
00590 (x) & (1ULL << 29) ? 29 :
00591 (x) & (1ULL << 28) ? 28 :
00592 (x) & (1ULL << 27) ? 27 :
00593 (x) & (1ULL << 26) ? 26 :
00594 (x) & (1ULL << 25) ? 25 :
00595 (x) & (1ULL << 24) ? 24 :
00596 (x) & (1ULL << 23) ? 23 :
00597 (x) & (1ULL << 22) ? 22 :
00598 (x) & (1ULL << 21) ? 21 :
00599 (x) & (1ULL << 20) ? 20 :
00600 (x) & (1ULL << 19) ? 19 :
00601 (x) & (1ULL << 18) ? 18 :
00602 (x) & (1ULL << 17) ? 17 :
00603 (x) & (1ULL << 16) ? 16 :
00604 (x) & (1ULL << 15) ? 15 :
00605 (x) & (1ULL << 14) ? 14 :
00606 (x) & (1ULL << 13) ? 13 :
00607 (x) & (1ULL << 12) ? 12 :
00608 (x) & (1ULL << 11) ? 11 :
00609 (x) & (1ULL << 10) ? 10 :
00610 (x) & (1ULL << 9) ? 9 :
00611 (x) & (1ULL << 8) ? 8 :
00612 (x) & (1ULL << 7) ? 7 :
00613 (x) & (1ULL << 6) ? 6 :
00614 (x) & (1ULL << 5) ? 5 :
00615 (x) & (1ULL << 4) ? 4 :
00616 (x) & (1ULL << 3) ? 3 :
00617 (x) & (1ULL << 2) ? 2 :
00618 (x) & (1ULL << 1) ? 1 :
00619 (x) & (1ULL << 0) ? 0 :
00620 ilog2_undefined());
00621
00622 return 31 - clz(x);
00623 }
00624
00626
00629
00630
00638 #define Test_align(val, n ) (!Tst_bits( val, (n) - 1 ) )
00639
00647 #define Get_align( val, n ) ( Rd_bits( val, (n) - 1 ) )
00648
00657 #define Set_align(lval, n, alg) ( Wr_bits(lval, (n) - 1, alg) )
00658
00666 #define Align_up( val, n ) (((val) + ((n) - 1)) & ~((n) - 1))
00667
00675 #define Align_down(val, n ) ( (val) & ~((n) - 1))
00676
00678
00679
00684
00685
00694 #define Abs(a) (((a) < 0 ) ? -(a) : (a))
00695 #ifndef abs
00696 #define abs(a) Abs(a)
00697 #endif
00698
00708 #define Min(a, b) (((a) < (b)) ? (a) : (b))
00709 #define min(a, b) Min(a, b)
00710
00720 #define Max(a, b) (((a) > (b)) ? (a) : (b))
00721 #define max(a, b) Max(a, b)
00722
00724
00725
00737 #define Long_call(addr) ((*(void (*)(void))(addr))())
00738
00741
00742
00749 #if (defined __GNUC__)
00750 #define Get_system_register(sysreg) __builtin_mfsr(sysreg)
00751 #elif (defined __ICCAVR__)
00752 #define Get_system_register(sysreg) __get_system_register(sysreg)
00753 #endif
00754
00760 #if (defined __GNUC__)
00761 #define Set_system_register(sysreg, value) __builtin_mtsr(sysreg, value)
00762 #elif (defined __ICCAVR__)
00763 #define Set_system_register(sysreg, value) __set_system_register(sysreg, value)
00764 #endif
00765
00767
00770
00771
00778 #if (defined __GNUC__)
00779 #define Get_debug_register(dbgreg) __builtin_mfdr(dbgreg)
00780 #elif (defined __ICCAVR__)
00781 #define Get_debug_register(dbgreg) __get_debug_register(dbgreg)
00782 #endif
00783
00789 #if (defined __GNUC__)
00790 #define Set_debug_register(dbgreg, value) __builtin_mtdr(dbgreg, value)
00791 #elif (defined __ICCAVR__)
00792 #define Set_debug_register(dbgreg, value) __set_debug_register(dbgreg, value)
00793 #endif
00794
00796
00797
00801
00802 #define MSB(u16) (((uint8_t* )&u16)[1])
00803 #define LSB(u16) (((uint8_t* )&u16)[0])
00804
00805 #define MSW(u32) (((uint16_t*)&u32)[1])
00806 #define LSW(u32) (((uint16_t*)&u32)[0])
00807 #define MSB0W(u32) (((U8 *)&(u32))[3]) //!< Most significant byte of 1st rank of \a u32.
00808 #define MSB1W(u32) (((U8 *)&(u32))[2]) //!< Most significant byte of 2nd rank of \a u32.
00809 #define MSB2W(u32) (((U8 *)&(u32))[1]) //!< Most significant byte of 3rd rank of \a u32.
00810 #define MSB3W(u32) (((U8 *)&(u32))[0]) //!< Most significant byte of 4th rank of \a u32.
00811 #define LSB3W(u32) MSB0W(u32) //!< Least significant byte of 4th rank of \a u32.
00812 #define LSB2W(u32) MSB1W(u32) //!< Least significant byte of 3rd rank of \a u32.
00813 #define LSB1W(u32) MSB2W(u32) //!< Least significant byte of 2nd rank of \a u32.
00814 #define LSB0W(u32) MSB3W(u32) //!< Least significant byte of 1st rank of \a u32.
00815
00816 #define MSB0(u32) (((uint8_t* )&u32)[3])
00817 #define MSB1(u32) (((uint8_t* )&u32)[2])
00818 #define MSB2(u32) (((uint8_t* )&u32)[1])
00819 #define MSB3(u32) (((uint8_t* )&u32)[0])
00820 #define LSB0(u32) MSB3(u32)
00821 #define LSB1(u32) MSB2(u32)
00822 #define LSB2(u32) MSB1(u32)
00823 #define LSB3(u32) MSB0(u32)
00824
00825 #define LE16(x) (x)
00826 #define le16_to_cpu(x) (x)
00827 #define cpu_to_le16(x) (x)
00828 #define LE16_TO_CPU(x) (x)
00829 #define CPU_TO_LE16(x) (x)
00830
00831 #define BE16(x) Swap16(x)
00832 #define be16_to_cpu(x) swap16(x)
00833 #define cpu_to_be16(x) swap16(x)
00834 #define BE16_TO_CPU(x) Swap16(x)
00835 #define CPU_TO_BE16(x) Swap16(x)
00836
00837 #define LE32(x) (x)
00838 #define le32_to_cpu(x) (x)
00839 #define cpu_to_le32(x) (x)
00840 #define LE32_TO_CPU(x) (x)
00841 #define CPU_TO_LE32(x) (x)
00842
00843 #define BE32(x) Swap32(x)
00844 #define be32_to_cpu(x) swap32(x)
00845 #define cpu_to_be32(x) swap32(x)
00846 #define BE32_TO_CPU(x) Swap32(x)
00847 #define CPU_TO_BE32(x) Swap32(x)
00848
00849
00850
00852
00853
00864
00865
00874 #define Swap16(u16) ((U16)(((U16)(u16) >> 8) |\
00875 ((U16)(u16) << 8)))
00876
00885 #define Swap32(u32) ((U32)(((U32)Swap16((U32)(u32) >> 16)) |\
00886 ((U32)Swap16((U32)(u32)) << 16)))
00887
00896 #define Swap64(u64) ((U64)(((U64)Swap32((U64)(u64) >> 32)) |\
00897 ((U64)Swap32((U64)(u64)) << 32)))
00898
00907 #define swap16(u16) Swap16(u16)
00908
00917 #define swap32(u32) Swap32(u32)
00918
00927 #define swap64(u64) ((U64)(((U64)swap32((U64)(u64) >> 32)) |\
00928 ((U64)swap32((U64)(u64)) << 32)))
00929
00931
00932
00935
00936
00937 #define _GLOBEXT_ extern //!< extern storage-class specifier.
00938 #define _CONST_TYPE_ const //!< const type qualifier.
00939 #define _MEM_TYPE_SLOW_ //!< Slow memory type.
00940 #define _MEM_TYPE_MEDFAST_ //!< Fairly fast memory type.
00941 #define _MEM_TYPE_FAST_ //!< Fast memory type.
00942
00943 typedef U8 Byte;
00944
00945 #define memcmp_ram2ram memcmp //!< Target-specific memcmp of RAM to RAM.
00946 #define memcmp_code2ram memcmp //!< Target-specific memcmp of RAM to NVRAM.
00947 #define memcpy_ram2ram memcpy //!< Target-specific memcpy from RAM to RAM.
00948 #define memcpy_code2ram memcpy //!< Target-specific memcpy from NVRAM to RAM.
00949
00951
00952 #include "preprocessor.h"
00953 #include "progmem.h"
00954 #include "interrupt.h"
00955
00956 #endif // UTILS_COMPILER_H