Simplify power6 wordcopy by adding [fwd|bwd]_align_merge macros.
[glibc.git] / sysdeps / powerpc / powerpc32 / power6 / wordcopy.c
blobd0df56f22c98e096ae0dc1ef9c23b2ceb82d2076
1 /* _memcopy.c -- subroutines for memory copy functions.
2 Copyright (C) 1991-2012 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Torbjorn Granlund (tege@sics.se).
5 Updated for POWER6 by Steven Munroe (sjmunroe@us.ibm.com).
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
21 /* BE VERY CAREFUL IF YOU CHANGE THIS CODE...! */
23 #include <stddef.h>
24 #include <memcopy.h>
26 /* _wordcopy_fwd_aligned -- Copy block beginning at SRCP to
27 block beginning at DSTP with LEN `op_t' words (not LEN bytes!).
28 Both SRCP and DSTP should be aligned for memory operations on `op_t's. */
30 void
31 _wordcopy_fwd_aligned (dstp, srcp, len)
32 long int dstp;
33 long int srcp;
34 size_t len;
36 op_t a0, a1;
38 if (len & 1)
40 ((op_t *) dstp)[0] = ((op_t *) srcp)[0];
42 if (len == 1)
43 return;
44 srcp += OPSIZ;
45 dstp += OPSIZ;
46 len -= 1;
51 a0 = ((op_t *) srcp)[0];
52 a1 = ((op_t *) srcp)[1];
53 ((op_t *) dstp)[0] = a0;
54 ((op_t *) dstp)[1] = a1;
56 srcp += 2 * OPSIZ;
57 dstp += 2 * OPSIZ;
58 len -= 2;
60 while (len != 0);
63 /* _wordcopy_fwd_dest_aligned -- Copy block beginning at SRCP to
64 block beginning at DSTP with LEN `op_t' words (not LEN bytes!).
65 DSTP should be aligned for memory operations on `op_t's, but SRCP must
66 *not* be aligned. */
68 #define fwd_align_merge(align) \
69 do \
70 { \
71 a1 = ((op_t *) srcp)[1]; \
72 a2 = ((op_t *) srcp)[2]; \
73 ((op_t *) dstp)[0] = MERGE (a0, align*8, a1, (32-align*8)); \
74 ((op_t *) dstp)[1] = MERGE (a1, align*8, a2, (32-align*8)); \
75 a0 = a2; \
76 srcp += 2 * OPSIZ; \
77 dstp += 2 * OPSIZ; \
78 len -= 2; \
79 } \
80 while (len != 0)
82 void
83 _wordcopy_fwd_dest_aligned (dstp, srcp, len)
84 long int dstp;
85 long int srcp;
86 size_t len;
88 op_t a0, a1, a2;
89 int sh_1, sh_2;
90 int align;
92 /* Calculate how to shift a word read at the memory operation
93 aligned srcp to make it aligned for copy. */
95 align = srcp % OPSIZ;
96 sh_1 = 8 * (srcp % OPSIZ);
97 sh_2 = 8 * OPSIZ - sh_1;
99 /* Make SRCP aligned by rounding it down to the beginning of the `op_t'
100 it points in the middle of. */
101 srcp &= -OPSIZ;
102 a0 = ((op_t *) srcp)[0];
104 if (len & 1)
106 a1 = ((op_t *) srcp)[1];
107 ((op_t *) dstp)[0] = MERGE (a0, sh_1, a1, sh_2);
109 if (len == 1)
110 return;
112 a0 = a1;
113 srcp += OPSIZ;
114 dstp += OPSIZ;
115 len -= 1;
118 switch (align)
120 case 1:
121 fwd_align_merge (1);
122 break;
123 case 2:
124 fwd_align_merge (2);
125 break;
126 case 3:
127 fwd_align_merge (3);
128 break;
133 /* _wordcopy_bwd_aligned -- Copy block finishing right before
134 SRCP to block finishing right before DSTP with LEN `op_t' words
135 (not LEN bytes!). Both SRCP and DSTP should be aligned for memory
136 operations on `op_t's. */
138 void
139 _wordcopy_bwd_aligned (dstp, srcp, len)
140 long int dstp;
141 long int srcp;
142 size_t len;
144 op_t a0, a1;
146 if (len & 1)
148 srcp -= OPSIZ;
149 dstp -= OPSIZ;
150 ((op_t *) dstp)[0] = ((op_t *) srcp)[0];
152 if (len == 1)
153 return;
154 len -= 1;
159 srcp -= 2 * OPSIZ;
160 dstp -= 2 * OPSIZ;
162 a1 = ((op_t *) srcp)[1];
163 a0 = ((op_t *) srcp)[0];
164 ((op_t *) dstp)[1] = a1;
165 ((op_t *) dstp)[0] = a0;
167 len -= 2;
169 while (len != 0);
172 #define bwd_align_merge(align) \
173 do \
175 srcp -= 2 * OPSIZ; \
176 dstp -= 2 * OPSIZ; \
177 a1 = ((op_t *) srcp)[1]; \
178 a0 = ((op_t *) srcp)[0]; \
179 ((op_t *) dstp)[1] = MERGE (a1, align*8, a2, (32-align*8)); \
180 ((op_t *) dstp)[0] = MERGE (a0, align*8, a1, (32-align*8)); \
181 a2 = a0; \
182 len -= 2; \
184 while (len != 0)
186 /* _wordcopy_bwd_dest_aligned -- Copy block finishing right
187 before SRCP to block finishing right before DSTP with LEN `op_t'
188 words (not LEN bytes!). DSTP should be aligned for memory
189 operations on `op_t', but SRCP must *not* be aligned. */
191 void
192 _wordcopy_bwd_dest_aligned (dstp, srcp, len)
193 long int dstp;
194 long int srcp;
195 size_t len;
197 op_t a0, a1, a2;
198 int sh_1, sh_2;
199 int align;
201 /* Calculate how to shift a word read at the memory operation
202 aligned srcp to make it aligned for copy. */
204 align = srcp % OPSIZ;
205 sh_1 = 8 * (srcp % OPSIZ);
206 sh_2 = 8 * OPSIZ - sh_1;
208 /* Make srcp aligned by rounding it down to the beginning of the op_t
209 it points in the middle of. */
210 srcp &= -OPSIZ;
211 a2 = ((op_t *) srcp)[0];
213 if (len & 1)
215 srcp -= OPSIZ;
216 dstp -= OPSIZ;
217 a1 = ((op_t *) srcp)[0];
218 ((op_t *) dstp)[0] = MERGE (a1, sh_1, a2, sh_2);
220 if (len == 1)
221 return;
223 a2 = a1;
224 len -= 1;
227 switch (align)
229 case 1:
230 bwd_align_merge (1);
231 break;
232 case 2:
233 bwd_align_merge (2);
234 break;
235 case 3:
236 bwd_align_merge (3);
237 break;