1 /* memrchr - find the last occurrence of a byte in a memory block
3 Copyright (C) 2015-2023 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/>. */
25 * ARMv8-a, AArch64, Advanced SIMD.
51 For each 16-byte chunk we calculate a 64-bit nibble mask value with four bits
52 per byte. We take 4 bits of every comparison byte with shift right and narrow
53 by 4 instruction. Since the bits in the nibble mask reflect the order in
54 which things occur in the original string, counting leading zeros identifies
55 exactly which byte matched. */
64 ld1 {vdata.16b}, [src]
65 dup vrepchr.16b, chrin
66 cmeq vhas_chr.16b, vdata.16b, vrepchr.16b
68 shrn vend.8b, vhas_chr.8h, 4 /* 128->64 */
71 cbz synd, L(start_loop)
74 sub result, endm1, synd, lsr 2
75 cmp cntin, synd, lsr 2
76 csel result, result, xzr, hi
81 subs cntrem, src, srcin
84 /* Make sure that it won't overread by a 16-byte chunk */
86 tbz cntrem, 4, L(loop32_2)
91 ldr qdata, [src, -32]!
92 cmeq vhas_chr.16b, vdata.16b, vrepchr.16b
93 umaxp vend.16b, vhas_chr.16b, vhas_chr.16b /* 128->64 */
99 subs cntrem, cntrem, 32
100 cmeq vhas_chr.16b, vdata.16b, vrepchr.16b
102 umaxp vend.16b, vhas_chr.16b, vhas_chr.16b /* 128->64 */
108 shrn vend.8b, vhas_chr.8h, 4 /* 128->64 */
116 sub tmp, tmp, synd, lsr 2
118 csel result, tmp, xzr, hs
126 libc_hidden_def (__memrchr)
127 weak_alias (__memrchr, memrchr)
128 libc_hidden_builtin_def (memrchr)