1// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5#include "go_asm.h"
6#include "textflag.h"
7
8TEXT ·Compare<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-56
9	// R0 = a_base (want in R0)
10	// R1 = a_len  (want in R1)
11	// R2 = a_cap  (unused)
12	// R3 = b_base (want in R2)
13	// R4 = b_len  (want in R3)
14	// R5 = b_cap  (unused)
15	MOVD	R3, R2
16	MOVD	R4, R3
17	B	cmpbody<>(SB)
18
19TEXT runtime·cmpstring<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-40
20	// R0 = a_base
21	// R1 = a_len
22	// R2 = b_base
23	// R3 = b_len
24	B	cmpbody<>(SB)
25
26// On entry:
27// R0 points to the start of a
28// R1 is the length of a
29// R2 points to the start of b
30// R3 is the length of b
31//
32// On exit:
33// R0 is the result
34// R4, R5, R6, R8, R9 and R10 are clobbered
35TEXT cmpbody<>(SB),NOSPLIT|NOFRAME,$0-0
36	CMP	R0, R2
37	BEQ	samebytes         // same starting pointers; compare lengths
38	CMP	R1, R3
39	CSEL	LT, R3, R1, R6    // R6 is min(R1, R3)
40
41	CBZ	R6, samebytes
42	BIC	$0xf, R6, R10
43	CBZ	R10, small        // length < 16
44	ADD	R0, R10           // end of chunk16
45	// length >= 16
46chunk16_loop:
47	LDP.P	16(R0), (R4, R8)
48	LDP.P	16(R2), (R5, R9)
49	CMP	R4, R5
50	BNE	cmp
51	CMP	R8, R9
52	BNE	cmpnext
53	CMP	R10, R0
54	BNE	chunk16_loop
55	AND	$0xf, R6, R6
56	CBZ	R6, samebytes
57	SUBS	$8, R6
58	BLT	tail
59	// the length of tail > 8 bytes
60	MOVD.P	8(R0), R4
61	MOVD.P	8(R2), R5
62	CMP	R4, R5
63	BNE	cmp
64	SUB	$8, R6
65	// compare last 8 bytes
66tail:
67	MOVD	(R0)(R6), R4
68	MOVD	(R2)(R6), R5
69	CMP	R4, R5
70	BEQ	samebytes
71cmp:
72	REV	R4, R4
73	REV	R5, R5
74	CMP	R4, R5
75ret:
76	MOVD	$1, R0
77	CNEG	HI, R0, R0
78	RET
79small:
80	TBZ	$3, R6, lt_8
81	MOVD	(R0), R4
82	MOVD	(R2), R5
83	CMP	R4, R5
84	BNE	cmp
85	SUBS	$8, R6
86	BEQ	samebytes
87	ADD	$8, R0
88	ADD	$8, R2
89	SUB	$8, R6
90	B	tail
91lt_8:
92	TBZ	$2, R6, lt_4
93	MOVWU	(R0), R4
94	MOVWU	(R2), R5
95	CMPW	R4, R5
96	BNE	cmp
97	SUBS	$4, R6
98	BEQ	samebytes
99	ADD	$4, R0
100	ADD	$4, R2
101lt_4:
102	TBZ	$1, R6, lt_2
103	MOVHU	(R0), R4
104	MOVHU	(R2), R5
105	CMPW	R4, R5
106	BNE	cmp
107	ADD	$2, R0
108	ADD	$2, R2
109lt_2:
110	TBZ	$0, R6, samebytes
111one:
112	MOVBU	(R0), R4
113	MOVBU	(R2), R5
114	CMPW	R4, R5
115	BNE	ret
116samebytes:
117	CMP	R3, R1
118	CSET	NE, R0
119	CNEG	LO, R0, R0
120	RET
121cmpnext:
122	REV	R8, R4
123	REV	R9, R5
124	CMP	R4, R5
125	B	ret
126