byteswap.h
Go to the documentation of this file.00001 #ifndef ETHERBOOT_BITS_BYTESWAP_H
00002 #define ETHERBOOT_BITS_BYTESWAP_H
00003
00004 FILE_LICENCE ( GPL2_OR_LATER );
00005
00006 static inline __attribute__ ((always_inline, const)) uint16_t
00007 __bswap_variable_16(uint16_t x)
00008 {
00009 __asm__("xchgb %b0,%h0\n\t"
00010 : "=q" (x)
00011 : "0" (x));
00012 return x;
00013 }
00014
00015 static inline __attribute__ ((always_inline, const)) uint32_t
00016 __bswap_variable_32(uint32_t x)
00017 {
00018 __asm__("xchgb %b0,%h0\n\t"
00019 "rorl $16,%0\n\t"
00020 "xchgb %b0,%h0"
00021 : "=q" (x)
00022 : "0" (x));
00023 return x;
00024 }
00025
00026 static inline __attribute__ ((always_inline, const)) uint64_t
00027 __bswap_variable_64(uint64_t x)
00028 {
00029 union {
00030 uint64_t qword;
00031 uint32_t dword[2];
00032 } u;
00033
00034 u.qword = x;
00035 u.dword[0] = __bswap_variable_32(u.dword[0]);
00036 u.dword[1] = __bswap_variable_32(u.dword[1]);
00037 __asm__("xchgl %0,%1"
00038 : "=r" ( u.dword[0] ), "=r" ( u.dword[1] )
00039 : "0" ( u.dword[0] ), "1" ( u.dword[1] ) );
00040 return u.qword;
00041 }
00042
00043 #endif