linux ttyname and ttyname_r: do not return wrong results
[glibc.git] / sysdeps / s390 / multiarch / wcscspn-vx.S
blobcd528907d6c3daa15c82f65b863e507f41b78cc4
1 /* Vector optimized 32/64 bit S/390 version of wcscspn.
2    Copyright (C) 2015-2017 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
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.
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.
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    <http://www.gnu.org/licenses/>.  */
19 #if defined HAVE_S390_VX_ASM_SUPPORT && IS_IN (libc)
21 # include "sysdep.h"
22 # include "asm-syntax.h"
24         .text
26 /* size_t wcscspn (const wchar_t *s, const wchar_t * reject)
27    The wcscspn() function calculates the length of the initial segment
28    of s which consists entirely of characters not in reject.
30    This method checks the length of reject string. If it fits entirely
31    in one vector register, a fast algorithm is used, which does not need
32    to check multiple parts of accept-string. Otherwise a slower full
33    check of accept-string is used.
35    register overview:
36    r3:  pointer to start of reject-string
37    r2:  pointer to start of search-string
38    r0:  loaded byte count of vlbb search-string
39    r4:  found byte index
40    r1:  current return len
41    v16: search-string
42    v17: reject-string
43    v18: temp-vreg
45    ONLY FOR SLOW:
46    v19: first reject-string
47    v20: zero for preparing acc-vector
48    v21: global mask; 1 indicates a match between
49         search-string-vreg and any reject-character
50    v22: current mask; 1 indicates a match between
51         search-string-vreg and any reject-character in current acc-vreg
52    v30, v31: for re-/storing registers r6, r8, r9
53    r5:  current len of reject-string
54    r6:  zero-index in search-string or 16 if no zero
55         or min(zero-index, loaded byte count)
56    r8:  >0, if former reject-string-part contains a zero,
57                         otherwise =0;
58    r9:  loaded byte count of vlbb reject-string
60 ENTRY(__wcscspn_vx)
61         .machine "z13"
62         .machinemode "zarch_nohighgprs"
64         tmll    %r2,3           /* Test if s is 4-byte aligned?  */
65         jne     .Lfallback      /* And use common-code variant if not.  */
67         /*
68           Check if reject-string fits in one vreg:
69           ----------------------------------------
70         */
71         vlbb    %v17,0(%r3),0   /* Load reject.  */
72         lcbb    %r0,0(%r3),0
73         jo      .Lcheck_onbb    /* Special case if reject
74                                    lays on block-boundary.  */
76 .Lcheck_notonbb:
77         lghi    %r1,0           /* Zero out current len.  */
78         vistrfs %v17,%v17       /* Fill with zeros after first zero.  */
79         je      .Lfast          /* Zero found -> reject fits in one vreg.  */
80         j       .Lslow          /* No zero -> reject exceeds one vreg.  */
83 .Lcheck_onbb:
84         /* Reject lays on block-boundary.  */
85         nill    %r0,65532       /* Recognize only fully loaded characters.  */
86         je      .Lcheck_onbb2   /* Reload vr, if we loaded no full wchar_t.  */
87         vfenezf %v18,%v17,%v17  /* Search zero in loaded reject bytes.  */
88         vlgvb   %r4,%v18,7      /* Get index of zero or 16 if not found.  */
89         clrjl   %r4,%r0,.Lcheck_notonbb /* Zero index < loaded bytes count ->
90                                             Reject fits in one vreg;
91                                             Fill with zeros and proceed
92                                             with FAST.  */
93 .Lcheck_onbb2:
94         vl      %v17,0(%r3)     /* Load reject, which exceeds loaded bytes.  */
95         j       .Lcheck_notonbb /* Check if reject fits in one vreg.  */
98         /*
99           Search s for reject in one vreg
100           -------------------------------
101         */
102 .Lfast:
103         /* Complete reject-string in v17 and remaining bytes are zero.  */
105         vlbb    %v16,0(%r2),6   /* Load s until next 4k-byte boundary.  */
106         lcbb    %r0,0(%r2),6    /* Get bytes to 4k-byte boundary or 16.  */
108         vfaezfs %v18,%v16,%v17,0 /* Find first element in v16
109                                     unequal to any in v17
110                                     or first zero element.  */
111         vlgvb   %r4,%v18,7      /* Load byte index of found element.  */
112         clrjl   %r4,%r0,.Lfast_loop_found2 /* If found index is within loaded
113                                                bytes, return with found element
114                                                index (=equal count).  */
116         /* Align s to 16 byte.  */
117         risbgn  %r4,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15.  */
118         lghi    %r1,16          /* current_len = 16.  */
119         slr     %r1,%r4         /* Compute bytes to 16bytes boundary.  */
121         /* Process s in 16byte aligned loop.  */
122 .Lfast_loop:
123         vl      %v16,0(%r1,%r2) /* Load search-string.  */
124         vfaezfs %v18,%v16,%v17,0 /* Find first element in v16 equal to any
125                                     in v17 or first zero element.  */
126         jno     .Lfast_loop_found
128         vl      %v16,16(%r1,%r2)
129         vfaezfs %v18,%v16,%v17,0
130         jno     .Lfast_loop_found16
132         vl      %v16,32(%r1,%r2)
133         vfaezfs %v18,%v16,%v17,0
134         jno     .Lfast_loop_found32
136         vl      %v16,48(%r1,%r2)
137         vfaezfs %v18,%v16,%v17,0
138         jno     .Lfast_loop_found48
140         aghi    %r1,64
141         j       .Lfast_loop     /* Loop if no element was unequal to reject
142                                    and not zero.  */
144         /* Found equal or zero element.  */
145 .Lfast_loop_found48:
146         aghi    %r1,16
147 .Lfast_loop_found32:
148         aghi    %r1,16
149 .Lfast_loop_found16:
150         aghi    %r1,16
151 .Lfast_loop_found:
152         vlgvb   %r4,%v18,7      /* Load byte index of found element or zero.  */
153 .Lfast_loop_found2:
154         algrk   %r2,%r1,%r4     /* Add found index to current len.  */
155         srlg    %r2,%r2,2       /* Convert byte-count to character-count.  */
156         br      %r14
160         /*
161           Search s for reject in multiple vregs
162           -------------------------------------
163         */
164 .Lslow:
165         /* Save registers.  */
166         vlvgg   %v30,%r6,0
167         vlvgp   %v31,%r8,%r9
169         /* Reject in v17 without zero.  */
170         vlr     %v19,%v17       /* Save first acc-part for a fast reload.  */
171         vzero   %v20            /* Zero for preparing acc-vector.  */
172         vone    %v24            /* One for checking result of former
173                                    string-part.  */
175         /* Align s to 16 byte.  */
176         risbg   %r4,%r2,60,128+63,0 /* Test if s is aligned and
177                                        %r4 = bits 60-63 'and' 15.   */
178         je      .Lslow_loop_str /* If s is aligned, loop aligned.  */
179         lghi    %r0,15
180         slr     %r0,%r4         /* Compute highest index to load (15-x).  */
181         vll     %v16,%r0,0(%r2) /* Load up to 16byte boundary (vll needs
182                                    highest index, remaining bytes are 0).  */
183         ahi     %r0,1           /* Work with loaded byte count.  */
184         vzero   %v21            /* Zero out global mask.  */
185         lghi    %r5,0           /* Set current len of reject-string to zero.  */
186         vfenezf %v18,%v16,%v16  /* Find zero in current string-part.  */
187         lghi    %r8,0           /* There is no zero in first reject-part.  */
188         vlgvb   %r6,%v18,7      /* Load byte index of zero or 16 if no zero.  */
189         clije   %r6,0,.Lslow_end /* If first element is zero -> return 0.  */
190         clr     %r0,%r6         /* cc==1 if loaded byte count < zero-index.  */
191         locrl   %r6,%r0         /* Load on cc==1; zero-index = lbc.  */
192         j       .Lslow_loop_acc
195         /* Process s in 16byte aligned loop.  */
196 .Lslow_next_str:
197         /* Check results of former processed str-part.  */
198         vfeef   %v18,%v21,%v24  /* Find first equal match in global mask
199                                    (ones in element).  */
200         vlgvb   %r4,%v18,7      /* Get index of first one (=equal) or 16.  */
201         /* Equal-index < min(zero-index, loaded byte count)
202            -> Return pointer to equal element.  */
203         clrjl   %r4,%r6,.Lslow_index_found
204         /* Zero-index < loaded byte count
205            -> Former str-part was last str-part
206            -> Return null  */
207         clrjl   %r6,%r0,.Lslow_end_not_found
209         /* All elements are zero (=no match) -> proceed with next str-part.  */
210         vlr     %v17,%v19       /* Load first part of reject (no zero).  */
211         algfr   %r1,%r0         /* Add loaded byte count to current len.  */
213 .Lslow_loop_str:
214         vl      %v16,0(%r1,%r2) /* Load search-string.  */
215         lghi    %r0,16          /* Loaded byte count is 16.  */
216         vzero   %v21            /* Zero out global mask.  */
217         lghi    %r5,0           /* Set current len of reject to zero.  */
218         vfenezf %v18,%v16,%v16  /* Find zero in current string-part.  */
219         lghi    %r8,0           /* There is no zero in first reject-part.  */
220         vlgvb   %r6,%v18,7      /* Load byte index of zero or 16 if no zero.  */
221         clije   %r6,0,.Lslow_end /* If first element is zero (end of string)
222                                      -> Return current length.  */
224 .Lslow_loop_acc:
225         vfaef   %v22,%v16,%v17,4 /* Create matching-mask (1 in mask ->
226                                     Character matches any rejected character in
227                                     this reject-string-part) IN=0, RT=1.  */
228         vlgvf   %r4,%v22,0      /* Get result of first element.  */
229         /* First element is equal to any rejected characters?
230            (All other parts of reject cannot lead to a match before this one)
231            -> Return current len, which is pointing to this element.  */
232         clijh   %r4,0,.Lslow_end
233         vo      %v21,%v21,%v22  /* Global-mask = global-|matching-mask.  */
234         /* Proceed with next acc until end of acc is reached.  */
237 .Lslow_next_acc:
238         clijh   %r8,0,.Lslow_next_str /* There was a zero in last reject-part
239                                           -> Add found index to current len
240                                              and end.  */
241         vlbb    %v17,16(%r5,%r3),6 /* Load next reject part.  */
242         aghi    %r5,16          /* Increment current len of reject-string.  */
243         lcbb    %r9,0(%r5,%r3),6 /* Get loaded byte count of reject-string.  */
244         jo      .Lslow_next_acc_onbb /* Jump away if reject-string is
245                                          on block-boundary.  */
246 .Lslow_next_acc_notonbb:
247         vistrfs %v17,%v17       /* Fill with zeros after first zero.  */
248         jo      .Lslow_loop_acc /* No zero found -> no preparation needed.  */
250 .Lslow_next_acc_prepare_zero:
251         /* Zero in reject-part: fill zeros with first-reject-character.  */
252         vlgvf   %r8,%v17,0      /* Load first element of reject-part.  */
253         clije   %r8,0,.Lslow_next_str /* Process next str-part if first
254                                           character in this part of reject
255                                           is a zero.  */
256         /* r8>0 -> zero found in this acc-part.  */
257         vrepf   %v18,%v17,0     /* Replicate first char accross all chars.  */
258         vceqf   %v22,%v20,%v17  /* Create a mask (v22) of null chars
259                                    by comparing with 0 (v20).  */
260         vsel    %v17,%v18,%v17,%v22 /* Replace null chars with first char.  */
261         j       .Lslow_loop_acc /* Reject-string part is prepared.  */
263 .Lslow_next_acc_onbb:
264         nill    %r9,65532       /* Recognize only fully loaded characters.  */
265         je      .Lslow_next_acc_onbb2 /* Reload vr, if no full wchar_t
266                                           loaded.  */
267         vfenezf %v18,%v17,%v17  /* Find zero in loaded bytes of reject part.  */
268         vlgvb   %r8,%v18,7      /* Load byte index of zero.  */
269         clrjl   %r8,%r9,.Lslow_next_acc_notonbb /* Found a zero in loaded bytes
270                                                     -> Prepare vreg.  */
271 .Lslow_next_acc_onbb2:
272         vl      %v17,0(%r5,%r3) /* Load over boundary ...  */
273         lghi    %r8,0           /* r8=0 -> no zero in this part of acc,
274                                    check for zero is in jump-target.  */
275         j       .Lslow_next_acc_notonbb /* ... and search for zero in
276                                             fully loaded vreg again.  */
278 .Lslow_end_not_found:
279         algfr   %r1,%r6         /* Add zero-index to current len.  */
280         j       .Lslow_end
281 .Lslow_index_found:
282         algfr   %r1,%r4         /* Add found index of char to current len.  */
283 .Lslow_end:
284         srlg    %r2,%r1,2       /* Convert byte-count to character-count.  */
285         /* Restore registers.  */
286         vlgvg   %r6,%v30,0
287         vlgvg   %r8,%v31,0
288         vlgvg   %r9,%v31,1
289         br      %r14
290 .Lfallback:
291         jg      __wcscspn_c
292 END(__wcscspn_vx)
293 #endif /* HAVE_S390_VX_ASM_SUPPORT && IS_IN (libc) */