Mike Frysinger | 3756609 | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Keep all the ugly #ifdef for system stuff here |
| 3 | */ |
| 4 | |
| 5 | #ifndef __COMPILER_H__ |
| 6 | #define __COMPILER_H__ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | |
| 10 | #ifdef USE_HOSTCC |
| 11 | |
| 12 | #if defined(__BEOS__) || \ |
| 13 | defined(__NetBSD__) || \ |
| 14 | defined(__FreeBSD__) || \ |
| 15 | defined(__sun__) || \ |
| 16 | defined(__APPLE__) |
| 17 | # include <inttypes.h> |
Jonathan Gray | 3715a54 | 2016-09-03 08:26:55 +1000 | [diff] [blame] | 18 | #elif defined(__linux__) || defined(__WIN32__) || defined(__MINGW32__) || defined(__OpenBSD__) |
Mike Frysinger | 3756609 | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 19 | # include <stdint.h> |
| 20 | #endif |
| 21 | |
| 22 | #include <errno.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <stdint.h> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | |
Mike Frysinger | 3756609 | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 28 | #if !defined(__WIN32__) && !defined(__MINGW32__) |
| 29 | # include <sys/mman.h> |
| 30 | #endif |
| 31 | |
| 32 | /* Not all systems (like Windows) has this define, and yes |
| 33 | * we do replace/emulate mmap() on those systems ... |
| 34 | */ |
| 35 | #ifndef MAP_FAILED |
| 36 | # define MAP_FAILED ((void *)-1) |
| 37 | #endif |
| 38 | |
| 39 | #include <fcntl.h> |
| 40 | #ifndef O_BINARY /* should be define'd on __WIN32__ */ |
| 41 | #define O_BINARY 0 |
| 42 | #endif |
| 43 | |
| 44 | #ifdef __linux__ |
| 45 | # include <endian.h> |
| 46 | # include <byteswap.h> |
Jeroen Hofstee | 5bce5dc | 2011-07-19 10:41:48 +0000 | [diff] [blame] | 47 | #elif defined(__MACH__) || defined(__FreeBSD__) |
Mike Frysinger | 3756609 | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 48 | # include <machine/endian.h> |
| 49 | typedef unsigned long ulong; |
Mike Frysinger | 3756609 | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 50 | #endif |
Jeroen Hofstee | ed8271d | 2014-10-07 22:42:27 +0200 | [diff] [blame] | 51 | #ifdef __FreeBSD__ |
| 52 | # include <sys/endian.h> /* htole32 and friends */ |
Jonathan Gray | 3715a54 | 2016-09-03 08:26:55 +1000 | [diff] [blame] | 53 | #elif defined(__OpenBSD__) |
| 54 | # include <endian.h> |
Jeroen Hofstee | ed8271d | 2014-10-07 22:42:27 +0200 | [diff] [blame] | 55 | #endif |
| 56 | |
Jeroen Hofstee | 9d16e93 | 2014-06-25 23:02:21 +0200 | [diff] [blame] | 57 | #include <time.h> |
Mike Frysinger | 3756609 | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 58 | |
| 59 | typedef uint8_t __u8; |
| 60 | typedef uint16_t __u16; |
| 61 | typedef uint32_t __u32; |
Mike Frysinger | b050c72 | 2010-04-20 05:49:30 -0400 | [diff] [blame] | 62 | typedef unsigned int uint; |
Mike Frysinger | 3756609 | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 63 | |
| 64 | #define uswap_16(x) \ |
| 65 | ((((x) & 0xff00) >> 8) | \ |
| 66 | (((x) & 0x00ff) << 8)) |
| 67 | #define uswap_32(x) \ |
| 68 | ((((x) & 0xff000000) >> 24) | \ |
| 69 | (((x) & 0x00ff0000) >> 8) | \ |
| 70 | (((x) & 0x0000ff00) << 8) | \ |
| 71 | (((x) & 0x000000ff) << 24)) |
| 72 | #define _uswap_64(x, sfx) \ |
| 73 | ((((x) & 0xff00000000000000##sfx) >> 56) | \ |
| 74 | (((x) & 0x00ff000000000000##sfx) >> 40) | \ |
| 75 | (((x) & 0x0000ff0000000000##sfx) >> 24) | \ |
| 76 | (((x) & 0x000000ff00000000##sfx) >> 8) | \ |
| 77 | (((x) & 0x00000000ff000000##sfx) << 8) | \ |
| 78 | (((x) & 0x0000000000ff0000##sfx) << 24) | \ |
| 79 | (((x) & 0x000000000000ff00##sfx) << 40) | \ |
| 80 | (((x) & 0x00000000000000ff##sfx) << 56)) |
| 81 | #if defined(__GNUC__) |
| 82 | # define uswap_64(x) _uswap_64(x, ull) |
| 83 | #else |
| 84 | # define uswap_64(x) _uswap_64(x, ) |
| 85 | #endif |
| 86 | |
Jonathan Gray | 3715a54 | 2016-09-03 08:26:55 +1000 | [diff] [blame] | 87 | #if defined(__OpenBSD__) |
| 88 | #define cpu_to_le16(x) htole16(x) |
| 89 | #define cpu_to_le32(x) htole32(x) |
| 90 | #define cpu_to_le64(x) htole64(x) |
| 91 | #define le16_to_cpu(x) letoh16(x) |
| 92 | #define le32_to_cpu(x) letoh32(x) |
| 93 | #define le64_to_cpu(x) letoh64(x) |
| 94 | #define cpu_to_be16(x) htobe16(x) |
| 95 | #define cpu_to_be32(x) htobe32(x) |
| 96 | #define cpu_to_be64(x) htobe64(x) |
| 97 | #define be16_to_cpu(x) betoh16(x) |
| 98 | #define be32_to_cpu(x) betoh32(x) |
| 99 | #define be64_to_cpu(x) betoh64(x) |
| 100 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
Mike Frysinger | 3756609 | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 101 | # define cpu_to_le16(x) (x) |
| 102 | # define cpu_to_le32(x) (x) |
| 103 | # define cpu_to_le64(x) (x) |
| 104 | # define le16_to_cpu(x) (x) |
| 105 | # define le32_to_cpu(x) (x) |
| 106 | # define le64_to_cpu(x) (x) |
| 107 | # define cpu_to_be16(x) uswap_16(x) |
| 108 | # define cpu_to_be32(x) uswap_32(x) |
| 109 | # define cpu_to_be64(x) uswap_64(x) |
| 110 | # define be16_to_cpu(x) uswap_16(x) |
| 111 | # define be32_to_cpu(x) uswap_32(x) |
| 112 | # define be64_to_cpu(x) uswap_64(x) |
| 113 | #else |
| 114 | # define cpu_to_le16(x) uswap_16(x) |
| 115 | # define cpu_to_le32(x) uswap_32(x) |
| 116 | # define cpu_to_le64(x) uswap_64(x) |
| 117 | # define le16_to_cpu(x) uswap_16(x) |
| 118 | # define le32_to_cpu(x) uswap_32(x) |
| 119 | # define le64_to_cpu(x) uswap_64(x) |
| 120 | # define cpu_to_be16(x) (x) |
| 121 | # define cpu_to_be32(x) (x) |
| 122 | # define cpu_to_be64(x) (x) |
| 123 | # define be16_to_cpu(x) (x) |
| 124 | # define be32_to_cpu(x) (x) |
| 125 | # define be64_to_cpu(x) (x) |
| 126 | #endif |
| 127 | |
| 128 | #else /* !USE_HOSTCC */ |
| 129 | |
Gabe Black | 0d296cc | 2014-10-15 04:38:30 -0600 | [diff] [blame] | 130 | #ifdef CONFIG_USE_STDINT |
| 131 | /* Provided by gcc. */ |
| 132 | #include <stdint.h> |
York Sun | 2e680f9 | 2015-12-16 14:12:24 +0800 | [diff] [blame] | 133 | #else |
| 134 | /* Type for `void *' pointers. */ |
| 135 | typedef unsigned long int uintptr_t; |
Gabe Black | 0d296cc | 2014-10-15 04:38:30 -0600 | [diff] [blame] | 136 | #endif |
| 137 | |
Mike Frysinger | 3756609 | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 138 | #include <linux/string.h> |
| 139 | #include <linux/types.h> |
| 140 | #include <asm/byteorder.h> |
| 141 | |
Simon Glass | a7551a3 | 2011-09-23 06:22:06 +0000 | [diff] [blame] | 142 | #if __SIZEOF_LONG__ == 8 |
| 143 | # define __WORDSIZE 64 |
| 144 | #elif __SIZEOF_LONG__ == 4 |
| 145 | # define __WORDSIZE 32 |
| 146 | #else |
| 147 | /* |
| 148 | * Assume 32-bit for now - only newer toolchains support this feature and |
| 149 | * this is only required for sandbox support at present. |
| 150 | */ |
| 151 | #define __WORDSIZE 32 |
| 152 | #endif |
| 153 | |
Simon Glass | c9502f4 | 2011-11-04 06:42:35 +0000 | [diff] [blame] | 154 | #endif /* USE_HOSTCC */ |
Mike Frysinger | 3756609 | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 155 | |
Matthias Kaehlcke | b63815e | 2009-12-22 23:05:45 +0100 | [diff] [blame] | 156 | #define likely(x) __builtin_expect(!!(x), 1) |
| 157 | #define unlikely(x) __builtin_expect(!!(x), 0) |
| 158 | |
Mike Frysinger | 3756609 | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 159 | #endif |