github.com/aloncn/graphics-go@v0.0.1/src/runtime/memmove_386.s (about) 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 // +build !plan9 27 28 #include "textflag.h" 29 30 TEXT runtime·memmove(SB), NOSPLIT, $0-12 31 MOVL to+0(FP), DI 32 MOVL from+4(FP), SI 33 MOVL n+8(FP), BX 34 35 // REP instructions have a high startup cost, so we handle small sizes 36 // with some straightline code. The REP MOVSL instruction is really fast 37 // for large sizes. The cutover is approximately 1K. We implement up to 38 // 128 because that is the maximum SSE register load (loading all data 39 // into registers lets us ignore copy direction). 40 tail: 41 TESTL BX, BX 42 JEQ move_0 43 CMPL BX, $2 44 JBE move_1or2 45 CMPL BX, $4 46 JB move_3 47 JE move_4 48 CMPL BX, $8 49 JBE move_5through8 50 CMPL BX, $16 51 JBE move_9through16 52 TESTL $0x4000000, runtime·cpuid_edx(SB) // check for sse2 53 JEQ nosse2 54 CMPL BX, $32 55 JBE move_17through32 56 CMPL BX, $64 57 JBE move_33through64 58 CMPL BX, $128 59 JBE move_65through128 60 // TODO: use branch table and BSR to make this just a single dispatch 61 62 nosse2: 63 /* 64 * check and set for backwards 65 */ 66 CMPL SI, DI 67 JLS back 68 69 /* 70 * forward copy loop 71 */ 72 forward: 73 MOVL BX, CX 74 SHRL $2, CX 75 ANDL $3, BX 76 77 REP; MOVSL 78 JMP tail 79 /* 80 * check overlap 81 */ 82 back: 83 MOVL SI, CX 84 ADDL BX, CX 85 CMPL CX, DI 86 JLS forward 87 /* 88 * whole thing backwards has 89 * adjusted addresses 90 */ 91 92 ADDL BX, DI 93 ADDL BX, SI 94 STD 95 96 /* 97 * copy 98 */ 99 MOVL BX, CX 100 SHRL $2, CX 101 ANDL $3, BX 102 103 SUBL $4, DI 104 SUBL $4, SI 105 REP; MOVSL 106 107 CLD 108 ADDL $4, DI 109 ADDL $4, SI 110 SUBL BX, DI 111 SUBL BX, SI 112 JMP tail 113 114 move_1or2: 115 MOVB (SI), AX 116 MOVB -1(SI)(BX*1), CX 117 MOVB AX, (DI) 118 MOVB CX, -1(DI)(BX*1) 119 RET 120 move_0: 121 RET 122 move_3: 123 MOVW (SI), AX 124 MOVB 2(SI), CX 125 MOVW AX, (DI) 126 MOVB CX, 2(DI) 127 RET 128 move_4: 129 // We need a separate case for 4 to make sure we write pointers atomically. 130 MOVL (SI), AX 131 MOVL AX, (DI) 132 RET 133 move_5through8: 134 MOVL (SI), AX 135 MOVL -4(SI)(BX*1), CX 136 MOVL AX, (DI) 137 MOVL CX, -4(DI)(BX*1) 138 RET 139 move_9through16: 140 MOVL (SI), AX 141 MOVL 4(SI), CX 142 MOVL -8(SI)(BX*1), DX 143 MOVL -4(SI)(BX*1), BP 144 MOVL AX, (DI) 145 MOVL CX, 4(DI) 146 MOVL DX, -8(DI)(BX*1) 147 MOVL BP, -4(DI)(BX*1) 148 RET 149 move_17through32: 150 MOVOU (SI), X0 151 MOVOU -16(SI)(BX*1), X1 152 MOVOU X0, (DI) 153 MOVOU X1, -16(DI)(BX*1) 154 RET 155 move_33through64: 156 MOVOU (SI), X0 157 MOVOU 16(SI), X1 158 MOVOU -32(SI)(BX*1), X2 159 MOVOU -16(SI)(BX*1), X3 160 MOVOU X0, (DI) 161 MOVOU X1, 16(DI) 162 MOVOU X2, -32(DI)(BX*1) 163 MOVOU X3, -16(DI)(BX*1) 164 RET 165 move_65through128: 166 MOVOU (SI), X0 167 MOVOU 16(SI), X1 168 MOVOU 32(SI), X2 169 MOVOU 48(SI), X3 170 MOVOU -64(SI)(BX*1), X4 171 MOVOU -48(SI)(BX*1), X5 172 MOVOU -32(SI)(BX*1), X6 173 MOVOU -16(SI)(BX*1), X7 174 MOVOU X0, (DI) 175 MOVOU X1, 16(DI) 176 MOVOU X2, 32(DI) 177 MOVOU X3, 48(DI) 178 MOVOU X4, -64(DI)(BX*1) 179 MOVOU X5, -48(DI)(BX*1) 180 MOVOU X6, -32(DI)(BX*1) 181 MOVOU X7, -16(DI)(BX*1) 182 RET