aboutsummaryrefslogtreecommitdiff
path: root/newlib/libc/machine/mips/strcmp.S
blob: 07b988a57672dfea6828de8e694fcd83f8fc93d2 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2014-2018 MIPS Tech, LLC
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef ANDROID_CHANGES
# include "machine/asm.h"
# include "machine/regdef.h"
#elif _LIBC
# include "machine/asm.h"
# include "machine/regdef.h"
#else
# include <regdef.h>
# include <sys/asm.h>
#endif

/* Testing on a little endian machine showed using CLZ was a
   performance loss, so we are not turning it on by default.  */
#if defined(ENABLE_CLZ) && (__mips_isa_rev > 1)
# define USE_CLZ
#elif (__mips_isa_rev >= 2)
# define USE_EXT 1
#else
# define USE_EXT 0
#endif

/* Some asm.h files do not have the L macro definition.  */
#ifndef L
# if _MIPS_SIM == _ABIO32
#  define L(label) $L ## label
# else
#  define L(label) .L ## label
# endif
#endif

/* Some asm.h files do not have the PTR_ADDIU macro definition.  */
#ifndef PTR_ADDIU
# ifdef USE_DOUBLE
#  define PTR_ADDIU       daddiu
# else
#  define PTR_ADDIU       addiu
# endif
#endif

/* Haven't yet found a configuration where DSP code outperforms
   normal assembly.  */
#define __mips_using_dsp 0

/* Allow the routine to be named something else if desired.  */
#ifndef STRCMP_NAME
# define STRCMP_NAME strcmp
#endif

#ifdef ANDROID_CHANGES
LEAF(STRCMP_NAME, 0)
#else
LEAF(STRCMP_NAME)
#endif
	.set	nomips16
	or	t0, a0, a1
	andi	t0, t0, 0x3
	bne	t0, zero, L(byteloop)

/* Both strings are 4 byte aligned at this point.  */

	li	t8, 0x01010101
#if !__mips_using_dsp
	li	t9, 0x7f7f7f7f
#endif

#if __mips_using_dsp
# define STRCMP32(OFFSET)	\
	lw	a2, OFFSET(a0); \
	lw	a3, OFFSET(a1); \
	subu_s.qb t0, t8, a2;	\
	bne	a2, a3, L(worddiff); \
	bne	t0, zero, L(returnzero)
#else  /* !__mips_using_dsp */
# define STRCMP32(OFFSET)	\
	lw	a2, OFFSET(a0); \
	lw	a3, OFFSET(a1); \
	subu	t0, a2, t8;	\
	nor	t1, a2, t9;	\
	bne	a2, a3, L(worddiff); \
	and	t1, t0, t1;	\
	bne	t1, zero, L(returnzero)
#endif /* __mips_using_dsp */

	.align 2
L(wordloop):
	STRCMP32(0)
	STRCMP32(4)
	STRCMP32(8)
	STRCMP32(12)
	STRCMP32(16)
	STRCMP32(20)
	STRCMP32(24)
	lw	a2, 28(a0)
	lw	a3, 28(a1)
#if __mips_using_dsp
	subu_s.qb t0, t8, a2
#else
	subu	t0, a2, t8
	nor	t1, a2, t9
	and	t1, t0, t1
#endif

	PTR_ADDIU a0, a0, 32
	bne	a2, a3, L(worddiff)
	PTR_ADDIU a1, a1, 32
	beq	t1, zero, L(wordloop)

L(returnzero):
	move	va0, zero
	jr	ra

	.align 2
L(worddiff):
#ifdef USE_CLZ
	xor	t0, a2, a3
	or	t0, t0, t1
# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
	wsbh	t0, t0
	rotr	t0, t0, 16
# endif /* LITTLE_ENDIAN */
	clz	t1, t0
	or	t0, t1, 24	/* Only care about multiples of 8.  */
	xor	t1, t1, t0	/* {0,8,16,24} => {24,16,8,0}  */
# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
	sllv	a2,a2,t1
	sllv	a3,a3,t1
