xref: /OK3568_Linux_fs/u-boot/include/compiler.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Keep all the ugly #ifdef for system stuff here
3*4882a593Smuzhiyun  */
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #ifndef __COMPILER_H__
6*4882a593Smuzhiyun #define __COMPILER_H__
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <stddef.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #ifdef USE_HOSTCC
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #if defined(__BEOS__)	 || \
13*4882a593Smuzhiyun     defined(__NetBSD__)  || \
14*4882a593Smuzhiyun     defined(__FreeBSD__) || \
15*4882a593Smuzhiyun     defined(__sun__)	 || \
16*4882a593Smuzhiyun     defined(__APPLE__)
17*4882a593Smuzhiyun # include <inttypes.h>
18*4882a593Smuzhiyun #elif defined(__linux__) || defined(__WIN32__) || defined(__MINGW32__) || defined(__OpenBSD__)
19*4882a593Smuzhiyun # include <stdint.h>
20*4882a593Smuzhiyun #endif
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include <errno.h>
23*4882a593Smuzhiyun #include <stdlib.h>
24*4882a593Smuzhiyun #include <stdint.h>
25*4882a593Smuzhiyun #include <stdio.h>
26*4882a593Smuzhiyun #include <string.h>
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #if !defined(__WIN32__) && !defined(__MINGW32__)
29*4882a593Smuzhiyun # include <sys/mman.h>
30*4882a593Smuzhiyun #endif
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /* Not all systems (like Windows) has this define, and yes
33*4882a593Smuzhiyun  * we do replace/emulate mmap() on those systems ...
34*4882a593Smuzhiyun  */
35*4882a593Smuzhiyun #ifndef MAP_FAILED
36*4882a593Smuzhiyun # define MAP_FAILED ((void *)-1)
37*4882a593Smuzhiyun #endif
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #include <fcntl.h>
40*4882a593Smuzhiyun #ifndef O_BINARY		/* should be define'd on __WIN32__ */
41*4882a593Smuzhiyun #define O_BINARY	0
42*4882a593Smuzhiyun #endif
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #ifdef __linux__
45*4882a593Smuzhiyun # include <endian.h>
46*4882a593Smuzhiyun # include <byteswap.h>
47*4882a593Smuzhiyun #elif defined(__MACH__) || defined(__FreeBSD__)
48*4882a593Smuzhiyun # include <machine/endian.h>
49*4882a593Smuzhiyun typedef unsigned long ulong;
50*4882a593Smuzhiyun #endif
51*4882a593Smuzhiyun #ifdef __FreeBSD__
52*4882a593Smuzhiyun # include <sys/endian.h> /* htole32 and friends */
53*4882a593Smuzhiyun #elif defined(__OpenBSD__)
54*4882a593Smuzhiyun # include <endian.h>
55*4882a593Smuzhiyun # define __BYTE_ORDER BYTE_ORDER
56*4882a593Smuzhiyun # define __LITTLE_ENDIAN LITTLE_ENDIAN
57*4882a593Smuzhiyun # define __BIG_ENDIAN BIG_ENDIAN
58*4882a593Smuzhiyun #endif
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun #include <time.h>
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun typedef uint8_t __u8;
63*4882a593Smuzhiyun typedef uint16_t __u16;
64*4882a593Smuzhiyun typedef uint32_t __u32;
65*4882a593Smuzhiyun typedef unsigned int uint;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun #define uswap_16(x) \
68*4882a593Smuzhiyun 	((((x) & 0xff00) >> 8) | \
69*4882a593Smuzhiyun 	 (((x) & 0x00ff) << 8))
70*4882a593Smuzhiyun #define uswap_32(x) \
71*4882a593Smuzhiyun 	((((x) & 0xff000000) >> 24) | \
72*4882a593Smuzhiyun 	 (((x) & 0x00ff0000) >>  8) | \
73*4882a593Smuzhiyun 	 (((x) & 0x0000ff00) <<  8) | \
74*4882a593Smuzhiyun 	 (((x) & 0x000000ff) << 24))
75*4882a593Smuzhiyun #define _uswap_64(x, sfx) \
76*4882a593Smuzhiyun 	((((x) & 0xff00000000000000##sfx) >> 56) | \
77*4882a593Smuzhiyun 	 (((x) & 0x00ff000000000000##sfx) >> 40) | \
78*4882a593Smuzhiyun 	 (((x) & 0x0000ff0000000000##sfx) >> 24) | \
79*4882a593Smuzhiyun 	 (((x) & 0x000000ff00000000##sfx) >>  8) | \
80*4882a593Smuzhiyun 	 (((x) & 0x00000000ff000000##sfx) <<  8) | \
81*4882a593Smuzhiyun 	 (((x) & 0x0000000000ff0000##sfx) << 24) | \
82*4882a593Smuzhiyun 	 (((x) & 0x000000000000ff00##sfx) << 40) | \
83*4882a593Smuzhiyun 	 (((x) & 0x00000000000000ff##sfx) << 56))
84*4882a593Smuzhiyun #if defined(__GNUC__)
85*4882a593Smuzhiyun # define uswap_64(x) _uswap_64(x, ull)
86*4882a593Smuzhiyun #else
87*4882a593Smuzhiyun # define uswap_64(x) _uswap_64(x, )
88*4882a593Smuzhiyun #endif
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun #if __BYTE_ORDER == __LITTLE_ENDIAN
91*4882a593Smuzhiyun # define cpu_to_le16(x)		(x)
92*4882a593Smuzhiyun # define cpu_to_le32(x)		(x)
93*4882a593Smuzhiyun # define cpu_to_le64(x)		(x)
94*4882a593Smuzhiyun # define le16_to_cpu(x)		(x)
95*4882a593Smuzhiyun # define le32_to_cpu(x)		(x)
96*4882a593Smuzhiyun # define le64_to_cpu(x)		(x)
97*4882a593Smuzhiyun # define cpu_to_be16(x)		uswap_16(x)
98*4882a593Smuzhiyun # define cpu_to_be32(x)		uswap_32(x)
99*4882a593Smuzhiyun # define cpu_to_be64(x)		uswap_64(x)
100*4882a593Smuzhiyun # define be16_to_cpu(x)		uswap_16(x)
101*4882a593Smuzhiyun # define be32_to_cpu(x)		uswap_32(x)
102*4882a593Smuzhiyun # define be64_to_cpu(x)		uswap_64(x)
103*4882a593Smuzhiyun #else
104*4882a593Smuzhiyun # define cpu_to_le16(x)		uswap_16(x)
105*4882a593Smuzhiyun # define cpu_to_le32(x)		uswap_32(x)
106*4882a593Smuzhiyun # define cpu_to_le64(x)		uswap_64(x)
107*4882a593Smuzhiyun # define le16_to_cpu(x)		uswap_16(x)
108*4882a593Smuzhiyun # define le32_to_cpu(x)		uswap_32(x)
109*4882a593Smuzhiyun # define le64_to_cpu(x)		uswap_64(x)
110*4882a593Smuzhiyun # define cpu_to_be16(x)		(x)
111*4882a593Smuzhiyun # define cpu_to_be32(x)		(x)
112*4882a593Smuzhiyun # define cpu_to_be64(x)		(x)
113*4882a593Smuzhiyun # define be16_to_cpu(x)		(x)
114*4882a593Smuzhiyun # define be32_to_cpu(x)		(x)
115*4882a593Smuzhiyun # define be64_to_cpu(x)		(x)
116*4882a593Smuzhiyun #endif
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun #else /* !USE_HOSTCC */
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun #ifdef CONFIG_USE_STDINT
121*4882a593Smuzhiyun /* Provided by gcc. */
122*4882a593Smuzhiyun #include <stdint.h>
123*4882a593Smuzhiyun #else
124*4882a593Smuzhiyun /* Type for `void *' pointers. */
125*4882a593Smuzhiyun typedef unsigned long int uintptr_t;
126*4882a593Smuzhiyun #endif
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun #include <linux/string.h>
129*4882a593Smuzhiyun #include <linux/types.h>
130*4882a593Smuzhiyun #include <asm/byteorder.h>
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun #if __SIZEOF_LONG__ == 8
133*4882a593Smuzhiyun # define __WORDSIZE	64
134*4882a593Smuzhiyun #elif __SIZEOF_LONG__ == 4
135*4882a593Smuzhiyun # define __WORDSIZE	32
136*4882a593Smuzhiyun #else
137*4882a593Smuzhiyun /*
138*4882a593Smuzhiyun  * Assume 32-bit for now - only newer toolchains support this feature and
139*4882a593Smuzhiyun  * this is only required for sandbox support at present.
140*4882a593Smuzhiyun  */
141*4882a593Smuzhiyun #define __WORDSIZE	32
142*4882a593Smuzhiyun #endif
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun #endif /* USE_HOSTCC */
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun #define likely(x)	__builtin_expect(!!(x), 1)
147*4882a593Smuzhiyun #define unlikely(x)	__builtin_expect(!!(x), 0)
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun #endif
150