blob: a43fb6a7386176ce556a60f04d0a2c19996e5745 [file] [log] [blame]
Mike Frysinger4ad8e9f2009-07-02 19:23:25 -04001/*
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 Gray482915d2016-09-03 08:26:55 +100018#elif defined(__linux__) || defined(__WIN32__) || defined(__MINGW32__) || defined(__OpenBSD__)
Mike Frysinger4ad8e9f2009-07-02 19:23:25 -040019# 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 Frysinger4ad8e9f2009-07-02 19:23:25 -040028#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 Hofsteecdb56232011-07-19 10:41:48 +000047#elif defined(__MACH__) || defined(__FreeBSD__)
Mike Frysinger4ad8e9f2009-07-02 19:23:25 -040048# include <machine/endian.h>
49typedef unsigned long ulong;
Mike Frysinger4ad8e9f2009-07-02 19:23:25 -040050#endif
Jeroen Hofsteee1774be2014-10-07 22:42:27 +020051#ifdef __FreeBSD__
52# include <sys/endian.h> /* htole32 and friends */
Jonathan Gray482915d2016-09-03 08:26:55 +100053#elif defined(__OpenBSD__)
54# include <endian.h>
Jonathan Grayc9520522016-11-26 15:18:00 +110055# define __BYTE_ORDER BYTE_ORDER
56# define __LITTLE_ENDIAN LITTLE_ENDIAN
57# define __BIG_ENDIAN BIG_ENDIAN
Jeroen Hofsteee1774be2014-10-07 22:42:27 +020058#endif
59
Jeroen Hofsteea8aebfe2014-06-25 23:02:21 +020060#include <time.h>
Mike Frysinger4ad8e9f2009-07-02 19:23:25 -040061
62typedef uint8_t __u8;
63typedef uint16_t __u16;
64typedef uint32_t __u32;
Mike Frysingerd5f644d2010-04-20 05:49:30 -040065typedef unsigned int uint;
Mike Frysinger4ad8e9f2009-07-02 19:23:25 -040066
67#define uswap_16(x) \
68 ((((x) & 0xff00) >> 8) | \
69 (((x) & 0x00ff) << 8))
70#define uswap_32(x) \
71 ((((x) & 0xff000000) >> 24) | \
72 (((x) & 0x00ff0000) >> 8) | \
73 (((x) & 0x0000ff00) << 8) | \
74 (((x) & 0x000000ff) << 24))
75#define _uswap_64(x, sfx) \
76 ((((x) & 0xff00000000000000##sfx) >> 56) | \
77 (((x) & 0x00ff000000000000##sfx) >> 40) | \
78 (((x) & 0x0000ff0000000000##sfx) >> 24) | \
79 (((x) & 0x000000ff00000000##sfx) >> 8) | \
80 (((x) & 0x00000000ff000000##sfx) << 8) | \
81 (((x) & 0x0000000000ff0000##sfx) << 24) | \
82 (((x) & 0x000000000000ff00##sfx) << 40) | \
83 (((x) & 0x00000000000000ff##sfx) << 56))
84#if defined(__GNUC__)
85# define uswap_64(x) _uswap_64(x, ull)
86#else
87# define uswap_64(x) _uswap_64(x, )
88#endif
89
Jonathan Grayc9520522016-11-26 15:18:00 +110090#if __BYTE_ORDER == __LITTLE_ENDIAN
Mike Frysinger4ad8e9f2009-07-02 19:23:25 -040091# define cpu_to_le16(x) (x)
92# define cpu_to_le32(x) (x)
93# define cpu_to_le64(x) (x)
94# define le16_to_cpu(x) (x)
95# define le32_to_cpu(x) (x)
96# define le64_to_cpu(x) (x)
97# define cpu_to_be16(x) uswap_16(x)
98# define cpu_to_be32(x) uswap_32(x)
99# define cpu_to_be64(x) uswap_64(x)
100# define be16_to_cpu(x) uswap_16(x)
101# define be32_to_cpu(x) uswap_32(x)
102# define be64_to_cpu(x) uswap_64(x)
103#else
104# define cpu_to_le16(x) uswap_16(x)
105# define cpu_to_le32(x) uswap_32(x)
106# define cpu_to_le64(x) uswap_64(x)
107# define le16_to_cpu(x) uswap_16(x)
108# define le32_to_cpu(x) uswap_32(x)
109# define le64_to_cpu(x) uswap_64(x)
110# define cpu_to_be16(x) (x)
111# define cpu_to_be32(x) (x)
112# define cpu_to_be64(x) (x)
113# define be16_to_cpu(x) (x)
114# define be32_to_cpu(x) (x)
115# define be64_to_cpu(x) (x)
116#endif
117
118#else /* !USE_HOSTCC */
119
Gabe Black3687fe42014-10-15 04:38:30 -0600120#ifdef CONFIG_USE_STDINT
121/* Provided by gcc. */
122#include <stdint.h>
York Sun9dfa9252015-12-16 14:12:24 +0800123#else
124/* Type for `void *' pointers. */
125typedef unsigned long int uintptr_t;
Gabe Black3687fe42014-10-15 04:38:30 -0600126#endif
127
Mike Frysinger4ad8e9f2009-07-02 19:23:25 -0400128#include <linux/string.h>
129#include <linux/types.h>
130#include <asm/byteorder.h>
131
Simon Glassac6cb142011-09-23 06:22:06 +0000132#if __SIZEOF_LONG__ == 8
133# define __WORDSIZE 64
134#elif __SIZEOF_LONG__ == 4
135# define __WORDSIZE 32
136#else
137/*
138 * Assume 32-bit for now - only newer toolchains support this feature and
139 * this is only required for sandbox support at present.
140 */
141#define __WORDSIZE 32
142#endif
143
Simon Glasse75d0b12011-11-04 06:42:35 +0000144#endif /* USE_HOSTCC */
Mike Frysinger4ad8e9f2009-07-02 19:23:25 -0400145
Matthias Kaehlcke71f15322009-12-22 23:05:45 +0100146#define likely(x) __builtin_expect(!!(x), 1)
147#define unlikely(x) __builtin_expect(!!(x), 0)
148
Mike Frysinger4ad8e9f2009-07-02 19:23:25 -0400149#endif