compiler.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 mempcpy(d,s,n) __builtin_mempcpy(d,s,n)
  13. #define memmove(d,s,n) __builtin_memmove(d,s,n)
  14. extern volatile void *memcpy_bytewise(volatile void *dst,
  15. const volatile void *src, size_t len);
  16. /*
  17. * The odd argument order allows memcpy() and mempcpy() to be implemented
  18. * as tail calls
  19. */
  20. extern void *__memxcpy_aligned(void *retval,
  21. const void * restrict src, size_t len,
  22. void * restrict dst);
  23. extern void *__memxcpy_bytewise(void *retval,
  24. const volatile void * restrict src, size_t len,
  25. void * restrict dst);
  26. #define likely(x) __builtin_expect(!!(x), 1)
  27. #define unlikely(x) __builtin_expect(!!(x), 0)
  28. /* Handy composite pointer types */
  29. typedef union xptr {
  30. uint32_t *l;
  31. uint16_t *w;
  32. uint8_t *b;
  33. void *v;
  34. size_t a;
  35. } xptr_t;
  36. typedef union xcptr {
  37. const uint32_t *l;
  38. const uint16_t *w;
  39. const uint8_t *b;
  40. const void *v;
  41. size_t a;
  42. } xcptr_t;
  43. /* The container_of construct: if p is a pointer to member m of
  44. container class c, then return a pointer to the container of which
  45. *p is a member. */
  46. #ifndef container_of
  47. # define container_of(p, c, m) ((c *)((char *)(p) - offsetof(c,m)))
  48. #endif
  49. #define offset_diff(c, m1, m2) ((ptrdiff_t)offsetof(c,m2) - \
  50. (ptrdiff_t)offsetof(c,m1))
  51. #define min(a,b) (((a) < (b)) ? (a) : (b))
  52. #define max(a,b) (((a) > (b)) ? (a) : (b))
  53. #define ARRAY_SIZE (sizeof(a)/sizeof(a[0]))
  54. #define alignof(a) __alignof__(a)
  55. #define no_return void __attribute__((noreturn))
  56. #define ___section(s,a,...) __attribute__((__section__(s)))
  57. #define __hot __attribute__((__hot__))
  58. #define __cold __attribute__((__cold__))
  59. #define __aligned(x) __attribute__((__aligned__(x)))
  60. #define __unused __attribute__((__unused__))
  61. #define __must_inline __attribute__((__always_inline__))
  62. #define __noinline __attribute__ ((__noinline__))
  63. #define __constfunc __attribute__((__const__))
  64. #define __purefunc __attribute__((__pure__))
  65. #define __fmt_printf(fstr,farg) __attribute__((__format__(__printf__,fstr,farg)))
  66. #define __is_constant(expr) __builtin_constant_p(expr)
  67. #else /* __ASSEMBLY__ */
  68. #define ___section(s,a,...) .pushsection s, a, ## __VA_ARGS__
  69. #endif /* __ASSEMBLY__ */
  70. #define __text_hot ___section(".text.hot","ax")
  71. #define __rodata_hot ___section(".rodata.hot","a")
  72. #define __data_hot ___section(".data.hot","aw")
  73. #define __rwtext ___section(".rwtext","awx")
  74. #define __sdata ___section(".sdata","aw")
  75. #define __string_hot ___section(".rodata.hot.str","aMS")
  76. #define __sbss ___section(".sbss.hot","aw",@nobits)
  77. #define __bss_hot ___section(".bss.hot","aw",@nobits)
  78. #define __esplink_head ___section(".dram.esplink.head","a",@nobits)
  79. #define __esplink ___section(".dram.esplink","a",@nobits)
  80. #define __dram_text ___section(".dram.text","ax")
  81. #define __dram_rodata ___section(".dram.rodata","a")
  82. #define __dram_string ___section(".dram.rodata.str","aMS")
  83. #define __dram_data ___section(".dram.data","aw")
  84. #define __dram_bss ___section(".dram.bss","aw",@nobits)
  85. #define __dram_noinit ___section(".dram.noinit","aw",@nobits)
  86. #ifndef __ASSEMBLY__
  87. #define hotstr(x) ({ static __string_hot const char _str[] = (x); _str; })
  88. #endif /* __ASSEMBLY__ */
  89. #endif /* COMPILER_H */