compiler.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef COMPILER_H
  2. #define COMPILER_H
  3. #ifndef __ASSEMBLY__
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <inttypes.h>
  7. #include <stdarg.h>
  8. #include <stdbool.h>
  9. /* Use builtin memcpy and memset optimizations */
  10. #define memset(s,c,n) __builtin_memset(s,c,n)
  11. #define memcpy(d,s,n) __builtin_memcpy(d,s,n)
  12. #define memmove(d,s,n) __builtin_memmove(d,s,n)
  13. extern void *__memcpy_aligned(void * __restrict,
  14. const void * __restrict, size_t);
  15. extern void *__memcpy_bytewise(volatile void * __restrict,
  16. const volatile void * __restrict, size_t);
  17. #define likely(x) __builtin_expect(!!(x), 1)
  18. #define unlikely(x) __builtin_expect(!!(x), 0)
  19. /* Handy composite pointer types */
  20. typedef union xptr {
  21. uint32_t *l;
  22. uint16_t *w;
  23. uint8_t *b;
  24. void *v;
  25. size_t a;
  26. } xptr_t;
  27. typedef union xcptr {
  28. const uint32_t *l;
  29. const uint16_t *w;
  30. const uint8_t *b;
  31. const void *v;
  32. size_t a;
  33. } xcptr_t;
  34. /* The container_of construct: if p is a pointer to member m of
  35. container class c, then return a pointer to the container of which
  36. *p is a member. */
  37. #ifndef container_of
  38. # define container_of(p, c, m) ((c *)((char *)(p) - offsetof(c,m)))
  39. #endif
  40. #define offset_diff(c, m1, m2) ((ptrdiff_t)offsetof(c,m2) - \
  41. (ptrdiff_t)offsetof(c,m1))
  42. #define min(a,b) (((a) < (b)) ? (a) : (b))
  43. #define max(a,b) (((a) > (b)) ? (a) : (b))
  44. #define ARRAY_SIZE (sizeof(a)/sizeof(a[0]))
  45. #define alignof(a) __alignof__(a)
  46. #define no_return void __attribute__((noreturn))
  47. #define ___section(s,a,...) __attribute__((__section__(s)))
  48. #define __hot __attribute__((__hot__))
  49. #define __cold __attribute__((__cold__))
  50. #define __aligned(x) __attribute__((__aligned__(x)))
  51. #define __unused __attribute__((__unused__))
  52. #define __must_inline __attribute__((__always_inline__))
  53. #define __noinline __attribute__ ((__noinline__))
  54. #define __constfunc __attribute__((__const__)
  55. #define __purefunc __attribute__((__pure__))
  56. #define __fmt_printf(fstr,farg) __attribute__((__format__(__printf__,fstr,farg)))
  57. #define __is_constant(expr) __builtin_constant_p(expr)
  58. #else /* __ASSEMBLY__ */
  59. #define ___section(s,a,...) .pushsection s, a, ## __VA_ARGS__
  60. #endif /* __ASSEMBLY__ */
  61. #define __text_hot ___section(".text.hot","ax")
  62. #define __rodata_hot ___section(".rodata.hot","a")
  63. #define __data_hot ___section(".data.hot","aw")
  64. #define __rwtext ___section(".rwtext","awx")
  65. #define __sdata ___section(".sdata","aw")
  66. #define __string_hot ___section(".rodata.hot.str","aMS")
  67. #define __sbss ___section(".sbss.hot","aw",@nobits)
  68. #define __bss_hot ___section(".bss.hot","aw",@nobits)
  69. #define __dram_text ___section(".dram.text","ax")
  70. #define __dram_rodata ___section(".dram.rodata","a")
  71. #define __dram_string ___section(".dram.rodata.str","aMS")
  72. #define __dram_data ___section(".dram.data","aw")
  73. #define __dram_bss ___section(".dram.bss","aw",@nobits)
  74. #define __dram_noinit ___section(".dram.noinit","aw",@nobits)
  75. #ifndef __ASSEMBLY__
  76. #define hotstr(x) ({ static __string_hot const char _str[] = (x); _str; })
  77. #endif /* __ASSEMBLY__ */
  78. #endif /* COMPILER_H */