memmove.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
  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, unsuccesfully, 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/types.h>
  27. #include <linux/stddef.h>
  28. #include <linux/compiler.h>
  29. #include <linux/module.h>
  30. #include <linux/string.h>
  31. #ifdef __HAVE_ARCH_MEMMOVE
  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. #ifdef CONFIG_OPT_LIB_FUNCTION
  37. const uint32_t *i_src;
  38. uint32_t *i_dst;
  39. #endif
  40. if (!c)
  41. return v_dst;
  42. /* Use memcpy when source is higher than dest */
  43. if (v_dst <= v_src)
  44. return memcpy(v_dst, v_src, c);
  45. #ifndef CONFIG_OPT_LIB_FUNCTION
  46. /* copy backwards, from end to beginning */
  47. src += c;
  48. dst += c;
  49. /* Simple, byte oriented memmove. */
  50. while (c--)
  51. *--dst = *--src;
  52. return v_dst;
  53. #else
  54. /* The following code tries to optimize the copy by using unsigned
  55. * alignment. This will work fine if both source and destination are
  56. * aligned on the same boundary. However, if they are aligned on
  57. * different boundaries shifts will be necessary. This might result in
  58. * bad performance on MicroBlaze systems without a barrel shifter.
  59. */
  60. /* FIXME this part needs more test */
  61. /* Do a descending copy - this is a bit trickier! */
  62. dst += c;
  63. src += c;
  64. if (c >= 4) {
  65. unsigned value, buf_hold;
  66. /* Align the destination to a word boundry. */
  67. /* This is done in an endian independant manner. */
  68. switch ((unsigned long)dst & 3) {
  69. case 3:
  70. *--dst = *--src;
  71. --c;
  72. case 2:
  73. *--dst = *--src;
  74. --c;
  75. case 1:
  76. *--dst = *--src;
  77. --c;
  78. }
  79. i_dst = (void *)dst;
  80. /* Choose a copy scheme based on the source */
  81. /* alignment relative to dstination. */
  82. switch ((unsigned long)src & 3) {
  83. case 0x0: /* Both byte offsets are aligned */
  84. i_src = (const void *)src;
  85. for (; c >= 4; c -= 4)
  86. *--i_dst = *--i_src;
  87. src = (const void *)i_src;
  88. break;
  89. case 0x1: /* Unaligned - Off by 1 */
  90. /* Word align the source */
  91. i_src = (const void *) (((unsigned)src + 4) & ~3);
  92. /* Load the holding buffer */
  93. buf_hold = *--i_src >> 24;
  94. for (; c >= 4; c -= 4) {
  95. value = *--i_src;
  96. *--i_dst = buf_hold << 8 | value;
  97. buf_hold = value >> 24;
  98. }
  99. /* Realign the source */
  100. src = (const void *)i_src;
  101. src += 1;
  102. break;
  103. case 0x2: /* Unaligned - Off by 2 */
  104. /* Word align the source */
  105. i_src = (const void *) (((unsigned)src + 4) & ~3);
  106. /* Load the holding buffer */
  107. buf_hold = *--i_src >> 16;
  108. for (; c >= 4; c -= 4) {
  109. value = *--i_src;
  110. *--i_dst = buf_hold << 16 | value;
  111. buf_hold = value >> 16;
  112. }
  113. /* Realign the source */
  114. src = (const void *)i_src;
  115. src += 2;
  116. break;
  117. case 0x3: /* Unaligned - Off by 3 */
  118. /* Word align the source */
  119. i_src = (const void *) (((unsigned)src + 4) & ~3);
  120. /* Load the holding buffer */
  121. buf_hold = *--i_src >> 8;
  122. for (; c >= 4; c -= 4) {
  123. value = *--i_src;
  124. *--i_dst = buf_hold << 24 | value;
  125. buf_hold = value >> 8;
  126. }
  127. /* Realign the source */
  128. src = (const void *)i_src;
  129. src += 3;
  130. break;
  131. }
  132. dst = (void *)i_dst;
  133. }
  134. /* simple fast copy, ... unless a cache boundry is crossed */
  135. /* Finish off any remaining bytes */
  136. switch (c) {
  137. case 4:
  138. *--dst = *--src;
  139. case 3:
  140. *--dst = *--src;
  141. case 2:
  142. *--dst = *--src;
  143. case 1:
  144. *--dst = *--src;
  145. }
  146. return v_dst;
  147. #endif
  148. }
  149. EXPORT_SYMBOL(memmove);
  150. #endif /* __HAVE_ARCH_MEMMOVE */