Debian Codegraph @

src/x32-packw/gen/x32-packw-x8v-gemm-goi-rvv-u2.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_u2(
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 // vlsseg2, LMUL must <= 4
64 vlmax = __riscv_vsetvlmax_e32m4();
65 // Pack 2 x nr weights
66 for (; k >= 2; k -= 2) {
67 uint32_t* out1 = out0 + nr;
68 // When vlsseg2, LMUL is contraint to 4. We need to use multiple of load & store.
69 const uint32_t* w_ptr = w0;
70 size_t remaining_n = nr;
71 do {
72 vuint32m4x2_t v_w_m4x2 = __riscv_vlsseg2e32_v_u32m4x2(w_ptr, kc_bstride, vlmax);
73 w_ptr += kc * vlmax;
74 vuint32m4_t v_w0 = __riscv_vget_v_u32m4x2_u32m4(v_w_m4x2, 0);
75 __riscv_vse32_v_u32m4(out0, v_w0, vlmax); out0 += vlmax;
76 vuint32m4_t v_w1 = __riscv_vget_v_u32m4x2_u32m4(v_w_m4x2, 1);
77 __riscv_vse32_v_u32m4(out1, v_w1, vlmax); out1 += vlmax;
78 remaining_n -= vlmax;
79 } while(remaining_n > 0);
80 out0 = out1;
81 w0 += 2;
82 }
83 vlmax = __riscv_vsetvlmax_e32m8();
84 // Pack nr weights
85 for (; k >= 1; k -= 1) {
86 vuint32m8_t v_w = __riscv_vlse32_v_u32m8(w0, kc_bstride, vlmax);
87 __riscv_vse32_v_u32m8(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_e32m8(n);
98 vuint32m8_t v_bias;
99 if XNN_LIKELY(b != NULL) {
100 v_bias = __riscv_vle32_v_u32m8(b, vl); b += vl;
101 } else {
102 v_bias = __riscv_vmv_v_x_u32m8(0, vl);
103 }
104 __riscv_vse32_v_u32m8(out, v_bias, vl); out += nr;
105
106 size_t vlmax;
107 uint32_t* out0 = out;
108 size_t k = kc;
109 // vlsseg2, LMUL must <= 4
110 vlmax = __riscv_vsetvlmax_e32m4();
111 // Pack 2 x n weights
112 for (; k >= 2; k -= 2) {
113 uint32_t* out1 = out0 + nr;
114 // When vlsseg2, LMUL is contraint to 4. We need to use multiple of load & store.
115 const uint32_t* w_ptr = w0;
116 unsigned char remaining_blocks = 2;
117 size_t remaining_n = n;
118 do {
119 if XNN_LIKELY(remaining_n >= vlmax) {
120 vl = vlmax;
121 } else {
122 vl = __riscv_vsetvl_e32m4(remaining_n);
123 }
124 vuint32m4x2_t v_w_m4x2 = __riscv_vlsseg2e32_v_u32m4x2(w_ptr, kc_bstride, vlmax);
125 w_ptr += kc * vl;
126 vuint32m4_t v_w0 = __riscv_vget_v_u32m4x2_u32m4(v_w_m4x2, 0);
127 __riscv_vse32_v_u32m4(out0, v_w0, vlmax); out0 += vlmax;
128 vuint32m4_t v_w1 = __riscv_vget_v_u32m4x2_u32m4(v_w_m4x2, 1);
129 __riscv_vse32_v_u32m4(out1, v_w1, vlmax); out1 += vlmax;
130 remaining_n -= vl;
131 remaining_blocks--;
132 } while(remaining_n > 0);
133 out0 = out1 + remaining_blocks * vlmax;
134 w0 += 2;
135 }
136 vlmax = __riscv_vsetvlmax_e32m8();
137 vl = __riscv_vsetvl_e32m8(n);
138 // Pack n weights
139 for (; k >= 1; k -= 1) {
140 vuint32m8_t v_w = __riscv_vlse32_v_u32m8(w0, kc_bstride, vl);
141 __riscv_vse32_v_u32m8(out0, v_w, vl);
142 out0 += vlmax;
143 w0 += 1;
144 }
145 out = (uint32_t*) ((uintptr_t) out0 + extra_bytes);
146 w0 += (nr - 1) * kc;
147 }
148 weights += nc * kc;
149 } while (--g != 0);
150 }