compiler.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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__)
  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. typedef unsigned long ulong;
  41. #endif
  42. typedef uint8_t __u8;
  43. typedef uint16_t __u16;
  44. typedef uint32_t __u32;
  45. typedef unsigned int uint;
  46. #define uswap_16(x) \
  47. ((((x) & 0xff00) >> 8) | \
  48. (((x) & 0x00ff) << 8))
  49. #define uswap_32(x) \
  50. ((((x) & 0xff000000) >> 24) | \
  51. (((x) & 0x00ff0000) >> 8) | \
  52. (((x) & 0x0000ff00) << 8) | \
  53. (((x) & 0x000000ff) << 24))
  54. #define _uswap_64(x, sfx) \
  55. ((((x) & 0xff00000000000000##sfx) >> 56) | \
  56. (((x) & 0x00ff000000000000##sfx) >> 40) | \
  57. (((x) & 0x0000ff0000000000##sfx) >> 24) | \
  58. (((x) & 0x000000ff00000000##sfx) >> 8) | \
  59. (((x) & 0x00000000ff000000##sfx) << 8) | \
  60. (((x) & 0x0000000000ff0000##sfx) << 24) | \
  61. (((x) & 0x000000000000ff00##sfx) << 40) | \
  62. (((x) & 0x00000000000000ff##sfx) << 56))
  63. #if defined(__GNUC__)
  64. # define uswap_64(x) _uswap_64(x, ull)
  65. #else
  66. # define uswap_64(x) _uswap_64(x, )
  67. #endif
  68. #if __BYTE_ORDER == __LITTLE_ENDIAN
  69. # define cpu_to_le16(x) (x)
  70. # define cpu_to_le32(x) (x)
  71. # define cpu_to_le64(x) (x)
  72. # define le16_to_cpu(x) (x)
  73. # define le32_to_cpu(x) (x)
  74. # define le64_to_cpu(x) (x)
  75. # define cpu_to_be16(x) uswap_16(x)
  76. # define cpu_to_be32(x) uswap_32(x)
  77. # define cpu_to_be64(x) uswap_64(x)
  78. # define be16_to_cpu(x) uswap_16(x)
  79. # define be32_to_cpu(x) uswap_32(x)
  80. # define be64_to_cpu(x) uswap_64(x)
  81. #else
  82. # define cpu_to_le16(x) uswap_16(x)
  83. # define cpu_to_le32(x) uswap_32(x)
  84. # define cpu_to_le64(x) uswap_64(x)
  85. # define le16_to_cpu(x) uswap_16(x)
  86. # define le32_to_cpu(x) uswap_32(x)
  87. # define le64_to_cpu(x) uswap_64(x)
  88. # define cpu_to_be16(x) (x)
  89. # define cpu_to_be32(x) (x)
  90. # define cpu_to_be64(x) (x)
  91. # define be16_to_cpu(x) (x)
  92. # define be32_to_cpu(x) (x)
  93. # define be64_to_cpu(x) (x)
  94. #endif
  95. #else /* !USE_HOSTCC */
  96. #include <linux/string.h>
  97. #include <linux/types.h>
  98. #include <asm/byteorder.h>
  99. #if __SIZEOF_LONG__ == 8
  100. # define __WORDSIZE 64
  101. #elif __SIZEOF_LONG__ == 4
  102. # define __WORDSIZE 32
  103. #else
  104. /*
  105. * Assume 32-bit for now - only newer toolchains support this feature and
  106. * this is only required for sandbox support at present.
  107. */
  108. #define __WORDSIZE 32
  109. #endif
  110. /* Type for `void *' pointers. */
  111. typedef unsigned long int uintptr_t;
  112. #endif /* USE_HOSTCC */
  113. /* compiler options */
  114. #define uninitialized_var(x) x = x
  115. #define likely(x) __builtin_expect(!!(x), 1)
  116. #define unlikely(x) __builtin_expect(!!(x), 0)
  117. #endif