1 /* Vector optimized 32/64 bit S/390 version of wmemchr.
2 Copyright (C) 2015-2023 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-wmemchr.h>
23 # include "asm-syntax.h"
27 /* wchar_t *wmemchr (const wchar_t *s, wchar_t c, size_t n)
28 Scans memory for character c
29 and returns pointer to first c.
44 .machinemode "zarch_nohighgprs"
46 # if !defined __s390x__
48 # endif /* !defined __s390x__ */
50 clgije %r4,0,.Lnf_end /* If len == 0 then exit. */
52 vlbb %v16,0(%r2),6 /* Load s until next 4k-byte boundary. */
53 lcbb %r0,0(%r2),6 /* Get bytes to 4k-byte boundary or 16. */
54 llgfr %r0,%r0 /* Convert 32bit to 64bit. */
56 tmll %r2,3 /* Test if s is 4-byte aligned? */
57 jne .Lfallback /* And use common-code variant if not. */
59 vlvgf %v18,%r3,0 /* Generate vector which elements are all c. */
61 lghi %r5,16 /* current_len = 16. */
63 /* Check range of maxlen and convert to byte-count. */
65 tmhh %r4,49152 /* Test bit 0 or 1 of maxlen. */
66 lghi %r1,-4 /* Max byte-count is 18446744073709551612. */
68 tmlh %r4,49152 /* Test bit 0 or 1 of maxlen. */
69 llilf %r1,4294967292 /* Max byte-count is 4294967292. */
70 # endif /* !__s390x__ */
71 sllg %r4,%r4,2 /* Convert character-count to byte-count. */
72 locgrne %r4,%r1 /* Use max byte-count, if bit 0/1 was one. */
74 clgrjhe %r0,%r4,.Llastcmp /* If (bytes to boundary) >= n,
77 vfeefs %v17,%v16,%v18 /* Find c. */
78 vlgvb %r1,%v17,7 /* Load byte index of c. */
79 clgrjl %r1,%r0,.Lfound2 /* Found c is within loaded bytes. */
81 /* Align s to 16 byte. */
82 risbgn %r1,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15. */
83 slr %r5,%r1 /* Compute bytes to 16bytes boundary. */
85 lgr %r0,%r5 /* If %r5 + 64 < n? -> loop64. */
87 clgrjl %r0,%r4,.Lloop64
91 clgrjhe %r5,%r4,.Llastcmp /* Do last compare if curr-len >= n. */
92 vfeefs %v17,%v16,%v18 /* Find c. */
93 jl .Lfound /* Jump away if c was found. */
97 clgrjhe %r5,%r4,.Llastcmp
103 clgrjhe %r5,%r4,.Llastcmp
104 vfeefs %v17,%v16,%v18
111 /* Use comparison result only if located within first n characters.
114 (current_len - n): [0...16[
115 first ignored match index = vr-width - (current_len - n) ]0...16]
117 vfeefs %v17,%v16,%v18 /* Find c. */
118 slgrk %r4,%r5,%r4 /* %r5 = current_len - n. */
119 lghi %r0,16 /* Register width = 16. */
120 vlgvb %r1,%v17,7 /* Extract found index or 16 if all equal. */
121 slr %r0,%r4 /* %r0 = first ignored match index. */
122 clrjl %r1,%r0,.Lfound2 /* Go away if miscompare is below n bytes. */
123 /* c not found within n-bytes. */
125 lghi %r2,0 /* Return null. */
137 vlgvb %r1,%v17,7 /* Load byte index of c. */
139 slgfi %r5,16 /* current_len -=16 */
140 algr %r5,%r1 /* Zero byte index is added to current len. */
141 la %r2,0(%r5,%r2) /* Return pointer to c. */
146 vfeefs %v17,%v16,%v18 /* Find c. */
147 jl .Lfound0 /* Jump away if c was found. */
149 vfeefs %v17,%v16,%v18
152 vfeefs %v17,%v16,%v18
155 vfeefs %v17,%v16,%v18
159 lgr %r0,%r5 /* If %r5 + 64 < n? -> loop64. */
161 clgrjl %r0,%r4,.Lloop64
168 # if ! HAVE_WMEMCHR_IFUNC
169 strong_alias (WMEMCHR_Z13, __wmemchr)
170 weak_alias (__wmemchr, wmemchr)
173 # if defined HAVE_S390_MIN_Z13_ZARCH_ASM_SUPPORT \
174 && defined SHARED && IS_IN (libc)
175 strong_alias (WMEMCHR_Z13, __GI___wmemchr)
176 weak_alias (WMEMCHR_Z13, __GI_wmemchr)