Debian Codegraph @

src/x32-packw/gen/x32-packw-x8v-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_x8v__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_e32m8());
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_e32m8();
53 vuint32m8_t v_bias;
54 if XNN_LIKELY(b != NULL) {
55 v_bias = __riscv_vle32_v_u32m8(b, vlmax); b += nr;
56 } else {
57 v_bias = __riscv_vmv_v_x_u32m8(0, vlmax);
58 }
59 __riscv_vse32_v_u32m8(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_e32m2();
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 // When vlsseg4, LMUL is contraint to 2. We need to use multiple of load & store.
71 const uint32_t* w_ptr = w0;
72 size_t remaining_n = nr;
73 do {
74 vuint32m2x4_t v_w_m2x4 = __riscv_vlsseg4e32_v_u32m2x4(w_ptr, kc_bstride, vlmax);
75 w_ptr += kc * vlmax;
76 vuint32m2_t v_w0 = __riscv_vget_v_u32m2x4_u32m2(v_w_m2x4, 0);
77 __riscv_vse32_v_u32m2(out0, v_w0, vlmax); out0 += vlmax;
78 vuint32m2_t v_w1 = __riscv_vget_v_u32m2x4_u32m2(v_w_m2x4, 1);
79 __riscv_vse32_v_u32m2(out1, v_w1, vlmax); out1 += vlmax;
80 vuint32m2_t v_w2 = __riscv_vget_v_u32m2x4_u32m2(v_w_m2x4, 2);
81 __riscv_vse32_v_u32m2(out2, v_w2, vlmax); out2 += vlmax;
82 vuint32m2_t v_w3 = __riscv_vget_v_u32m2x4_u32m2(v_w_m2x4, 3);
83 __riscv_vse32_v_u32m2(out3, v_w3, vlmax); out3 += vlmax;
84 remaining_n -= vlmax;
85 } while(remaining_n > 0);
86 out0 = out3;
87 w0 += 4;
88 }
89 vlmax = __riscv_vsetvlmax_e32m8();
90 // Pack nr weights
91 for (; k >= 1; k -= 1) {
92 vuint32m8_t v_w = __riscv_vlse32_v_u32m8(w0, kc_bstride, vlmax);
93 __riscv_vse32_v_u32m8(out0, v_w, vlmax);
94 out0 += vlmax;
95 w0 += 1;
96 }
97 out = (uint32_t*) ((uintptr_t) out0 + extra_bytes);
98 w0 += (nr - 1) * kc;
99 }
100 // NC remainder: process n < NR
101 if (n > 0) {
102 // Pack nr bias at begining of tile
103 size_t vl = __riscv_vsetvl_e32m8(n);
104 vuint32m8_t v_bias;
105 if XNN_LIKELY(b != NULL) {
106 v_bias = __riscv_vle32_v_u32m8(b, vl); b += vl;
107 } else {
108 v_bias = __riscv_vmv_v_x_u32m8(0, vl);
109 }
110 __riscv_vse32_v_u32m8(out, v_bias, vl); out += nr;
111
112 size_t vlmax;
113 uint32_t* out0 = out;
114 size_t k = kc;
115 // vlsseg4, LMUL must <= 2
116 vlmax = __riscv_vsetvlmax_e32m2();
117 // Pack 4 x n weights
118 for (; k >= 4; k -= 4) {
119 uint32_t* out1 = out0 + nr;
120 uint32_t* out2 = out1 + nr;
121 uint32_t* out3 = out2 + nr;
122 // When vlsseg4, LMUL is contraint to 2. We need to use multiple of load & store.
123 const uint32_t* w_ptr = w0;
124 unsigned char remaining_blocks = 4;
125 size_t remaining_n = n;
126 do {
127 size_t vl;
128 if XNN_LIKELY(remaining_n >= vlmax) {
129 vl = vlmax;
130 } else {
131 vl = __riscv_vsetvl_e32m2(remaining_n);
132 }
133 vuint32m2x4_t v_w_m2x4 = __riscv_vlsseg4e32_v_u32m2x4(w_ptr, kc_bstride, vl);
134 w_ptr += kc * vl;
135 vuint32m2_t v_w0 = __riscv_vget_v_u32m2x4_u32m2(v_w_m2x4, 0);
136 __riscv_vse32_v_u32m2(out0, v_w0, vl); out0 += vlmax;
137 vuint32m2_t v_w1 = __riscv_vget_v_u32m2x4_u32m2(v_w_m2x4, 1);
138 __riscv_vse32_v_u32m2(out1, v_w1, vl); out1 += vlmax;
139 vuint32m2_t v_w2 = __riscv_vget_v_u32m2x4_u32m2(v_w_m2x4, 2);
140 __riscv_vse32_v_u32m2(out2, v_w2, vl); out2 += vlmax;
141 vuint32m2_t v_w3 = __riscv_vget_v_u32m2x4_u32m2(v_w_m2x4, 3);
142 __riscv_vse32_v_u32m2(out3, v_w3, vl); out3 += vlmax;
143 remaining_n -= vl;
144 remaining_blocks--;
145 } while(remaining_n > 0);
146 out0 = out3 + remaining_blocks * vlmax;
147 w0 += 4;
148 }
149 vlmax = __riscv_vsetvlmax_e32m8();
150 vl = __riscv_vsetvl_e32m8(n);
151 // Pack n weights
152 for (; k >= 1; k -= 1) {
153 vuint32m8_t v_w = __riscv_vlse32_v_u32m8(w0, kc_bstride, vl);
154 __riscv_vse32_v_u32m8(out0, v_w, vl);
155 out0 += vlmax;
156 w0 += 1;
157 }
158 out = (uint32_t*) ((uintptr_t) out0 + extra_bytes);
159 w0 += (nr - 1) * kc;
160 }
161 weights += nc * kc;
162 } while (--g != 0);
163 }