1// Derived from Inferno's libkern/memmove-386.s (adapted for amd64) 2// https://bitbucket.org/inferno-os/inferno-os/src/master/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-24 32 33 MOVQ to+0(FP), DI 34 MOVQ from+8(FP), SI 35 MOVQ n+16(FP), BX 36 37 // REP instructions have a high startup cost, so we handle small sizes 38 // with some straightline code. The REP MOVSQ instruction is really fast 39 // for large sizes. The cutover is approximately 1K. 40tail: 41 TESTQ BX, BX 42 JEQ move_0 43 CMPQ BX, $2 44 JBE move_1or2 45 CMPQ BX, $4 46 JBE move_3or4 47 CMPQ BX, $8 48 JB move_5through7 49 JE move_8 50 CMPQ BX, $16 51 JBE move_9through16 52 53/* 54 * check and set for backwards 55 */ 56 CMPQ SI, DI 57 JLS back 58 59/* 60 * forward copy loop 61 */ 62forward: 63 MOVQ BX, CX 64 SHRQ $3, CX 65 ANDQ $7, BX 66 67 REP; MOVSQ 68 JMP tail 69 70back: 71/* 72 * check overlap 73 */ 74 MOVQ SI, CX 75 ADDQ BX, CX 76 CMPQ CX, DI 77 JLS forward 78 79/* 80 * whole thing backwards has 81 * adjusted addresses 82 */ 83 ADDQ BX, DI 84 ADDQ BX, SI 85 STD 86 87/* 88 * copy 89 */ 90 MOVQ BX, CX 91 SHRQ $3, CX 92 ANDQ $7, BX 93 94 SUBQ $8, DI 95 SUBQ $8, SI 96 REP; MOVSQ 97 98 CLD 99 ADDQ $8, DI 100 ADDQ $8, SI 101 SUBQ BX, DI 102 SUBQ BX, SI 103 JMP tail 104 105move_1or2: 106 MOVB (SI), AX 107 MOVB -1(SI)(BX*1), CX 108 MOVB AX, (DI) 109 MOVB CX, -1(DI)(BX*1) 110 RET 111move_0: 112 RET 113move_3or4: 114 MOVW (SI), AX 115 MOVW -2(SI)(BX*1), CX 116 MOVW AX, (DI) 117 MOVW CX, -2(DI)(BX*1) 118 RET 119move_5through7: 120 MOVL (SI), AX 121 MOVL -4(SI)(BX*1), CX 122 MOVL AX, (DI) 123 MOVL CX, -4(DI)(BX*1) 124 RET 125move_8: 126 // We need a separate case for 8 to make sure we write pointers atomically. 127 MOVQ (SI), AX 128 MOVQ AX, (DI) 129 RET 130move_9through16: 131 MOVQ (SI), AX 132 MOVQ -8(SI)(BX*1), CX 133 MOVQ AX, (DI) 134 MOVQ CX, -8(DI)(BX*1) 135 RET 136