]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - wimboot/wimboot-2.7.3/src/byteswap.h
added Spanish (Latinoamérica) translation (#1865)
[Ventoy.git] / wimboot / wimboot-2.7.3 / src / byteswap.h
1 #ifndef _BITS_BYTESWAP_H
2 #define _BITS_BYTESWAP_H
3
4 /** @file
5 *
6 * Byte-order swapping functions
7 *
8 */
9
10 #include <stdint.h>
11
12 static inline __attribute__ (( always_inline, const )) uint16_t
13 __bswap_variable_16 ( uint16_t x ) {
14 __asm__ ( "xchgb %b0,%h0" : "=Q" ( x ) : "0" ( x ) );
15 return x;
16 }
17
18 static inline __attribute__ (( always_inline )) void
19 __bswap_16s ( uint16_t *x ) {
20 __asm__ ( "rorw $8, %0" : "+m" ( *x ) );
21 }
22
23 static inline __attribute__ (( always_inline, const )) uint32_t
24 __bswap_variable_32 ( uint32_t x ) {
25 __asm__ ( "bswapl %k0" : "=r" ( x ) : "0" ( x ) );
26 return x;
27 }
28
29 static inline __attribute__ (( always_inline )) void
30 __bswap_32s ( uint32_t *x ) {
31 __asm__ ( "bswapl %k0" : "=r" ( *x ) : "0" ( *x ) );
32 }
33
34 #ifdef __x86_64__
35
36 static inline __attribute__ (( always_inline, const )) uint64_t
37 __bswap_variable_64 ( uint64_t x ) {
38 __asm__ ( "bswapq %q0" : "=r" ( x ) : "0" ( x ) );
39 return x;
40 }
41
42 #else /* __x86_64__ */
43
44 static inline __attribute__ (( always_inline, const )) uint64_t
45 __bswap_variable_64 ( uint64_t x ) {
46 uint32_t in_high = ( x >> 32 );
47 uint32_t in_low = ( x & 0xffffffffUL );
48 uint32_t out_high;
49 uint32_t out_low;
50
51 __asm__ ( "bswapl %0\n\t"
52 "bswapl %1\n\t"
53 "xchgl %0,%1\n\t"
54 : "=r" ( out_high ), "=r" ( out_low )
55 : "0" ( in_high ), "1" ( in_low ) );
56
57 return ( ( ( ( uint64_t ) out_high ) << 32 ) |
58 ( ( uint64_t ) out_low ) );
59 }
60
61 #endif /* __x86_64__ */
62
63 static inline __attribute__ (( always_inline )) void
64 __bswap_64s ( uint64_t *x ) {
65 *x = __bswap_variable_64 ( *x );
66 }
67
68 /**
69 * Byte-swap a 16-bit constant
70 *
71 * @v value Constant value
72 * @ret swapped Byte-swapped value
73 */
74 #define __bswap_constant_16( value ) \
75 ( ( ( (value) & 0x00ff ) << 8 ) | \
76 ( ( (value) & 0xff00 ) >> 8 ) )
77
78 /**
79 * Byte-swap a 32-bit constant
80 *
81 * @v value Constant value
82 * @ret swapped Byte-swapped value
83 */
84 #define __bswap_constant_32( value ) \
85 ( ( ( (value) & 0x000000ffUL ) << 24 ) | \
86 ( ( (value) & 0x0000ff00UL ) << 8 ) | \
87 ( ( (value) & 0x00ff0000UL ) >> 8 ) | \
88 ( ( (value) & 0xff000000UL ) >> 24 ) )
89
90 /**
91 * Byte-swap a 64-bit constant
92 *
93 * @v value Constant value
94 * @ret swapped Byte-swapped value
95 */
96 #define __bswap_constant_64( value ) \
97 ( ( ( (value) & 0x00000000000000ffULL ) << 56 ) | \
98 ( ( (value) & 0x000000000000ff00ULL ) << 40 ) | \
99 ( ( (value) & 0x0000000000ff0000ULL ) << 24 ) | \
100 ( ( (value) & 0x00000000ff000000ULL ) << 8 ) | \
101 ( ( (value) & 0x000000ff00000000ULL ) >> 8 ) | \
102 ( ( (value) & 0x0000ff0000000000ULL ) >> 24 ) | \
103 ( ( (value) & 0x00ff000000000000ULL ) >> 40 ) | \
104 ( ( (value) & 0xff00000000000000ULL ) >> 56 ) )
105
106 /**
107 * Byte-swap a 16-bit value
108 *
109 * @v value Value
110 * @ret swapped Byte-swapped value
111 */
112 #define __bswap_16( value ) \
113 ( __builtin_constant_p (value) ? \
114 ( ( uint16_t ) __bswap_constant_16 ( ( uint16_t ) (value) ) ) \
115 : __bswap_variable_16 (value) )
116 #define bswap_16( value ) __bswap_16 (value)
117
118 /**
119 * Byte-swap a 32-bit value
120 *
121 * @v value Value
122 * @ret swapped Byte-swapped value
123 */
124 #define __bswap_32( value ) \
125 ( __builtin_constant_p (value) ? \
126 ( ( uint32_t ) __bswap_constant_32 ( ( uint32_t ) (value) ) ) \
127 : __bswap_variable_32 (value) )
128 #define bswap_32( value ) __bswap_32 (value)
129
130 /**
131 * Byte-swap a 64-bit value
132 *
133 * @v value Value
134 * @ret swapped Byte-swapped value
135 */
136 #define __bswap_64( value ) \
137 ( __builtin_constant_p (value) ? \
138 ( ( uint64_t ) __bswap_constant_64 ( ( uint64_t ) (value) ) ) \
139 : __bswap_variable_64 (value) )
140 #define bswap_64( value ) __bswap_64 (value)
141
142 #define __cpu_to_leNN( bits, value ) (value)
143 #define __cpu_to_beNN( bits, value ) __bswap_ ## bits (value)
144 #define __leNN_to_cpu( bits, value ) (value)
145 #define __beNN_to_cpu( bits, value ) __bswap_ ## bits (value)
146 #define __cpu_to_leNNs( bits, ptr ) do { } while ( 0 )
147 #define __cpu_to_beNNs( bits, ptr ) __bswap_ ## bits ## s (ptr)
148 #define __leNN_to_cpus( bits, ptr ) do { } while ( 0 )
149 #define __beNN_to_cpus( bits, ptr ) __bswap_ ## bits ## s (ptr)
150
151 #define cpu_to_le16( value ) __cpu_to_leNN ( 16, value )
152 #define cpu_to_le32( value ) __cpu_to_leNN ( 32, value )
153 #define cpu_to_le64( value ) __cpu_to_leNN ( 64, value )
154 #define cpu_to_be16( value ) __cpu_to_beNN ( 16, value )
155 #define cpu_to_be32( value ) __cpu_to_beNN ( 32, value )
156 #define cpu_to_be64( value ) __cpu_to_beNN ( 64, value )
157 #define le16_to_cpu( value ) __leNN_to_cpu ( 16, value )
158 #define le32_to_cpu( value ) __leNN_to_cpu ( 32, value )
159 #define le64_to_cpu( value ) __leNN_to_cpu ( 64, value )
160 #define be16_to_cpu( value ) __beNN_to_cpu ( 16, value )
161 #define be32_to_cpu( value ) __beNN_to_cpu ( 32, value )
162 #define be64_to_cpu( value ) __beNN_to_cpu ( 64, value )
163 #define cpu_to_le16s( ptr ) __cpu_to_leNNs ( 16, ptr )
164 #define cpu_to_le32s( ptr ) __cpu_to_leNNs ( 32, ptr )
165 #define cpu_to_le64s( ptr ) __cpu_to_leNNs ( 64, ptr )
166 #define cpu_to_be16s( ptr ) __cpu_to_beNNs ( 16, ptr )
167 #define cpu_to_be32s( ptr ) __cpu_to_beNNs ( 32, ptr )
168 #define cpu_to_be64s( ptr ) __cpu_to_beNNs ( 64, ptr )
169 #define le16_to_cpus( ptr ) __leNN_to_cpus ( 16, ptr )
170 #define le32_to_cpus( ptr ) __leNN_to_cpus ( 32, ptr )
171 #define le64_to_cpus( ptr ) __leNN_to_cpus ( 64, ptr )
172 #define be16_to_cpus( ptr ) __beNN_to_cpus ( 16, ptr )
173 #define be32_to_cpus( ptr ) __beNN_to_cpus ( 32, ptr )
174 #define be64_to_cpus( ptr ) __beNN_to_cpus ( 64, ptr )
175
176 #define htonll( value ) cpu_to_be64 (value)
177 #define ntohll( value ) be64_to_cpu (value)
178 #define htonl( value ) cpu_to_be32 (value)
179 #define ntohl( value ) be32_to_cpu (value)
180 #define htons( value ) cpu_to_be16 (value)
181 #define ntohs( value ) be16_to_cpu (value)
182
183 #endif /* _BITS_BYTESWAP_H */