compiler.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Keep all the ugly #ifdef for system stuff here
  3. */
  4. #ifndef __COMPILER_H__
  5. #define __COMPILER_H__
  6. #include <stddef.h>
  7. #ifdef USE_HOSTCC
  8. #if defined(__BEOS__) || \
  9. defined(__NetBSD__) || \
  10. defined(__FreeBSD__) || \
  11. defined(__sun__) || \
  12. defined(__APPLE__)
  13. # include <inttypes.h>
  14. #elif defined(__linux__) || defined(__WIN32__) || defined(__MINGW32__) || defined(__OpenBSD__)
  15. # include <stdint.h>
  16. #endif
  17. #include <errno.h>
  18. #include <stdlib.h>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #if !defined(__WIN32__) && !defined(__MINGW32__)
  23. # include <sys/mman.h>
  24. #endif
  25. /* Not all systems (like Windows) has this define, and yes
  26. * we do replace/emulate mmap() on those systems ...
  27. */
  28. #ifndef MAP_FAILED
  29. # define MAP_FAILED ((void *)-1)
  30. #endif
  31. #include <fcntl.h>
  32. #ifndef O_BINARY /* should be define'd on __WIN32__ */
  33. #define O_BINARY 0
  34. #endif
  35. #ifdef __linux__
  36. # include <endian.h>
  37. # include <byteswap.h>
  38. #elif defined(__MACH__) || defined(__FreeBSD__)
  39. # include <machine/endian.h>
  40. #endif
  41. #ifdef __FreeBSD__
  42. # include <sys/endian.h> /* htole32 and friends */
  43. # define __BYTE_ORDER BYTE_ORDER
  44. # define __LITTLE_ENDIAN LITTLE_ENDIAN
  45. # define __BIG_ENDIAN BIG_ENDIAN
  46. #elif defined(__OpenBSD__)
  47. # include <endian.h>
  48. # define __BYTE_ORDER BYTE_ORDER
  49. # define __LITTLE_ENDIAN LITTLE_ENDIAN
  50. # define __BIG_ENDIAN BIG_ENDIAN
  51. #endif
  52. #include <time.h>
  53. typedef uint8_t __u8;
  54. typedef uint16_t __u16;
  55. typedef uint32_t __u32;
  56. typedef unsigned int uint;
  57. typedef unsigned long ulong;
  58. #define uswap_16(x) \
  59. ((((x) & 0xff00) >> 8) | \
  60. (((x) & 0x00ff) << 8))
  61. #define uswap_32(x) \
  62. ((((x) & 0xff000000) >> 24) | \
  63. (((x) & 0x00ff0000) >> 8) | \
  64. (((x) & 0x0000ff00) << 8) | \
  65. (((x) & 0x000000ff) << 24))
  66. #define _uswap_64(x, sfx) \
  67. ((((x) & 0xff00000000000000##sfx) >> 56) | \
  68. (((x) & 0x00ff000000000000##sfx) >> 40) | \
  69. (((x) & 0x0000ff0000000000##sfx) >> 24) | \
  70. (((x) & 0x000000ff00000000##sfx) >> 8) | \
  71. (((x) & 0x00000000ff000000##sfx) << 8) | \
  72. (((x) & 0x0000000000ff0000##sfx) << 24) | \
  73. (((x) & 0x000000000000ff00##sfx) << 40) | \
  74. (((x) & 0x00000000000000ff##sfx) << 56))
  75. #if defined(__GNUC__)
  76. # define uswap_64(x) _uswap_64(x, ull)
  77. #else
  78. # define uswap_64(x) _uswap_64(x, )
  79. #endif
  80. #if __BYTE_ORDER == __LITTLE_ENDIAN
  81. # define cpu_to_le16(x) (x)
  82. # define cpu_to_le32(x) (x)
  83. # define cpu_to_le64(x) (x)
  84. # define le16_to_cpu(x) (x)
  85. # define le32_to_cpu(x) (x)
  86. # define le64_to_cpu(x) (x)
  87. # define cpu_to_be16(x) uswap_16(x)
  88. # define cpu_to_be32(x) uswap_32(x)
  89. # define cpu_to_be64(x) uswap_64(x)
  90. # define be16_to_cpu(x) uswap_16(x)
  91. # define be32_to_cpu(x) uswap_32(x)
  92. # define be64_to_cpu(x) uswap_64(x)
  93. #else
  94. # define cpu_to_le16(x) uswap_16(x)
  95. # define cpu_to_le32(x) uswap_32(x)
  96. # define cpu_to_le64(x) uswap_64(x)
  97. # define le16_to_cpu(x) uswap_16(x)
  98. # define le32_to_cpu(x) uswap_32(x)
  99. # define le64_to_cpu(x) uswap_64(x)
  100. # define cpu_to_be16(x) (x)
  101. # define cpu_to_be32(x) (x)
  102. # define cpu_to_be64(x) (x)
  103. # define be16_to_cpu(x) (x)
  104. # define be32_to_cpu(x) (x)
  105. # define be64_to_cpu(x) (x)
  106. #endif
  107. #else /* !USE_HOSTCC */
  108. /* Type for `void *' pointers. */
  109. typedef unsigned long int uintptr_t;
  110. #include <linux/string.h>
  111. #include <linux/types.h>
  112. #include <asm/byteorder.h>
  113. #if __SIZEOF_LONG__ == 8
  114. # define __WORDSIZE 64
  115. #elif __SIZEOF_LONG__ == 4
  116. # define __WORDSIZE 32
  117. #else
  118. /*
  119. * Assume 32-bit for now - only newer toolchains support this feature and
  120. * this is only required for sandbox support at present.
  121. */
  122. #define __WORDSIZE 32
  123. #endif
  124. #endif /* USE_HOSTCC */
  125. #define likely(x) __builtin_expect(!!(x), 1)
  126. #define unlikely(x) __builtin_expect(!!(x), 0)
  127. #endif