unaligned.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * include/asm-v850/unaligned.h -- Unaligned memory access
  3. *
  4. * Copyright (C) 2001 NEC Corporation
  5. * Copyright (C) 2001 Miles Bader <miles@gnu.org>
  6. *
  7. * This file is subject to the terms and conditions of the GNU General
  8. * Public License. See the file COPYING in the main directory of this
  9. * archive for more details.
  10. *
  11. * This file is a copy of the arm version, include/asm-arm/unaligned.h
  12. *
  13. * Note that some v850 chips support unaligned access, but it seems too
  14. * annoying to use.
  15. */
  16. #ifndef __V850_UNALIGNED_H__
  17. #define __V850_UNALIGNED_H__
  18. #include <asm/types.h>
  19. extern int __bug_unaligned_x(void *ptr);
  20. /*
  21. * What is the most efficient way of loading/storing an unaligned value?
  22. *
  23. * That is the subject of this file. Efficiency here is defined as
  24. * minimum code size with minimum register usage for the common cases.
  25. * It is currently not believed that long longs are common, so we
  26. * trade efficiency for the chars, shorts and longs against the long
  27. * longs.
  28. *
  29. * Current stats with gcc 2.7.2.2 for these functions:
  30. *
  31. * ptrsize get: code regs put: code regs
  32. * 1 1 1 1 2
  33. * 2 3 2 3 2
  34. * 4 7 3 7 3
  35. * 8 20 6 16 6
  36. *
  37. * gcc 2.95.1 seems to code differently:
  38. *
  39. * ptrsize get: code regs put: code regs
  40. * 1 1 1 1 2
  41. * 2 3 2 3 2
  42. * 4 7 4 7 4
  43. * 8 19 8 15 6
  44. *
  45. * which may or may not be more efficient (depending upon whether
  46. * you can afford the extra registers). Hopefully the gcc 2.95
  47. * is inteligent enough to decide if it is better to use the
  48. * extra register, but evidence so far seems to suggest otherwise.
  49. *
  50. * Unfortunately, gcc is not able to optimise the high word
  51. * out of long long >> 32, or the low word from long long << 32
  52. */
  53. #define __get_unaligned_2(__p) \
  54. (__p[0] | __p[1] << 8)
  55. #define __get_unaligned_4(__p) \
  56. (__p[0] | __p[1] << 8 | __p[2] << 16 | __p[3] << 24)
  57. #define get_unaligned(ptr) \
  58. ({ \
  59. __typeof__(*(ptr)) __v; \
  60. __u8 *__p = (__u8 *)(ptr); \
  61. switch (sizeof(*(ptr))) { \
  62. case 1: __v = *(ptr); break; \
  63. case 2: __v = __get_unaligned_2(__p); break; \
  64. case 4: __v = __get_unaligned_4(__p); break; \
  65. case 8: { \
  66. unsigned int __v1, __v2; \
  67. __v2 = __get_unaligned_4((__p+4)); \
  68. __v1 = __get_unaligned_4(__p); \
  69. __v = ((unsigned long long)__v2 << 32 | __v1); \
  70. } \
  71. break; \
  72. default: __v = __bug_unaligned_x(__p); break; \
  73. } \
  74. __v; \
  75. })
  76. static inline void __put_unaligned_2(__u32 __v, register __u8 *__p)
  77. {
  78. *__p++ = __v;
  79. *__p++ = __v >> 8;
  80. }
  81. static inline void __put_unaligned_4(__u32 __v, register __u8 *__p)
  82. {
  83. __put_unaligned_2(__v >> 16, __p + 2);
  84. __put_unaligned_2(__v, __p);
  85. }
  86. static inline void __put_unaligned_8(const unsigned long long __v, register __u8 *__p)
  87. {
  88. /*
  89. * tradeoff: 8 bytes of stack for all unaligned puts (2
  90. * instructions), or an extra register in the long long
  91. * case - go for the extra register.
  92. */
  93. __put_unaligned_4(__v >> 32, __p+4);
  94. __put_unaligned_4(__v, __p);
  95. }
  96. /*
  97. * Try to store an unaligned value as efficiently as possible.
  98. */
  99. #define put_unaligned(val,ptr) \
  100. ({ \
  101. switch (sizeof(*(ptr))) { \
  102. case 1: \
  103. *(ptr) = (val); \
  104. break; \
  105. case 2: __put_unaligned_2((val),(__u8 *)(ptr)); \
  106. break; \
  107. case 4: __put_unaligned_4((val),(__u8 *)(ptr)); \
  108. break; \
  109. case 8: __put_unaligned_8((val),(__u8 *)(ptr)); \
  110. break; \
  111. default: __bug_unaligned_x(ptr); \
  112. break; \
  113. } \
  114. (void) 0; \
  115. })
  116. #endif /* __V850_UNALIGNED_H__ */