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

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