iPXE
byteswap.h
Go to the documentation of this file.
00001 #ifndef _BITS_BYTESWAP_H
00002 #define _BITS_BYTESWAP_H
00003 
00004 /** @file
00005  *
00006  * Byte-order swapping functions
00007  *
00008  */
00009 
00010 #include <stdint.h>
00011 
00012 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00013 
00014 static inline __attribute__ (( always_inline, const )) uint16_t
00015 __bswap_variable_16 ( uint16_t x ) {
00016         __asm__ ( "xchgb %b0,%h0" : "=q" ( x ) : "0" ( x ) );
00017         return x;
00018 }
00019 
00020 static inline __attribute__ (( always_inline )) void
00021 __bswap_16s ( uint16_t *x ) {
00022         __asm__ ( "rorw $8, %0" : "+m" ( *x ) );
00023 }
00024 
00025 static inline __attribute__ (( always_inline, const )) uint32_t
00026 __bswap_variable_32 ( uint32_t x ) {
00027         __asm__ ( "bswapl %0" : "=r" ( x ) : "0" ( x ) );
00028         return x;
00029 }
00030 
00031 static inline __attribute__ (( always_inline )) void
00032 __bswap_32s ( uint32_t *x ) {
00033         __asm__ ( "bswapl %0" : "=r" ( *x ) : "0" ( *x ) );
00034 }
00035 
00036 static inline __attribute__ (( always_inline, const )) uint64_t
00037 __bswap_variable_64 ( uint64_t x ) {
00038         uint32_t in_high = ( x >> 32 );
00039         uint32_t in_low = ( x & 0xffffffffUL );
00040         uint32_t out_high;
00041         uint32_t out_low;
00042 
00043         __asm__ ( "bswapl %0\n\t"
00044                   "bswapl %1\n\t"
00045                   "xchgl %0,%1\n\t"
00046                   : "=r" ( out_high ), "=r" ( out_low )
00047                   : "0" ( in_high ), "1" ( in_low ) );
00048 
00049         return ( ( ( ( uint64_t ) out_high ) << 32 ) |
00050                  ( ( uint64_t ) out_low ) );
00051 }
00052 
00053 static inline __attribute__ (( always_inline )) void
00054 __bswap_64s ( uint64_t *x ) {
00055         struct {
00056                 uint32_t __attribute__ (( may_alias )) low;
00057                 uint32_t __attribute__ (( may_alias )) high;
00058         } __attribute__ (( may_alias )) *dwords = ( ( void * ) x );
00059         uint32_t discard;
00060 
00061         __asm__ ( "movl %0,%2\n\t"
00062                   "bswapl %2\n\t"
00063                   "xchgl %2,%1\n\t"
00064                   "bswapl %2\n\t"
00065                   "movl %2,%0\n\t"
00066                   : "+m" ( dwords->low ), "+m" ( dwords->high ),
00067                     "=r" ( discard ) );
00068 }
00069 
00070 #endif /* _BITS_BYTESWAP_H */