-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathidamax_amd64-simd_broken
178 lines (137 loc) · 3.02 KB
/
idamax_amd64-simd_broken
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// func Idamax(N int, X []float64, incX int) int
TEXT ·Idamax(SB), 7, $0
MOVL N+0(FP), BP
MOVQ X+8(FP), SI // X.data
MOVL incX+24(FP), AX
// Check data bounaries
MOVL BP, CX
DECL CX
IMULL AX, CX // CX = incX * (N - 1)
CMPL CX, X_len+16(FP)
JGE panic
// Max fp value
XORPD X0, X0 // X0 = {0, 0}
// Max index
PXOR X1, X1 // X1 = {0, 0}
// Increment register
PCMPEQL X2, X2
PSRLQ $63, X2 // X2 = {1, 1}
// Index register
MOVQ X2, X3
PSLLDQ $8, X3 // X3 = {0, 1}
// Mask for sign bit clear
PCMPEQL X4, X4
PSRLQ $1, X4
// Setup stride
SALQ $3, AX // AX = sizeof(float64) * incX
// Check that there are 2 or more values for SIMD calculations
SUBQ $2, BP
JL rest // There are less than 2 values to process
// Increment register
PSLLQ $1, X2 // X2 = (2, 2)
// Check if incX != 1
CMPQ AX, $8
JNE with_stride
// Fully optimized loop (for incX == incY == 1)
full_simd_loop:
// Load two values
MOVUPD (SI), X5
// Clear sign on two values
ANDPD X4, X5
// Update data pointer
ADDQ $16, SI
// Compare first two values with max values
MOVAPD X0, X6
CMPPD X5, X6, 5 // NLT
// Clear previous max indexes
PAND X6, X1
// Select indexes of max values
PANDN X3, X6
// Update max indexes
POR X6, X1
// Update max values
MAXPD X5, X0
// Update indexes
PADDQ X2, X3
SUBQ $2, BP
JGE full_simd_loop // There are 2 or more values to process
JMP hsum
with_stride:
// Setup long stride
MOVQ AX, CX
SALQ $1, CX // CX = 16 * incX
half_simd_loop:
// Load two values
MOVLPD (SI), X5
MOVHPD (SI)(AX*1), X5
// Clear sign on two values
ANDPD X4, X5
// Update data pointer using long stride
ADDQ CX, SI
// Compare first two values with max values
MOVAPD X0, X6
CMPPD X5, X6, 5 // NLT
// Clear previous max indexes
PAND X6, X1
// Select indexes of max values
PANDN X3, X6
// Update max indexes
POR X6, X1
// Update max values
MAXPD X5, X0
// Update indexes
PADDQ X2, X3
SUBQ $2, BP
JGE half_simd_loop // There are 2 or more values to process
hsum:
// Increment register
//PSRLQ $1, X2
// Uvectorize max values and indexes
MOVHLPS X0, X5
PSHUFL $0xee, X1, X7
// BUG: we need to return less index if there are two identical max vals
// Compare max values
MOVSD X0, X6
CMPSD X5, X6, 5 // NLT
// Clear previous max indexes
PAND X6, X1
// Select indexes of max values
PANDN X7, X6
// Update max indexes
POR X6, X1
// Update max value
MAXSD X5, X0
rest:
// Undo last SUBQ
ADDQ $2, BP
// Check that are there any value to process
JE end
//loop:
// Load value
MOVSD (SI), X5
// Clear sign
ANDPD X4, X5
// Update data pointers
//ADDQ AX, SI
// Compare current value with max value
MOVSD X0, X6
CMPSD X5, X6, 5 // NLT
// Clear previous max indexes
PAND X6, X1
// Select indexes of max values
PANDN X3, X6
// Update max indexes
POR X6, X1
// Update max value
//MAXSD X5, X0
// Update indexes
//PADDQ X2, X3
//DECQ BP
//JNE loop
end:
// Return the max index
MOVSD X1, r+32(FP)
RET
panic:
CALL runtime·panicindex(SB)
RET