memmove.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* Copy memory to memory until the specified number of bytes
  2. has been copied. Overlap is handled correctly.
  3. Copyright (C) 1991, 1995, 1996, 1997, 2003 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Torbjorn Granlund (tege@sics.se).
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, see
  16. <http://www.gnu.org/licenses/>. */
  17. #include <string.h>
  18. #include "memcopy.h"
  19. #include "pagecopy.h"
  20. #ifdef __ARCH_HAS_BWD_MEMCPY__
  21. /* generic-opt memmove assumes memcpy does forward copying! */
  22. #include "_memcpy_fwd.c"
  23. #endif
  24. static void _wordcopy_bwd_aligned (long int dstp, long int srcp, size_t len)
  25. {
  26. op_t a0 = 0;
  27. op_t a1 = 0;
  28. switch (len % 8)
  29. {
  30. case 2:
  31. srcp -= 2 * OPSIZ;
  32. dstp -= 1 * OPSIZ;
  33. a0 = ((op_t *) srcp)[1];
  34. len += 6;
  35. goto do1;
  36. case 3:
  37. srcp -= 3 * OPSIZ;
  38. dstp -= 2 * OPSIZ;
  39. a1 = ((op_t *) srcp)[2];
  40. len += 5;
  41. goto do2;
  42. case 4:
  43. srcp -= 4 * OPSIZ;
  44. dstp -= 3 * OPSIZ;
  45. a0 = ((op_t *) srcp)[3];
  46. len += 4;
  47. goto do3;
  48. case 5:
  49. srcp -= 5 * OPSIZ;
  50. dstp -= 4 * OPSIZ;
  51. a1 = ((op_t *) srcp)[4];
  52. len += 3;
  53. goto do4;
  54. case 6:
  55. srcp -= 6 * OPSIZ;
  56. dstp -= 5 * OPSIZ;
  57. a0 = ((op_t *) srcp)[5];
  58. len += 2;
  59. goto do5;
  60. case 7:
  61. srcp -= 7 * OPSIZ;
  62. dstp -= 6 * OPSIZ;
  63. a1 = ((op_t *) srcp)[6];
  64. len += 1;
  65. goto do6;
  66. case 0:
  67. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  68. return;
  69. srcp -= 8 * OPSIZ;
  70. dstp -= 7 * OPSIZ;
  71. a0 = ((op_t *) srcp)[7];
  72. goto do7;
  73. case 1:
  74. srcp -= 9 * OPSIZ;
  75. dstp -= 8 * OPSIZ;
  76. a1 = ((op_t *) srcp)[8];
  77. len -= 1;
  78. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  79. goto do0;
  80. goto do8; /* No-op. */
  81. }
  82. do
  83. {
  84. do8:
  85. a0 = ((op_t *) srcp)[7];
  86. ((op_t *) dstp)[7] = a1;
  87. do7:
  88. a1 = ((op_t *) srcp)[6];
  89. ((op_t *) dstp)[6] = a0;
  90. do6:
  91. a0 = ((op_t *) srcp)[5];
  92. ((op_t *) dstp)[5] = a1;
  93. do5:
  94. a1 = ((op_t *) srcp)[4];
  95. ((op_t *) dstp)[4] = a0;
  96. do4:
  97. a0 = ((op_t *) srcp)[3];
  98. ((op_t *) dstp)[3] = a1;
  99. do3:
  100. a1 = ((op_t *) srcp)[2];
  101. ((op_t *) dstp)[2] = a0;
  102. do2:
  103. a0 = ((op_t *) srcp)[1];
  104. ((op_t *) dstp)[1] = a1;
  105. do1:
  106. a1 = ((op_t *) srcp)[0];
  107. ((op_t *) dstp)[0] = a0;
  108. srcp -= 8 * OPSIZ;
  109. dstp -= 8 * OPSIZ;
  110. len -= 8;
  111. }
  112. while (len != 0);
  113. /* This is the right position for do0. Please don't move
  114. it into the loop. */
  115. do0:
  116. ((op_t *) dstp)[7] = a1;
  117. }
  118. /* _wordcopy_bwd_dest_aligned -- Copy block finishing right
  119. before SRCP to block finishing right before DSTP with LEN `op_t'
  120. words (not LEN bytes!). DSTP should be aligned for memory
  121. operations on `op_t', but SRCP must *not* be aligned. */
  122. static void _wordcopy_bwd_dest_aligned (long int dstp, long int srcp, size_t len)
  123. {
  124. op_t a0 = 0;
  125. op_t a1 = 0;
  126. op_t a2 = 0;
  127. op_t a3 = 0;
  128. int sh_1, sh_2;
  129. /* Calculate how to shift a word read at the memory operation
  130. aligned srcp to make it aligned for copy. */
  131. sh_1 = 8 * (srcp % OPSIZ);
  132. sh_2 = 8 * OPSIZ - sh_1;
  133. /* Make srcp aligned by rounding it down to the beginning of the op_t
  134. it points in the middle of. */
  135. srcp &= -OPSIZ;
  136. srcp += OPSIZ;
  137. switch (len % 4)
  138. {
  139. case 2:
  140. srcp -= 3 * OPSIZ;
  141. dstp -= 1 * OPSIZ;
  142. a2 = ((op_t *) srcp)[2];
  143. a1 = ((op_t *) srcp)[1];
  144. len += 2;
  145. goto do1;
  146. case 3:
  147. srcp -= 4 * OPSIZ;
  148. dstp -= 2 * OPSIZ;
  149. a3 = ((op_t *) srcp)[3];
  150. a2 = ((op_t *) srcp)[2];
  151. len += 1;
  152. goto do2;
  153. case 0:
  154. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  155. return;
  156. srcp -= 5 * OPSIZ;
  157. dstp -= 3 * OPSIZ;
  158. a0 = ((op_t *) srcp)[4];
  159. a3 = ((op_t *) srcp)[3];
  160. goto do3;
  161. case 1:
  162. srcp -= 6 * OPSIZ;
  163. dstp -= 4 * OPSIZ;
  164. a1 = ((op_t *) srcp)[5];
  165. a0 = ((op_t *) srcp)[4];
  166. len -= 1;
  167. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  168. goto do0;
  169. goto do4; /* No-op. */
  170. }
  171. do
  172. {
  173. do4:
  174. a3 = ((op_t *) srcp)[3];
  175. ((op_t *) dstp)[3] = MERGE (a0, sh_1, a1, sh_2);
  176. do3:
  177. a2 = ((op_t *) srcp)[2];
  178. ((op_t *) dstp)[2] = MERGE (a3, sh_1, a0, sh_2);
  179. do2:
  180. a1 = ((op_t *) srcp)[1];
  181. ((op_t *) dstp)[1] = MERGE (a2, sh_1, a3, sh_2);
  182. do1:
  183. a0 = ((op_t *) srcp)[0];
  184. ((op_t *) dstp)[0] = MERGE (a1, sh_1, a2, sh_2);
  185. srcp -= 4 * OPSIZ;
  186. dstp -= 4 * OPSIZ;
  187. len -= 4;
  188. }
  189. while (len != 0);
  190. /* This is the right position for do0. Please don't move
  191. it into the loop. */
  192. do0:
  193. ((op_t *) dstp)[3] = MERGE (a0, sh_1, a1, sh_2);
  194. }
  195. void *memmove (void *dest, const void *src, size_t len)
  196. {
  197. unsigned long int dstp = (long int) dest;
  198. unsigned long int srcp = (long int) src;
  199. /* This test makes the forward copying code be used whenever possible.
  200. Reduces the working set. */
  201. if (dstp - srcp >= len) /* *Unsigned* compare! */
  202. {
  203. #ifndef __ARCH_HAS_BWD_MEMCPY__
  204. /* Backward memcpy implementation cannot be used */
  205. memcpy(dest, src, len);
  206. #else
  207. /* Copy from the beginning to the end. */
  208. /* If there not too few bytes to copy, use word copy. */
  209. if (len >= OP_T_THRES)
  210. {
  211. /* Copy just a few bytes to make DSTP aligned. */
  212. len -= (-dstp) % OPSIZ;
  213. BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
  214. /* Copy whole pages from SRCP to DSTP by virtual address
  215. manipulation, as much as possible. */
  216. PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);
  217. /* Copy from SRCP to DSTP taking advantage of the known
  218. alignment of DSTP. Number of bytes remaining is put
  219. in the third argument, i.e. in LEN. This number may
  220. vary from machine to machine. */
  221. WORD_COPY_FWD (dstp, srcp, len, len);
  222. /* Fall out and copy the tail. */
  223. }
  224. /* There are just a few bytes to copy. Use byte memory operations. */
  225. BYTE_COPY_FWD (dstp, srcp, len);
  226. #endif
  227. }
  228. else
  229. {
  230. /* Copy from the end to the beginning. */
  231. srcp += len;
  232. dstp += len;
  233. /* If there not too few bytes to copy, use word copy. */
  234. if (len >= OP_T_THRES)
  235. {
  236. /* Copy just a few bytes to make DSTP aligned. */
  237. len -= dstp % OPSIZ;
  238. BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ);
  239. /* Copy from SRCP to DSTP taking advantage of the known
  240. alignment of DSTP. Number of bytes remaining is put
  241. in the third argument, i.e. in LEN. This number may
  242. vary from machine to machine. */
  243. WORD_COPY_BWD (dstp, srcp, len, len);
  244. /* Fall out and copy the tail. */
  245. }
  246. /* There are just a few bytes to copy. Use byte memory operations. */
  247. BYTE_COPY_BWD (dstp, srcp, len);
  248. }
  249. return (dest);
  250. }
  251. libc_hidden_weak(memmove)