iPXE
byteswap.h
Go to the documentation of this file.
1 #ifndef BYTESWAP_H
2 #define BYTESWAP_H
3 
4 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
5 
6 #include <stdint.h>
7 #include <endian.h>
8 #include <bits/byteswap.h>
9 
10 /**
11  * Byte-swap a 16-bit constant
12  *
13  * @v value Constant value
14  * @ret swapped Byte-swapped value
15  */
16 #define __bswap_constant_16( value ) \
17  ( ( ( (value) & 0x00ff ) << 8 ) | \
18  ( ( (value) & 0xff00 ) >> 8 ) )
19 
20 /**
21  * Byte-swap a 32-bit constant
22  *
23  * @v value Constant value
24  * @ret swapped Byte-swapped value
25  */
26 #define __bswap_constant_32( value ) \
27  ( ( ( (value) & 0x000000ffUL ) << 24 ) | \
28  ( ( (value) & 0x0000ff00UL ) << 8 ) | \
29  ( ( (value) & 0x00ff0000UL ) >> 8 ) | \
30  ( ( (value) & 0xff000000UL ) >> 24 ) )
31 
32 /**
33  * Byte-swap a 64-bit constant
34  *
35  * @v value Constant value
36  * @ret swapped Byte-swapped value
37  */
38 #define __bswap_constant_64( value ) \
39  ( ( ( (value) & 0x00000000000000ffULL ) << 56 ) | \
40  ( ( (value) & 0x000000000000ff00ULL ) << 40 ) | \
41  ( ( (value) & 0x0000000000ff0000ULL ) << 24 ) | \
42  ( ( (value) & 0x00000000ff000000ULL ) << 8 ) | \
43  ( ( (value) & 0x000000ff00000000ULL ) >> 8 ) | \
44  ( ( (value) & 0x0000ff0000000000ULL ) >> 24 ) | \
45  ( ( (value) & 0x00ff000000000000ULL ) >> 40 ) | \
46  ( ( (value) & 0xff00000000000000ULL ) >> 56 ) )
47 
48 /**
49  * Byte-swap a 16-bit value
50  *
51  * @v value Value
52  * @ret swapped Byte-swapped value
53  */
54 #define __bswap_16( value ) \
55  ( __builtin_constant_p (value) ? \
56  ( ( uint16_t ) __bswap_constant_16 ( ( uint16_t ) (value) ) ) \
57  : __bswap_variable_16 (value) )
58 #define bswap_16( value ) __bswap_16 (value)
59 
60 /**
61  * Byte-swap a 32-bit value
62  *
63  * @v value Value
64  * @ret swapped Byte-swapped value
65  */
66 #define __bswap_32( value ) \
67  ( __builtin_constant_p (value) ? \
68  ( ( uint32_t ) __bswap_constant_32 ( ( uint32_t ) (value) ) ) \
69  : __bswap_variable_32 (value) )
70 #define bswap_32( value ) __bswap_32 (value)
71 
72 /**
73  * Byte-swap a 64-bit value
74  *
75  * @v value Value
76  * @ret swapped Byte-swapped value
77  */
78 #define __bswap_64( value ) \
79  ( __builtin_constant_p (value) ? \
80  ( ( uint64_t ) __bswap_constant_64 ( ( uint64_t ) (value) ) ) \
81  : __bswap_variable_64 (value) )
82 #define bswap_64( value ) __bswap_64 (value)
83 
84 #if __BYTE_ORDER == __LITTLE_ENDIAN
85 #define __cpu_to_leNN( bits, value ) (value)
86 #define __cpu_to_beNN( bits, value ) __bswap_ ## bits (value)
87 #define __leNN_to_cpu( bits, value ) (value)
88 #define __beNN_to_cpu( bits, value ) __bswap_ ## bits (value)
89 #define __cpu_to_leNNs( bits, ptr ) do { } while ( 0 )
90 #define __cpu_to_beNNs( bits, ptr ) __bswap_ ## bits ## s (ptr)
91 #define __leNN_to_cpus( bits, ptr ) do { } while ( 0 )
92 #define __beNN_to_cpus( bits, ptr ) __bswap_ ## bits ## s (ptr)
93 #endif
94 
95 #if __BYTE_ORDER == __BIG_ENDIAN
96 #define __cpu_to_leNN( bits, value ) __bswap_ ## bits (value)
97 #define __cpu_to_beNN( bits, value ) (value)
98 #define __leNN_to_cpu( bits, value ) __bswap_ ## bits (value)
99 #define __beNN_to_cpu( bits, value ) (value)
100 #define __cpu_to_leNNs( bits, ptr ) __bswap_ ## bits ## s (ptr)
101 #define __cpu_to_beNNs( bits, ptr ) do { } while ( 0 )
102 #define __leNN_to_cpus( bits, ptr ) __bswap_ ## bits ## s (ptr)
103 #define __beNN_to_cpus( bits, ptr ) do { } while ( 0 )
104 #endif
105 
106 #define cpu_to_le16( value ) __cpu_to_leNN ( 16, value )
107 #define cpu_to_le32( value ) __cpu_to_leNN ( 32, value )
108 #define cpu_to_le64( value ) __cpu_to_leNN ( 64, value )
109 #define cpu_to_be16( value ) __cpu_to_beNN ( 16, value )
110 #define cpu_to_be32( value ) __cpu_to_beNN ( 32, value )
111 #define cpu_to_be64( value ) __cpu_to_beNN ( 64, value )
112 #define le16_to_cpu( value ) __leNN_to_cpu ( 16, value )
113 #define le32_to_cpu( value ) __leNN_to_cpu ( 32, value )
114 #define le64_to_cpu( value ) __leNN_to_cpu ( 64, value )
115 #define be16_to_cpu( value ) __beNN_to_cpu ( 16, value )
116 #define be32_to_cpu( value ) __beNN_to_cpu ( 32, value )
117 #define be64_to_cpu( value ) __beNN_to_cpu ( 64, value )
118 #define cpu_to_le16s( ptr ) __cpu_to_leNNs ( 16, ptr )
119 #define cpu_to_le32s( ptr ) __cpu_to_leNNs ( 32, ptr )
120 #define cpu_to_le64s( ptr ) __cpu_to_leNNs ( 64, ptr )
121 #define cpu_to_be16s( ptr ) __cpu_to_beNNs ( 16, ptr )
122 #define cpu_to_be32s( ptr ) __cpu_to_beNNs ( 32, ptr )
123 #define cpu_to_be64s( ptr ) __cpu_to_beNNs ( 64, ptr )
124 #define le16_to_cpus( ptr ) __leNN_to_cpus ( 16, ptr )
125 #define le32_to_cpus( ptr ) __leNN_to_cpus ( 32, ptr )
126 #define le64_to_cpus( ptr ) __leNN_to_cpus ( 64, ptr )
127 #define be16_to_cpus( ptr ) __beNN_to_cpus ( 16, ptr )
128 #define be32_to_cpus( ptr ) __beNN_to_cpus ( 32, ptr )
129 #define be64_to_cpus( ptr ) __beNN_to_cpus ( 64, ptr )
130 
131 #define htonll( value ) cpu_to_be64 (value)
132 #define ntohll( value ) be64_to_cpu (value)
133 #define htonl( value ) cpu_to_be32 (value)
134 #define ntohl( value ) be32_to_cpu (value)
135 #define htons( value ) cpu_to_be16 (value)
136 #define ntohs( value ) be16_to_cpu (value)
137 
138 #endif /* BYTESWAP_H */
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Byte-order swapping functions.