2 * memchr - find a character in a memory zone
4 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 * See https://llvm.org/LICENSE.txt for license information.
6 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
15 #include "../asmdefs.h"
17 /* Arguments and results. */
42 * For each 32-byte chunk we calculate a 64-bit syndrome value, with two bits
43 * per byte. For each tuple, bit 0 is set if the relevant byte matched the
44 * requested character and bit 1 is not used (faster than using a 32bit
45 * syndrome). Since the bits in the syndrome reflect exactly the order in which
46 * things occur in the original string, counting trailing zeros allows to
47 * identify exactly which byte has matched.
50 ENTRY (__memchr_aarch64)
51 /* Do not dereference srcin if no bytes to compare. */
52 cbz cntin, L(zero_length)
54 * Magic constant 0x40100401 allows us to identify which lane matches
58 movk wtmp2, #0x4010, lsl #16
59 dup vrepchr.16b, chrin
60 /* Work with aligned 32-byte chunks */
62 dup vrepmask.4s, wtmp2
64 and cntrem, cntin, #31
68 * Input string is not 32-byte aligned. We calculate the syndrome
69 * value for the aligned 32 bytes block containing the first bytes
70 * and mask the irrelevant part.
73 ld1 {vdata1.16b, vdata2.16b}, [src], #32
75 adds cntin, cntin, tmp
76 cmeq vhas_chr1.16b, vdata1.16b, vrepchr.16b
77 cmeq vhas_chr2.16b, vdata2.16b, vrepchr.16b
78 and vhas_chr1.16b, vhas_chr1.16b, vrepmask.16b
79 and vhas_chr2.16b, vhas_chr2.16b, vrepmask.16b
80 addp vend.16b, vhas_chr1.16b, vhas_chr2.16b /* 256->128 */
81 addp vend.16b, vend.16b, vend.16b /* 128->64 */
83 /* Clear the soff*2 lower bits */
87 /* The first block can also be the last */
89 /* Have we found something already? */
93 ld1 {vdata1.16b, vdata2.16b}, [src], #32
94 subs cntin, cntin, #32
95 cmeq vhas_chr1.16b, vdata1.16b, vrepchr.16b
96 cmeq vhas_chr2.16b, vdata2.16b, vrepchr.16b
97 /* If we're out of data we finish regardless of the result */
99 /* Use a fast check for the termination condition */
100 orr vend.16b, vhas_chr1.16b, vhas_chr2.16b
101 addp vend.2d, vend.2d, vend.2d
103 /* We're not out of data, loop if we haven't found the character */
107 /* Termination condition found, let's calculate the syndrome value */
108 and vhas_chr1.16b, vhas_chr1.16b, vrepmask.16b
109 and vhas_chr2.16b, vhas_chr2.16b, vrepmask.16b
110 addp vend.16b, vhas_chr1.16b, vhas_chr2.16b /* 256->128 */
111 addp vend.16b, vend.16b, vend.16b /* 128->64 */
113 /* Only do the clear for the last possible block */
117 /* Clear the (32 - ((cntrem + soff) % 32)) * 2 upper bits */
118 add tmp, cntrem, soff
126 /* Count the trailing zeros using bit reversing */
128 /* Compensate the last post-increment */
130 /* Check that we have found a character */
132 /* And count the leading zeros */
134 /* Compute the potential result */
135 add result, src, synd, lsr #1
136 /* Select result or NULL */
137 csel result, xzr, result, eq
144 END (__memchr_aarch64)