Add 64-bit byte-swapping operations.
[people/xl0/gpxe.git] / src / arch / i386 / include / bits / byteswap.h
1 #ifndef ETHERBOOT_BITS_BYTESWAP_H
2 #define ETHERBOOT_BITS_BYTESWAP_H
3
4 static inline __attribute__ ((always_inline)) uint16_t
5 __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
13 static inline __attribute__ ((always_inline)) uint32_t
14 __i386_bswap_32(uint32_t x)
15 {
16         __asm__("xchgb %b0,%h0\n\t"
17                 "rorl $16,%0\n\t"
18                 "xchgb %b0,%h0"
19                 : "=q" (x)
20                 : "0" (x));
21         return x;
22 }
23
24 static inline __attribute__ ((always_inline)) uint64_t
25 __i386_bswap_64(uint64_t x)
26 {
27         union {
28                 uint64_t qword;
29                 uint32_t dword[2]; 
30         } u;
31
32         u.qword = x;
33         u.dword[0] = __i386_bswap_32(u.dword[0]);
34         u.dword[1] = __i386_bswap_32(u.dword[1]);
35         __asm__("xchgl %0,%1"
36                 : "=r" ( u.dword[0] ), "=r" ( u.dword[1] )
37                 : "0" ( u.dword[0] ), "1" ( u.dword[1] ) );
38         return u.qword;
39 }
40
41 #define __bswap_constant_16(x) \
42         ((uint16_t)((((uint16_t)(x) & 0x00ff) << 8) | \
43                     (((uint16_t)(x) & 0xff00) >> 8)))
44
45 #define __bswap_constant_32(x) \
46         ((uint32_t)((((uint32_t)(x) & 0x000000ffU) << 24) | \
47                     (((uint32_t)(x) & 0x0000ff00U) <<  8) | \
48                     (((uint32_t)(x) & 0x00ff0000U) >>  8) | \
49                     (((uint32_t)(x) & 0xff000000U) >> 24)))
50
51 #define __bswap_constant_64(x) \
52         ((uint64_t)((((uint64_t)(x) & 0x00000000000000ffULL) << 56) | \
53                     (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
54                     (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
55                     (((uint64_t)(x) & 0x00000000ff000000ULL) <<  8) | \
56                     (((uint64_t)(x) & 0x000000ff00000000ULL) >>  8) | \
57                     (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
58                     (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
59                     (((uint64_t)(x) & 0xff00000000000000ULL) >> 56)))
60
61 #define __bswap_16(x) \
62         ((uint16_t)(__builtin_constant_p(x) ? \
63         __bswap_constant_16(x) : \
64         __i386_bswap_16(x)))
65
66 #define __bswap_32(x) \
67         ((uint32_t)(__builtin_constant_p(x) ? \
68         __bswap_constant_32(x) : \
69         __i386_bswap_32(x)))
70
71 #define __bswap_64(x) \
72         ((uint64_t)(__builtin_constant_p(x) ? \
73         __bswap_constant_64(x) : \
74         __i386_bswap_64(x)))
75
76 #endif /* ETHERBOOT_BITS_BYTESWAP_H */