memcpy.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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, 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. #include <asm/system.h>
  32. #ifdef __HAVE_ARCH_MEMCPY
  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. #ifndef CONFIG_OPT_LIB_FUNCTION
  38. /* Simple, byte oriented memcpy. */
  39. while (c--)
  40. *dst++ = *src++;
  41. return v_dst;
  42. #else
  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. const uint32_t *i_src;
  50. uint32_t *i_dst;
  51. if (c >= 4) {
  52. unsigned value, buf_hold;
  53. /* Align the dstination to a word boundry. */
  54. /* This is done in an endian independant manner. */
  55. switch ((unsigned long)dst & 3) {
  56. case 1:
  57. *dst++ = *src++;
  58. --c;
  59. case 2:
  60. *dst++ = *src++;
  61. --c;
  62. case 3:
  63. *dst++ = *src++;
  64. --c;
  65. }
  66. i_dst = (void *)dst;
  67. /* Choose a copy scheme based on the source */
  68. /* alignment relative to dstination. */
  69. switch ((unsigned long)src & 3) {
  70. case 0x0: /* Both byte offsets are aligned */
  71. i_src = (const void *)src;
  72. for (; c >= 4; c -= 4)
  73. *i_dst++ = *i_src++;
  74. src = (const void *)i_src;
  75. break;
  76. case 0x1: /* Unaligned - Off by 1 */
  77. /* Word align the source */
  78. i_src = (const void *) ((unsigned)src & ~3);
  79. /* Load the holding buffer */
  80. buf_hold = *i_src++ << 8;
  81. for (; c >= 4; c -= 4) {
  82. value = *i_src++;
  83. *i_dst++ = buf_hold | value >> 24;
  84. buf_hold = value << 8;
  85. }
  86. /* Realign the source */
  87. src = (const void *)i_src;
  88. src -= 3;
  89. break;
  90. case 0x2: /* Unaligned - Off by 2 */
  91. /* Word align the source */
  92. i_src = (const void *) ((unsigned)src & ~3);
  93. /* Load the holding buffer */
  94. buf_hold = *i_src++ << 16;
  95. for (; c >= 4; c -= 4) {
  96. value = *i_src++;
  97. *i_dst++ = buf_hold | value >> 16;
  98. buf_hold = value << 16;
  99. }
  100. /* Realign the source */
  101. src = (const void *)i_src;
  102. src -= 2;
  103. break;
  104. case 0x3: /* Unaligned - Off by 3 */
  105. /* Word align the source */
  106. i_src = (const void *) ((unsigned)src & ~3);
  107. /* Load the holding buffer */
  108. buf_hold = *i_src++ << 24;
  109. for (; c >= 4; c -= 4) {
  110. value = *i_src++;
  111. *i_dst++ = buf_hold | value >> 8;
  112. buf_hold = value << 24;
  113. }
  114. /* Realign the source */
  115. src = (const void *)i_src;
  116. src -= 1;
  117. break;
  118. }
  119. dst = (void *)i_dst;
  120. }
  121. /* Finish off any remaining bytes */
  122. /* simple fast copy, ... unless a cache boundry is crossed */
  123. switch (c) {
  124. case 3:
  125. *dst++ = *src++;
  126. case 2:
  127. *dst++ = *src++;
  128. case 1:
  129. *dst++ = *src++;
  130. }
  131. return v_dst;
  132. #endif
  133. }
  134. EXPORT_SYMBOL(memcpy);
  135. #endif /* __HAVE_ARCH_MEMCPY */