github.com/gopherd/gonum@v0.0.4/internal/asm/f64/axpyincto_amd64.s (about)

     1  // Copyright ©2015 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  //
     5  // Some of the loop unrolling code is copied from:
     6  // http://golang.org/src/math/big/arith_amd64.s
     7  // which is distributed under these terms:
     8  //
     9  // Copyright (c) 2012 The Go Authors. All rights reserved.
    10  //
    11  // Redistribution and use in source and binary forms, with or without
    12  // modification, are permitted provided that the following conditions are
    13  // met:
    14  //
    15  //    * Redistributions of source code must retain the above copyright
    16  // notice, this list of conditions and the following disclaimer.
    17  //    * Redistributions in binary form must reproduce the above
    18  // copyright notice, this list of conditions and the following disclaimer
    19  // in the documentation and/or other materials provided with the
    20  // distribution.
    21  //    * Neither the name of Google Inc. nor the names of its
    22  // contributors may be used to endorse or promote products derived from
    23  // this software without specific prior written permission.
    24  //
    25  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    26  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    27  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    28  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    29  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    30  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    31  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    32  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    33  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    34  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    35  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    36  
    37  // +build !noasm,!gccgo,!safe
    38  
    39  #include "textflag.h"
    40  
    41  #define X_PTR SI
    42  #define Y_PTR DI
    43  #define DST_PTR DX
    44  #define IDX AX
    45  #define LEN CX
    46  #define TAIL BX
    47  #define INC_X R8
    48  #define INCx3_X R11
    49  #define INC_Y R9
    50  #define INCx3_Y R12
    51  #define INC_DST R10
    52  #define INCx3_DST R13
    53  #define ALPHA X0
    54  #define ALPHA_2 X1
    55  
    56  // func AxpyIncTo(dst []float64, incDst, idst uintptr, alpha float64, x, y []float64, n, incX, incY, ix, iy uintptr)
    57  TEXT ·AxpyIncTo(SB), NOSPLIT, $0
    58  	MOVQ dst_base+0(FP), DST_PTR // DST_PTR := &dst
    59  	MOVQ x_base+48(FP), X_PTR    // X_PTR := &x
    60  	MOVQ y_base+72(FP), Y_PTR    // Y_PTR := &y
    61  	MOVQ n+96(FP), LEN           // LEN := n
    62  	CMPQ LEN, $0                 // if LEN == 0 { return }
    63  	JE   end
    64  
    65  	MOVQ ix+120(FP), INC_X
    66  	LEAQ (X_PTR)(INC_X*8), X_PTR       // X_PTR = &(x[ix])
    67  	MOVQ iy+128(FP), INC_Y
    68  	LEAQ (Y_PTR)(INC_Y*8), Y_PTR       // Y_PTR = &(dst[idst])
    69  	MOVQ idst+32(FP), INC_DST
    70  	LEAQ (DST_PTR)(INC_DST*8), DST_PTR // DST_PTR = &(y[iy])
    71  
    72  	MOVQ  incX+104(FP), INC_X    // INC_X = incX * sizeof(float64)
    73  	SHLQ  $3, INC_X
    74  	MOVQ  incY+112(FP), INC_Y    // INC_Y = incY * sizeof(float64)
    75  	SHLQ  $3, INC_Y
    76  	MOVQ  incDst+24(FP), INC_DST // INC_DST = incDst * sizeof(float64)
    77  	SHLQ  $3, INC_DST
    78  	MOVSD alpha+40(FP), ALPHA
    79  
    80  	MOVQ LEN, TAIL
    81  	ANDQ $3, TAIL   // TAIL = n % 4
    82  	SHRQ $2, LEN    // LEN = floor( n / 4 )
    83  	JZ   tail_start // if LEN == 0 { goto tail_start }
    84  
    85  	MOVSD ALPHA, ALPHA_2                  // ALPHA_2 = ALPHA for pipelining
    86  	LEAQ  (INC_X)(INC_X*2), INCx3_X       // INCx3_X = INC_X * 3
    87  	LEAQ  (INC_Y)(INC_Y*2), INCx3_Y       // INCx3_Y = INC_Y * 3
    88  	LEAQ  (INC_DST)(INC_DST*2), INCx3_DST // INCx3_DST = INC_DST * 3
    89  
    90  loop:  // do {  // y[i] += alpha * x[i] unrolled 2x.
    91  	MOVSD (X_PTR), X2            // X_i = x[i]
    92  	MOVSD (X_PTR)(INC_X*1), X3
    93  	MOVSD (X_PTR)(INC_X*2), X4
    94  	MOVSD (X_PTR)(INCx3_X*1), X5
    95  
    96  	MULSD ALPHA, X2   // X_i *= a
    97  	MULSD ALPHA_2, X3
    98  	MULSD ALPHA, X4
    99  	MULSD ALPHA_2, X5
   100  
   101  	ADDSD (Y_PTR), X2            // X_i += y[i]
   102  	ADDSD (Y_PTR)(INC_Y*1), X3
   103  	ADDSD (Y_PTR)(INC_Y*2), X4
   104  	ADDSD (Y_PTR)(INCx3_Y*1), X5
   105  
   106  	MOVSD X2, (DST_PTR)              // y[i] = X_i
   107  	MOVSD X3, (DST_PTR)(INC_DST*1)
   108  	MOVSD X4, (DST_PTR)(INC_DST*2)
   109  	MOVSD X5, (DST_PTR)(INCx3_DST*1)
   110  
   111  	LEAQ (X_PTR)(INC_X*4), X_PTR       // X_PTR = &(X_PTR[incX*4])
   112  	LEAQ (Y_PTR)(INC_Y*4), Y_PTR       // Y_PTR = &(Y_PTR[incY*4])
   113  	LEAQ (DST_PTR)(INC_DST*4), DST_PTR // DST_PTR = &(DST_PTR[incDst*4]
   114  	DECQ LEN
   115  	JNZ  loop                          // } while --LEN > 0
   116  	CMPQ TAIL, $0                      // if TAIL == 0 { return }
   117  	JE   end
   118  
   119  tail_start: // Reset Loop registers
   120  	MOVQ TAIL, LEN // Loop counter: LEN = TAIL
   121  	SHRQ $1, LEN   // LEN = floor( LEN / 2 )
   122  	JZ   tail_one
   123  
   124  tail_two:
   125  	MOVSD (X_PTR), X2              // X_i = x[i]
   126  	MOVSD (X_PTR)(INC_X*1), X3
   127  	MULSD ALPHA, X2                // X_i *= a
   128  	MULSD ALPHA, X3
   129  	ADDSD (Y_PTR), X2              // X_i += y[i]
   130  	ADDSD (Y_PTR)(INC_Y*1), X3
   131  	MOVSD X2, (DST_PTR)            // y[i] = X_i
   132  	MOVSD X3, (DST_PTR)(INC_DST*1)
   133  
   134  	LEAQ (X_PTR)(INC_X*2), X_PTR       // X_PTR = &(X_PTR[incX*2])
   135  	LEAQ (Y_PTR)(INC_Y*2), Y_PTR       // Y_PTR = &(Y_PTR[incY*2])
   136  	LEAQ (DST_PTR)(INC_DST*2), DST_PTR // DST_PTR = &(DST_PTR[incY*2]
   137  
   138  	ANDQ $1, TAIL
   139  	JZ   end      // if TAIL == 0 { goto end }
   140  
   141  tail_one:
   142  	MOVSD (X_PTR), X2   // X2 = x[i]
   143  	MULSD ALPHA, X2     // X2 *= a
   144  	ADDSD (Y_PTR), X2   // X2 += y[i]
   145  	MOVSD X2, (DST_PTR) // y[i] = X2
   146  
   147  end:
   148  	RET