1 | /* Vector optimized 32/64 bit S/390 version of strncpy. |
2 | Copyright (C) 2015-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
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 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <ifunc-strncpy.h> |
20 | #include "sysdep.h" |
21 | #include "asm-syntax.h" |
22 | |
23 | .text |
24 | |
25 | #if HAVE_STRNCPY_Z13 |
26 | /* char * strncpy (const char *dest, const char *src, size_t n) |
27 | Copy at most n characters of string src to dest. |
28 | |
29 | Register usage: |
30 | -r0=dest pointer for return |
31 | -r1=tmp, zero byte index |
32 | -r2=dest |
33 | -r3=src |
34 | -r4=n |
35 | -r5=current_len |
36 | -r6=tmp, loaded bytes |
37 | -r7=tmp, border |
38 | -v16=part of src |
39 | -v17=index of zero |
40 | -v18=part of src |
41 | -v31=register save area for r6, r7 |
42 | */ |
43 | ENTRY(STRNCPY_Z13) |
44 | .machine "z13" |
45 | .machinemode "zarch_nohighgprs" |
46 | |
47 | # if !defined __s390x__ |
48 | llgfr %r4,%r4 |
49 | # endif /* !defined __s390x__ */ |
50 | |
51 | clgfi %r4,0 |
52 | ber %r14 /* Nothing to do, if n == 0. */ |
53 | lgr %r0,%r2 /* Save destination pointer for return. */ |
54 | vlvgp %v31,%r6,%r7 /* Save registers. */ |
55 | |
56 | vlbb %v16,0(%r3),6 /* Load s until next 4k-byte boundary. */ |
57 | lcbb %r6,0(%r3),6 /* Get bytes to 4k-byte boundary or 16. */ |
58 | llgfr %r6,%r6 /* Convert 32bit to 64bit. */ |
59 | |
60 | lghi %r5,0 /* current_len = 0. */ |
61 | |
62 | clgrjle %r4,%r6,.Lremaining_v16 /* If n <= loaded-bytes |
63 | -> process remaining. */ |
64 | |
65 | /* n > loaded-byte-count. */ |
66 | vfenezb %v17,%v16,%v16 /* Find element not equal with zero search. */ |
67 | vlgvb %r1,%v17,7 /* Load zero index or 16 if not found. */ |
68 | clrjl %r1,%r6,.Lfound_v16_store /* Found zero within loaded bytes, |
69 | copy and return. */ |
70 | |
71 | /* Align s to 16 byte. */ |
72 | risbgn %r7,%r3,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15. */ |
73 | lghi %r5,15 /* current_len = 15. */ |
74 | slr %r5,%r7 /* Compute highest index to 16byte boundary. */ |
75 | |
76 | /* Zero not found and n > loaded-byte-count. */ |
77 | vstl %v16,%r5,0(%r2) /* Copy loaded characters - no zero. */ |
78 | ahi %r5,1 /* Start loop at next character. */ |
79 | |
80 | /* Now we are 16byte aligned, so we can load |
81 | a full vreg without page fault. */ |
82 | lgr %r1,%r5 /* If %r5 + 64 < maxlen? -> loop64. */ |
83 | aghi %r1,64 |
84 | clgrjl %r1,%r4,.Lloop64 |
85 | |
86 | vl %v16,0(%r5,%r3) /* Load s. */ |
87 | clgijl %r4,17,.Lremaining_v16 /* If n <= 16, process remaining |
88 | bytes. */ |
89 | .Llt64: |
90 | lgr %r7,%r4 |
91 | slgfi %r7,16 /* border_len = n - 16. */ |
92 | |
93 | clgrjhe %r5,%r7,.Lremaining_v16 /* If current_len >= border |
94 | then process remaining bytes. */ |
95 | vfenezbs %v17,%v16,%v16 /* Find element not equal with zero search. */ |
96 | je .Lfound_v16 /* Jump away if zero was found. */ |
97 | vl %v18,16(%r5,%r3) /* Load next part of s. */ |
98 | vst %v16,0(%r5,%r2) /* Store previous part without zero to dst. */ |
99 | aghi %r5,16 |
100 | |
101 | clgrjhe %r5,%r7,.Lremaining_v18 |
102 | vfenezbs %v17,%v18,%v18 |
103 | je .Lfound_v18 |
104 | vl %v16,16(%r5,%r3) |
105 | vst %v18,0(%r5,%r2) |
106 | aghi %r5,16 |
107 | |
108 | clgrjhe %r5,%r7,.Lremaining_v16 |
109 | vfenezbs %v17,%v16,%v16 |
110 | je .Lfound_v16 |
111 | vl %v18,16(%r5,%r3) |
112 | vst %v16,0(%r5,%r2) |
113 | aghi %r5,16 |
114 | |
115 | .Lremaining_v18: |
116 | vlr %v16,%v18 |
117 | .Lremaining_v16: |
118 | /* v16 contains the remaining bytes [1...16]. |
119 | Store remaining bytes and append string-termination. */ |
120 | vfenezb %v17,%v16,%v16 /* Find element not equal with zero search. */ |
121 | slgrk %r7,%r4,%r5 /* Remaining bytes = maxlen - current_len. */ |
122 | aghi %r7,-1 /* vstl needs highest index. */ |
123 | la %r2,0(%r5,%r2) /* vstl has no index register. */ |
124 | vlgvb %r1,%v17,7 /* Load zero index or 16 if not found. */ |
125 | /* Zero in remaining bytes? -> jump away (zero-index < max-index) |
126 | Do not jump away if zero-index == max-index, |
127 | but simply copy zero with vstl below. */ |
128 | clrjl %r1,%r7,.Lfound_v16_store |
129 | vstl %v16,%r7,0(%r2) /* Store remaining bytes without null |
130 | termination!. */ |
131 | .Lend: |
132 | /* Restore saved registers. */ |
133 | vlgvg %r6,%v31,0 |
134 | vlgvg %r7,%v31,1 |
135 | lgr %r2,%r0 /* Load saved dest-ptr. */ |
136 | br %r14 |
137 | |
138 | |
139 | .Lfound_v16_32: |
140 | aghi %r5,32 |
141 | j .Lfound_v16 |
142 | .Lfound_v18_48: |
143 | aghi %r5,32 |
144 | .Lfound_v18_16: |
145 | aghi %r5,16 |
146 | .Lfound_v18: |
147 | vlr %v16,%v18 |
148 | .Lfound_v16: |
149 | /* v16 contains a zero. Store remaining bytes to zero. current_len |
150 | has not reached border, thus checking for n is not needed! */ |
151 | vlgvb %r1,%v17,7 /* Load byte index of zero. */ |
152 | la %r2,0(%r5,%r2) /* vstl has no support for index-register. */ |
153 | .Lfound_v16_store: |
154 | vstl %v16,%r1,0(%r2) /* Copy characters including zero. */ |
155 | /* Fill remaining bytes with zero - remaining count always > 0. */ |
156 | algr %r5,%r1 /* Remaining bytes (=%r4) = ... */ |
157 | slgr %r4,%r5 /* = n - (current_len + zero_index + 1). */ |
158 | la %r2,0(%r1,%r2) /* Pointer to zero. start filling beyond. */ |
159 | aghi %r4,-2 /* mvc with exrl needs count - 1. |
160 | (additional -1, see remaining bytes above) */ |
161 | srlg %r6,%r4,8 /* Split into 256 byte blocks. */ |
162 | ltgr %r6,%r6 |
163 | je .Lzero_lt256 |
164 | .Lzero_loop256: |
165 | mvc 1(256,%r2),0(%r2) /* Fill 256 zeros at once. */ |
166 | la %r2,256(%r2) |
167 | brctg %r6,.Lzero_loop256 /* Loop until all blocks are processed. */ |
168 | .Lzero_lt256: |
169 | exrl %r4,.Lmvc_lt256 |
170 | j .Lend |
171 | .Lmvc_lt256: |
172 | mvc 1(1,%r2),0(%r2) |
173 | |
174 | .Lloop64: |
175 | vl %v16,0(%r5,%r3) /* Load s. */ |
176 | vfenezbs %v17,%v16,%v16 /* Find element not equal with zero search. */ |
177 | je .Lfound_v16 /* Jump away if zero was found. */ |
178 | vl %v18,16(%r5,%r3) /* Load next part of s. */ |
179 | vst %v16,0(%r5,%r2) /* Store previous part without zero to dst. */ |
180 | vfenezbs %v17,%v18,%v18 |
181 | je .Lfound_v18_16 |
182 | vl %v16,32(%r5,%r3) |
183 | vst %v18,16(%r5,%r2) |
184 | vfenezbs %v17,%v16,%v16 |
185 | je .Lfound_v16_32 |
186 | vl %v18,48(%r5,%r3) |
187 | vst %v16,32(%r5,%r2) |
188 | vfenezbs %v17,%v18,%v18 |
189 | je .Lfound_v18_48 |
190 | vst %v18,48(%r5,%r2) |
191 | |
192 | aghi %r5,64 |
193 | lgr %r1,%r5 /* If %r5 + 64 < maxlen? -> loop64. */ |
194 | aghi %r1,64 |
195 | clgrjl %r1,%r4,.Lloop64 |
196 | |
197 | vl %v16,0(%r5,%r3) /* Load s. */ |
198 | j .Llt64 |
199 | END(STRNCPY_Z13) |
200 | |
201 | # if ! HAVE_STRNCPY_IFUNC |
202 | strong_alias (STRNCPY_Z13, strncpy) |
203 | # endif |
204 | |
205 | # if ! HAVE_STRNCPY_Z900_G5 && defined SHARED && IS_IN (libc) |
206 | strong_alias (STRNCPY_Z13, __GI_strncpy) |
207 | # endif |
208 | #endif |
209 | |