xref: /openbmc/u-boot/include/compiler.h (revision 5bce5dc33d6313ace88bef2081f71532c9e6a4f1)
137566090SMike Frysinger /*
237566090SMike Frysinger  * Keep all the ugly #ifdef for system stuff here
337566090SMike Frysinger  */
437566090SMike Frysinger 
537566090SMike Frysinger #ifndef __COMPILER_H__
637566090SMike Frysinger #define __COMPILER_H__
737566090SMike Frysinger 
837566090SMike Frysinger #include <stddef.h>
937566090SMike Frysinger 
1037566090SMike Frysinger #ifdef USE_HOSTCC
1137566090SMike Frysinger 
1237566090SMike Frysinger #if defined(__BEOS__)	 || \
1337566090SMike Frysinger     defined(__NetBSD__)  || \
1437566090SMike Frysinger     defined(__FreeBSD__) || \
1537566090SMike Frysinger     defined(__sun__)	 || \
1637566090SMike Frysinger     defined(__APPLE__)
1737566090SMike Frysinger # include <inttypes.h>
1837566090SMike Frysinger #elif defined(__linux__) || defined(__WIN32__) || defined(__MINGW32__)
1937566090SMike Frysinger # include <stdint.h>
2037566090SMike Frysinger #endif
2137566090SMike Frysinger 
2237566090SMike Frysinger #include <errno.h>
2337566090SMike Frysinger #include <stdlib.h>
2437566090SMike Frysinger #include <stdint.h>
2537566090SMike Frysinger #include <stdio.h>
2637566090SMike Frysinger #include <string.h>
2737566090SMike Frysinger 
2837566090SMike Frysinger #if !defined(__WIN32__) && !defined(__MINGW32__)
2937566090SMike Frysinger # include <sys/mman.h>
3037566090SMike Frysinger #endif
3137566090SMike Frysinger 
3237566090SMike Frysinger /* Not all systems (like Windows) has this define, and yes
3337566090SMike Frysinger  * we do replace/emulate mmap() on those systems ...
3437566090SMike Frysinger  */
3537566090SMike Frysinger #ifndef MAP_FAILED
3637566090SMike Frysinger # define MAP_FAILED ((void *)-1)
3737566090SMike Frysinger #endif
3837566090SMike Frysinger 
3937566090SMike Frysinger #include <fcntl.h>
4037566090SMike Frysinger #ifndef O_BINARY		/* should be define'd on __WIN32__ */
4137566090SMike Frysinger #define O_BINARY	0
4237566090SMike Frysinger #endif
4337566090SMike Frysinger 
4437566090SMike Frysinger #ifdef __linux__
4537566090SMike Frysinger # include <endian.h>
4637566090SMike Frysinger # include <byteswap.h>
47*5bce5dc3SJeroen Hofstee #elif defined(__MACH__) || defined(__FreeBSD__)
4837566090SMike Frysinger # include <machine/endian.h>
4937566090SMike Frysinger typedef unsigned long ulong;
5037566090SMike Frysinger #endif
5137566090SMike Frysinger 
5237566090SMike Frysinger typedef uint8_t __u8;
5337566090SMike Frysinger typedef uint16_t __u16;
5437566090SMike Frysinger typedef uint32_t __u32;
55b050c72dSMike Frysinger typedef unsigned int uint;
5637566090SMike Frysinger 
5737566090SMike Frysinger #define uswap_16(x) \
5837566090SMike Frysinger 	((((x) & 0xff00) >> 8) | \
5937566090SMike Frysinger 	 (((x) & 0x00ff) << 8))
6037566090SMike Frysinger #define uswap_32(x) \
6137566090SMike Frysinger 	((((x) & 0xff000000) >> 24) | \
6237566090SMike Frysinger 	 (((x) & 0x00ff0000) >>  8) | \
6337566090SMike Frysinger 	 (((x) & 0x0000ff00) <<  8) | \
6437566090SMike Frysinger 	 (((x) & 0x000000ff) << 24))
6537566090SMike Frysinger #define _uswap_64(x, sfx) \
6637566090SMike Frysinger 	((((x) & 0xff00000000000000##sfx) >> 56) | \
6737566090SMike Frysinger 	 (((x) & 0x00ff000000000000##sfx) >> 40) | \
6837566090SMike Frysinger 	 (((x) & 0x0000ff0000000000##sfx) >> 24) | \
6937566090SMike Frysinger 	 (((x) & 0x000000ff00000000##sfx) >>  8) | \
7037566090SMike Frysinger 	 (((x) & 0x00000000ff000000##sfx) <<  8) | \
7137566090SMike Frysinger 	 (((x) & 0x0000000000ff0000##sfx) << 24) | \
7237566090SMike Frysinger 	 (((x) & 0x000000000000ff00##sfx) << 40) | \
7337566090SMike Frysinger 	 (((x) & 0x00000000000000ff##sfx) << 56))
7437566090SMike Frysinger #if defined(__GNUC__)
7537566090SMike Frysinger # define uswap_64(x) _uswap_64(x, ull)
7637566090SMike Frysinger #else
7737566090SMike Frysinger # define uswap_64(x) _uswap_64(x, )
7837566090SMike Frysinger #endif
7937566090SMike Frysinger 
8037566090SMike Frysinger #if __BYTE_ORDER == __LITTLE_ENDIAN
8137566090SMike Frysinger # define cpu_to_le16(x)		(x)
8237566090SMike Frysinger # define cpu_to_le32(x)		(x)
8337566090SMike Frysinger # define cpu_to_le64(x)		(x)
8437566090SMike Frysinger # define le16_to_cpu(x)		(x)
8537566090SMike Frysinger # define le32_to_cpu(x)		(x)
8637566090SMike Frysinger # define le64_to_cpu(x)		(x)
8737566090SMike Frysinger # define cpu_to_be16(x)		uswap_16(x)
8837566090SMike Frysinger # define cpu_to_be32(x)		uswap_32(x)
8937566090SMike Frysinger # define cpu_to_be64(x)		uswap_64(x)
9037566090SMike Frysinger # define be16_to_cpu(x)		uswap_16(x)
9137566090SMike Frysinger # define be32_to_cpu(x)		uswap_32(x)
9237566090SMike Frysinger # define be64_to_cpu(x)		uswap_64(x)
9337566090SMike Frysinger #else
9437566090SMike Frysinger # define cpu_to_le16(x)		uswap_16(x)
9537566090SMike Frysinger # define cpu_to_le32(x)		uswap_32(x)
9637566090SMike Frysinger # define cpu_to_le64(x)		uswap_64(x)
9737566090SMike Frysinger # define le16_to_cpu(x)		uswap_16(x)
9837566090SMike Frysinger # define le32_to_cpu(x)		uswap_32(x)
9937566090SMike Frysinger # define le64_to_cpu(x)		uswap_64(x)
10037566090SMike Frysinger # define cpu_to_be16(x)		(x)
10137566090SMike Frysinger # define cpu_to_be32(x)		(x)
10237566090SMike Frysinger # define cpu_to_be64(x)		(x)
10337566090SMike Frysinger # define be16_to_cpu(x)		(x)
10437566090SMike Frysinger # define be32_to_cpu(x)		(x)
10537566090SMike Frysinger # define be64_to_cpu(x)		(x)
10637566090SMike Frysinger #endif
10737566090SMike Frysinger 
10837566090SMike Frysinger #else /* !USE_HOSTCC */
10937566090SMike Frysinger 
11037566090SMike Frysinger #include <linux/string.h>
11137566090SMike Frysinger #include <linux/types.h>
11237566090SMike Frysinger #include <asm/byteorder.h>
11337566090SMike Frysinger 
11437566090SMike Frysinger /* Types for `void *' pointers. */
11537566090SMike Frysinger #if __WORDSIZE == 64
11637566090SMike Frysinger typedef unsigned long int       uintptr_t;
11737566090SMike Frysinger #else
11837566090SMike Frysinger typedef unsigned int            uintptr_t;
11937566090SMike Frysinger #endif
12037566090SMike Frysinger 
12137566090SMike Frysinger #endif
12237566090SMike Frysinger 
123d8bc55a6SAnton Vorontsov /* compiler options */
124d8bc55a6SAnton Vorontsov #define uninitialized_var(x)		x = x
125d8bc55a6SAnton Vorontsov 
126b63815e3SMatthias Kaehlcke #define likely(x)	__builtin_expect(!!(x), 1)
127b63815e3SMatthias Kaehlcke #define unlikely(x)	__builtin_expect(!!(x), 0)
128b63815e3SMatthias Kaehlcke 
12937566090SMike Frysinger #endif
130