swab.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * include/asm-xtensa/swab.h
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2001 - 2005 Tensilica Inc.
  9. */
  10. #ifndef _XTENSA_SWAB_H
  11. #define _XTENSA_SWAB_H
  12. #include <linux/types.h>
  13. #include <linux/compiler.h>
  14. #define __SWAB_64_THRU_32__
  15. static inline __attribute_const__ __u32 __arch_swab32(__u32 x)
  16. {
  17. __u32 res;
  18. /* instruction sequence from Xtensa ISA release 2/2000 */
  19. __asm__("ssai 8 \n\t"
  20. "srli %0, %1, 16 \n\t"
  21. "src %0, %0, %1 \n\t"
  22. "src %0, %0, %0 \n\t"
  23. "src %0, %1, %0 \n"
  24. : "=&a" (res)
  25. : "a" (x)
  26. );
  27. return res;
  28. }
  29. #define __arch_swab32 __arch_swab32
  30. static inline __attribute_const__ __u16 __arch_swab16(__u16 x)
  31. {
  32. /* Given that 'short' values are signed (i.e., can be negative),
  33. * we cannot assume that the upper 16-bits of the register are
  34. * zero. We are careful to mask values after shifting.
  35. */
  36. /* There exists an anomaly between xt-gcc and xt-xcc. xt-gcc
  37. * inserts an extui instruction after putting this function inline
  38. * to ensure that it uses only the least-significant 16 bits of
  39. * the result. xt-xcc doesn't use an extui, but assumes the
  40. * __asm__ macro follows convention that the upper 16 bits of an
  41. * 'unsigned short' result are still zero. This macro doesn't
  42. * follow convention; indeed, it leaves garbage in the upport 16
  43. * bits of the register.
  44. * Declaring the temporary variables 'res' and 'tmp' to be 32-bit
  45. * types while the return type of the function is a 16-bit type
  46. * forces both compilers to insert exactly one extui instruction
  47. * (or equivalent) to mask off the upper 16 bits. */
  48. __u32 res;
  49. __u32 tmp;
  50. __asm__("extui %1, %2, 8, 8\n\t"
  51. "slli %0, %2, 8 \n\t"
  52. "or %0, %0, %1 \n"
  53. : "=&a" (res), "=&a" (tmp)
  54. : "a" (x)
  55. );
  56. return res;
  57. }
  58. #define __arch_swab16 __arch_swab16
  59. #endif /* _XTENSA_SWAB_H */