1#ifndef ETHERBOOT_BITS_BYTESWAP_H
2#define ETHERBOOT_BITS_BYTESWAP_H
3
4#include "types.h"
5static inline uint16_t __i386_bswap_16(uint16_t x)
6{
7	__asm__("xchgb %b0,%h0\n\t"
8		: "=q" (x)
9		: "0" (x));
10	return x;
11}
12
13static inline uint32_t __i386_bswap_32(uint32_t x)
14{
15	__asm__("xchgb %b0,%h0\n\t"
16		"rorl $16,%0\n\t"
17		"xchgb %b0,%h0"
18		: "=q" (x)
19		: "0" (x));
20	return x;
21}
22
23
24#define __bswap_constant_16(x) \
25	((uint16_t)((((uint16_t)(x) & 0x00ff) << 8) | \
26		(((uint16_t)(x) & 0xff00) >> 8)))
27
28#define __bswap_constant_32(x) \
29	((uint32_t)((((uint32_t)(x) & 0x000000ffU) << 24) | \
30		(((uint32_t)(x) & 0x0000ff00U) <<  8) | \
31		(((uint32_t)(x) & 0x00ff0000U) >>  8) | \
32		(((uint32_t)(x) & 0xff000000U) >> 24)))
33
34#define __bswap_16(x) \
35	(__builtin_constant_p(x) ? \
36	__bswap_constant_16(x) : \
37	__i386_bswap_16(x))
38
39
40#define __bswap_32(x) \
41	(__builtin_constant_p(x) ? \
42	__bswap_constant_32(x) : \
43	__i386_bswap_32(x))
44
45
46#endif /* ETHERBOOT_BITS_BYTESWAP_H */
47