# else
	srlv	a2,a2,t1
	srlv	a3,a3,t1
# endif
	subu	va0, a2, a3
	jr	ra
#else /* USE_CLZ */
# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
	andi	a0, a2, 0xff	/* abcd => d */
	andi	a1, a3, 0xff
	beq	a0, zero, L(wexit01)
#  if USE_EXT
	ext	t8, a2, 8, 8
	bne	a0, a1, L(wexit01)
	ext	t9, a3, 8, 8
	beq	t8, zero, L(wexit89)
	ext	a0, a2, 16, 8
	bne	t8, t9, L(wexit89)
	ext	a1, a3, 16, 8
#  else  /* !USE_EXT */
	srl	t8, a2, 8
	bne	a0, a1, L(wexit01)
	srl	t9, a3, 8
	andi	t8, t8, 0xff
	andi	t9, t9, 0xff
	beq	t8, zero, L(wexit89)
	srl	a0, a2, 16
	bne	t8, t9, L(wexit89)
	srl	a1, a3, 16
	andi	a0, a0, 0xff
	andi	a1, a1, 0xff
#  endif  /* !USE_EXT */

# else /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
	srl	a0, a2, 24	/* abcd => a */
	srl	a1, a3, 24
	beq	a0, zero, L(wexit01)

#  if USE_EXT
	ext	t8, a2, 16, 8
	bne	a0, a1, L(wexit01)
	ext	t9, a3, 16, 8
	beq	t8, zero, L(wexit89)
	ext	a0, a2, 8, 8
	bne	t8, t9, L(wexit89)
	ext	a1, a3, 8, 8
#  else /* ! USE_EXT */
	srl	t8, a2, 16
	bne	a0, a1, L(wexit01)
	srl	t9, a3, 16
	andi	t8, t8, 0xff
	andi	t9, t9, 0xff
	beq	t8, zero, L(wexit89)
	srl	a0, a2, 8
	bne	t8, t9, L(wexit89)
	srl	a1, a3, 8
	andi	a0, a0, 0xff
	andi	a1, a1, 0xff
#  endif  /* USE_EXT */

# endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */

	beq	a0, zero, L(wexit01)
	bne	a0, a1, L(wexit01)

# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
	srl a0, a2, 24
	srl a1, a3, 24
# else /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
	move a0, a2
	move a1, a3
# endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */

L(wexit01):
	subu	va0, a0, a1
	jr	ra

L(wexit89):
	subu	va0, t8, t9
	jr	ra

#endif /* USE_CLZ */

#define DELAY_NOP nop

/* It might seem better to do the 'beq' instruction between the two 'lbu'
   instructions so that the nop is not needed but testing showed that this
   code is actually faster (based on glibc strcmp test).  */

#define BYTECMP01(OFFSET) \
	lbu	a3, OFFSET(a1); \
	DELAY_NOP; \
	beq	a2, zero, L(bexit01); \
	lbu	t8, OFFSET+1(a0); \
	bne	a2, a3, L(bexit01)

#define BYTECMP89(OFFSET) \
	lbu	t9, OFFSET(a1); \
	DELAY_NOP; \
	beq	t8, zero, L(bexit89); \
	lbu	a2, OFFSET+1(a0); \
	bne	t8, t9, L(bexit89)

	.align 2
L(byteloop):
	lbu	a2, 0(a0)
	BYTECMP01(0)
	BYTECMP89(1)
	BYTECMP01(2)
	BYTECMP89(3)
	BYTECMP01(4)
	BYTECMP89(5)
	BYTECMP01(6)
	lbu	t9, 7(a1)

	PTR_ADDIU a0, a0, 8
	beq	t8, zero, L(bexit89)
	PTR_ADDIU a1, a1, 8
	beq	t8, t9, L(byteloop)

L(bexit89):
	subu	va0, t8, t9
	jr	ra

L(bexit01):
	subu	va0, a2, a3
	jr	ra

	.set	at

END(STRCMP_NAME)