Mike Frysinger | 4ad8e9f | 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 | 482915d | 2016-09-03 08:26:55 +1000 | [diff] [blame] | 18 | #elif defined(__linux__) || defined(__WIN32__) || defined(__MINGW32__) || defined(__OpenBSD__) |
Mike Frysinger | 4ad8e9f | 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 | 4ad8e9f | 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 | cdb5623 | 2011-07-19 10:41:48 +0000 | [diff] [blame] | 47 | #elif defined(__MACH__) || defined(__FreeBSD__) |
Mike Frysinger | 4ad8e9f | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 48 | # include <machine/endian.h> |
Mike Frysinger | 4ad8e9f | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 49 | #endif |
Jeroen Hofstee | e1774be | 2014-10-07 22:42:27 +0200 | [diff] [blame] | 50 | #ifdef __FreeBSD__ |
| 51 | # include <sys/endian.h> /* htole32 and friends */ |
Justin Hibbits | d77118c | 2018-01-29 22:23:36 -0600 | [diff] [blame] | 52 | # define __BYTE_ORDER BYTE_ORDER |
| 53 | # define __LITTLE_ENDIAN LITTLE_ENDIAN |
| 54 | # define __BIG_ENDIAN BIG_ENDIAN |
Jonathan Gray | 482915d | 2016-09-03 08:26:55 +1000 | [diff] [blame] | 55 | #elif defined(__OpenBSD__) |
| 56 | # include <endian.h> |
Jonathan Gray | c952052 | 2016-11-26 15:18:00 +1100 | [diff] [blame] | 57 | # define __BYTE_ORDER BYTE_ORDER |
| 58 | # define __LITTLE_ENDIAN LITTLE_ENDIAN |
| 59 | # define __BIG_ENDIAN BIG_ENDIAN |
Jeroen Hofstee | e1774be | 2014-10-07 22:42:27 +0200 | [diff] [blame] | 60 | #endif |
| 61 | |
Jeroen Hofstee | a8aebfe | 2014-06-25 23:02:21 +0200 | [diff] [blame] | 62 | #include <time.h> |
Mike Frysinger | 4ad8e9f | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 63 | |
| 64 | typedef uint8_t __u8; |
| 65 | typedef uint16_t __u16; |
| 66 | typedef uint32_t __u32; |
Mike Frysinger | d5f644d | 2010-04-20 05:49:30 -0400 | [diff] [blame] | 67 | typedef unsigned int uint; |
Sergei Trofimovich | 9057f13 | 2019-12-30 15:53:41 +0000 | [diff] [blame] | 68 | typedef unsigned long ulong; |
Mike Frysinger | 4ad8e9f | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 69 | |
| 70 | #define uswap_16(x) \ |
| 71 | ((((x) & 0xff00) >> 8) | \ |
| 72 | (((x) & 0x00ff) << 8)) |
| 73 | #define uswap_32(x) \ |
| 74 | ((((x) & 0xff000000) >> 24) | \ |
| 75 | (((x) & 0x00ff0000) >> 8) | \ |
| 76 | (((x) & 0x0000ff00) << 8) | \ |
| 77 | (((x) & 0x000000ff) << 24)) |
| 78 | #define _uswap_64(x, sfx) \ |
| 79 | ((((x) & 0xff00000000000000##sfx) >> 56) | \ |
| 80 | (((x) & 0x00ff000000000000##sfx) >> 40) | \ |
| 81 | (((x) & 0x0000ff0000000000##sfx) >> 24) | \ |
| 82 | (((x) & 0x000000ff00000000##sfx) >> 8) | \ |
| 83 | (((x) & 0x00000000ff000000##sfx) << 8) | \ |
| 84 | (((x) & 0x0000000000ff0000##sfx) << 24) | \ |
| 85 | (((x) & 0x000000000000ff00##sfx) << 40) | \ |
| 86 | (((x) & 0x00000000000000ff##sfx) << 56)) |
| 87 | #if defined(__GNUC__) |
| 88 | # define uswap_64(x) _uswap_64(x, ull) |
| 89 | #else |
| 90 | # define uswap_64(x) _uswap_64(x, ) |
| 91 | #endif |
| 92 | |
Jonathan Gray | c952052 | 2016-11-26 15:18:00 +1100 | [diff] [blame] | 93 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
Mike Frysinger | 4ad8e9f | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 94 | # define cpu_to_le16(x) (x) |
| 95 | # define cpu_to_le32(x) (x) |
| 96 | # define cpu_to_le64(x) (x) |
| 97 | # define le16_to_cpu(x) (x) |
| 98 | # define le32_to_cpu(x) (x) |
| 99 | # define le64_to_cpu(x) (x) |
| 100 | # define cpu_to_be16(x) uswap_16(x) |
| 101 | # define cpu_to_be32(x) uswap_32(x) |
| 102 | # define cpu_to_be64(x) uswap_64(x) |
| 103 | # define be16_to_cpu(x) uswap_16(x) |
| 104 | # define be32_to_cpu(x) uswap_32(x) |
| 105 | # define be64_to_cpu(x) uswap_64(x) |
| 106 | #else |
| 107 | # define cpu_to_le16(x) uswap_16(x) |
| 108 | # define cpu_to_le32(x) uswap_32(x) |
| 109 | # define cpu_to_le64(x) uswap_64(x) |
| 110 | # define le16_to_cpu(x) uswap_16(x) |
| 111 | # define le32_to_cpu(x) uswap_32(x) |
| 112 | # define le64_to_cpu(x) uswap_64(x) |
| 113 | # define cpu_to_be16(x) (x) |
| 114 | # define cpu_to_be32(x) (x) |
| 115 | # define cpu_to_be64(x) (x) |
| 116 | # define be16_to_cpu(x) (x) |
| 117 | # define be32_to_cpu(x) (x) |
| 118 | # define be64_to_cpu(x) (x) |
| 119 | #endif |
| 120 | |
| 121 | #else /* !USE_HOSTCC */ |
| 122 | |
York Sun | 9dfa925 | 2015-12-16 14:12:24 +0800 | [diff] [blame] | 123 | /* Type for `void *' pointers. */ |
| 124 | typedef unsigned long int uintptr_t; |
Gabe Black | 3687fe4 | 2014-10-15 04:38:30 -0600 | [diff] [blame] | 125 | |
Mike Frysinger | 4ad8e9f | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 126 | #include <linux/string.h> |
| 127 | #include <linux/types.h> |
| 128 | #include <asm/byteorder.h> |
| 129 | |
Simon Glass | ac6cb14 | 2011-09-23 06:22:06 +0000 | [diff] [blame] | 130 | #if __SIZEOF_LONG__ == 8 |
| 131 | # define __WORDSIZE 64 |
| 132 | #elif __SIZEOF_LONG__ == 4 |
| 133 | # define __WORDSIZE 32 |
| 134 | #else |
| 135 | /* |
| 136 | * Assume 32-bit for now - only newer toolchains support this feature and |
| 137 | * this is only required for sandbox support at present. |
| 138 | */ |
| 139 | #define __WORDSIZE 32 |
| 140 | #endif |
| 141 | |
Simon Glass | e75d0b1 | 2011-11-04 06:42:35 +0000 | [diff] [blame] | 142 | #endif /* USE_HOSTCC */ |
Mike Frysinger | 4ad8e9f | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 143 | |
Matthias Kaehlcke | 71f1532 | 2009-12-22 23:05:45 +0100 | [diff] [blame] | 144 | #define likely(x) __builtin_expect(!!(x), 1) |
| 145 | #define unlikely(x) __builtin_expect(!!(x), 0) |
| 146 | |
Mike Frysinger | 4ad8e9f | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 147 | #endif |