1 /* Vector optimized 32/64 bit S/390 version of wcsnlen.
2 Copyright (C) 2015-2024 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 <https://www.gnu.org/licenses/>. */
19 #include <ifunc-wcsnlen.h>
23 # include "asm-syntax.h"
27 /* size_t wcsnlen (const wchar_t *s, size_t maxlen)
28 Returns the number of characters in s or at most maxlen.
33 -r3=maxlen (number of characters to be read)
35 -r5=current_len and return_value
41 .machinemode "zarch_nohighgprs"
43 # if !defined __s390x__
45 # endif /* !defined __s390x__ */
47 clgfi %r3,0 /* if maxlen == 0, return 0. */
51 vlbb %v16,0(%r2),6 /* Load s until next 4k-byte boundary. */
52 lcbb %r1,0(%r2),6 /* Get bytes to 4k-byte boundary or 16. */
53 llgfr %r1,%r1 /* Convert 32bit to 64bit. */
55 tmll %r2,3 /* Test if s is 4-byte aligned? */
56 jne .Lfallback /* And use common-code variant if not. */
58 /* Check range of maxlen and convert to byte-count. */
60 tmhh %r3,49152 /* Test bit 0 or 1 of maxlen. */
61 lghi %r4,-4 /* Max byte-count is 18446744073709551612. */
63 tmlh %r3,49152 /* Test bit 0 or 1 of maxlen. */
64 llilf %r4,4294967292 /* Max byte-count is 4294967292. */
65 # endif /* !__s390x__ */
66 sllg %r3,%r3,2 /* Convert character-count to byte-count. */
67 locgrne %r3,%r4 /* Use max byte-count, if bit 0/1 was one. */
69 vfenezf %v16,%v16,%v16 /* Find element not equal with zero search. */
71 locgrh %r1,%r3 /* loaded_byte_count
72 = min (loaded_byte_count, maxlen) */
74 vlgvb %r5,%v16,7 /* Load zero index or 16 if not found. */
75 clrjl %r5,%r1,.Lend /* Found zero within loaded bytes -> return. */
77 clgr %r1,%r3 /* If loaded_byte_count == maxlen -> end. */
81 /* Align s to 16 byte. */
82 risbgn %r4,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15. */
83 lghi %r5,16 /* current_len = 16. */
84 slr %r5,%r4 /* Compute bytes to 16bytes boundary. */
86 lgr %r1,%r5 /* If %r5 + 64 < maxlen? -> loop64. */
88 clgrjl %r1,%r3,.Lloop64
90 /* Find zero in max 64byte with aligned s. */
92 vl %v16,0(%r5,%r2) /* Load s. */
93 vfenezfs %v16,%v16,%v16 /* Find element not equal with zero search. */
94 je .Lfound /* Jump away if zero was found. */
96 clgrjhe %r5,%r3,.Lfound /* If current_len >= maxlen -> end. */
98 vfenezfs %v16,%v16,%v16
101 clgrjhe %r5,%r3,.Lfound
103 vfenezfs %v16,%v16,%v16
106 clgrjhe %r5,%r3,.Lfound
108 vfenezfs %v16,%v16,%v16
118 vlgvb %r4,%v16,7 /* Load byte index of zero or 16 if no zero. */
122 locgrh %r5,%r3 /* Return min (current_len, maxlen). */
124 srlg %r2,%r5,2 /* Convert byte-count to character-count. */
127 /* Find zero in 16byte aligned loop. */
129 vl %v16,0(%r5,%r2) /* Load s. */
130 vfenezfs %v16,%v16,%v16 /* Find element not equal with zero search. */
131 je .Lfound /* Jump away if zero was found. */
133 vfenezfs %v16,%v16,%v16
136 vfenezfs %v16,%v16,%v16
139 vfenezfs %v16,%v16,%v16
143 lgr %r1,%r5 /* If %r5 + 64 < maxlen? -> loop64. */
145 clgrjl %r1,%r3,.Lloop64
153 # if ! HAVE_WCSNLEN_IFUNC
154 strong_alias (WCSNLEN_Z13, __wcsnlen)
155 weak_alias (__wcsnlen, wcsnlen)