swab.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. #ifndef _PARISC_SWAB_H
  3. #define _PARISC_SWAB_H
  4. #include <asm/bitsperlong.h>
  5. #include <linux/types.h>
  6. #include <linux/compiler.h>
  7. #define __SWAB_64_THRU_32__
  8. static inline __attribute_const__ __u16 __arch_swab16(__u16 x)
  9. {
  10. __asm__("dep %0, 15, 8, %0\n\t" /* deposit 00ab -> 0bab */
  11. "shd %%r0, %0, 8, %0" /* shift 000000ab -> 00ba */
  12. : "=r" (x)
  13. : "0" (x));
  14. return x;
  15. }
  16. #define __arch_swab16 __arch_swab16
  17. static inline __attribute_const__ __u32 __arch_swab24(__u32 x)
  18. {
  19. __asm__("shd %0, %0, 8, %0\n\t" /* shift xabcxabc -> cxab */
  20. "dep %0, 15, 8, %0\n\t" /* deposit cxab -> cbab */
  21. "shd %%r0, %0, 8, %0" /* shift 0000cbab -> 0cba */
  22. : "=r" (x)
  23. : "0" (x));
  24. return x;
  25. }
  26. static inline __attribute_const__ __u32 __arch_swab32(__u32 x)
  27. {
  28. unsigned int temp;
  29. __asm__("shd %0, %0, 16, %1\n\t" /* shift abcdabcd -> cdab */
  30. "dep %1, 15, 8, %1\n\t" /* deposit cdab -> cbab */
  31. "shd %0, %1, 8, %0" /* shift abcdcbab -> dcba */
  32. : "=r" (x), "=&r" (temp)
  33. : "0" (x));
  34. return x;
  35. }
  36. #define __arch_swab32 __arch_swab32
  37. #if __BITS_PER_LONG > 32
  38. /*
  39. ** From "PA-RISC 2.0 Architecture", HP Professional Books.
  40. ** See Appendix I page 8 , "Endian Byte Swapping".
  41. **
  42. ** Pretty cool algorithm: (* == zero'd bits)
  43. ** PERMH 01234567 -> 67452301 into %0
  44. ** HSHL 67452301 -> 7*5*3*1* into %1
  45. ** HSHR 67452301 -> *6*4*2*0 into %0
  46. ** OR %0 | %1 -> 76543210 into %0 (all done!)
  47. */
  48. static inline __attribute_const__ __u64 __arch_swab64(__u64 x)
  49. {
  50. __u64 temp;
  51. __asm__("permh,3210 %0, %0\n\t"
  52. "hshl %0, 8, %1\n\t"
  53. "hshr,u %0, 8, %0\n\t"
  54. "or %1, %0, %0"
  55. : "=r" (x), "=&r" (temp)
  56. : "0" (x));
  57. return x;
  58. }
  59. #define __arch_swab64 __arch_swab64
  60. #endif /* __BITS_PER_LONG > 32 */
  61. #endif /* _PARISC_SWAB_H */