memmove.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #if defined(__ARCH_HAS_BWD_MEMCPY__) || defined(__mips__)
  21. /* generic-opt memmove assumes memcpy does forward copying! */
  22. /* also needed for MIPS as its memcpy does not support overlapping regions */
  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. /* Calling memcpy() from memmove() should be skipped in two cases:
  205. * a) if arch's memcpy uses a backward copying (SH4)
  206. * b) if arch's memcpy is not fully safe for overlapping regions (MIPS)
  207. */
  208. #if !defined(__ARCH_HAS_BWD_MEMCPY_) && !defined(__mips__)
  209. memcpy(dest, src, len);
  210. #else
  211. /* Copy from the beginning to the end. */
  212. /* If there not too few bytes to copy, use word copy. */
  213. if (len >= OP_T_THRES)
  214. {
  215. /* Copy just a few bytes to make DSTP aligned. */
  216. len -= (-dstp) % OPSIZ;
  217. BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
  218. /* Copy whole pages from SRCP to DSTP by virtual address
  219. manipulation, as much as possible. */
  220. PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);
  221. /* Copy from SRCP to DSTP taking advantage of the known
  222. alignment of DSTP. Number of bytes remaining is put
  223. in the third argument, i.e. in LEN. This number may
  224. vary from machine to machine. */
  225. WORD_COPY_FWD (dstp, srcp, len, len);
  226. /* Fall out and copy the tail. */
  227. }
  228. /* There are just a few bytes to copy. Use byte memory operations. */
  229. BYTE_COPY_FWD (dstp, srcp, len);
  230. #endif
  231. }
  232. else
  233. {
  234. /* Copy from the end to the beginning. */
  235. srcp += len;
  236. dstp += len;
  237. /* If there not too few bytes to copy, use word copy. */
  238. if (len >= OP_T_THRES)
  239. {
  240. /* Copy just a few bytes to make DSTP aligned. */
  241. len -= dstp % OPSIZ;
  242. BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ);
  243. /* Copy from SRCP to DSTP taking advantage of the known
  244. alignment of DSTP. Number of bytes remaining is put
  245. in the third argument, i.e. in LEN. This number may
  246. vary from machine to machine. */
  247. WORD_COPY_BWD (dstp, srcp, len, len);
  248. /* Fall out and copy the tail. */
  249. }
  250. /* There are just a few bytes to copy. Use byte memory operations. */
  251. BYTE_COPY_BWD (dstp, srcp, len);
  252. }
  253. return (dest);
  254. }
  255. libc_hidden_weak(memmove)