]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/loongarch/lp64/multiarch/memchr-aligned.S
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / loongarch / lp64 / multiarch / memchr-aligned.S
1 /* Optimized memchr implementation using basic LoongArch instructions.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library. If not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <sysdep.h>
20 #include <sys/regdef.h>
21 #include <sys/asm.h>
22
23 #if IS_IN (libc)
24 # define MEMCHR_NAME __memchr_aligned
25 #else
26 # define MEMCHR_NAME memchr
27 #endif
28
29 LEAF(MEMCHR_NAME, 6)
30 beqz a2, L(out)
31 andi t1, a0, 0x7
32 add.d a5, a0, a2
33 bstrins.d a0, zero, 2, 0
34
35 ld.d t0, a0, 0
36 bstrins.d a1, a1, 15, 8
37 lu12i.w a3, 0x01010
38 slli.d t2, t1, 03
39
40 bstrins.d a1, a1, 31, 16
41 ori a3, a3, 0x101
42 li.d t7, -1
43 li.d t8, 8
44
45 bstrins.d a1, a1, 63, 32
46 bstrins.d a3, a3, 63, 32
47 sll.d t2, t7, t2
48 xor t0, t0, a1
49
50
51 addi.d a6, a5, -1
52 slli.d a4, a3, 7
53 sub.d t1, t8, t1
54 orn t0, t0, t2
55
56 sub.d t2, t0, a3
57 andn t3, a4, t0
58 bstrins.d a6, zero, 2, 0
59 and t0, t2, t3
60
61 bgeu t1, a2, L(end)
62 L(loop):
63 bnez t0, L(found)
64 ld.d t1, a0, 8
65 xor t0, t1, a1
66
67 addi.d a0, a0, 8
68 sub.d t2, t0, a3
69 andn t3, a4, t0
70 and t0, t2, t3
71
72
73 bne a0, a6, L(loop)
74 L(end):
75 sub.d t1, a5, a6
76 ctz.d t0, t0
77 srli.d t0, t0, 3
78
79 sltu t1, t0, t1
80 add.d a0, a0, t0
81 maskeqz a0, a0, t1
82 jr ra
83
84 L(found):
85 ctz.d t0, t0
86 srli.d t0, t0, 3
87 add.d a0, a0, t0
88 jr ra
89
90 L(out):
91 move a0, zero
92 jr ra
93 END(MEMCHR_NAME)
94
95 libc_hidden_builtin_def (MEMCHR_NAME)