byteorder.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef _ASM_X86_BYTEORDER_H
  2. #define _ASM_X86_BYTEORDER_H
  3. #include <asm/types.h>
  4. #include <linux/compiler.h>
  5. #ifdef __GNUC__
  6. #ifdef __i386__
  7. static __inline__ __attribute_const__ __u32 ___arch__swab32(__u32 x)
  8. {
  9. #ifdef CONFIG_X86_BSWAP
  10. __asm__("bswap %0" : "=r" (x) : "0" (x));
  11. #else
  12. __asm__("xchgb %b0,%h0\n\t" /* swap lower bytes */
  13. "rorl $16,%0\n\t" /* swap words */
  14. "xchgb %b0,%h0" /* swap higher bytes */
  15. :"=q" (x)
  16. : "0" (x));
  17. #endif
  18. return x;
  19. }
  20. static __inline__ __attribute_const__ __u64 ___arch__swab64(__u64 val)
  21. {
  22. union {
  23. struct { __u32 a,b; } s;
  24. __u64 u;
  25. } v;
  26. v.u = val;
  27. #ifdef CONFIG_X86_BSWAP
  28. __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1"
  29. : "=r" (v.s.a), "=r" (v.s.b)
  30. : "0" (v.s.a), "1" (v.s.b));
  31. #else
  32. v.s.a = ___arch__swab32(v.s.a);
  33. v.s.b = ___arch__swab32(v.s.b);
  34. __asm__("xchgl %0,%1" : "=r" (v.s.a), "=r" (v.s.b) : "0" (v.s.a), "1" (v.s.b));
  35. #endif
  36. return v.u;
  37. }
  38. #else /* __i386__ */
  39. static __inline__ __attribute_const__ __u64 ___arch__swab64(__u64 x)
  40. {
  41. __asm__("bswapq %0" : "=r" (x) : "0" (x));
  42. return x;
  43. }
  44. static __inline__ __attribute_const__ __u32 ___arch__swab32(__u32 x)
  45. {
  46. __asm__("bswapl %0" : "=r" (x) : "0" (x));
  47. return x;
  48. }
  49. #endif
  50. /* Do not define swab16. Gcc is smart enough to recognize "C" version and
  51. convert it into rotation or exhange. */
  52. #define __arch__swab64(x) ___arch__swab64(x)
  53. #define __arch__swab32(x) ___arch__swab32(x)
  54. #define __BYTEORDER_HAS_U64__
  55. #endif /* __GNUC__ */
  56. #include <linux/byteorder/little_endian.h>
  57. #endif /* _ASM_X86_BYTEORDER_H */