xref: /openbmc/u-boot/include/compiler.h (revision 8d52ea6d)
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>
18 #elif defined(__linux__) || defined(__WIN32__) || defined(__MINGW32__)
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 
28 extern int errno;
29 
30 #if !defined(__WIN32__) && !defined(__MINGW32__)
31 # include <sys/mman.h>
32 #endif
33 
34 /* Not all systems (like Windows) has this define, and yes
35  * we do replace/emulate mmap() on those systems ...
36  */
37 #ifndef MAP_FAILED
38 # define MAP_FAILED ((void *)-1)
39 #endif
40 
41 #include <fcntl.h>
42 #ifndef O_BINARY		/* should be define'd on __WIN32__ */
43 #define O_BINARY	0
44 #endif
45 
46 #ifdef __linux__
47 # include <endian.h>
48 # include <byteswap.h>
49 #elif defined(__MACH__)
50 # include <machine/endian.h>
51 typedef unsigned long ulong;
52 typedef unsigned int  uint;
53 #endif
54 
55 typedef uint8_t __u8;
56 typedef uint16_t __u16;
57 typedef uint32_t __u32;
58 typedef unsigned int uint;
59 
60 #define uswap_16(x) \
61 	((((x) & 0xff00) >> 8) | \
62 	 (((x) & 0x00ff) << 8))
63 #define uswap_32(x) \
64 	((((x) & 0xff000000) >> 24) | \
65 	 (((x) & 0x00ff0000) >>  8) | \
66 	 (((x) & 0x0000ff00) <<  8) | \
67 	 (((x) & 0x000000ff) << 24))
68 #define _uswap_64(x, sfx) \
69 	((((x) & 0xff00000000000000##sfx) >> 56) | \
70 	 (((x) & 0x00ff000000000000##sfx) >> 40) | \
71 	 (((x) & 0x0000ff0000000000##sfx) >> 24) | \
72 	 (((x) & 0x000000ff00000000##sfx) >>  8) | \
73 	 (((x) & 0x00000000ff000000##sfx) <<  8) | \
74 	 (((x) & 0x0000000000ff0000##sfx) << 24) | \
75 	 (((x) & 0x000000000000ff00##sfx) << 40) | \
76 	 (((x) & 0x00000000000000ff##sfx) << 56))
77 #if defined(__GNUC__)
78 # define uswap_64(x) _uswap_64(x, ull)
79 #else
80 # define uswap_64(x) _uswap_64(x, )
81 #endif
82 
83 #if __BYTE_ORDER == __LITTLE_ENDIAN
84 # define cpu_to_le16(x)		(x)
85 # define cpu_to_le32(x)		(x)
86 # define cpu_to_le64(x)		(x)
87 # define le16_to_cpu(x)		(x)
88 # define le32_to_cpu(x)		(x)
89 # define le64_to_cpu(x)		(x)
90 # define cpu_to_be16(x)		uswap_16(x)
91 # define cpu_to_be32(x)		uswap_32(x)
92 # define cpu_to_be64(x)		uswap_64(x)
93 # define be16_to_cpu(x)		uswap_16(x)
94 # define be32_to_cpu(x)		uswap_32(x)
95 # define be64_to_cpu(x)		uswap_64(x)
96 #else
97 # define cpu_to_le16(x)		uswap_16(x)
98 # define cpu_to_le32(x)		uswap_32(x)
99 # define cpu_to_le64(x)		uswap_64(x)
100 # define le16_to_cpu(x)		uswap_16(x)
101 # define le32_to_cpu(x)		uswap_32(x)
102 # define le64_to_cpu(x)		uswap_64(x)
103 # define cpu_to_be16(x)		(x)
104 # define cpu_to_be32(x)		(x)
105 # define cpu_to_be64(x)		(x)
106 # define be16_to_cpu(x)		(x)
107 # define be32_to_cpu(x)		(x)
108 # define be64_to_cpu(x)		(x)
109 #endif
110 
111 #else /* !USE_HOSTCC */
112 
113 #include <linux/string.h>
114 #include <linux/types.h>
115 #include <asm/byteorder.h>
116 
117 /* Types for `void *' pointers. */
118 #if __WORDSIZE == 64
119 typedef unsigned long int       uintptr_t;
120 #else
121 typedef unsigned int            uintptr_t;
122 #endif
123 
124 #endif
125 
126 /* compiler options */
127 #define uninitialized_var(x)		x = x
128 
129 #define likely(x)	__builtin_expect(!!(x), 1)
130 #define unlikely(x)	__builtin_expect(!!(x), 0)
131 
132 #endif
133