memmove.c 7.2 KB

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