memcpy.c 4.6 KB

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