linux ttyname and ttyname_r: do not return wrong results
[glibc.git] / sysdeps / s390 / multiarch / strnlen-vx.S
blobda83a0d4f04b71db61dd75e4d20b194e9d88b97c
1 /* Vector optimized 32/64 bit S/390 version of strnlen.
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 strnlen (const char *s, size_t maxlen)
27    Returns the number of characters in s or at most maxlen.
29    Register usage:
30    -r1=tmp
31    -r2=address of string
32    -r3=maxlen (number of characters to be read)
33    -r4=tmp
34    -r5=current_len and return_value
35    -v16=part of s
37 ENTRY(__strnlen_vx)
38         .machine "z13"
39         .machinemode "zarch_nohighgprs"
41 # if !defined __s390x__
42         llgfr   %r3,%r3
43 # endif /* !defined __s390x__ */
45         clgfi   %r3,0           /* if maxlen == 0, return 0.  */
46         locgre  %r2,%r3
47         ber     %r14
49         vlbb    %v16,0(%r2),6   /* Load s until next 4k-byte boundary.  */
50         lcbb    %r1,0(%r2),6    /* Get bytes to 4k-byte boundary or 16.  */
51         llgfr   %r1,%r1         /* Convert 32bit to 64bit.  */
53         vfenezb %v16,%v16,%v16  /* Find element not equal with zero search.  */
54         clgr    %r1,%r3
55         locgrh  %r1,%r3         /* loaded_byte_count
56                                    = min (loaded_byte_count, maxlen)  */
58         vlgvb   %r5,%v16,7      /* Load zero index or 16 if not found.  */
59         clr     %r5,%r1         /* If found zero within loaded bytes?  */
60         locgrl  %r2,%r5         /* Then copy return value.  */
61         blr     %r14            /* And return.  */
63         clgr    %r1,%r3         /* If loaded_byte_count == maxlen?  */
64         locgre  %r2,%r3         /* Then copy return value.  */
65         ber     %r14            /* And return.  */
67         /* Align s to 16 byte.  */
68         risbgn  %r4,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15.  */
69         lghi    %r5,16          /* current_len = 16.  */
70         slr     %r5,%r4         /* Compute bytes to 16bytes boundary.  */
72         lgr     %r1,%r5         /* If %r5 + 64 < maxlen? -> loop64.  */
73         aghi    %r1,64
74         clgrjl  %r1,%r3,.Lloop64
76         /* Find zero in max 64byte with aligned s.  */
77 .Llt64:
78         vl      %v16,0(%r5,%r2) /* Load s.  */
79         vfenezbs %v16,%v16,%v16 /* Find element not equal with zero search.  */
80         je      .Lfound         /* Jump away if zero was found.  */
81         aghi    %r5,16
82         clgrjhe %r5,%r3,.Lfound /* current_len >= maxlen -> end.  */
83         vl      %v16,0(%r5,%r2)
84         vfenezbs %v16,%v16,%v16
85         je      .Lfound
86         aghi    %r5,16
87         clgrjhe %r5,%r3,.Lfound
88         vl      %v16,0(%r5,%r2)
89         vfenezbs %v16,%v16,%v16
90         je      .Lfound
91         aghi    %r5,16
92         clgrjhe %r5,%r3,.Lfound
93         vl      %v16,0(%r5,%r2)
94         vfenezbs %v16,%v16,%v16
95         j       .Lfound
97 .Lfound48:
98         aghi    %r5,16
99 .Lfound32:
100         aghi    %r5,16
101 .Lfound16:
102         aghi    %r5,16
103 .Lfound:
104         vlgvb   %r4,%v16,7      /* Load byte index of zero or 16 if no zero.  */
105         algr    %r5,%r4
107         clgr    %r5,%r3
108         locgrh  %r5,%r3         /* Return min (current_len, maxlen).  */
109         lgr     %r2,%r5
110         br      %r14
112         /* Find zero in 16 byte aligned loop.  */
113 .Lloop64:
114         vl      %v16,0(%r5,%r2) /* Load s.  */
115         vfenezbs %v16,%v16,%v16 /* Find element not equal with zero search.  */
116         je      .Lfound         /* Jump away if zero was found.  */
117         vl      %v16,16(%r5,%r2)
118         vfenezbs %v16,%v16,%v16
119         je      .Lfound16
120         vl      %v16,32(%r5,%r2)
121         vfenezbs %v16,%v16,%v16
122         je      .Lfound32
123         vl      %v16,48(%r5,%r2)
124         vfenezbs %v16,%v16,%v16
125         je      .Lfound48
127         aghi    %r5,64
128         lgr     %r1,%r5         /* If %r5 + 64 < maxlen? -> loop64.  */
129         aghi    %r1,64
130         clgrjl  %r1,%r3,.Lloop64
132         j       .Llt64
133 END(__strnlen_vx)
134 #endif /* HAVE_S390_VX_ASM_SUPPORT && IS_IN (libc) */