00001
00038 #ifndef COMPILER_H_INCLUDED
00039 #define COMPILER_H_INCLUDED
00040
00041 #include <stdint.h>
00042
00056
00057 #if defined(__CHECKER__)
00058 # include <compiler/sparse.h>
00059 #elif defined(__GNUC__)
00060 # include <compiler/gcc.h>
00061 #elif defined(__ICCAVR32__) || defined(__ICCAVR__)
00062 # include <compiler/iar.h>
00063 #endif
00064
00066
00067
00079 #ifndef __bitwise
00080 # define __bitwise
00081 #endif
00082
00090 #ifndef __virtual
00091 # define __virtual
00092 #endif
00093
00103 #ifndef __physical
00104 # define __physical
00105 #endif
00106
00117 #ifndef __force
00118 # define __force
00119 #endif
00120
00121
00123
00124
00133 #if !defined(__noreturn) || defined(__DOXYGEN__)
00134 # define __noreturn
00135 #endif
00136
00144 #if !defined(__must_check) || defined(__DOXYGEN__)
00145 # define __must_check
00146 #endif
00147
00158 #if !defined(__used) || defined(__DOXYGEN__)
00159 # define __used
00160 #endif
00161
00170 #ifndef __always_inline
00171 # define __always_inline inline
00172 #endif
00173
00181 #ifndef __nonnull
00182 # define __nonnull(...)
00183 #endif
00184
00192 #ifndef __printf_format
00193 # define __printf_format(fmt_index, first_arg_index)
00194 #endif
00195
00212 #ifndef compiler_align_data
00213 # define compiler_align_data(byte_alignment)
00214 #endif
00215
00217
00219
00220
00225 #ifndef likely
00226 # define likely(exp) (exp)
00227 #endif
00228
00233 #ifndef unlikely
00234 # define unlikely(exp) (exp)
00235 #endif
00236
00245 #ifndef barrier
00246 # define barrier() do { } while (0)
00247 # warning Compiler does not support barrier() -- expect breakage!
00248 #endif
00249
00258 #ifndef is_constant
00259 # define is_constant(exp) (0)
00260 #endif
00261
00263
00265
00266
00273 #define COMPILER_PRAGMA(arg) _Pragma(#arg)
00274
00280 #ifndef COMPILER_PACK_SET
00281 # define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment))
00282 #endif
00283
00289 #ifndef COMPILER_PACK_RESET
00290 # define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack())
00291 #endif
00292
00304 #ifndef ERROR_FUNC
00305 # define ERROR_FUNC(name, msg) \
00306 extern int name(void)
00307 #endif
00308
00310
00311 ERROR_FUNC(compiler_priv_bad_size, "Invalid parameter size");
00312
00313 #define compiler_priv_demux_size(size, func, ...) \
00314 (((size) == 1) ? func##8(__VA_ARGS__) : \
00315 ((size) == 2) ? func##16(__VA_ARGS__) : \
00316 ((size) == 4) ? func##32(__VA_ARGS__) : \
00317 compiler_priv_bad_size())
00318
00325
00331 #ifndef compiler_ctz
00332 static inline int_fast8_t compiler_priv_ctz8(uint8_t x)
00333 {
00334 int_fast8_t bit = 0;
00335
00336 if (!(x & 0x0f)) {
00337 bit += 4;
00338 x >>= 4;
00339 }
00340 if (!(x & 0x03)) {
00341 bit += 2;
00342 x >>= 2;
00343 }
00344 if (!(x & 0x01))
00345 bit++;
00346
00347 return bit;
00348 }
00349
00350 static inline int_fast8_t compiler_priv_ctz16(uint16_t x)
00351 {
00352 int_fast8_t bit = 0;
00353
00354 if (!(x & 0x00ff)) {
00355 bit += 8;
00356 x >>= 8;
00357 }
00358
00359 return bit + compiler_priv_ctz8(x);
00360 }
00361
00362 static inline int_fast8_t compiler_priv_ctz32(uint32_t x)
00363 {
00364 int_fast8_t bit = 0;
00365
00366 if (!(x & 0x0000ffff)) {
00367 bit += 16;
00368 x >>= 16;
00369 }
00370
00371 return bit + compiler_priv_ctz16(x);
00372 }
00373
00374 #define compiler_ctz(x) \
00375 compiler_priv_demux_size(sizeof(x), compiler_priv_ctz, (x))
00376 #endif
00377
00383 #ifndef compiler_clz
00384 static inline int_fast8_t compiler_priv_clz8(uint8_t x)
00385 {
00386 int_fast8_t bit = 0;
00387
00388 if (x & 0xf0)
00389 x >>= 4;
00390 else
00391 bit += 4;
00392
00393 if (x & 0x0c)
00394 x >>= 2;
00395 else
00396 bit += 2;
00397
00398 if (!(x & 0x02))
00399 bit++;
00400
00401 return bit;
00402 }
00403
00404 static inline int_fast8_t compiler_priv_clz16(uint16_t x)
00405 {
00406 int_fast8_t bit = 0;
00407
00408 if (x & 0xff00)
00409 x >>= 8;
00410 else
00411 bit += 8;
00412
00413 return bit + compiler_priv_clz8(x);
00414 }
00415
00416 static inline int_fast8_t compiler_priv_clz32(uint32_t x)
00417 {
00418 int_fast8_t bit = 0;
00419
00420 if (x & 0xffff0000)
00421 x >>= 16;
00422 else
00423 bit += 16;
00424
00425 return bit + compiler_priv_clz16(x);
00426 }
00427
00428 #define compiler_clz(x) \
00429 compiler_priv_demux_size(sizeof(x), compiler_priv_clz, (x))
00430 #endif
00431
00437 #ifndef compiler_brev
00438 static inline uint8_t compiler_priv_brev8(uint8_t x)
00439 {
00440
00441 x = ((x >> 1) & 0x55) | ((x & 0x55) << 1);
00442
00443 x = ((x >> 2) & 0x33) | ((x & 0x33) << 2);
00444
00445 x = ((x >> 4) & 0x0f) | ((x & 0x0f) << 4);
00446
00447 return x;
00448 }
00449
00450 static inline uint16_t compiler_priv_brev16(uint16_t x)
00451 {
00452
00453 x = ((x >> 1) & 0x5555) | ((x & 0x5555) << 1);
00454
00455 x = ((x >> 2) & 0x3333) | ((x & 0x3333) << 2);
00456
00457 x = ((x >> 4) & 0x0f0f) | ((x & 0x0f0f) << 4);
00458
00459 x = ((x >> 8) & 0x00ff) | ((x & 0x00ff) << 8);
00460
00461 return x;
00462 }
00463
00464 static inline uint32_t compiler_priv_brev32(uint32_t x)
00465 {
00466
00467 x = ((x >> 1) & 0x55555555) | ((x & 0x55555555) << 1);
00468
00469 x = ((x >> 2) & 0x33333333) | ((x & 0x33333333) << 2);
00470
00471 x = ((x >> 4) & 0x0f0f0f0f) | ((x & 0x0f0f0f0f) << 4);
00472
00473 x = ((x >> 8) & 0x00ff00ff) | ((x & 0x00ff00ff) << 8);
00474
00475 x = (x >> 16) | (x << 16);
00476
00477 return x;
00478 }
00479
00480 #define compiler_brev(x) \
00481 compiler_priv_demux_size(sizeof(x), compiler_priv_brev, (x))
00482 #endif
00483
00484 static inline int8_t compiler_min_s8(int8_t a, int8_t b)
00485 {
00486 return (a < b) ? a : b;
00487 }
00488
00489 static inline int16_t compiler_min_s16(int16_t a, int16_t b)
00490 {
00491 return (a < b) ? a : b;
00492 }
00493
00494 static inline int32_t compiler_min_s32(int32_t a, int32_t b)
00495 {
00496 return (a < b) ? a : b;
00497 }
00498
00499 static inline int64_t compiler_min_s64(int64_t a, int64_t b)
00500 {
00501 return (a < b) ? a : b;
00502 }
00503
00504 static inline uint8_t compiler_min_u8(uint8_t a, uint8_t b)
00505 {
00506 return (a < b) ? a : b;
00507 }
00508
00509 static inline uint16_t compiler_min_u16(uint16_t a, uint16_t b)
00510 {
00511 return (a < b) ? a : b;
00512 }
00513
00514 static inline uint32_t compiler_min_u32(uint32_t a, uint32_t b)
00515 {
00516 return (a < b) ? a : b;
00517 }
00518
00519 static inline uint64_t compiler_min_u64(uint64_t a, uint64_t b)
00520 {
00521 return (a < b) ? a : b;
00522 }
00523
00524 static inline int8_t compiler_max_s8(int8_t a, int8_t b)
00525 {
00526 return (a > b) ? a : b;
00527 }
00528
00529 static inline int16_t compiler_max_s16(int16_t a, int16_t b)
00530 {
00531 return (a > b) ? a : b;
00532 }
00533
00534 static inline int32_t compiler_max_s32(int32_t a, int32_t b)
00535 {
00536 return (a > b) ? a : b;
00537 }
00538
00539 static inline int64_t compiler_max_s64(int64_t a, int64_t b)
00540 {
00541 return (a > b) ? a : b;
00542 }
00543
00544 static inline uint8_t compiler_max_u8(uint8_t a, uint8_t b)
00545 {
00546 return (a > b) ? a : b;
00547 }
00548
00549 static inline uint16_t compiler_max_u16(uint16_t a, uint16_t b)
00550 {
00551 return (a > b) ? a : b;
00552 }
00553
00554 static inline uint32_t compiler_max_u32(uint32_t a, uint32_t b)
00555 {
00556 return (a > b) ? a : b;
00557 }
00558
00559 static inline uint64_t compiler_max_u64(uint64_t a, uint64_t b)
00560 {
00561 return (a > b) ? a : b;
00562 }
00563
00565
00567
00568
00585
00587
00588 #endif