1// Inferno's libkern/memmove-386.s
2// https://bitbucket.org/inferno-os/inferno-os/src/default/libkern/memmove-386.s
3//
4//         Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
5//         Revisions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com).  All rights reserved.
6//         Portions Copyright 2009 The Go Authors. All rights reserved.
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and associated documentation files (the "Software"), to deal
10// in the Software without restriction, including without limitation the rights
11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12// copies of the Software, and to permit persons to whom the Software is
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in
16// all copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24// THE SOFTWARE.
25
26#include "textflag.h"
27
28// See memmove Go doc for important implementation constraints.
29
30// func memmove(to, from unsafe.Pointer, n uintptr)
31TEXT runtime·memmove(SB), NOSPLIT, $0-12
32	MOVL	to+0(FP), DI
33	MOVL	from+4(FP), SI
34	MOVL	n+8(FP), BX
35
36	// REP instructions have a high startup cost, so we handle small sizes
37	// with some straightline code. The REP MOVSL instruction is really fast
38	// for large sizes. The cutover is approximately 1K.
39tail:
40	TESTL	BX, BX
41	JEQ	move_0
42	CMPL	BX, $2
43	JBE	move_1or2
44	CMPL	BX, $4
45	JB	move_3
46	JE	move_4
47	CMPL	BX, $8
48	JBE	move_5through8
49	CMPL	BX, $16
50	JBE	move_9through16
51
52/*
53 * check and set for backwards
54 */
55	CMPL	SI, DI
56	JLS	back
57
58/*
59 * forward copy loop
60 */
61forward:
62	MOVL	BX, CX
63	SHRL	$2, CX
64	ANDL	$3, BX
65
66	REP;	MOVSL
67	JMP	tail
68/*
69 * check overlap
70 */
71back:
72	MOVL	SI, CX
73	ADDL	BX, CX
74	CMPL	CX, DI
75	JLS	forward
76/*
77 * whole thing backwards has
78 * adjusted addresses
79 */
80
81	ADDL	BX, DI
82	ADDL	BX, SI
83	STD
84
85/*
86 * copy
87 */
88	MOVL	BX, CX
89	SHRL	$2, CX
90	ANDL	$3, BX
91
92	SUBL	$4, DI
93	SUBL	$4, SI
94	REP;	MOVSL
95
96	CLD
97	ADDL	$4, DI
98	ADDL	$4, SI
99	SUBL	BX, DI
100	SUBL	BX, SI
101	JMP	tail
102
103move_1or2:
104	MOVB	(SI), AX
105	MOVB	-1(SI)(BX*1), CX
106	MOVB	AX, (DI)
107	MOVB	CX, -1(DI)(BX*1)
108	RET
109move_0:
110	RET
111move_3:
112	MOVW	(SI), AX
113	MOVB	2(SI), CX
114	MOVW	AX, (DI)
115	MOVB	CX, 2(DI)
116	RET
117move_4:
118	// We need a separate case for 4 to make sure we write pointers atomically.
119	MOVL	(SI), AX
120	MOVL	AX, (DI)
121	RET
122move_5through8:
123	MOVL	(SI), AX
124	MOVL	-4(SI)(BX*1), CX
125	MOVL	AX, (DI)
126	MOVL	CX, -4(DI)(BX*1)
127	RET
128move_9through16:
129	MOVL	(SI), AX
130	MOVL	4(SI), CX
131	MOVL	-8(SI)(BX*1), DX
132	MOVL	-4(SI)(BX*1), BP
133	MOVL	AX, (DI)
134	MOVL	CX, 4(DI)
135	MOVL	DX, -8(DI)(BX*1)
136	MOVL	BP, -4(DI)(BX*1)
137	RET
138