1 | /* Copyright (c) 2017 SiFive Inc. All rights reserved. |
---|
2 | |
---|
3 | This copyrighted material is made available to anyone wishing to use, |
---|
4 | modify, copy, or redistribute it subject to the terms and conditions |
---|
5 | of the FreeBSD License. This program is distributed in the hope that |
---|
6 | it will be useful, but WITHOUT ANY WARRANTY expressed or implied, |
---|
7 | including the implied warranties of MERCHANTABILITY or FITNESS FOR |
---|
8 | A PARTICULAR PURPOSE. A copy of this license is available at |
---|
9 | http://www.opensource.org/licenses. |
---|
10 | */ |
---|
11 | |
---|
12 | #include <sys/asm.h> |
---|
13 | |
---|
14 | #if BYTE_ORDER != LITTLE_ENDIAN |
---|
15 | # error |
---|
16 | #endif |
---|
17 | |
---|
18 | .text |
---|
19 | .globl strcmp |
---|
20 | .type strcmp, @function |
---|
21 | strcmp: |
---|
22 | or a4, a0, a1 |
---|
23 | li t2, -1 |
---|
24 | and a4, a4, SZREG-1 |
---|
25 | bnez a4, .Lmisaligned |
---|
26 | |
---|
27 | #if SZREG == 4 |
---|
28 | li a5, 0x7f7f7f7f |
---|
29 | #else |
---|
30 | ld a5, mask |
---|
31 | #endif |
---|
32 | |
---|
33 | .macro check_one_word i n |
---|
34 | REG_L a2, \i*SZREG(a0) |
---|
35 | REG_L a3, \i*SZREG(a1) |
---|
36 | |
---|
37 | and t0, a2, a5 |
---|
38 | or t1, a2, a5 |
---|
39 | add t0, t0, a5 |
---|
40 | or t0, t0, t1 |
---|
41 | |
---|
42 | bne t0, t2, .Lnull\i |
---|
43 | .if \i+1-\n |
---|
44 | bne a2, a3, .Lmismatch |
---|
45 | .else |
---|
46 | add a0, a0, \n*SZREG |
---|
47 | add a1, a1, \n*SZREG |
---|
48 | beq a2, a3, .Lloop |
---|
49 | # fall through to .Lmismatch |
---|
50 | .endif |
---|
51 | .endm |
---|
52 | |
---|
53 | .macro foundnull i n |
---|
54 | .ifne \i |
---|
55 | .Lnull\i: |
---|
56 | add a0, a0, \i*SZREG |
---|
57 | add a1, a1, \i*SZREG |
---|
58 | .ifeq \i-1 |
---|
59 | .Lnull0: |
---|
60 | .endif |
---|
61 | bne a2, a3, .Lmisaligned |
---|
62 | li a0, 0 |
---|
63 | ret |
---|
64 | .endif |
---|
65 | .endm |
---|
66 | |
---|
67 | .Lloop: |
---|
68 | # examine full words at a time, favoring strings of a couple dozen chars |
---|
69 | #if __riscv_xlen == 32 |
---|
70 | check_one_word 0 5 |
---|
71 | check_one_word 1 5 |
---|
72 | check_one_word 2 5 |
---|
73 | check_one_word 3 5 |
---|
74 | check_one_word 4 5 |
---|
75 | #else |
---|
76 | check_one_word 0 3 |
---|
77 | check_one_word 1 3 |
---|
78 | check_one_word 2 3 |
---|
79 | #endif |
---|
80 | # backwards branch to .Lloop contained above |
---|
81 | |
---|
82 | .Lmismatch: |
---|
83 | # words don't match, but a2 has no null byte. |
---|
84 | #if __riscv_xlen == 64 |
---|
85 | sll a4, a2, 48 |
---|
86 | sll a5, a3, 48 |
---|
87 | bne a4, a5, .Lmismatch_upper |
---|
88 | sll a4, a2, 32 |
---|
89 | sll a5, a3, 32 |
---|
90 | bne a4, a5, .Lmismatch_upper |
---|
91 | #endif |
---|
92 | sll a4, a2, 16 |
---|
93 | sll a5, a3, 16 |
---|
94 | bne a4, a5, .Lmismatch_upper |
---|
95 | |
---|
96 | srl a4, a2, 8*SZREG-16 |
---|
97 | srl a5, a3, 8*SZREG-16 |
---|
98 | sub a0, a4, a5 |
---|
99 | and a1, a0, 0xff |
---|
100 | bnez a1, 1f |
---|
101 | ret |
---|
102 | |
---|
103 | .Lmismatch_upper: |
---|
104 | srl a4, a4, 8*SZREG-16 |
---|
105 | srl a5, a5, 8*SZREG-16 |
---|
106 | sub a0, a4, a5 |
---|
107 | and a1, a0, 0xff |
---|
108 | bnez a1, 1f |
---|
109 | ret |
---|
110 | |
---|
111 | 1:and a4, a4, 0xff |
---|
112 | and a5, a5, 0xff |
---|
113 | sub a0, a4, a5 |
---|
114 | ret |
---|
115 | |
---|
116 | .Lmisaligned: |
---|
117 | # misaligned |
---|
118 | lbu a2, 0(a0) |
---|
119 | lbu a3, 0(a1) |
---|
120 | add a0, a0, 1 |
---|
121 | add a1, a1, 1 |
---|
122 | bne a2, a3, 1f |
---|
123 | bnez a2, .Lmisaligned |
---|
124 | |
---|
125 | 1: |
---|
126 | sub a0, a2, a3 |
---|
127 | ret |
---|
128 | |
---|
129 | # cases in which a null byte was detected |
---|
130 | #if __riscv_xlen == 32 |
---|
131 | foundnull 0 5 |
---|
132 | foundnull 1 5 |
---|
133 | foundnull 2 5 |
---|
134 | foundnull 3 5 |
---|
135 | foundnull 4 5 |
---|
136 | #else |
---|
137 | foundnull 0 3 |
---|
138 | foundnull 1 3 |
---|
139 | foundnull 2 3 |
---|
140 | #endif |
---|
141 | .size strcmp, .-strcmp |
---|
142 | |
---|
143 | #if SZREG == 8 |
---|
144 | .section .srodata.cst8,"aM",@progbits,8 |
---|
145 | .align 3 |
---|
146 | mask: |
---|
147 | .dword 0x7f7f7f7f7f7f7f7f |
---|
148 | #endif |
---|