You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

byteswap.h 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #ifndef _BITS_BYTESWAP_H
  2. #define _BITS_BYTESWAP_H
  3. /** @file
  4. *
  5. * Byte-order swapping functions
  6. *
  7. */
  8. #include <stdint.h>
  9. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  10. static inline __attribute__ (( always_inline, const )) uint16_t
  11. __bswap_variable_16 ( uint16_t x ) {
  12. __asm__ ( "xchgb %b0,%h0" : "=q" ( x ) : "0" ( x ) );
  13. return x;
  14. }
  15. static inline __attribute__ (( always_inline )) void
  16. __bswap_16s ( uint16_t *x ) {
  17. __asm__ ( "rorw $8, %0" : "+m" ( *x ) );
  18. }
  19. static inline __attribute__ (( always_inline, const )) uint32_t
  20. __bswap_variable_32 ( uint32_t x ) {
  21. __asm__ ( "bswapl %0" : "=r" ( x ) : "0" ( x ) );
  22. return x;
  23. }
  24. static inline __attribute__ (( always_inline )) void
  25. __bswap_32s ( uint32_t *x ) {
  26. __asm__ ( "bswapl %0" : "=r" ( *x ) : "0" ( *x ) );
  27. }
  28. static inline __attribute__ (( always_inline, const )) uint64_t
  29. __bswap_variable_64 ( uint64_t x ) {
  30. uint32_t in_high = ( x >> 32 );
  31. uint32_t in_low = ( x & 0xffffffffUL );
  32. uint32_t out_high;
  33. uint32_t out_low;
  34. __asm__ ( "bswapl %0\n\t"
  35. "bswapl %1\n\t"
  36. "xchgl %0,%1\n\t"
  37. : "=r" ( out_high ), "=r" ( out_low )
  38. : "0" ( in_high ), "1" ( in_low ) );
  39. return ( ( ( ( uint64_t ) out_high ) << 32 ) |
  40. ( ( uint64_t ) out_low ) );
  41. }
  42. static inline __attribute__ (( always_inline )) void
  43. __bswap_64s ( uint64_t *x ) {
  44. struct {
  45. uint32_t __attribute__ (( may_alias )) low;
  46. uint32_t __attribute__ (( may_alias )) high;
  47. } __attribute__ (( may_alias )) *dwords = ( ( void * ) x );
  48. uint32_t discard;
  49. __asm__ ( "movl %0,%2\n\t"
  50. "bswapl %2\n\t"
  51. "xchgl %2,%1\n\t"
  52. "bswapl %2\n\t"
  53. "movl %2,%0\n\t"
  54. : "+m" ( dwords->low ), "+m" ( dwords->high ),
  55. "=r" ( discard ) );
  56. }
  57. #endif /* _BITS_BYTESWAP_H */