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.
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:
9 // Copyright (c) 2012 The Go Authors. All rights reserved.
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
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
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.
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.
37 //+build !noasm,!appengine
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 }
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])
72 MOVQ incX+104(FP), INC_X // INC_X = incX * sizeof(float64)
74 MOVQ incY+112(FP), INC_Y // INC_Y = incY * sizeof(float64)
76 MOVQ incDst+24(FP), INC_DST // INC_DST = incDst * sizeof(float64)
78 MOVSD alpha+40(FP), ALPHA
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 }
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
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
96 MULSD ALPHA, X2 // X_i *= a
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
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)
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]
115 JNZ loop // } while --LEN > 0
116 CMPQ TAIL, $0 // if TAIL == 0 { return }
119 tail_start: // Reset Loop registers
120 MOVQ TAIL, LEN // Loop counter: LEN = TAIL
121 SHRQ $1, LEN // LEN = floor( LEN / 2 )
125 MOVSD (X_PTR), X2 // X_i = x[i]
126 MOVSD (X_PTR)(INC_X*1), X3
127 MULSD ALPHA, X2 // X_i *= a
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)
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]
139 JZ end // if TAIL == 0 { goto end }
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