Debian Codegraph @

src/x32-packw/gen/x32-packw-x1v-gemm-goi-rvv-u4.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/x32-packw/rvv.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2024 SiFive, Inc.
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9
10
11 #include <assert.h>
12 #include <stddef.h>
13 #include <stdint.h>
14
15 #include <riscv_vector.h>
16
17 #include "xnnpack/packw.h"
18
19 void xnn_x32_packw_gemm_goi_ukernel_x1v__rvv_u4(
20 size_t g,
21 size_t nc,
22 size_t kc,
23 size_t nr,
24 size_t kr,
25 size_t sr,
26 const uint32_t* weights,
27 const uint32_t* bias,
28 const void* scale,
29 uint32_t* packed_weights,
30 size_t extra_bytes,
31 const void* params)
32 {
33 assert(g != 0);
34 assert(nc != 0);
35 assert(kc != 0);
36 assert(nr == __riscv_vsetvlmax_e32m1());
37 assert(kr == 1);
38 assert(sr == 1);
39 assert(weights != NULL);
40 assert(packed_weights != NULL);
41
42 uint32_t* out = packed_weights;
43 const uint32_t* b = bias;
44 size_t kc_bstride = kc * 4;
45
46 do {
47 const uint32_t* w0 = weights;
48 size_t n = nc;
49 // NC main loop: process multiple of NR
50 for (;n >= nr; n -= nr) {
51 // Pack nr bias at begining of tile
52 size_t vlmax = __riscv_vsetvlmax_e32m1();
53 vuint32m1_t v_bias;
54 if XNN_LIKELY(b != NULL) {
55 v_bias = __riscv_vle32_v_u32m1(b, vlmax); b += nr;
56 } else {
57 v_bias = __riscv_vmv_v_x_u32m1(0, vlmax);
58 }
59 __riscv_vse32_v_u32m1(out, v_bias, vlmax); out += nr;
60
61 uint32_t* out0 = out;
62 size_t k = kc;
63 // vlsseg4, LMUL must <= 2
64 vlmax = __riscv_vsetvlmax_e32m1();
65 // Pack 4 x nr weights
66 for (; k >= 4; k -= 4) {
67 uint32_t* out1 = out0 + nr;
68 uint32_t* out2 = out1 + nr;
69 uint32_t* out3 = out2 + nr;
70 // 1 load & store can pack 4 x nr weights
71 vuint32m1x4_t v_w_m1x4 = __riscv_vlsseg4e32_v_u32m1x4(w0, kc_bstride, vlmax);
72 vuint32m1_t v_w0 = __riscv_vget_v_u32m1x4_u32m1(v_w_m1x4, 0);
73 __riscv_vse32_v_u32m1(out0, v_w0, vlmax);
74 vuint32m1_t v_w1 = __riscv_vget_v_u32m1x4_u32m1(v_w_m1x4, 1);
75 __riscv_vse32_v_u32m1(out1, v_w1, vlmax);
76 vuint32m1_t v_w2 = __riscv_vget_v_u32m1x4_u32m1(v_w_m1x4, 2);
77 __riscv_vse32_v_u32m1(out2, v_w2, vlmax);
78 vuint32m1_t v_w3 = __riscv_vget_v_u32m1x4_u32m1(v_w_m1x4, 3);
79 __riscv_vse32_v_u32m1(out3, v_w3, vlmax);
80 out0 = out3 + vlmax;
81 w0 += 4;
82 }
83 vlmax = __riscv_vsetvlmax_e32m1();
84 // Pack nr weights
85 for (; k >= 1; k -= 1) {
86 vuint32m1_t v_w = __riscv_vlse32_v_u32m1(w0, kc_bstride, vlmax);
87 __riscv_vse32_v_u32m1(out0, v_w, vlmax);
88 out0 += vlmax;
89 w0 += 1;
90 }
91 out = (uint32_t*) ((uintptr_t) out0 + extra_bytes);
92 w0 += (nr - 1) * kc;
93 }
94 // NC remainder: process n < NR
95 if (n > 0) {
96 // Pack nr bias at begining of tile
97 size_t vl = __riscv_vsetvl_e32m1(n);
98 vuint32m1_t v_bias;
99 if XNN_LIKELY(b != NULL) {
100 v_bias = __riscv_vle32_v_u32m1(b, vl); b += vl;
101 } else {
102 v_bias = __riscv_vmv_v_x_u32m1(0, vl);
103 }
104 __riscv_vse32_v_u32m1(out, v_bias, vl); out += nr;
105
106 size_t vlmax;
107 uint32_t* out0 = out;
108 size_t k = kc;
109 // vlsseg4, LMUL must <= 2
110 vlmax = __riscv_vsetvlmax_e32m1();
111 // Pack 4 x n weights
112 for (; k >= 4; k -= 4) {
113 uint32_t* out1 = out0 + nr;
114 uint32_t* out2 = out1 + nr;
115 uint32_t* out3 = out2 + nr;
116 // 1 load & store can pack 4 x nr weights
117 size_t vl = __riscv_vsetvl_e32m1(n);
118 vuint32m1x4_t v_w_m1x4 = __riscv_vlsseg4e32_v_u32m1x4(w0, kc_bstride, vl);
119 vuint32m1_t v_w0 = __riscv_vget_v_u32m1x4_u32m1(v_w_m1x4, 0);
120 __riscv_vse32_v_u32m1(out0, v_w0, vl);
121 vuint32m1_t v_w1 = __riscv_vget_v_u32m1x4_u32m1(v_w_m1x4, 1);
122 __riscv_vse32_v_u32m1(out1, v_w1, vl);
123 vuint32m1_t v_w2 = __riscv_vget_v_u32m1x4_u32m1(v_w_m1x4, 2);
124 __riscv_vse32_v_u32m1(out2, v_w2, vl);
125 vuint32m1_t v_w3 = __riscv_vget_v_u32m1x4_u32m1(v_w_m1x4, 3);
126 __riscv_vse32_v_u32m1(out3, v_w3, vl);
127 out0 = out3 + vlmax;
128 w0 += 4;
129 }
130 vlmax = __riscv_vsetvlmax_e32m1();
131 vl = __riscv_vsetvl_e32m1(n);
132 // Pack n weights
133 for (; k >= 1; k -= 1) {
134 vuint32m1_t v_w = __riscv_vlse32_v_u32m1(w0, kc_bstride, vl);
135 __riscv_vse32_v_u32m1(out0, v_w, vl);
136 out0 += vlmax;
137 w0 += 1;
138 }
139 out = (uint32_t*) ((uintptr_t) out0 + extra_bytes);
140 w0 += (nr - 1) * kc;
141 }
142 weights += nc * kc;
143 } while (--g != 0);
144 }