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