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
41 // func DdotUnitary(x, y []float64) (sum float64)
42 // This function assumes len(y) >= len(x).
43 TEXT ·DotUnitary(SB), NOSPLIT, $0
45 MOVQ x_len+8(FP), DI // n = len(x)
48 MOVSD $(0.0), X7 // sum = 0
49 MOVSD $(0.0), X8 // sum = 0
53 JL tail_uni // if n < 0 goto tail_uni
56 // sum += x[i] * y[i] unrolled 4x.
57 MOVUPD 0(R8)(SI*8), X0
58 MOVUPD 0(R9)(SI*8), X1
59 MOVUPD 16(R8)(SI*8), X2
60 MOVUPD 16(R9)(SI*8), X3
68 JGE loop_uni // if n >= 0 goto loop_uni
72 JLE end_uni // if n <= 0 goto end_uni
75 // sum += x[i] * y[i] for the remaining 1-3 elements.
83 JNZ onemore_uni // if n != 0 goto onemore_uni
86 // Add the four sums together.
91 MOVSD X7, sum+48(FP) // Return final sum.
94 // func DdotInc(x, y []float64, n, incX, incY, ix, iy uintptr) (sum float64)
95 TEXT ·DotInc(SB), NOSPLIT, $0
100 MOVQ incY+64(FP), R12
104 MOVSD $(0.0), X7 // sum = 0
105 LEAQ (R8)(R13*8), SI // p = &x[ix]
106 LEAQ (R9)(R14*8), DI // q = &y[ix]
107 SHLQ $3, R11 // incX *= sizeof(float64)
108 SHLQ $3, R12 // indY *= sizeof(float64)
110 SUBQ $2, CX // n -= 2
111 JL tail_inc // if n < 0 goto tail_inc
114 // sum += *p * *q unrolled 2x.
117 ADDQ R11, SI // p += incX
118 ADDQ R12, DI // q += incY
121 ADDQ R11, SI // p += incX
122 ADDQ R12, DI // q += incY
127 SUBQ $2, CX // n -= 2
128 JGE loop_inc // if n >= 0 goto loop_inc
131 ADDQ $2, CX // n += 2
132 JLE end_inc // if n <= 0 goto end_inc
134 // sum += *p * *q for the last iteration if n is odd.
140 // Add the two sums together.
144 MOVSD X7, sum+88(FP) // Return final sum.