github.com/primecitizens/pcz/std@v0.2.1/core/mem/move_plan9_386.s (about)

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