-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
182 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright ©2016 The Gonum Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build !noasm,!appengine,!safe | ||
|
||
#include "textflag.h" | ||
|
||
#define X_PTR SI | ||
#define IDX AX | ||
#define LEN CX | ||
#define TAIL BX | ||
#define SUM X0 | ||
#define SUM_1 X1 | ||
#define SUM_2 X2 | ||
#define SUM_3 X3 | ||
|
||
// func Sum(x []float64) float64 | ||
TEXT ·Sum(SB), NOSPLIT, $0 | ||
MOVQ x_base+0(FP), X_PTR // X_PTR = &x | ||
MOVQ x_len+8(FP), LEN // LEN = len(x) | ||
XORQ IDX, IDX // i = 0 | ||
PXOR SUM, SUM // p_sum_i = 0 | ||
CMPQ LEN, $0 // if LEN == 0 { return 0 } | ||
JE sum_end | ||
|
||
PXOR SUM_1, SUM_1 | ||
PXOR SUM_2, SUM_2 | ||
PXOR SUM_3, SUM_3 | ||
|
||
MOVQ X_PTR, TAIL // Check memory alignment | ||
ANDQ $15, TAIL // TAIL = &y % 16 | ||
JZ no_trim // if TAIL == 0 { goto no_trim } | ||
|
||
// Align on 16-byte boundary | ||
ADDSD (X_PTR), X0 // X0 += x[0] | ||
INCQ IDX // i++ | ||
DECQ LEN // LEN-- | ||
DECQ TAIL // TAIL-- | ||
JZ sum_end // if TAIL == 0 { return } | ||
|
||
no_trim: | ||
MOVQ LEN, TAIL | ||
SHRQ $4, LEN // LEN = floor( n / 16 ) | ||
JZ sum_tail_start // if LEN == 0 { goto tail_start } | ||
|
||
sum_loop: // sum 16x wide do { | ||
ADDPD (SI)(AX*8), SUM // sum_i += x[i:i+1] | ||
ADDPD 16(SI)(AX*8), SUM_1 | ||
ADDPD 32(SI)(AX*8), SUM_2 | ||
ADDPD 48(SI)(AX*8), SUM_3 | ||
ADDPD 64(SI)(AX*8), SUM | ||
ADDPD 80(SI)(AX*8), SUM_1 | ||
ADDPD 96(SI)(AX*8), SUM_2 | ||
ADDPD 112(SI)(AX*8), SUM_3 | ||
ADDQ $16, IDX // i += 16 | ||
DECQ LEN | ||
JNZ sum_loop // } while --CX > 0 | ||
|
||
sum_tail_start: // Reset loop registers | ||
TESTQ $8, TAIL | ||
JZ sum_tail4 | ||
|
||
ADDPD (SI)(AX*8), SUM // sum_i += x[i:i+1] | ||
ADDPD 16(SI)(AX*8), SUM_1 | ||
ADDPD 32(SI)(AX*8), SUM_2 | ||
ADDPD 48(SI)(AX*8), SUM_3 | ||
ADDQ $8, IDX | ||
|
||
sum_tail4: | ||
ADDPD SUM_3, SUM | ||
ADDPD SUM_2, SUM_1 | ||
|
||
TESTQ $4, TAIL | ||
JZ sum_tail2 | ||
|
||
ADDPD (SI)(AX*8), SUM // sum_i += x[i:i+1] | ||
ADDPD 16(SI)(AX*8), SUM_1 | ||
ADDQ $4, IDX | ||
|
||
sum_tail2: | ||
ADDPD SUM_1, SUM | ||
|
||
TESTQ $2, TAIL | ||
JZ sum_tail1 | ||
|
||
ADDPD (SI)(AX*8), SUM // sum_i += x[i:i+1] | ||
ADDQ $2, IDX | ||
|
||
sum_tail1: | ||
HADDPD SUM, SUM | ||
|
||
TESTQ $1, TAIL | ||
JZ sum_end | ||
|
||
ADDSD (SI)(IDX*8), SUM | ||
|
||
sum_end: // return sum | ||
MOVSD SUM, sum+24(FP) | ||
RET |