gonum.org/v1/gonum@v0.14.0/internal/asm/f64/axpyinc_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 DI 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 R9 52 #define INCx3_DST R12 53 #define ALPHA X0 54 #define ALPHA_2 X1 55 56 // func AxpyInc(alpha float64, x, y []float64, n, incX, incY, ix, iy uintptr) 57 TEXT ·AxpyInc(SB), NOSPLIT, $0 58 MOVQ x_base+8(FP), X_PTR // X_PTR = &x 59 MOVQ y_base+32(FP), Y_PTR // Y_PTR = &y 60 MOVQ n+56(FP), LEN // LEN = n 61 CMPQ LEN, $0 // if LEN == 0 { return } 62 JE end 63 64 MOVQ ix+80(FP), INC_X 65 MOVQ iy+88(FP), INC_Y 66 LEAQ (X_PTR)(INC_X*8), X_PTR // X_PTR = &(x[ix]) 67 LEAQ (Y_PTR)(INC_Y*8), Y_PTR // Y_PTR = &(y[iy]) 68 MOVQ Y_PTR, DST_PTR // DST_PTR = Y_PTR // Write pointer 69 70 MOVQ incX+64(FP), INC_X // INC_X = incX * sizeof(float64) 71 SHLQ $3, INC_X 72 MOVQ incY+72(FP), INC_Y // INC_Y = incY * sizeof(float64) 73 SHLQ $3, INC_Y 74 75 MOVSD alpha+0(FP), ALPHA // ALPHA = alpha 76 MOVQ LEN, TAIL 77 ANDQ $3, TAIL // TAIL = n % 4 78 SHRQ $2, LEN // LEN = floor( n / 4 ) 79 JZ tail_start // if LEN == 0 { goto tail_start } 80 81 MOVAPS ALPHA, ALPHA_2 // ALPHA_2 = ALPHA for pipelining 82 LEAQ (INC_X)(INC_X*2), INCx3_X // INCx3_X = INC_X * 3 83 LEAQ (INC_Y)(INC_Y*2), INCx3_Y // INCx3_Y = INC_Y * 3 84 85 loop: // do { // y[i] += alpha * x[i] unrolled 4x. 86 MOVSD (X_PTR), X2 // X_i = x[i] 87 MOVSD (X_PTR)(INC_X*1), X3 88 MOVSD (X_PTR)(INC_X*2), X4 89 MOVSD (X_PTR)(INCx3_X*1), X5 90 91 MULSD ALPHA, X2 // X_i *= a 92 MULSD ALPHA_2, X3 93 MULSD ALPHA, X4 94 MULSD ALPHA_2, X5 95 96 ADDSD (Y_PTR), X2 // X_i += y[i] 97 ADDSD (Y_PTR)(INC_Y*1), X3 98 ADDSD (Y_PTR)(INC_Y*2), X4 99 ADDSD (Y_PTR)(INCx3_Y*1), X5 100 101 MOVSD X2, (DST_PTR) // y[i] = X_i 102 MOVSD X3, (DST_PTR)(INC_DST*1) 103 MOVSD X4, (DST_PTR)(INC_DST*2) 104 MOVSD X5, (DST_PTR)(INCx3_DST*1) 105 106 LEAQ (X_PTR)(INC_X*4), X_PTR // X_PTR = &(X_PTR[incX*4]) 107 LEAQ (Y_PTR)(INC_Y*4), Y_PTR // Y_PTR = &(Y_PTR[incY*4]) 108 DECQ LEN 109 JNZ loop // } while --LEN > 0 110 CMPQ TAIL, $0 // if TAIL == 0 { return } 111 JE end 112 113 tail_start: // Reset Loop registers 114 MOVQ TAIL, LEN // Loop counter: LEN = TAIL 115 SHRQ $1, LEN // LEN = floor( LEN / 2 ) 116 JZ tail_one 117 118 tail_two: 119 MOVSD (X_PTR), X2 // X_i = x[i] 120 MOVSD (X_PTR)(INC_X*1), X3 121 MULSD ALPHA, X2 // X_i *= a 122 MULSD ALPHA, X3 123 ADDSD (Y_PTR), X2 // X_i += y[i] 124 ADDSD (Y_PTR)(INC_Y*1), X3 125 MOVSD X2, (DST_PTR) // y[i] = X_i 126 MOVSD X3, (DST_PTR)(INC_DST*1) 127 128 LEAQ (X_PTR)(INC_X*2), X_PTR // X_PTR = &(X_PTR[incX*2]) 129 LEAQ (Y_PTR)(INC_Y*2), Y_PTR // Y_PTR = &(Y_PTR[incY*2]) 130 131 ANDQ $1, TAIL 132 JZ end // if TAIL == 0 { goto end } 133 134 tail_one: 135 // y[i] += alpha * x[i] for the last n % 4 iterations. 136 MOVSD (X_PTR), X2 // X2 = x[i] 137 MULSD ALPHA, X2 // X2 *= a 138 ADDSD (Y_PTR), X2 // X2 += y[i] 139 MOVSD X2, (DST_PTR) // y[i] = X2 140 141 end: 142 RET