byteorder.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * include/asm-xtensa/byteorder.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_BYTEORDER_H
  11. #define _XTENSA_BYTEORDER_H
  12. #include <asm/types.h>
  13. static __inline__ __attribute_const__ __u32 ___arch__swab32(__u32 x)
  14. {
  15. __u32 res;
  16. /* instruction sequence from Xtensa ISA release 2/2000 */
  17. __asm__("ssai 8 \n\t"
  18. "srli %0, %1, 16 \n\t"
  19. "src %0, %0, %1 \n\t"
  20. "src %0, %0, %0 \n\t"
  21. "src %0, %1, %0 \n"
  22. : "=&a" (res)
  23. : "a" (x)
  24. );
  25. return res;
  26. }
  27. static __inline__ __attribute_const__ __u16 ___arch__swab16(__u16 x)
  28. {
  29. /* Given that 'short' values are signed (i.e., can be negative),
  30. * we cannot assume that the upper 16-bits of the register are
  31. * zero. We are careful to mask values after shifting.
  32. */
  33. /* There exists an anomaly between xt-gcc and xt-xcc. xt-gcc
  34. * inserts an extui instruction after putting this function inline
  35. * to ensure that it uses only the least-significant 16 bits of
  36. * the result. xt-xcc doesn't use an extui, but assumes the
  37. * __asm__ macro follows convention that the upper 16 bits of an
  38. * 'unsigned short' result are still zero. This macro doesn't
  39. * follow convention; indeed, it leaves garbage in the upport 16
  40. * bits of the register.
  41. * Declaring the temporary variables 'res' and 'tmp' to be 32-bit
  42. * types while the return type of the function is a 16-bit type
  43. * forces both compilers to insert exactly one extui instruction
  44. * (or equivalent) to mask off the upper 16 bits. */
  45. __u32 res;
  46. __u32 tmp;
  47. __asm__("extui %1, %2, 8, 8\n\t"
  48. "slli %0, %2, 8 \n\t"
  49. "or %0, %0, %1 \n"
  50. : "=&a" (res), "=&a" (tmp)
  51. : "a" (x)
  52. );
  53. return res;
  54. }
  55. #define __arch__swab32(x) ___arch__swab32(x)
  56. #define __arch__swab16(x) ___arch__swab16(x)
  57. #if !defined(__STRICT_ANSI__) || defined(__KERNEL__)
  58. # define __BYTEORDER_HAS_U64__
  59. # define __SWAB_64_THRU_32__
  60. #endif
  61. #ifdef __XTENSA_EL__
  62. # include <linux/byteorder/little_endian.h>
  63. #elif defined(__XTENSA_EB__)
  64. # include <linux/byteorder/big_endian.h>
  65. #else
  66. # error processor byte order undefined!
  67. #endif
  68. #endif /* __ASM_XTENSA_BYTEORDER_H */