1 /* strchrnul - find a character or nul in a string
3 Copyright (C) 2014-2020 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library. If not, see
19 <https://www.gnu.org/licenses/>. */
29 /* Arguments and results. */
35 /* Locals and temporaries. */
54 For each 32-byte hunk we calculate a 64-bit syndrome value, with
55 two bits per byte (LSB is always in bits 0 and 1, for both big
56 and little-endian systems). For each tuple, bit 0 is set iff
57 the relevant byte matched the requested character or nul. Since the
58 bits in the syndrome reflect exactly the order in which things occur
59 in the original string a count_trailing_zeros() operation will
60 identify exactly which byte is causing the termination. */
64 /* Magic constant 0x40100401 to allow us to identify which lane
65 matches the termination condition. */
67 movk wtmp2, #0x4010, lsl #16
68 dup vrepchr.16b, chrin
69 bic src, srcin, #31 /* Work with aligned 32-byte hunks. */
70 dup vrepmask.4s, wtmp2
74 /* Input string is not 32-byte aligned. Rather than forcing
75 the padding bytes to a safe value, we calculate the syndrome
76 for all the bytes, but then mask off those bits of the
77 syndrome that are related to the padding. */
78 ld1 {vdata1.16b, vdata2.16b}, [src], #32
80 cmeq vhas_nul1.16b, vdata1.16b, #0
81 cmeq vhas_chr1.16b, vdata1.16b, vrepchr.16b
82 cmeq vhas_nul2.16b, vdata2.16b, #0
83 cmeq vhas_chr2.16b, vdata2.16b, vrepchr.16b
84 orr vhas_chr1.16b, vhas_chr1.16b, vhas_nul1.16b
85 orr vhas_chr2.16b, vhas_chr2.16b, vhas_nul2.16b
86 and vhas_chr1.16b, vhas_chr1.16b, vrepmask.16b
87 and vhas_chr2.16b, vhas_chr2.16b, vrepmask.16b
89 addp vend1.16b, vhas_chr1.16b, vhas_chr2.16b // 256->128
91 addp vend1.16b, vend1.16b, vend1.16b // 128->64
95 bic tmp1, tmp3, tmp1 // Mask padding bits.
99 ld1 {vdata1.16b, vdata2.16b}, [src], #32
100 cmeq vhas_nul1.16b, vdata1.16b, #0
101 cmeq vhas_chr1.16b, vdata1.16b, vrepchr.16b
102 cmeq vhas_nul2.16b, vdata2.16b, #0
103 cmeq vhas_chr2.16b, vdata2.16b, vrepchr.16b
104 /* Use a fast check for the termination condition. */
105 orr vhas_chr1.16b, vhas_nul1.16b, vhas_chr1.16b
106 orr vhas_chr2.16b, vhas_nul2.16b, vhas_chr2.16b
107 orr vend1.16b, vhas_chr1.16b, vhas_chr2.16b
108 addp vend1.2d, vend1.2d, vend1.2d
109 mov tmp1, vend1.2d[0]
112 /* Termination condition found. Now need to establish exactly why
114 and vhas_chr1.16b, vhas_chr1.16b, vrepmask.16b
115 and vhas_chr2.16b, vhas_chr2.16b, vrepmask.16b
116 addp vend1.16b, vhas_chr1.16b, vhas_chr2.16b // 256->128
117 addp vend1.16b, vend1.16b, vend1.16b // 128->64
119 mov tmp1, vend1.2d[0]
121 /* Count the trailing zeros, by bit reversing... */
123 /* Re-bias source. */
125 clz tmp1, tmp1 /* ... and counting the leading zeros. */
126 /* tmp1 is twice the offset into the fragment. */
127 add result, src, tmp1, lsr #1
131 weak_alias (__strchrnul, strchrnul)