]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - wimboot/wimboot-2.7.3/src/byteswap.h
1 #ifndef _BITS_BYTESWAP_H
2 #define _BITS_BYTESWAP_H
6 * Byte-order swapping functions
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
) );
18 static inline __attribute__ (( always_inline
)) void
19 __bswap_16s ( uint16_t *x
) {
20 __asm__ ( "rorw $8, %0" : "+m" ( *x
) );
23 static inline __attribute__ (( always_inline
, const )) uint32_t
24 __bswap_variable_32 ( uint32_t x
) {
25 __asm__ ( "bswapl %k0" : "=r" ( x
) : "0" ( x
) );
29 static inline __attribute__ (( always_inline
)) void
30 __bswap_32s ( uint32_t *x
) {
31 __asm__ ( "bswapl %k0" : "=r" ( *x
) : "0" ( *x
) );
36 static inline __attribute__ (( always_inline
, const )) uint64_t
37 __bswap_variable_64 ( uint64_t x
) {
38 __asm__ ( "bswapq %q0" : "=r" ( x
) : "0" ( x
) );
42 #else /* __x86_64__ */
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
);
51 __asm__ ( "bswapl %0\n\t"
54 : "=r" ( out_high
), "=r" ( out_low
)
55 : "0" ( in_high
), "1" ( in_low
) );
57 return ( ( ( ( uint64_t ) out_high
) << 32 ) |
58 ( ( uint64_t ) out_low
) );
61 #endif /* __x86_64__ */
63 static inline __attribute__ (( always_inline
)) void
64 __bswap_64s ( uint64_t *x
) {
65 *x
= __bswap_variable_64 ( *x
);
69 * Byte-swap a 16-bit constant
71 * @v value Constant value
72 * @ret swapped Byte-swapped value
74 #define __bswap_constant_16( value ) \
75 ( ( ( (value) & 0x00ff ) << 8 ) | \
76 ( ( (value) & 0xff00 ) >> 8 ) )
79 * Byte-swap a 32-bit constant
81 * @v value Constant value
82 * @ret swapped Byte-swapped value
84 #define __bswap_constant_32( value ) \
85 ( ( ( (value) & 0x000000ffUL ) << 24 ) | \
86 ( ( (value) & 0x0000ff00UL ) << 8 ) | \
87 ( ( (value) & 0x00ff0000UL ) >> 8 ) | \
88 ( ( (value) & 0xff000000UL ) >> 24 ) )
91 * Byte-swap a 64-bit constant
93 * @v value Constant value
94 * @ret swapped Byte-swapped value
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 ) )
107 * Byte-swap a 16-bit value
110 * @ret swapped Byte-swapped value
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)
119 * Byte-swap a 32-bit value
122 * @ret swapped Byte-swapped value
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)
131 * Byte-swap a 64-bit value
134 * @ret swapped Byte-swapped value
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)
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)
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 )
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)
183 #endif /* _BITS_BYTESWAP_H */