about summary refs log tree commit diff
path: root/sysdeps/loongarch/lp64/multiarch/strrchr-aligned.S
blob: a73deb78407976d070b7029899e2835cec5e713b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Optimized strrchr implementation using basic LoongArch instructions.
   Copyright (C) 2023 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library.  If not, see
   <https://www.gnu.org/licenses/>.  */

#include <sysdep.h>
#include <sys/regdef.h>
#include <sys/asm.h>

#if IS_IN (libc)
# define STRRCHR __strrchr_aligned
#else
# define STRRCHR strrchr
#endif

LEAF(STRRCHR, 6)
    slli.d      t0, a0, 3
    bstrins.d   a0, zero, 2, 0
    lu12i.w     a2, 0x01010
    ld.d        t2, a0, 0

    andi        a1, a1, 0xff
    ori         a2, a2, 0x101
    li.d        t3, -1
    bstrins.d	a2, a2, 63, 32

    sll.d       t5, t3, t0
    slli.d      a3, a2, 7
    orn         t4, t2, t5
    mul.d       a1, a1, a2

    sub.d       t0, t4, a2
    andn        t1, a3, t4
    and         t1, t0, t1
    beqz        t1, L(find_tail)


    ctz.d       t0, t1
    orn         t0, zero, t0
    xor         t2, t4, a1
    srl.d       t0, t3, t0

    orn         t2, t2, t0
    orn         t2, t2, t5
    revb.d      t2, t2
    sub.d       t1, t2, a2

    andn        t0, a3, t2
    and         t1, t0, t1
    ctz.d       t0, t1
    srli.d      t0, t0, 3

    addi.d      a0, a0, 7
    sub.d       a0, a0, t0
    maskeqz     a0, a0, t1
    jr          ra


L(find_tail):
    addi.d      a4, a0, 8
    addi.d      a0, a0, 8
L(loop_ascii):
    ld.d        t2, a0, 0
    sub.d       t1, t2, a2

    and         t0, t1, a3
    bnez        t0, L(more_check)
    ld.d        t2, a0, 8
    sub.d       t1, t2, a2

    and         t0, t1, a3
    addi.d      a0, a0, 16
    beqz        t0, L(loop_ascii)
    addi.d      a0, a0, -8

L(more_check):
    andn        t0, a3, t2
    and         t1, t1, t0
    bnez        t1, L(tail)
    addi.d      a0, a0, 8


L(loop_nonascii):
    ld.d        t2, a0, 0
    sub.d       t1, t2, a2
    andn        t0, a3, t2
    and         t1, t0, t1

    bnez        t1, L(tail)
    ld.d        t2, a0, 8
    addi.d      a0, a0, 16
    sub.d       t1, t2, a2

    andn        t0, a3, t2
    and         t1, t0, t1
    beqz        t1, L(loop_nonascii)
    addi.d      a0, a0, -8

L(tail):
    ctz.d       t0, t1
    orn         t0, zero, t0
    xor         t2, t2, a1
    srl.d       t0, t3, t0


    orn         t2, t2, t0
    revb.d      t2, t2
    sub.d       t1, t2, a2
    andn        t0, a3, t2

    and         t1, t0, t1
    bnez        t1, L(count_pos)
L(find_loop):
    beq         a0, a4, L(find_end)
    ld.d        t2, a0, -8

    addi.d      a0, a0, -8
    xor         t2, t2, a1
    sub.d       t1, t2, a2
    andn        t0, a3, t2

    and         t1, t0, t1
    beqz        t1, L(find_loop)
    revb.d      t2, t2
    sub.d       t1, t2, a2


    andn        t0, a3, t2
    and         t1, t0, t1
L(count_pos):
    ctz.d       t0, t1
    addi.d      a0, a0, 7

    srli.d      t0, t0, 3
    sub.d       a0, a0, t0
    jr          ra
    nop

L(find_end):
    xor         t2, t4, a1
    orn         t2, t2, t5
    revb.d      t2, t2
    sub.d       t1, t2, a2


    andn        t0, a3, t2
    and         t1, t0, t1
    ctz.d       t0, t1
    srli.d      t0, t0, 3

    addi.d      a0, a4, -1
    sub.d       a0, a0, t0
    maskeqz     a0, a0, t1
    jr          ra
END(STRRCHR)

libc_hidden_builtin_def(STRRCHR)