compiler.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. extern int errno;
  23. #if !defined(__WIN32__) && !defined(__MINGW32__)
  24. # include <sys/mman.h>
  25. #endif
  26. /* Not all systems (like Windows) has this define, and yes
  27. * we do replace/emulate mmap() on those systems ...
  28. */
  29. #ifndef MAP_FAILED
  30. # define MAP_FAILED ((void *)-1)
  31. #endif
  32. #include <fcntl.h>
  33. #ifndef O_BINARY /* should be define'd on __WIN32__ */
  34. #define O_BINARY 0
  35. #endif
  36. #ifdef __linux__
  37. # include <endian.h>
  38. # include <byteswap.h>
  39. #elif defined(__MACH__)
  40. # include <machine/endian.h>
  41. typedef unsigned long ulong;
  42. typedef unsigned int uint;
  43. #endif
  44. typedef uint8_t __u8;
  45. typedef uint16_t __u16;
  46. typedef uint32_t __u32;
  47. #define uswap_16(x) \
  48. ((((x) & 0xff00) >> 8) | \
  49. (((x) & 0x00ff) << 8))
  50. #define uswap_32(x) \
  51. ((((x) & 0xff000000) >> 24) | \
  52. (((x) & 0x00ff0000) >> 8) | \
  53. (((x) & 0x0000ff00) << 8) | \
  54. (((x) & 0x000000ff) << 24))
  55. #define _uswap_64(x, sfx) \
  56. ((((x) & 0xff00000000000000##sfx) >> 56) | \
  57. (((x) & 0x00ff000000000000##sfx) >> 40) | \
  58. (((x) & 0x0000ff0000000000##sfx) >> 24) | \
  59. (((x) & 0x000000ff00000000##sfx) >> 8) | \
  60. (((x) & 0x00000000ff000000##sfx) << 8) | \
  61. (((x) & 0x0000000000ff0000##sfx) << 24) | \
  62. (((x) & 0x000000000000ff00##sfx) << 40) | \
  63. (((x) & 0x00000000000000ff##sfx) << 56))
  64. #if defined(__GNUC__)
  65. # define uswap_64(x) _uswap_64(x, ull)
  66. #else
  67. # define uswap_64(x) _uswap_64(x, )
  68. #endif
  69. #if __BYTE_ORDER == __LITTLE_ENDIAN
  70. # define cpu_to_le16(x) (x)
  71. # define cpu_to_le32(x) (x)
  72. # define cpu_to_le64(x) (x)
  73. # define le16_to_cpu(x) (x)
  74. # define le32_to_cpu(x) (x)
  75. # define le64_to_cpu(x) (x)
  76. # define cpu_to_be16(x) uswap_16(x)
  77. # define cpu_to_be32(x) uswap_32(x)
  78. # define cpu_to_be64(x) uswap_64(x)
  79. # define be16_to_cpu(x) uswap_16(x)
  80. # define be32_to_cpu(x) uswap_32(x)
  81. # define be64_to_cpu(x) uswap_64(x)
  82. #else
  83. # define cpu_to_le16(x) uswap_16(x)
  84. # define cpu_to_le32(x) uswap_32(x)
  85. # define cpu_to_le64(x) uswap_64(x)
  86. # define le16_to_cpu(x) uswap_16(x)
  87. # define le32_to_cpu(x) uswap_32(x)
  88. # define le64_to_cpu(x) uswap_64(x)
  89. # define cpu_to_be16(x) (x)
  90. # define cpu_to_be32(x) (x)
  91. # define cpu_to_be64(x) (x)
  92. # define be16_to_cpu(x) (x)
  93. # define be32_to_cpu(x) (x)
  94. # define be64_to_cpu(x) (x)
  95. #endif
  96. #else /* !USE_HOSTCC */
  97. #include <linux/string.h>
  98. #include <linux/types.h>
  99. #include <asm/byteorder.h>
  100. /* Types for `void *' pointers. */
  101. #if __WORDSIZE == 64
  102. typedef unsigned long int uintptr_t;
  103. #else
  104. typedef unsigned int uintptr_t;
  105. #endif
  106. #endif
  107. /* compiler options */
  108. #define uninitialized_var(x) x = x
  109. #define likely(x) __builtin_expect(!!(x), 1)
  110. #define unlikely(x) __builtin_expect(!!(x), 0)
  111. #endif