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