add commentary on DAXPY, add RVV version
[libreriscv.git] / simple_v_extension / daxpy_example.mdwn
1 # c code
2 void daxpy(size_t n, double a, const double x[], double y[])
3 {
4 for (size_t i = 0; i < n; i++) {
5 y[i] = a*x[i] + y[i];
6 }
7 }
8
9 # SV Version
10 # a0 is n, a1 is ptr to x[0], a2 is ptr to y[0], fa0 is a (scalar)
11 VBLK.REG[0] = {type: F, isvec: 1, regkey: a3, regidx: a3, elwidth: dflt}
12 VBLK.REG[1] = {type: F, isvec: 1, regkey: a7, regidx: a7, elwidth: dflt}
13 loop:
14 VBLK.SETVL t0, a0, #4 # MVL=4, vl = t0 = min(a0, MVL))
15 c.ld a3, a1 # load 4 registers a3-6 from x
16 c.slli t1, t0, 3 # t1 = vl * 8 (in bytes)
17 c.ld a7, a2 # load 4 registers a7-10 from y
18 c.add a1, a1, t1 # increment pointer to x by vl*8
19 fmadd a7, a3, fa0, a7 # v1 += v0 * fa0 (y = a * x + y)
20 c.sub a0, a0, t0 # n -= vl (t0)
21 c.st a7, a2 # store 4 registers a7-10 to y
22 c.add a2, a2, t1 # increment pointer to y by vl*8
23 c.bnez a0, loop # repeat if n != 0
24
25 # RVV version
26 # a0 is n, a1 is pointer to x[0], a2 is pointer to y[0], fa0 is a
27 0: li t0, 2<<25
28 4: vsetdcfg t0 # enable 2 64b Fl.Pt. registers
29 loop:
30 8: setvl t0, a0 # vl = t0 = min(mvl, n)
31 c: vld v0, a1 # load vector x
32 10: slli t1, t0, 3 # t1 = vl * 8 (in bytes)
33 14: vld v1, a2 # load vector y
34 18: add a1, a1, t1 # increment pointer to x by vl*8
35 1c: vfmadd v1, v0, fa0, v1 # v1 += v0 * fa0 (y = a * x + y)
36 20: sub a0, a0, t0 # n -= vl (t0)
37 24: vst v1, a2 # store Y
38 28: add a2, a2, t1 # increment pointer to y by vl*8
39 2c: bnez a0, loop # repeat if n != 0
40 30: ret # return