-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathiamax_thunderx2t99.c
377 lines (332 loc) · 10.5 KB
/
iamax_thunderx2t99.c
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/***************************************************************************
Copyright (c) 2017, The OpenBLAS Project
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. Neither the name of the OpenBLAS project nor the names of
its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBLAS PROJECT OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#include "common.h"
#define N "x0" /* vector length */
#define X "x1" /* "X" vector address */
#define INC_X "x2" /* "X" stride */
#define INDEX "x3" /* index of max/min value */
#define Z "x4" /* vector index */
#define J "x5" /* loop variable */
#if !defined(DOUBLE)
#define MAXF "s0"
#define TMPF0 "s1"
#define TMPF1 "s4"
#define N_KERNEL_SIZE "64"
#define SZ "4"
#define N_DIV_SHIFT "6"
#define N_REM_MASK "63"
#define INC_SHIFT "2"
#else
#define MAXF "d0"
#define TMPF0 "d1"
#define TMPF1 "d4"
#define N_KERNEL_SIZE "32"
#define SZ "8"
#define N_DIV_SHIFT "5"
#define N_REM_MASK "31"
#define INC_SHIFT "3"
#endif
/******************************************************************************/
#if !defined(DOUBLE)
#define KERNEL_F \
"ldp q2, q3, ["X"] \n" \
"ldp q4, q5, ["X", #32] \n" \
"ldp q6, q7, ["X", #64] \n" \
"ldp q16, q17, ["X", #96] \n" \
"ldp q18, q19, ["X", #128] \n" \
"ldp q20, q21, ["X", #160] \n" \
"ldp q22, q23, ["X", #192] \n" \
"ldp q24, q25, ["X", #224] \n" \
"add "X", "X", #256 \n" \
"fabs v2.4s, v2.4s \n" \
"fabs v3.4s, v3.4s \n" \
"fabs v4.4s, v4.4s \n" \
"fabs v5.4s, v5.4s \n" \
"fabs v6.4s, v6.4s \n" \
"fabs v7.4s, v7.4s \n" \
"fabs v16.4s, v16.4s \n" \
"fabs v17.4s, v17.4s \n" \
"fabs v18.4s, v18.4s \n" \
"fabs v19.4s, v19.4s \n" \
"fabs v20.4s, v20.4s \n" \
"fabs v21.4s, v21.4s \n" \
"fabs v22.4s, v22.4s \n" \
"fabs v23.4s, v23.4s \n" \
"fabs v24.4s, v24.4s \n" \
"fabs v25.4s, v25.4s \n" \
"fmax v2.4s, v2.4s, v3.4s \n" \
"fmax v4.4s, v4.4s, v5.4s \n" \
"fmax v6.4s, v6.4s, v7.4s \n" \
"fmax v16.4s, v16.4s, v17.4s \n" \
"fmax v18.4s, v18.4s, v19.4s \n" \
"fmax v20.4s, v20.4s, v21.4s \n" \
"fmax v22.4s, v22.4s, v23.4s \n" \
"fmax v24.4s, v24.4s, v25.4s \n" \
"PRFM PLDL1KEEP, ["X", #1024] \n" \
"PRFM PLDL1KEEP, ["X", #1024+64] \n" \
"PRFM PLDL1KEEP, ["X", #1024+128] \n" \
"PRFM PLDL1KEEP, ["X", #1024+192] \n" \
"fmax v2.4s, v2.4s, v4.4s \n" \
"fmax v6.4s, v6.4s, v16.4s \n" \
"fmax v18.4s, v18.4s, v20.4s \n" \
"fmax v22.4s, v22.4s, v24.4s \n" \
"fmax v2.4s, v2.4s, v6.4s \n" \
"fmax v18.4s, v18.4s, v22.4s \n" \
"fmax v2.4s, v2.4s, v18.4s \n" \
"fmaxv "TMPF0", v2.4s \n" \
"fcmp "MAXF", "TMPF0" \n" \
"fcsel "MAXF", "MAXF", "TMPF0", ge \n" \
"csel "INDEX", "INDEX", "Z", ge \n" \
"add "Z", "Z", #"N_KERNEL_SIZE" \n"
#else
#define KERNEL_F \
"ldp q2, q3, ["X"] \n" \
"ldp q4, q5, ["X", #32] \n" \
"ldp q6, q7, ["X", #64] \n" \
"ldp q16, q17, ["X", #96] \n" \
"ldp q18, q19, ["X", #128] \n" \
"ldp q20, q21, ["X", #160] \n" \
"ldp q22, q23, ["X", #192] \n" \
"ldp q24, q25, ["X", #224] \n" \
"add "X", "X", #256 \n" \
"fabs v2.2d, v2.2d \n" \
"fabs v3.2d, v3.2d \n" \
"fabs v4.2d, v4.2d \n" \
"fabs v5.2d, v5.2d \n" \
"fabs v6.2d, v6.2d \n" \
"fabs v7.2d, v7.2d \n" \
"fabs v16.2d, v16.2d \n" \
"fabs v17.2d, v17.2d \n" \
"fabs v18.2d, v18.2d \n" \
"fabs v19.2d, v19.2d \n" \
"fabs v20.2d, v20.2d \n" \
"fabs v21.2d, v21.2d \n" \
"fabs v22.2d, v22.2d \n" \
"fabs v23.2d, v23.2d \n" \
"fabs v24.2d, v24.2d \n" \
"fabs v25.2d, v25.2d \n" \
"fmax v2.2d, v2.2d, v3.2d \n" \
"fmax v4.2d, v4.2d, v5.2d \n" \
"fmax v6.2d, v6.2d, v7.2d \n" \
"fmax v16.2d, v16.2d, v17.2d \n" \
"fmax v18.2d, v18.2d, v19.2d \n" \
"fmax v20.2d, v20.2d, v21.2d \n" \
"fmax v22.2d, v22.2d, v23.2d \n" \
"fmax v24.2d, v24.2d, v25.2d \n" \
"PRFM PLDL1KEEP, ["X", #1024] \n" \
"PRFM PLDL1KEEP, ["X", #1024+64] \n" \
"PRFM PLDL1KEEP, ["X", #1024+128] \n" \
"PRFM PLDL1KEEP, ["X", #1024+192] \n" \
"fmax v2.2d, v2.2d, v4.2d \n" \
"fmax v6.2d, v6.2d, v16.2d \n" \
"fmax v18.2d, v18.2d, v20.2d \n" \
"fmax v22.2d, v22.2d, v24.2d \n" \
"fmax v2.2d, v2.2d, v6.2d \n" \
"fmax v18.2d, v18.2d, v22.2d \n" \
"fmax v2.2d, v2.2d, v18.2d \n" \
"ins v3.d[0], v2.d[1] \n" \
"fmax "TMPF0", d3, d2 \n" \
"fcmp "MAXF", "TMPF0" \n" \
"fcsel "MAXF", "MAXF", "TMPF0", ge \n" \
"csel "INDEX", "INDEX", "Z", ge \n" \
"add "Z", "Z", #"N_KERNEL_SIZE" \n"
#endif
#define KERNEL_F_FINALIZE \
"sub x6, "INDEX", #1 \n" \
"lsl x6, x6, #"INC_SHIFT" \n" \
"add x7, x7, x6 \n" \
"mov x6, #0 \n" \
"1: \n" \
"add x6, x6, #1 \n" \
"cmp x6, #"N_KERNEL_SIZE" \n" \
"bge 2f \n" \
"ldr "TMPF1", [x7] \n" \
"fabs "TMPF1", "TMPF1" \n" \
"fcmp "MAXF", "TMPF1" \n" \
"add x7, x7, #"SZ" \n" \
"bne 1b \n" \
"2: \n" \
"sub x6, x6, #1 \n" \
"add "INDEX", "INDEX", x6 \n"
#define INIT \
"lsl "INC_X", "INC_X", #"INC_SHIFT" \n" \
"ldr "MAXF", ["X"] \n" \
"add "X", "X", "INC_X" \n" \
"mov "Z", #1 \n" \
"mov "INDEX", "Z" \n" \
"fabs "MAXF", "MAXF" \n"
#define KERNEL_S1 \
"ldr "TMPF0", ["X"] \n" \
"add "X", "X", "INC_X" \n" \
"add "Z", "Z", #1 \n" \
"fabs "TMPF0", "TMPF0" \n" \
"fcmp "MAXF", "TMPF0" \n" \
"fcsel "MAXF", "MAXF", "TMPF0", ge \n" \
"csel "INDEX", "INDEX", "Z", ge \n"
#if defined(SMP)
extern int blas_level1_thread_with_return_value(int mode, BLASLONG m, BLASLONG n,
BLASLONG k, void *alpha, void *a, BLASLONG lda, void *b, BLASLONG ldb,
void *c, BLASLONG ldc, int (*function)(), int nthreads);
#endif
static BLASLONG __attribute__((noinline)) iamax_compute(BLASLONG n, FLOAT *x, BLASLONG inc_x)
{
BLASLONG index = 0;
if ( n < 0 ) return index;
__asm__ __volatile__ (
" mov "N", %[N_] \n"
" mov "X", %[X_] \n"
" mov "INC_X", %[INCX_] \n"
" cmp "N", xzr \n"
" ble 10f //iamax_kernel_zero \n"
" cmp "INC_X", xzr \n"
" ble 10f //iamax_kernel_zero \n"
" cmp "INC_X", #1 \n"
" bne 5f //iamax_kernel_S_BEGIN \n"
" mov x7, "X" \n"
"1: //iamax_kernel_F_BEGIN: \n"
" "INIT" \n"
" subs "N", "N", #1 \n"
" ble 9f //iamax_kernel_L999 \n"
" asr "J", "N", #"N_DIV_SHIFT" \n"
" cmp "J", xzr \n"
" beq 3f //iamax_kernel_F1 \n"
" add "Z", "Z", #1 \n"
"2: //iamax_kernel_F: \n"
" "KERNEL_F" \n"
" subs "J", "J", #1 \n"
" bne 2b //iamax_kernel_F \n"
" "KERNEL_F_FINALIZE" \n"
" sub "Z", "Z", #1 \n"
"3: //iamax_kernel_F1: \n"
" ands "J", "N", #"N_REM_MASK" \n"
" ble 9f //iamax_kernel_L999 \n"
"4: //iamax_kernel_F10: \n"
" "KERNEL_S1" \n"
" subs "J", "J", #1 \n"
" bne 4b //iamax_kernel_F10 \n"
" b 9f //iamax_kernel_L999 \n"
"5: //iamax_kernel_S_BEGIN: \n"
" "INIT" \n"
" subs "N", "N", #1 \n"
" ble 9f //iamax_kernel_L999 \n"
" asr "J", "N", #2 \n"
" cmp "J", xzr \n"
" ble 7f //iamax_kernel_S1 \n"
"6: //iamax_kernel_S4: \n"
" "KERNEL_S1" \n"
" "KERNEL_S1" \n"
" "KERNEL_S1" \n"
" "KERNEL_S1" \n"
" subs "J", "J", #1 \n"
" bne 6b //iamax_kernel_S4 \n"
"7: //iamax_kernel_S1: \n"
" ands "J", "N", #3 \n"
" ble 9f //iamax_kernel_L999 \n"
"8: //iamax_kernel_S10: \n"
" "KERNEL_S1" \n"
" subs "J", "J", #1 \n"
" bne 8b //iamax_kernel_S10 \n"
"9: //iamax_kernel_L999: \n"
" mov x0, "INDEX" \n"
" b 11f //iamax_kernel_DONE \n"
"10: //iamax_kernel_zero: \n"
" mov x0, xzr \n"
"11: //iamax_kernel_DONE: \n"
" mov %[INDEX_], "INDEX" \n"
: [INDEX_] "=r" (index) //%0
: [N_] "r" (n), //%1
[X_] "r" (x), //%2
[INCX_] "r" (inc_x) //%3
: "cc",
"memory",
"x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
"d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7"
);
return index;
}
#if defined(SMP)
static int iamax_thread_function(BLASLONG n, BLASLONG dummy0,
BLASLONG dummy1, FLOAT dummy2, FLOAT *x, BLASLONG inc_x, FLOAT *y,
BLASLONG inc_y, FLOAT *result, BLASLONG dummy3)
{
*(BLASLONG *)result = iamax_compute(n, x, inc_x);
return 0;
}
#endif
BLASLONG CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
{
#if defined(SMP)
int nthreads;
FLOAT dummy_alpha;
#endif
BLASLONG max_index = 0;
#if defined(SMP)
if (inc_x == 0 || n <= 10000)
nthreads = 1;
else
nthreads = num_cpu_avail(1);
if (nthreads == 1) {
max_index = iamax_compute(n, x, inc_x);
} else {
BLASLONG i, width, cur_index;
int num_cpu;
int mode;
char result[MAX_CPU_NUMBER * sizeof(double) * 2];
FLOAT max = -1.0;
#if !defined(DOUBLE)
mode = BLAS_SINGLE;
#else
mode = BLAS_DOUBLE;
#endif
blas_level1_thread_with_return_value(mode, n, 0, 0, &dummy_alpha,
x, inc_x, NULL, 0, result, 0,
( void *)iamax_thread_function, nthreads);
num_cpu = 0;
i = n;
cur_index = 0;
while (i > 0) {
FLOAT elem;
BLASLONG cur_max_index;
cur_max_index = *(BLASLONG *)&result[num_cpu * sizeof(double) * 2];
elem = x[((cur_index + cur_max_index - 1) * inc_x)];
elem = fabs(elem);
if (elem >= max) {
max = elem;
max_index = cur_index + cur_max_index;
}
width = blas_quickdivide(i + nthreads - num_cpu - 1,
nthreads - num_cpu);
i -= width;
cur_index += width;
num_cpu ++;
}
}
#else
max_index = iamax_compute(n, x, inc_x);
#endif
return max_index;
}