1 // Copyright ©2016 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 #define MOVDDUP_ALPHA LONG $0x44120FF2; WORD $0x0824 // @ MOVDDUP XMM0, 8[RSP]
51 // func ScalUnitary(alpha float64, x []float64)
52 TEXT ·ScalUnitary(SB), NOSPLIT, $0
53 MOVDDUP_ALPHA // ALPHA = { alpha, alpha }
54 MOVQ x_base+8(FP), X_PTR // X_PTR = &x
55 MOVQ x_len+16(FP), LEN // LEN = len(x)
57 JE end // if LEN == 0 { return }
58 XORQ IDX, IDX // IDX = 0
61 ANDQ $7, TAIL // TAIL = LEN % 8
62 SHRQ $3, LEN // LEN = floor( LEN / 8 )
63 JZ tail_start // if LEN == 0 { goto tail_start }
67 loop: // do { // x[i] *= alpha unrolled 8x.
68 MOVUPS (X_PTR)(IDX*8), X2 // X_i = x[i]
69 MOVUPS 16(X_PTR)(IDX*8), X3
70 MOVUPS 32(X_PTR)(IDX*8), X4
71 MOVUPS 48(X_PTR)(IDX*8), X5
73 MULPD ALPHA, X2 // X_i *= ALPHA
78 MOVUPS X2, (X_PTR)(IDX*8) // x[i] = X_i
79 MOVUPS X3, 16(X_PTR)(IDX*8)
80 MOVUPS X4, 32(X_PTR)(IDX*8)
81 MOVUPS X5, 48(X_PTR)(IDX*8)
83 ADDQ $8, IDX // i += 8
85 JNZ loop // while --LEN > 0
87 JE end // if TAIL == 0 { return }
89 tail_start: // Reset loop registers
90 MOVQ TAIL, LEN // Loop counter: LEN = TAIL
91 SHRQ $1, LEN // LEN = floor( TAIL / 2 )
92 JZ tail_one // if n == 0 goto end
95 MOVUPS (X_PTR)(IDX*8), X2 // X_i = x[i]
96 MULPD ALPHA, X2 // X_i *= ALPHA
97 MOVUPS X2, (X_PTR)(IDX*8) // x[i] = X_i
98 ADDQ $2, IDX // i += 2
100 JNZ tail_two // while --LEN > 0
103 JZ end // if TAIL == 0 { return }
106 // x[i] *= alpha for the remaining element.
107 MOVSD (X_PTR)(IDX*8), X2
109 MOVSD X2, (X_PTR)(IDX*8)