memcpy.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 memcpy.
  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 __HAVE_ARCH_MEMCPY
  32. #ifndef CONFIG_OPT_LIB_FUNCTION
  33. void *memcpy(void *v_dst, const void *v_src, __kernel_size_t c)
  34. {
  35. const char *src = v_src;
  36. char *dst = v_dst;
  37. /* Simple, byte oriented memcpy. */
  38. while (c--)
  39. *dst++ = *src++;
  40. return v_dst;
  41. }
  42. #else /* CONFIG_OPT_LIB_FUNCTION */
  43. void *memcpy(void *v_dst, const void *v_src, __kernel_size_t c)
  44. {
  45. const char *src = v_src;
  46. char *dst = v_dst;
  47. /* The following code tries to optimize the copy by using unsigned
  48. * alignment. This will work fine if both source and destination are
  49. * aligned on the same boundary. However, if they are aligned on
  50. * different boundaries shifts will be necessary. This might result in
  51. * bad performance on MicroBlaze systems without a barrel shifter.
  52. */
  53. const uint32_t *i_src;
  54. uint32_t *i_dst;
  55. if (likely(c >= 4)) {
  56. unsigned value, buf_hold;
  57. /* Align the destination to a word boundary. */
  58. /* This is done in an endian independent manner. */
  59. switch ((unsigned long)dst & 3) {
  60. case 1:
  61. *dst++ = *src++;
  62. --c;
  63. case 2:
  64. *dst++ = *src++;
  65. --c;
  66. case 3:
  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 & ~3);
  83. #ifndef __MICROBLAZEEL__
  84. /* Load the holding buffer */
  85. buf_hold = *i_src++ << 8;
  86. for (; c >= 4; c -= 4) {
  87. value = *i_src++;
  88. *i_dst++ = buf_hold | value >> 24;
  89. buf_hold = value << 8;
  90. }
  91. #else
  92. /* Load the holding buffer */
  93. buf_hold = (*i_src++ & 0xFFFFFF00) >> 8;
  94. for (; c >= 4; c -= 4) {
  95. value = *i_src++;
  96. *i_dst++ = buf_hold | ((value & 0xFF) << 24);
  97. buf_hold = (value & 0xFFFFFF00) >> 8;
  98. }
  99. #endif
  100. /* Realign the source */
  101. src = (const void *)i_src;
  102. src -= 3;
  103. break;
  104. case 0x2: /* Unaligned - Off by 2 */
  105. /* Word align the source */
  106. i_src = (const void *) ((unsigned)src & ~3);
  107. #ifndef __MICROBLAZEEL__
  108. /* Load the holding buffer */
  109. buf_hold = *i_src++ << 16;
  110. for (; c >= 4; c -= 4) {
  111. value = *i_src++;
  112. *i_dst++ = buf_hold | value >> 16;
  113. buf_hold = value << 16;
  114. }
  115. #else
  116. /* Load the holding buffer */
  117. buf_hold = (*i_src++ & 0xFFFF0000) >> 16;
  118. for (; c >= 4; c -= 4) {
  119. value = *i_src++;
  120. *i_dst++ = buf_hold | ((value & 0xFFFF) << 16);
  121. buf_hold = (value & 0xFFFF0000) >> 16;
  122. }
  123. #endif
  124. /* Realign the source */
  125. src = (const void *)i_src;
  126. src -= 2;
  127. break;
  128. case 0x3: /* Unaligned - Off by 3 */
  129. /* Word align the source */
  130. i_src = (const void *) ((unsigned)src & ~3);
  131. #ifndef __MICROBLAZEEL__
  132. /* Load the holding buffer */
  133. buf_hold = *i_src++ << 24;
  134. for (; c >= 4; c -= 4) {
  135. value = *i_src++;
  136. *i_dst++ = buf_hold | value >> 8;
  137. buf_hold = value << 24;
  138. }
  139. #else
  140. /* Load the holding buffer */
  141. buf_hold = (*i_src++ & 0xFF000000) >> 24;
  142. for (; c >= 4; c -= 4) {
  143. value = *i_src++;
  144. *i_dst++ = buf_hold | ((value & 0xFFFFFF) << 8);
  145. buf_hold = (value & 0xFF000000) >> 24;
  146. }
  147. #endif
  148. /* Realign the source */
  149. src = (const void *)i_src;
  150. src -= 1;
  151. break;
  152. }
  153. dst = (void *)i_dst;
  154. }
  155. /* Finish off any remaining bytes */
  156. /* simple fast copy, ... unless a cache boundary is crossed */
  157. switch (c) {
  158. case 3:
  159. *dst++ = *src++;
  160. case 2:
  161. *dst++ = *src++;
  162. case 1:
  163. *dst++ = *src++;
  164. }
  165. return v_dst;
  166. }
  167. #endif /* CONFIG_OPT_LIB_FUNCTION */
  168. EXPORT_SYMBOL(memcpy);
  169. #endif /* __HAVE_ARCH_MEMCPY */