1/* Optimized strlen implementation using LoongArch LSX instructions.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
3
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library. If not, see
18 <https://www.gnu.org/licenses/>. */
19
20#include <sysdep.h>
21#include <sys/regdef.h>
22#include <sys/asm.h>
23
24#if IS_IN (libc) && !defined __loongarch_soft_float
25
26#ifndef AS_STRCHRNUL
27# define STRCHR __strchr_lsx
28#endif
29
30LEAF(STRCHR, 6)
31 andi t1, a0, 0xf
32 bstrins.d a0, zero, 3, 0
33 vld vr0, a0, 0
34 li.d t2, -1
35
36 vreplgr2vr.b vr1, a1
37 sll.d t3, t2, t1
38 vxor.v vr2, vr0, vr1
39 vmin.bu vr0, vr0, vr2
40
41 vmsknz.b vr0, vr0
42 movfr2gr.s t0, fa0
43 ext.w.h t0, t0
44 orn t0, t0, t3
45
46 beq t0, t2, L(loop)
47L(found):
48 cto.w t0, t0
49 add.d a0, a0, t0
50#ifndef AS_STRCHRNUL
51 vreplve.b vr2, vr2, t0
52 vpickve2gr.bu t1, vr2, 0
53 masknez a0, a0, t1
54#endif
55 jr ra
56
57
58L(loop):
59 vld vr0, a0, 16
60 addi.d a0, a0, 16
61 vxor.v vr2, vr0, vr1
62 vmin.bu vr0, vr0, vr2
63
64 vsetanyeqz.b fcc0, vr0
65 bceqz fcc0, L(loop)
66 vmsknz.b vr0, vr0
67 movfr2gr.s t0, fa0
68
69 b L(found)
70END(STRCHR)
71
72libc_hidden_builtin_def (STRCHR)
73#endif
74

source code of glibc/sysdeps/loongarch/lp64/multiarch/strchr-lsx.S