1/* Optimized strlen implementation using basic LoongArch 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)
25# define STRLEN __strlen_aligned
26#else
27# define STRLEN strlen
28#endif
29
30LEAF(STRLEN, 6)
31 move a1, a0
32 bstrins.d a0, zero, 2, 0
33 lu12i.w a2, 0x01010
34 li.w t0, -1
35
36 ld.d t2, a0, 0
37 andi t1, a1, 0x7
38 ori a2, a2, 0x101
39 slli.d t1, t1, 3
40
41 bstrins.d a2, a2, 63, 32
42 sll.d t1, t0, t1
43 slli.d t3, a2, 7
44 nor a3, zero, t3
45
46 orn t2, t2, t1
47 sub.d t0, t2, a2
48 nor t1, t2, a3
49 and t0, t0, t1
50
51
52 bnez t0, L(count_pos)
53 addi.d a0, a0, 8
54L(loop_16_7bit):
55 ld.d t2, a0, 0
56 sub.d t1, t2, a2
57
58 and t0, t1, t3
59 bnez t0, L(more_check)
60 ld.d t2, a0, 8
61 sub.d t1, t2, a2
62
63 and t0, t1, t3
64 addi.d a0, a0, 16
65 beqz t0, L(loop_16_7bit)
66 addi.d a0, a0, -8
67
68L(more_check):
69 nor t0, t2, a3
70 and t0, t1, t0
71 bnez t0, L(count_pos)
72 addi.d a0, a0, 8
73
74
75L(loop_16_8bit):
76 ld.d t2, a0, 0
77 sub.d t1, t2, a2
78 nor t0, t2, a3
79 and t0, t0, t1
80
81 bnez t0, L(count_pos)
82 ld.d t2, a0, 8
83 addi.d a0, a0, 16
84 sub.d t1, t2, a2
85
86 nor t0, t2, a3
87 and t0, t0, t1
88 beqz t0, L(loop_16_8bit)
89 addi.d a0, a0, -8
90
91L(count_pos):
92 ctz.d t1, t0
93 sub.d a0, a0, a1
94 srli.d t1, t1, 3
95 add.d a0, a0, t1
96
97 jr ra
98END(STRLEN)
99
100libc_hidden_builtin_def (STRLEN)
101

source code of glibc/sysdeps/loongarch/lp64/multiarch/strlen-aligned.S