memmove.c 7.3 KB

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