17c478bd9Sstevel@tonic-gate #ifndef ETHERBOOT_BITS_BYTESWAP_H
27c478bd9Sstevel@tonic-gate #define ETHERBOOT_BITS_BYTESWAP_H
37c478bd9Sstevel@tonic-gate 
47c478bd9Sstevel@tonic-gate #include "types.h"
__i386_bswap_16(uint16_t x)57c478bd9Sstevel@tonic-gate static inline uint16_t __i386_bswap_16(uint16_t x)
67c478bd9Sstevel@tonic-gate {
77c478bd9Sstevel@tonic-gate 	__asm__("xchgb %b0,%h0\n\t"
87c478bd9Sstevel@tonic-gate 		: "=q" (x)
97c478bd9Sstevel@tonic-gate 		: "0" (x));
107c478bd9Sstevel@tonic-gate 	return x;
117c478bd9Sstevel@tonic-gate }
127c478bd9Sstevel@tonic-gate 
__i386_bswap_32(uint32_t x)137c478bd9Sstevel@tonic-gate static inline uint32_t __i386_bswap_32(uint32_t x)
147c478bd9Sstevel@tonic-gate {
157c478bd9Sstevel@tonic-gate 	__asm__("xchgb %b0,%h0\n\t"
167c478bd9Sstevel@tonic-gate 		"rorl $16,%0\n\t"
177c478bd9Sstevel@tonic-gate 		"xchgb %b0,%h0"
187c478bd9Sstevel@tonic-gate 		: "=q" (x)
197c478bd9Sstevel@tonic-gate 		: "0" (x));
207c478bd9Sstevel@tonic-gate 	return x;
217c478bd9Sstevel@tonic-gate }
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #define __bswap_constant_16(x) \
257c478bd9Sstevel@tonic-gate 	((uint16_t)((((uint16_t)(x) & 0x00ff) << 8) | \
267c478bd9Sstevel@tonic-gate 		(((uint16_t)(x) & 0xff00) >> 8)))
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #define __bswap_constant_32(x) \
297c478bd9Sstevel@tonic-gate 	((uint32_t)((((uint32_t)(x) & 0x000000ffU) << 24) | \
307c478bd9Sstevel@tonic-gate 		(((uint32_t)(x) & 0x0000ff00U) <<  8) | \
317c478bd9Sstevel@tonic-gate 		(((uint32_t)(x) & 0x00ff0000U) >>  8) | \
327c478bd9Sstevel@tonic-gate 		(((uint32_t)(x) & 0xff000000U) >> 24)))
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #define __bswap_16(x) \
357c478bd9Sstevel@tonic-gate 	(__builtin_constant_p(x) ? \
367c478bd9Sstevel@tonic-gate 	__bswap_constant_16(x) : \
377c478bd9Sstevel@tonic-gate 	__i386_bswap_16(x))
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define __bswap_32(x) \
417c478bd9Sstevel@tonic-gate 	(__builtin_constant_p(x) ? \
427c478bd9Sstevel@tonic-gate 	__bswap_constant_32(x) : \
437c478bd9Sstevel@tonic-gate 	__i386_bswap_32(x))
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #endif /* ETHERBOOT_BITS_BYTESWAP_H */
47