memmove.c 7.4 KB

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