memmove.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (C) 2008-2009 Michal Simek <[email protected]>
  3. * Copyright (C) 2008-2009 PetaLogix
  4. * Copyright (C) 2007 John Williams
  5. *
  6. * Reasonably optimised generic C-code for memcpy on Microblaze
  7. * This is generic C code to do efficient, alignment-aware memmove.
  8. *
  9. * It is based on demo code originally Copyright 2001 by Intel Corp, taken from
  10. * http://www.embedded.com/showArticle.jhtml?articleID=19205567
  11. *
  12. * Attempts were made, unsuccessfully, to contact the original
  13. * author of this code (Michael Morrow, Intel). Below is the original
  14. * copyright notice.
  15. *
  16. * This software has been developed by Intel Corporation.
  17. * Intel specifically disclaims all warranties, express or
  18. * implied, and all liability, including consequential and
  19. * other indirect damages, for the use of this program, including
  20. * liability for infringement of any proprietary rights,
  21. * and including the warranties of merchantability and fitness
  22. * for a particular purpose. Intel does not assume any
  23. * responsibility for and errors which may appear in this program
  24. * not any responsibility to update it.
  25. */
  26. #include <linux/export.h>
  27. #include <linux/types.h>
  28. #include <linux/stddef.h>
  29. #include <linux/compiler.h>
  30. #include <linux/string.h>
  31. #ifdef CONFIG_OPT_LIB_FUNCTION
  32. void *memmove(void *v_dst, const void *v_src, __kernel_size_t c)
  33. {
  34. const char *src = v_src;
  35. char *dst = v_dst;
  36. const uint32_t *i_src;
  37. uint32_t *i_dst;
  38. if (!c)
  39. return v_dst;
  40. /* Use memcpy when source is higher than dest */
  41. if (v_dst <= v_src)
  42. return memcpy(v_dst, v_src, c);
  43. /* The following code tries to optimize the copy by using unsigned
  44. * alignment. This will work fine if both source and destination are
  45. * aligned on the same boundary. However, if they are aligned on
  46. * different boundaries shifts will be necessary. This might result in
  47. * bad performance on MicroBlaze systems without a barrel shifter.
  48. */
  49. /* FIXME this part needs more test */
  50. /* Do a descending copy - this is a bit trickier! */
  51. dst += c;
  52. src += c;
  53. if (c >= 4) {
  54. unsigned value, buf_hold;
  55. /* Align the destination to a word boundary. */
  56. /* This is done in an endian independent manner. */
  57. switch ((unsigned long)dst & 3) {
  58. case 3:
  59. *--dst = *--src;
  60. --c;
  61. fallthrough;
  62. case 2:
  63. *--dst = *--src;
  64. --c;
  65. fallthrough;
  66. case 1:
  67. *--dst = *--src;
  68. --c;
  69. }
  70. i_dst = (void *)dst;
  71. /* Choose a copy scheme based on the source */
  72. /* alignment relative to destination. */
  73. switch ((unsigned long)src & 3) {
  74. case 0x0: /* Both byte offsets are aligned */
  75. i_src = (const void *)src;
  76. for (; c >= 4; c -= 4)
  77. *--i_dst = *--i_src;
  78. src = (const void *)i_src;
  79. break;
  80. case 0x1: /* Unaligned - Off by 1 */
  81. /* Word align the source */
  82. i_src = (const void *) (((unsigned)src + 4) & ~3);
  83. #ifndef __MICROBLAZEEL__
  84. /* Load the holding buffer */
  85. buf_hold = *--i_src >> 24;
  86. for (; c >= 4; c -= 4) {
  87. value = *--i_src;
  88. *--i_dst = buf_hold << 8 | value;
  89. buf_hold = value >> 24;
  90. }
  91. #else
  92. /* Load the holding buffer */
  93. buf_hold = (*--i_src & 0xFF) << 24;
  94. for (; c >= 4; c -= 4) {
  95. value = *--i_src;
  96. *--i_dst = buf_hold |
  97. ((value & 0xFFFFFF00) >> 8);
  98. buf_hold = (value & 0xFF) << 24;
  99. }
  100. #endif
  101. /* Realign the source */
  102. src = (const void *)i_src;
  103. src += 1;
  104. break;
  105. case 0x2: /* Unaligned - Off by 2 */
  106. /* Word align the source */
  107. i_src = (const void *) (((unsigned)src + 4) & ~3);
  108. #ifndef __MICROBLAZEEL__
  109. /* Load the holding buffer */
  110. buf_hold = *--i_src >> 16;
  111. for (; c >= 4; c -= 4) {
  112. value = *--i_src;
  113. *--i_dst = buf_hold << 16 | value;
  114. buf_hold = value >> 16;
  115. }
  116. #else
  117. /* Load the holding buffer */
  118. buf_hold = (*--i_src & 0xFFFF) << 16;
  119. for (; c >= 4; c -= 4) {
  120. value = *--i_src;
  121. *--i_dst = buf_hold |
  122. ((value & 0xFFFF0000) >> 16);
  123. buf_hold = (value & 0xFFFF) << 16;
  124. }
  125. #endif
  126. /* Realign the source */
  127. src = (const void *)i_src;
  128. src += 2;
  129. break;
  130. case 0x3: /* Unaligned - Off by 3 */
  131. /* Word align the source */
  132. i_src = (const void *) (((unsigned)src + 4) & ~3);
  133. #ifndef __MICROBLAZEEL__
  134. /* Load the holding buffer */
  135. buf_hold = *--i_src >> 8;
  136. for (; c >= 4; c -= 4) {
  137. value = *--i_src;
  138. *--i_dst = buf_hold << 24 | value;
  139. buf_hold = value >> 8;
  140. }
  141. #else
  142. /* Load the holding buffer */
  143. buf_hold = (*--i_src & 0xFFFFFF) << 8;
  144. for (; c >= 4; c -= 4) {
  145. value = *--i_src;
  146. *--i_dst = buf_hold |
  147. ((value & 0xFF000000) >> 24);
  148. buf_hold = (value & 0xFFFFFF) << 8;
  149. }
  150. #endif
  151. /* Realign the source */
  152. src = (const void *)i_src;
  153. src += 3;
  154. break;
  155. }
  156. dst = (void *)i_dst;
  157. }
  158. /* simple fast copy, ... unless a cache boundary is crossed */
  159. /* Finish off any remaining bytes */
  160. switch (c) {
  161. case 4:
  162. *--dst = *--src;
  163. fallthrough;
  164. case 3:
  165. *--dst = *--src;
  166. fallthrough;
  167. case 2:
  168. *--dst = *--src;
  169. fallthrough;
  170. case 1:
  171. *--dst = *--src;
  172. }
  173. return v_dst;
  174. }
  175. EXPORT_SYMBOL(memmove);
  176. #endif /* CONFIG_OPT_LIB_FUNCTION */