unaligned.h 3.5 KB

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