Debian Codegraph @

kernels/sifive_x280/1f/bli_axpyf_sifive_x280_asm.c

raw symbols SCIP
1 /*
2
3 BLIS
4 An object-based framework for developing high-performance BLAS-like
5 libraries.
6
7 Copyright (C) 2023, SiFive, Inc.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are
11 met:
12 - Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
14 - Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
17 - Neither the name(s) of the copyright holder(s) nor the names of its
18 contributors may be used to endorse or promote products derived
19 from this software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 */
34
35 #include "blis.h"
36 #include <math.h>
37 #include <riscv_vector.h>
38 #include <stdbool.h>
39 #include <stddef.h>
40
41 #define FLT_SIZE 4
42 #define FLT_LOAD "flw "
43 #define VLE "vle32.v "
44 #define VLSE "vlse32.v "
45 #define VSE "vse32.v "
46 #define VSSE "vsse32.v "
47
48 void bli_saxpyf_sifive_x280_asm(conj_t conja, conj_t conjx, dim_t m, dim_t b,
49 const void *restrict alpha_, const void *restrict a_, inc_t inca,
50 inc_t lda, const void *restrict x_, inc_t incx,
51 void *restrict y_, inc_t incy, const cntx_t *restrict cntx) {
52 (void)conja;
53 (void)conjx;
54 (void)cntx;
55 const float *restrict alpha = alpha_;
56 const float *restrict a = a_;
57 const float *restrict x = x_;
58 float *restrict y = y_;
59
60 if (m == 0 || b == 0)
61 return;
62 __asm__(FLT_LOAD "ft11, (%0)" : : "r"(alpha));
63 inca *= FLT_SIZE;
64 lda *= FLT_SIZE;
65 incx *= FLT_SIZE;
66 incy *= FLT_SIZE;
67 size_t avl = m;
68 while (avl) {
69 // process vl elements of y at a time
70 size_t vl;
71 __asm__ volatile("vsetvli %0, %1, e%2, m8, ta, ma"
72 : "=r"(vl)
73 : "r"(avl), "i"(8 * FLT_SIZE));
74 // x_tmp traverses x
75 // a points to the vl x b block of a needed this iteration
76 // a_tmp traverses the columns of this block
77 const float* restrict x_tmp = x;
78 const float* restrict a_tmp = a;
79 __asm__(FLT_LOAD "ft0, (%0)" : : "r"(x_tmp));
80 if (inca == FLT_SIZE)
81 __asm__(VLE "v0, (%0)" : : "r"(a_tmp));
82 else
83 __asm__(VLSE "v0, (%0), %1" : : "r"(a_tmp), "r"(inca));
84 __asm__("vfmul.vf v0, v0, ft0");
85 __asm__("add %0, %0, %1" : "+r"(x_tmp) : "r"(incx));
86 __asm__("add %0, %0, %1" : "+r"(a_tmp) : "r"(lda));
87
88 for (dim_t i = 1; i < b; ++i) {
89 __asm__(FLT_LOAD "ft0, (%0)" : : "r"(x_tmp));
90 if (inca == FLT_SIZE)
91 __asm__(VLE "v24, (%0)" : : "r"(a_tmp));
92 else
93 __asm__(VLSE "v24, (%0), %1" : : "r"(a_tmp), "r"(inca));
94 __asm__("add %0, %0, %1" : "+r"(x_tmp) : "r"(incx));
95 __asm__("add %0, %0, %1" : "+r"(a_tmp) : "r"(lda));
96 __asm__("vfmacc.vf v0, ft0, v24");
97 }
98
99 if (incy == FLT_SIZE) {
100 __asm__(VLE "v24, (%0)" : : "r"(y));
101 __asm__("vfmacc.vf v24, ft11, v0");
102 __asm__(VSE "v24, (%0)" : : "r"(y));
103 } else {
104 __asm__(VLSE "v24, (%0), %1" : : "r"(y), "r"(incy));
105 __asm__("vfmacc.vf v24, ft11, v0");
106 __asm__(VSSE "v24, (%0), %1" : : "r"(y), "r"(incy));
107 }
108
109 __asm__("add %0, %0, %1" : "+r"(a) : "r"(vl * inca));
110 __asm__("add %0, %0, %1" : "+r"(y) : "r"(vl * incy));
111 avl -= vl;
112 }
113 return;
114 }
115
116 #undef FLT_SIZE
117 #undef FLT_LOAD
118 #undef VLE
119 #undef VLSE
120 #undef VSE
121 #undef VSSE
122
123 #define FLT_SIZE 8
124 #define FLT_LOAD "fld "
125 #define VLE "vle64.v "
126 #define VLSE "vlse64.v "
127 #define VSE "vse64.v "
128 #define VSSE "vsse64.v "
129
130 void bli_daxpyf_sifive_x280_asm(conj_t conja, conj_t conjx, dim_t m, dim_t b,
131 const void *restrict alpha_, const void *restrict a_, inc_t inca,
132 inc_t lda, const void *restrict x_, inc_t incx,
133 void *restrict y_, inc_t incy, const cntx_t *restrict cntx) {
134 (void)conja;
135 (void)conjx;
136 (void)cntx;
137 const double *restrict alpha = alpha_;
138 const double *restrict a = a_;
139 const double *restrict x = x_;
140 double *restrict y = y_;
141
142 if (m == 0 || b == 0)
143 return;
144 __asm__(FLT_LOAD "ft11, (%0)" : : "r"(alpha));
145 inca *= FLT_SIZE;
146 lda *= FLT_SIZE;
147 incx *= FLT_SIZE;
148 incy *= FLT_SIZE;
149 size_t avl = m;
150 while (avl) {
151 // process vl elements of y at a time
152 size_t vl;
153 __asm__ volatile("vsetvli %0, %1, e%2, m8, ta, ma"
154 : "=r"(vl)
155 : "r"(avl), "i"(8 * FLT_SIZE));
156 // x_tmp traverses x
157 // a points to the vl x b block of a needed this iteration
158 // a_tmp traverses the columns of this block
159 const double* restrict x_tmp = x;
160 const double* restrict a_tmp = a;
161 __asm__(FLT_LOAD "ft0, (%0)" : : "r"(x_tmp));
162 if (inca == FLT_SIZE)
163 __asm__(VLE "v0, (%0)" : : "r"(a_tmp));
164 else
165 __asm__(VLSE "v0, (%0), %1" : : "r"(a_tmp), "r"(inca));
166 __asm__("vfmul.vf v0, v0, ft0");
167 __asm__("add %0, %0, %1" : "+r"(x_tmp) : "r"(incx));
168 __asm__("add %0, %0, %1" : "+r"(a_tmp) : "r"(lda));
169
170 for (dim_t i = 1; i < b; ++i) {
171 __asm__(FLT_LOAD "ft0, (%0)" : : "r"(x_tmp));
172 if (inca == FLT_SIZE)
173 __asm__(VLE "v24, (%0)" : : "r"(a_tmp));
174 else
175 __asm__(VLSE "v24, (%0), %1" : : "r"(a_tmp), "r"(inca));
176 __asm__("add %0, %0, %1" : "+r"(x_tmp) : "r"(incx));
177 __asm__("add %0, %0, %1" : "+r"(a_tmp) : "r"(lda));
178 __asm__("vfmacc.vf v0, ft0, v24");
179 }
180
181 if (incy == FLT_SIZE) {
182 __asm__(VLE "v24, (%0)" : : "r"(y));
183 __asm__("vfmacc.vf v24, ft11, v0");
184 __asm__(VSE "v24, (%0)" : : "r"(y));
185 } else {
186 __asm__(VLSE "v24, (%0), %1" : : "r"(y), "r"(incy));
187 __asm__("vfmacc.vf v24, ft11, v0");
188 __asm__(VSSE "v24, (%0), %1" : : "r"(y), "r"(incy));
189 }
190
191 __asm__("add %0, %0, %1" : "+r"(a) : "r"(vl * inca));
192 __asm__("add %0, %0, %1" : "+r"(y) : "r"(vl * incy));
193 avl -= vl;
194 }
195 return;
196 }
197
198 #undef FLT_SIZE
199 #undef FLT_LOAD
200 #undef VLE
201 #undef VLSE
202 #undef VSE
203 #undef VSSE
204
205 #define FLT_SIZE 4
206 #define FLT_LOAD "flw "
207 #define VLSEG "vlseg2e32.v "
208 #define VLSSEG "vlsseg2e32.v "
209 #define VSSEG "vsseg2e32.v "
210 #define VSSSEG "vssseg2e32.v "
211
212 void bli_caxpyf_sifive_x280_asm(conj_t conja, conj_t conjx, dim_t m, dim_t b,
213 const void *restrict alpha_, const void *restrict a_,
214 inc_t inca, inc_t lda, const void *restrict x_,
215 inc_t incx, void *restrict y_, inc_t incy,
216 const cntx_t *restrict cntx) {
217 (void)cntx;
218 const scomplex *restrict alpha = alpha_;
219 const scomplex *restrict a = a_;
220 const scomplex *restrict x = x_;
221 scomplex *restrict y = y_;
222
223 if (m == 0 || b == 0)
224 return;
225 __asm__(FLT_LOAD "ft10, (%0)" : : "r"(alpha));
226 __asm__(FLT_LOAD "ft11, %1(%0)" : : "r"(alpha), "I"(FLT_SIZE));
227 inca *= 2 * FLT_SIZE;
228 lda *= 2 * FLT_SIZE;
229 incx *= 2 * FLT_SIZE;
230 incy *= 2 * FLT_SIZE;
231 size_t avl = m;
232 while (avl) {
233 size_t vl;
234 __asm__ volatile("vsetvli %0, %1, e%2, m4, ta, ma"
235 : "=r"(vl)
236 : "r"(avl), "i"(8 * FLT_SIZE));
237 const scomplex* restrict x_tmp = x;
238 const scomplex* restrict a_tmp = a;
239 __asm__(FLT_LOAD "ft0, (%0)" : : "r"(x_tmp));
240 __asm__(FLT_LOAD "ft1, %1(%0)" : : "r"(x_tmp), "I"(FLT_SIZE));
241 if (inca == 2 * FLT_SIZE)
242 __asm__(VLSEG "v24, (%0)" : : "r"(a_tmp));
243 else
244 __asm__(VLSSEG "v24, (%0), %1" : : "r"(a_tmp), "r"(inca));
245 __asm__("add %0, %0, %1" : "+r"(x_tmp) : "r"(incx));
246 __asm__("add %0, %0, %1" : "+r"(a_tmp) : "r"(lda));
247 __asm__("vfmul.vf v0, v24, ft0");
248 __asm__("vfmul.vf v4, v24, ft1");
249 if (conja == BLIS_NO_CONJUGATE && conjx == BLIS_NO_CONJUGATE) {
250 __asm__("vfnmsac.vf v0, ft1, v28");
251 __asm__("vfmacc.vf v4, ft0, v28");
252 } else if (conja == BLIS_NO_CONJUGATE && conjx == BLIS_CONJUGATE) {
253 __asm__("vfmacc.vf v0, ft1, v28");
254 __asm__("vfmsac.vf v4, ft0, v28");
255 } else if (conja == BLIS_CONJUGATE && conjx == BLIS_NO_CONJUGATE) {
256 __asm__("vfmacc.vf v0, ft1, v28");
257 __asm__("vfnmsac.vf v4, ft0, v28");
258 } else {
259 __asm__("vfnmsac.vf v0, ft1, v28");
260 __asm__("vfnmacc.vf v4, ft0, v28");
261 }
262
263 for (dim_t i = 1; i < b; ++i) {
264 __asm__(FLT_LOAD "ft0, (%0)" : : "r"(x_tmp));
265 __asm__(FLT_LOAD "ft1, %1(%0)" : : "r"(x_tmp), "I"(FLT_SIZE));
266 if (inca == 2 * FLT_SIZE)
267 __asm__(VLSEG "v24, (%0)" : : "r"(a_tmp));
268 else
269 __asm__(VLSSEG "v24, (%0), %1" : : "r"(a_tmp), "r"(inca));
270 __asm__("add %0, %0, %1" : "+r"(x_tmp) : "r"(incx));
271 __asm__("add %0, %0, %1" : "+r"(a_tmp) : "r"(lda));
272 __asm__("vfmacc.vf v0, ft0, v24");
273 if (conja == BLIS_NO_CONJUGATE && conjx == BLIS_NO_CONJUGATE) {
274 __asm__("vfmacc.vf v4, ft1, v24");
275 __asm__("vfnmsac.vf v0, ft1, v28");
276 __asm__("vfmacc.vf v4, ft0, v28");
277 } else if (conja == BLIS_NO_CONJUGATE && conjx == BLIS_CONJUGATE) {
278 __asm__("vfnmsac.vf v4, ft1, v24");
279 __asm__("vfmacc.vf v0, ft1, v28");
280 __asm__("vfmacc.vf v4, ft0, v28");
281 } else if (conja == BLIS_CONJUGATE && conjx == BLIS_NO_CONJUGATE) {
282 __asm__("vfmacc.vf v4, ft1, v24");
283 __asm__("vfmacc.vf v0, ft1, v28");
284 __asm__("vfnmsac.vf v4, ft0, v28");
285 } else { // conja == BLIS_CONJUGATE && conjx == BLIS_CONJUGATE
286 __asm__("vfnmsac.vf v4, ft1, v24");
287 __asm__("vfnmsac.vf v0, ft1, v28");
288 __asm__("vfnmsac.vf v4, ft0, v28");
289 }
290 }
291
292 if (incy == 2 * FLT_SIZE) {
293 __asm__(VLSEG "v24, (%0)" : : "r"(y));
294 __asm__("vfmacc.vf v24, ft10, v0");
295 __asm__("vfmacc.vf v28, ft10, v4");
296 __asm__("vfnmsac.vf v24, ft11, v4");
297 __asm__("vfmacc.vf v28, ft11, v0");
298 __asm__(VSSEG "v24, (%0)" : : "r"(y));
299 } else {
300 __asm__(VLSSEG "v24, (%0), %1" : : "r"(y), "r"(incy));
301 __asm__("vfmacc.vf v24, ft10, v0");
302 __asm__("vfmacc.vf v28, ft10, v4");
303 __asm__("vfnmsac.vf v24, ft11, v4");
304 __asm__("vfmacc.vf v28, ft11, v0");
305 __asm__(VSSSEG "v24, (%0), %1" : : "r"(y), "r"(incy));
306 }
307
308 __asm__("add %0, %0, %1" : "+r"(a) : "r"(vl * inca));
309 __asm__("add %0, %0, %1" : "+r"(y) : "r"(vl * incy));
310 avl -= vl;
311 }
312 return;
313 }
314
315 #undef FLT_SIZE
316 #undef FLT_LOAD
317 #undef VLSEG
318 #undef VLSSEG
319 #undef VSSEG
320 #undef VSSSEG
321
322 #define FLT_SIZE 8
323 #define FLT_LOAD "fld "
324 #define VLSEG "vlseg2e64.v "
325 #define VLSSEG "vlsseg2e64.v "
326 #define VSSEG "vsseg2e64.v "
327 #define VSSSEG "vssseg2e64.v "
328
329 void bli_zaxpyf_sifive_x280_asm(conj_t conja, conj_t conjx, dim_t m, dim_t b,
330 const void *restrict alpha_, const void *restrict a_,
331 inc_t inca, inc_t lda, const void *restrict x_,
332 inc_t incx, void *restrict y_, inc_t incy,
333 const cntx_t *restrict cntx) {
334 (void)cntx;
335 const dcomplex *restrict alpha = alpha_;
336 const dcomplex *restrict a = a_;
337 const dcomplex *restrict x = x_;
338 dcomplex *restrict y = y_;
339
340 if (m == 0 || b == 0)
341 return;
342 __asm__(FLT_LOAD "ft10, (%0)" : : "r"(alpha));
343 __asm__(FLT_LOAD "ft11, %1(%0)" : : "r"(alpha), "I"(FLT_SIZE));
344 inca *= 2 * FLT_SIZE;
345 lda *= 2 * FLT_SIZE;
346 incx *= 2 * FLT_SIZE;
347 incy *= 2 * FLT_SIZE;
348 size_t avl = m;
349 while (avl) {
350 size_t vl;
351 __asm__ volatile("vsetvli %0, %1, e%2, m4, ta, ma"
352 : "=r"(vl)
353 : "r"(avl), "i"(8 * FLT_SIZE));
354 const dcomplex* restrict x_tmp = x;
355 const dcomplex* restrict a_tmp = a;
356 __asm__(FLT_LOAD "ft0, (%0)" : : "r"(x_tmp));
357 __asm__(FLT_LOAD "ft1, %1(%0)" : : "r"(x_tmp), "I"(FLT_SIZE));
358 if (inca == 2 * FLT_SIZE)
359 __asm__(VLSEG "v24, (%0)" : : "r"(a_tmp));
360 else
361 __asm__(VLSSEG "v24, (%0), %1" : : "r"(a_tmp), "r"(inca));
362 __asm__("add %0, %0, %1" : "+r"(x_tmp) : "r"(incx));
363 __asm__("add %0, %0, %1" : "+r"(a_tmp) : "r"(lda));
364 __asm__("vfmul.vf v0, v24, ft0");
365 __asm__("vfmul.vf v4, v24, ft1");
366 if (conja == BLIS_NO_CONJUGATE && conjx == BLIS_NO_CONJUGATE) {
367 __asm__("vfnmsac.vf v0, ft1, v28");
368 __asm__("vfmacc.vf v4, ft0, v28");
369 } else if (conja == BLIS_NO_CONJUGATE && conjx == BLIS_CONJUGATE) {
370 __asm__("vfmacc.vf v0, ft1, v28");
371 __asm__("vfmsac.vf v4, ft0, v28");
372 } else if (conja == BLIS_CONJUGATE && conjx == BLIS_NO_CONJUGATE) {
373 __asm__("vfmacc.vf v0, ft1, v28");
374 __asm__("vfnmsac.vf v4, ft0, v28");
375 } else {
376 __asm__("vfnmsac.vf v0, ft1, v28");
377 __asm__("vfnmacc.vf v4, ft0, v28");
378 }
379
380 for (dim_t i = 1; i < b; ++i) {
381 __asm__(FLT_LOAD "ft0, (%0)" : : "r"(x_tmp));
382 __asm__(FLT_LOAD "ft1, %1(%0)" : : "r"(x_tmp), "I"(FLT_SIZE));
383 if (inca == 2 * FLT_SIZE)
384 __asm__(VLSEG "v24, (%0)" : : "r"(a_tmp));
385 else
386 __asm__(VLSSEG "v24, (%0), %1" : : "r"(a_tmp), "r"(inca));
387 __asm__("add %0, %0, %1" : "+r"(x_tmp) : "r"(incx));
388 __asm__("add %0, %0, %1" : "+r"(a_tmp) : "r"(lda));
389 __asm__("vfmacc.vf v0, ft0, v24");
390 if (conja == BLIS_NO_CONJUGATE && conjx == BLIS_NO_CONJUGATE) {
391 __asm__("vfmacc.vf v4, ft1, v24");
392 __asm__("vfnmsac.vf v0, ft1, v28");
393 __asm__("vfmacc.vf v4, ft0, v28");
394 } else if (conja == BLIS_NO_CONJUGATE && conjx == BLIS_CONJUGATE) {
395 __asm__("vfnmsac.vf v4, ft1, v24");
396 __asm__("vfmacc.vf v0, ft1, v28");
397 __asm__("vfmacc.vf v4, ft0, v28");
398 } else if (conja == BLIS_CONJUGATE && conjx == BLIS_NO_CONJUGATE) {
399 __asm__("vfmacc.vf v4, ft1, v24");
400 __asm__("vfmacc.vf v0, ft1, v28");
401 __asm__("vfnmsac.vf v4, ft0, v28");
402 } else { // conja == BLIS_CONJUGATE && conjx == BLIS_CONJUGATE
403 __asm__("vfnmsac.vf v4, ft1, v24");
404 __asm__("vfnmsac.vf v0, ft1, v28");
405 __asm__("vfnmsac.vf v4, ft0, v28");
406 }
407 }
408
409 if (incy == 2 * FLT_SIZE) {
410 __asm__(VLSEG "v24, (%0)" : : "r"(y));
411 __asm__("vfmacc.vf v24, ft10, v0");
412 __asm__("vfmacc.vf v28, ft10, v4");
413 __asm__("vfnmsac.vf v24, ft11, v4");
414 __asm__("vfmacc.vf v28, ft11, v0");
415 __asm__(VSSEG "v24, (%0)" : : "r"(y));
416 } else {
417 __asm__(VLSSEG "v24, (%0), %1" : : "r"(y), "r"(incy));
418 __asm__("vfmacc.vf v24, ft10, v0");
419 __asm__("vfmacc.vf v28, ft10, v4");
420 __asm__("vfnmsac.vf v24, ft11, v4");
421 __asm__("vfmacc.vf v28, ft11, v0");
422 __asm__(VSSSEG "v24, (%0), %1" : : "r"(y), "r"(incy));
423 }
424
425 __asm__("add %0, %0, %1" : "+r"(a) : "r"(vl * inca));
426 __asm__("add %0, %0, %1" : "+r"(y) : "r"(vl * incy));
427 avl -= vl;
428 }
429 return;
430 }