wordcopy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* _memcopy.c -- subroutines for memory copy functions.
  2. Copyright (C) 1991-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* BE VERY CAREFUL IF YOU CHANGE THIS CODE...! */
  16. #include <stddef.h>
  17. #include <libc-diag.h>
  18. /* Compiling with -O1 might warn that 'a2' and 'a3' may be used
  19. uninitialized. There are only two ways to arrive at labels 'do4', 'do3'
  20. or 'do1', all of which use 'a2' or 'a3' in the MERGE macro: either from
  21. the earlier switch case statement or via a loop iteration. In all cases
  22. the switch statement or previous loop sets both 'a2' and 'a3'.
  23. Since the usage is within the MERGE macro we disable the
  24. warning in the definition, but only in this file. */
  25. DIAG_PUSH_NEEDS_COMMENT;
  26. DIAG_IGNORE_NEEDS_COMMENT_GCC (6, "-Wmaybe-uninitialized");
  27. #include <memcopy.h>
  28. DIAG_POP_NEEDS_COMMENT;
  29. /* _wordcopy_fwd_aligned -- Copy block beginning at SRCP to
  30. block beginning at DSTP with LEN `op_t' words (not LEN bytes!).
  31. Both SRCP and DSTP should be aligned for memory operations on `op_t's. */
  32. #ifndef WORDCOPY_FWD_ALIGNED
  33. # define WORDCOPY_FWD_ALIGNED _wordcopy_fwd_aligned
  34. #endif
  35. void
  36. WORDCOPY_FWD_ALIGNED (long int dstp, long int srcp, size_t len)
  37. {
  38. op_t a0, a1;
  39. switch (len % 8)
  40. {
  41. case 2:
  42. a0 = ((op_t *) srcp)[0];
  43. srcp -= 6 * OPSIZ;
  44. dstp -= 7 * OPSIZ;
  45. len += 6;
  46. goto do1;
  47. case 3:
  48. a1 = ((op_t *) srcp)[0];
  49. srcp -= 5 * OPSIZ;
  50. dstp -= 6 * OPSIZ;
  51. len += 5;
  52. goto do2;
  53. case 4:
  54. a0 = ((op_t *) srcp)[0];
  55. srcp -= 4 * OPSIZ;
  56. dstp -= 5 * OPSIZ;
  57. len += 4;
  58. goto do3;
  59. case 5:
  60. a1 = ((op_t *) srcp)[0];
  61. srcp -= 3 * OPSIZ;
  62. dstp -= 4 * OPSIZ;
  63. len += 3;
  64. goto do4;
  65. case 6:
  66. a0 = ((op_t *) srcp)[0];
  67. srcp -= 2 * OPSIZ;
  68. dstp -= 3 * OPSIZ;
  69. len += 2;
  70. goto do5;
  71. case 7:
  72. a1 = ((op_t *) srcp)[0];
  73. srcp -= 1 * OPSIZ;
  74. dstp -= 2 * OPSIZ;
  75. len += 1;
  76. goto do6;
  77. case 0:
  78. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  79. return;
  80. a0 = ((op_t *) srcp)[0];
  81. srcp -= 0 * OPSIZ;
  82. dstp -= 1 * OPSIZ;
  83. goto do7;
  84. case 1:
  85. a1 = ((op_t *) srcp)[0];
  86. srcp -=-1 * OPSIZ;
  87. dstp -= 0 * OPSIZ;
  88. len -= 1;
  89. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  90. goto do0;
  91. goto do8; /* No-op. */
  92. }
  93. do
  94. {
  95. do8:
  96. a0 = ((op_t *) srcp)[0];
  97. /* Compiling with -O1 may warn that 'a1' may be used uninitialized.
  98. There are only two ways to arrive at label 'do8' and they are via a
  99. do-while loop iteration or directly via the earlier switch 'case 1:'
  100. case. The switch case always sets 'a1' and all previous loop
  101. iterations will also have set 'a1' before the use. */
  102. DIAG_PUSH_NEEDS_COMMENT;
  103. DIAG_IGNORE_NEEDS_COMMENT_GCC (6, "-Wmaybe-uninitialized");
  104. ((op_t *) dstp)[0] = a1;
  105. DIAG_POP_NEEDS_COMMENT;
  106. do7:
  107. a1 = ((op_t *) srcp)[1];
  108. ((op_t *) dstp)[1] = a0;
  109. do6:
  110. a0 = ((op_t *) srcp)[2];
  111. ((op_t *) dstp)[2] = a1;
  112. do5:
  113. a1 = ((op_t *) srcp)[3];
  114. ((op_t *) dstp)[3] = a0;
  115. do4:
  116. a0 = ((op_t *) srcp)[4];
  117. ((op_t *) dstp)[4] = a1;
  118. do3:
  119. a1 = ((op_t *) srcp)[5];
  120. ((op_t *) dstp)[5] = a0;
  121. do2:
  122. a0 = ((op_t *) srcp)[6];
  123. ((op_t *) dstp)[6] = a1;
  124. do1:
  125. a1 = ((op_t *) srcp)[7];
  126. ((op_t *) dstp)[7] = a0;
  127. srcp += 8 * OPSIZ;
  128. dstp += 8 * OPSIZ;
  129. len -= 8;
  130. }
  131. while (len != 0);
  132. /* This is the right position for do0. Please don't move
  133. it into the loop. */
  134. do0:
  135. ((op_t *) dstp)[0] = a1;
  136. }
  137. /* _wordcopy_fwd_dest_aligned -- Copy block beginning at SRCP to
  138. block beginning at DSTP with LEN `op_t' words (not LEN bytes!).
  139. DSTP should be aligned for memory operations on `op_t's, but SRCP must
  140. *not* be aligned. */
  141. #ifndef WORDCOPY_FWD_DEST_ALIGNED
  142. # define WORDCOPY_FWD_DEST_ALIGNED _wordcopy_fwd_dest_aligned
  143. #endif
  144. void
  145. WORDCOPY_FWD_DEST_ALIGNED (long int dstp, long int srcp, size_t len)
  146. {
  147. op_t a0, a1, a2, a3;
  148. int sh_1, sh_2;
  149. /* Calculate how to shift a word read at the memory operation
  150. aligned srcp to make it aligned for copy. */
  151. sh_1 = 8 * (srcp % OPSIZ);
  152. sh_2 = 8 * OPSIZ - sh_1;
  153. /* Make SRCP aligned by rounding it down to the beginning of the `op_t'
  154. it points in the middle of. */
  155. srcp &= -OPSIZ;
  156. switch (len % 4)
  157. {
  158. case 2:
  159. a1 = ((op_t *) srcp)[0];
  160. a2 = ((op_t *) srcp)[1];
  161. srcp -= 1 * OPSIZ;
  162. dstp -= 3 * OPSIZ;
  163. len += 2;
  164. goto do1;
  165. case 3:
  166. a0 = ((op_t *) srcp)[0];
  167. a1 = ((op_t *) srcp)[1];
  168. srcp -= 0 * OPSIZ;
  169. dstp -= 2 * OPSIZ;
  170. len += 1;
  171. goto do2;
  172. case 0:
  173. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  174. return;
  175. a3 = ((op_t *) srcp)[0];
  176. a0 = ((op_t *) srcp)[1];
  177. srcp -=-1 * OPSIZ;
  178. dstp -= 1 * OPSIZ;
  179. len += 0;
  180. goto do3;
  181. case 1:
  182. a2 = ((op_t *) srcp)[0];
  183. a3 = ((op_t *) srcp)[1];
  184. srcp -=-2 * OPSIZ;
  185. dstp -= 0 * OPSIZ;
  186. len -= 1;
  187. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  188. goto do0;
  189. goto do4; /* No-op. */
  190. }
  191. do
  192. {
  193. do4:
  194. a0 = ((op_t *) srcp)[0];
  195. ((op_t *) dstp)[0] = MERGE (a2, sh_1, a3, sh_2);
  196. do3:
  197. a1 = ((op_t *) srcp)[1];
  198. ((op_t *) dstp)[1] = MERGE (a3, sh_1, a0, sh_2);
  199. do2:
  200. a2 = ((op_t *) srcp)[2];
  201. ((op_t *) dstp)[2] = MERGE (a0, sh_1, a1, sh_2);
  202. do1:
  203. a3 = ((op_t *) srcp)[3];
  204. ((op_t *) dstp)[3] = MERGE (a1, sh_1, a2, sh_2);
  205. srcp += 4 * OPSIZ;
  206. dstp += 4 * OPSIZ;
  207. len -= 4;
  208. }
  209. while (len != 0);
  210. /* This is the right position for do0. Please don't move
  211. it into the loop. */
  212. do0:
  213. ((op_t *) dstp)[0] = MERGE (a2, sh_1, a3, sh_2);
  214. }
  215. /* _wordcopy_bwd_aligned -- Copy block finishing right before
  216. SRCP to block finishing right before DSTP with LEN `op_t' words
  217. (not LEN bytes!). Both SRCP and DSTP should be aligned for memory
  218. operations on `op_t's. */
  219. #ifndef WORDCOPY_BWD_ALIGNED
  220. # define WORDCOPY_BWD_ALIGNED _wordcopy_bwd_aligned
  221. #endif
  222. void
  223. WORDCOPY_BWD_ALIGNED (long int dstp, long int srcp, size_t len)
  224. {
  225. op_t a0, a1;
  226. switch (len % 8)
  227. {
  228. case 2:
  229. srcp -= 2 * OPSIZ;
  230. dstp -= 1 * OPSIZ;
  231. a0 = ((op_t *) srcp)[1];
  232. len += 6;
  233. goto do1;
  234. case 3:
  235. srcp -= 3 * OPSIZ;
  236. dstp -= 2 * OPSIZ;
  237. a1 = ((op_t *) srcp)[2];
  238. len += 5;
  239. goto do2;
  240. case 4:
  241. srcp -= 4 * OPSIZ;
  242. dstp -= 3 * OPSIZ;
  243. a0 = ((op_t *) srcp)[3];
  244. len += 4;
  245. goto do3;
  246. case 5:
  247. srcp -= 5 * OPSIZ;
  248. dstp -= 4 * OPSIZ;
  249. a1 = ((op_t *) srcp)[4];
  250. len += 3;
  251. goto do4;
  252. case 6:
  253. srcp -= 6 * OPSIZ;
  254. dstp -= 5 * OPSIZ;
  255. a0 = ((op_t *) srcp)[5];
  256. len += 2;
  257. goto do5;
  258. case 7:
  259. srcp -= 7 * OPSIZ;
  260. dstp -= 6 * OPSIZ;
  261. a1 = ((op_t *) srcp)[6];
  262. len += 1;
  263. goto do6;
  264. case 0:
  265. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  266. return;
  267. srcp -= 8 * OPSIZ;
  268. dstp -= 7 * OPSIZ;
  269. a0 = ((op_t *) srcp)[7];
  270. goto do7;
  271. case 1:
  272. srcp -= 9 * OPSIZ;
  273. dstp -= 8 * OPSIZ;
  274. a1 = ((op_t *) srcp)[8];
  275. len -= 1;
  276. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  277. goto do0;
  278. goto do8; /* No-op. */
  279. }
  280. do
  281. {
  282. do8:
  283. a0 = ((op_t *) srcp)[7];
  284. /* Check the comment on WORDCOPY_FWD_ALIGNED. */
  285. DIAG_PUSH_NEEDS_COMMENT;
  286. DIAG_IGNORE_NEEDS_COMMENT_GCC (6, "-Wmaybe-uninitialized");
  287. ((op_t *) dstp)[7] = a1;
  288. DIAG_POP_NEEDS_COMMENT;
  289. do7:
  290. a1 = ((op_t *) srcp)[6];
  291. ((op_t *) dstp)[6] = a0;
  292. do6:
  293. a0 = ((op_t *) srcp)[5];
  294. ((op_t *) dstp)[5] = a1;
  295. do5:
  296. a1 = ((op_t *) srcp)[4];
  297. ((op_t *) dstp)[4] = a0;
  298. do4:
  299. a0 = ((op_t *) srcp)[3];
  300. ((op_t *) dstp)[3] = a1;
  301. do3:
  302. a1 = ((op_t *) srcp)[2];
  303. ((op_t *) dstp)[2] = a0;
  304. do2:
  305. a0 = ((op_t *) srcp)[1];
  306. ((op_t *) dstp)[1] = a1;
  307. do1:
  308. a1 = ((op_t *) srcp)[0];
  309. ((op_t *) dstp)[0] = a0;
  310. srcp -= 8 * OPSIZ;
  311. dstp -= 8 * OPSIZ;
  312. len -= 8;
  313. }
  314. while (len != 0);
  315. /* This is the right position for do0. Please don't move
  316. it into the loop. */
  317. do0:
  318. ((op_t *) dstp)[7] = a1;
  319. }
  320. /* _wordcopy_bwd_dest_aligned -- Copy block finishing right
  321. before SRCP to block finishing right before DSTP with LEN `op_t'
  322. words (not LEN bytes!). DSTP should be aligned for memory
  323. operations on `op_t', but SRCP must *not* be aligned. */
  324. #ifndef WORDCOPY_BWD_DEST_ALIGNED
  325. # define WORDCOPY_BWD_DEST_ALIGNED _wordcopy_bwd_dest_aligned
  326. #endif
  327. void
  328. WORDCOPY_BWD_DEST_ALIGNED (long int dstp, long int srcp, size_t len)
  329. {
  330. op_t a0, a1, a2, a3;
  331. int sh_1, sh_2;
  332. /* Calculate how to shift a word read at the memory operation
  333. aligned srcp to make it aligned for copy. */
  334. sh_1 = 8 * (srcp % OPSIZ);
  335. sh_2 = 8 * OPSIZ - sh_1;
  336. /* Make srcp aligned by rounding it down to the beginning of the op_t
  337. it points in the middle of. */
  338. srcp &= -OPSIZ;
  339. srcp += OPSIZ;
  340. switch (len % 4)
  341. {
  342. case 2:
  343. srcp -= 3 * OPSIZ;
  344. dstp -= 1 * OPSIZ;
  345. a2 = ((op_t *) srcp)[2];
  346. a1 = ((op_t *) srcp)[1];
  347. len += 2;
  348. goto do1;
  349. case 3:
  350. srcp -= 4 * OPSIZ;
  351. dstp -= 2 * OPSIZ;
  352. a3 = ((op_t *) srcp)[3];
  353. a2 = ((op_t *) srcp)[2];
  354. len += 1;
  355. goto do2;
  356. case 0:
  357. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  358. return;
  359. srcp -= 5 * OPSIZ;
  360. dstp -= 3 * OPSIZ;
  361. a0 = ((op_t *) srcp)[4];
  362. a3 = ((op_t *) srcp)[3];
  363. goto do3;
  364. case 1:
  365. srcp -= 6 * OPSIZ;
  366. dstp -= 4 * OPSIZ;
  367. a1 = ((op_t *) srcp)[5];
  368. a0 = ((op_t *) srcp)[4];
  369. len -= 1;
  370. if (OP_T_THRES <= 3 * OPSIZ && len == 0)
  371. goto do0;
  372. goto do4; /* No-op. */
  373. }
  374. do
  375. {
  376. do4:
  377. a3 = ((op_t *) srcp)[3];
  378. ((op_t *) dstp)[3] = MERGE (a0, sh_1, a1, sh_2);
  379. do3:
  380. a2 = ((op_t *) srcp)[2];
  381. ((op_t *) dstp)[2] = MERGE (a3, sh_1, a0, sh_2);
  382. do2:
  383. a1 = ((op_t *) srcp)[1];
  384. ((op_t *) dstp)[1] = MERGE (a2, sh_1, a3, sh_2);
  385. do1:
  386. a0 = ((op_t *) srcp)[0];
  387. ((op_t *) dstp)[0] = MERGE (a1, sh_1, a2, sh_2);
  388. srcp -= 4 * OPSIZ;
  389. dstp -= 4 * OPSIZ;
  390. len -= 4;
  391. }
  392. while (len != 0);
  393. /* This is the right position for do0. Please don't move
  394. it into the loop. */
  395. do0:
  396. ((op_t *) dstp)[3] = MERGE (a0, sh_1, a1, sh_2);
  397. }