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.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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__ ( "rev16 %0, %1" : "=l" ( x ) : "l" ( x ) );
  13. return x;
  14. }
  15. static inline __attribute__ (( always_inline )) void
  16. __bswap_16s ( uint16_t *x ) {
  17. *x = __bswap_variable_16 ( *x );
  18. }
  19. static inline __attribute__ (( always_inline, const )) uint32_t
  20. __bswap_variable_32 ( uint32_t x ) {
  21. __asm__ ( "rev %0, %1" : "=l" ( x ) : "l" ( x ) );
  22. return x;
  23. }
  24. static inline __attribute__ (( always_inline )) void
  25. __bswap_32s ( uint32_t *x ) {
  26. *x = __bswap_variable_32 ( *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 = __bswap_variable_32 ( in_low );
  33. uint32_t out_low = __bswap_variable_32 ( in_high );
  34. return ( ( ( ( uint64_t ) out_high ) << 32 ) |
  35. ( ( uint64_t ) out_low ) );
  36. }
  37. static inline __attribute__ (( always_inline )) void
  38. __bswap_64s ( uint64_t *x ) {
  39. *x = __bswap_variable_64 ( *x );
  40. }
  41. #endif