Home | History | Annotate | Download | only in runtime
      1 // Inferno's libkern/memmove-386.s
      2 // http://code.google.com/p/inferno-os/source/browse/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 TEXT runtimememmove(SB), NOSPLIT, $0-12
     29 	MOVL	to+0(FP), DI
     30 	MOVL	from+4(FP), SI
     31 	MOVL	n+8(FP), BX
     32 
     33 	// REP instructions have a high startup cost, so we handle small sizes
     34 	// with some straightline code.  The REP MOVSL instruction is really fast
     35 	// for large sizes.  The cutover is approximately 1K.
     36 tail:
     37 	TESTL	BX, BX
     38 	JEQ	move_0
     39 	CMPL	BX, $2
     40 	JBE	move_1or2
     41 	CMPL	BX, $4
     42 	JBE	move_3or4
     43 	CMPL	BX, $8
     44 	JBE	move_5through8
     45 	CMPL	BX, $16
     46 	JBE	move_9through16
     47 
     48 /*
     49  * check and set for backwards
     50  */
     51 	CMPL	SI, DI
     52 	JLS	back
     53 
     54 /*
     55  * forward copy loop
     56  */
     57 forward:
     58 	MOVL	BX, CX
     59 	SHRL	$2, CX
     60 	ANDL	$3, BX
     61 
     62 	REP;	MOVSL
     63 	JMP	tail
     64 /*
     65  * check overlap
     66  */
     67 back:
     68 	MOVL	SI, CX
     69 	ADDL	BX, CX
     70 	CMPL	CX, DI
     71 	JLS	forward
     72 /*
     73  * whole thing backwards has
     74  * adjusted addresses
     75  */
     76 
     77 	ADDL	BX, DI
     78 	ADDL	BX, SI
     79 	STD
     80 
     81 /*
     82  * copy
     83  */
     84 	MOVL	BX, CX
     85 	SHRL	$2, CX
     86 	ANDL	$3, BX
     87 
     88 	SUBL	$4, DI
     89 	SUBL	$4, SI
     90 	REP;	MOVSL
     91 
     92 	CLD
     93 	ADDL	$4, DI
     94 	ADDL	$4, SI
     95 	SUBL	BX, DI
     96 	SUBL	BX, SI
     97 	JMP	tail
     98 
     99 move_1or2:
    100 	MOVB	(SI), AX
    101 	MOVB	-1(SI)(BX*1), CX
    102 	MOVB	AX, (DI)
    103 	MOVB	CX, -1(DI)(BX*1)
    104 	RET
    105 move_0:
    106 	RET
    107 move_3or4:
    108 	MOVW	(SI), AX
    109 	MOVW	-2(SI)(BX*1), CX
    110 	MOVW	AX, (DI)
    111 	MOVW	CX, -2(DI)(BX*1)
    112 	RET
    113 move_5through8:
    114 	MOVL	(SI), AX
    115 	MOVL	-4(SI)(BX*1), CX
    116 	MOVL	AX, (DI)
    117 	MOVL	CX, -4(DI)(BX*1)
    118 	RET
    119 move_9through16:
    120 	MOVL	(SI), AX
    121 	MOVL	4(SI), CX
    122 	MOVL	-8(SI)(BX*1), DX
    123 	MOVL	-4(SI)(BX*1), BP
    124 	MOVL	AX, (DI)
    125 	MOVL	CX, 4(DI)
    126 	MOVL	DX, -8(DI)(BX*1)
    127 	MOVL	BP, -4(DI)(BX*1)
    128 	RET
    129