Debian Codegraph @

src/qd8-f32-qc8w-gemm/gen/qd8-f32-qc8w-gemm-5x4v-minmax-rvv.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/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 #include <assert.h>
11
12 #include <riscv_vector.h>
13
14 #include "xnnpack/gemm.h"
15 #include "xnnpack/math.h"
16
17 void xnn_qd8_f32_qc8w_gemm_minmax_ukernel_5x4v__rvv(
18 size_t mr,
19 size_t nc,
20 size_t kc,
21 const int8_t* restrict a,
22 size_t a_stride,
23 const void* restrict w,
24 float* restrict c,
25 size_t cm_stride,
26 size_t cn_stride,
27 const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
28 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)])
29 {
30 assert(mr != 0);
31 assert(mr <= 5);
32 assert(nc != 0);
33 assert(kc != 0);
34
35 const int8_t* a0 = a;
36 float* c0 = c;
37 const int8_t* a1 = (const int8_t*) ((uintptr_t) a0 + a_stride);
38 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
39 if XNN_UNPREDICTABLE(mr < 2) {
40 a1 = a0;
41 c1 = c0;
42 }
43 const int8_t* a2 = (const int8_t*) ((uintptr_t) a1 + a_stride);
44 float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
45 if XNN_UNPREDICTABLE(mr <= 2) {
46 a2 = a1;
47 c2 = c1;
48 }
49 const int8_t* a3 = (const int8_t*) ((uintptr_t) a2 + a_stride);
50 float* c3 = (float*) ((uintptr_t) c2 + cm_stride);
51 if XNN_UNPREDICTABLE(mr < 4) {
52 a3 = a2;
53 c3 = c2;
54 }
55 const int8_t* a4 = (const int8_t*) ((uintptr_t) a3 + a_stride);
56 float* c4 = (float*) ((uintptr_t) c3 + cm_stride);
57 if XNN_UNPREDICTABLE(mr <= 4) {
58 a4 = a3;
59 c4 = c3;
60 }
61
62 const size_t nr = __riscv_vsetvlmax_e32m4();
63 size_t vl = nr;
64 do {
65 if XNN_UNLIKELY(nc < nr) {
66 vl = __riscv_vsetvl_e32m4(nc);
67 }
68 nc = nc - vl;
69
70 vint32m4_t vksum = __riscv_vle32_v_i32m4((const int32_t*)w, vl);
71 w = (const int32_t*) w + nr;
72 const int32_t vinput_zero_point0 = quantization_params[0].zero_point;
73 const int32_t vinput_zero_point1 = quantization_params[1].zero_point;
74 const int32_t vinput_zero_point2 = quantization_params[2].zero_point;
75 const int32_t vinput_zero_point3 = quantization_params[3].zero_point;
76 const int32_t vinput_zero_point4 = quantization_params[4].zero_point;
77 vint32m4_t vacc0 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point0, vl);
78 vint32m4_t vacc1 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point1, vl);
79 vint32m4_t vacc2 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point2, vl);
80 vint32m4_t vacc3 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point3, vl);
81 vint32m4_t vacc4 = __riscv_vmul_vx_i32m4(vksum, vinput_zero_point4, vl);
82
83 size_t k = kc;
84 do {
85 const int8_t va0 = *a0++;
86 const int8_t va1 = *a1++;
87 const int8_t va2 = *a2++;
88 const int8_t va3 = *a3++;
89 const int8_t va4 = *a4++;
90 const vint8m1_t vb = __riscv_vle8_v_i8m1((const int8_t*) w, vl);
91 w = (const int8_t*) w + nr;
92
93 vint16m2_t va0b = __riscv_vwmul_vx_i16m2(vb, va0, vl);
94 vacc0 = __riscv_vwadd_wv_i32m4(vacc0, va0b, vl);
95 vint16m2_t va1b = __riscv_vwmul_vx_i16m2(vb, va1, vl);
96 vacc1 = __riscv_vwadd_wv_i32m4(vacc1, va1b, vl);
97 vint16m2_t va2b = __riscv_vwmul_vx_i16m2(vb, va2, vl);
98 vacc2 = __riscv_vwadd_wv_i32m4(vacc2, va2b, vl);
99 vint16m2_t va3b = __riscv_vwmul_vx_i16m2(vb, va3, vl);
100 vacc3 = __riscv_vwadd_wv_i32m4(vacc3, va3b, vl);
101 vint16m2_t va4b = __riscv_vwmul_vx_i16m2(vb, va4, vl);
102 vacc4 = __riscv_vwadd_wv_i32m4(vacc4, va4b, vl);
103
104 k -= sizeof(int8_t);
105 } while (k != 0);
106 // i32 -> f32
107 vfloat32m4_t vout0 = __riscv_vfcvt_f_x_v_f32m4(vacc0, vl);
108 vfloat32m4_t vout1 = __riscv_vfcvt_f_x_v_f32m4(vacc1, vl);
109 vfloat32m4_t vout2 = __riscv_vfcvt_f_x_v_f32m4(vacc2, vl);
110 vfloat32m4_t vout3 = __riscv_vfcvt_f_x_v_f32m4(vacc3, vl);
111 vfloat32m4_t vout4 = __riscv_vfcvt_f_x_v_f32m4(vacc4, vl);
112
113 // vout * input_scale
114 const float vinput_scale0 = quantization_params[0].inv_scale;
115 const float vinput_scale1 = quantization_params[1].inv_scale;
116 const float vinput_scale2 = quantization_params[2].inv_scale;
117 const float vinput_scale3 = quantization_params[3].inv_scale;
118 const float vinput_scale4 = quantization_params[4].inv_scale;
119 vout0 = __riscv_vfmul_vf_f32m4(vout0, vinput_scale0, vl);
120 vout1 = __riscv_vfmul_vf_f32m4(vout1, vinput_scale1, vl);
121 vout2 = __riscv_vfmul_vf_f32m4(vout2, vinput_scale2, vl);
122 vout3 = __riscv_vfmul_vf_f32m4(vout3, vinput_scale3, vl);
123 vout4 = __riscv_vfmul_vf_f32m4(vout4, vinput_scale4, vl);
124
125 const vfloat32m4_t vfilter_output_scale = __riscv_vle32_v_f32m4((const float*) w, vl);
126 w = (const float*) w + nr;
127 vout0 = __riscv_vfmul_vv_f32m4(vout0, vfilter_output_scale, vl);
128 vout1 = __riscv_vfmul_vv_f32m4(vout1, vfilter_output_scale, vl);
129 vout2 = __riscv_vfmul_vv_f32m4(vout2, vfilter_output_scale, vl);
130 vout3 = __riscv_vfmul_vv_f32m4(vout3, vfilter_output_scale, vl);
131 vout4 = __riscv_vfmul_vv_f32m4(vout4, vfilter_output_scale, vl);
132
133 const vfloat32m4_t vbias = __riscv_vle32_v_f32m4((const float*) w, vl);
134 w = (const float*) w + nr;
135 vout0 = __riscv_vfadd_vv_f32m4(vout0, vbias, vl);
136 vout1 = __riscv_vfadd_vv_f32m4(vout1, vbias, vl);
137 vout2 = __riscv_vfadd_vv_f32m4(vout2, vbias, vl);
138 vout3 = __riscv_vfadd_vv_f32m4(vout3, vbias, vl);
139 vout4 = __riscv_vfadd_vv_f32m4(vout4, vbias, vl);
140
141 const float vmin = params->scalar.min;
142 vout0 = __riscv_vfmax_vf_f32m4(vout0, vmin, vl);
143 vout1 = __riscv_vfmax_vf_f32m4(vout1, vmin, vl);
144 vout2 = __riscv_vfmax_vf_f32m4(vout2, vmin, vl);
145 vout3 = __riscv_vfmax_vf_f32m4(vout3, vmin, vl);
146 vout4 = __riscv_vfmax_vf_f32m4(vout4, vmin, vl);
147 const float vmax = params->scalar.max;
148 vout0 = __riscv_vfmin_vf_f32m4(vout0, vmax, vl);
149 vout1 = __riscv_vfmin_vf_f32m4(vout1, vmax, vl);
150 vout2 = __riscv_vfmin_vf_f32m4(vout2, vmax, vl);
151 vout3 = __riscv_vfmin_vf_f32m4(vout3, vmax, vl);
152 vout4 = __riscv_vfmin_vf_f32m4(vout4, vmax, vl);
153
154 // store 5 x vl results to c
155 __riscv_vse32_v_f32m4(c0, vout0, vl);
156 c0 = (float*) ((uintptr_t) c0 + cn_stride);
157 __riscv_vse32_v_f32m4(c1, vout1, vl);
158 c1 = (float*) ((uintptr_t) c1 + cn_stride);
159 __riscv_vse32_v_f32m4(c2, vout2, vl);
160 c2 = (float*) ((uintptr_t) c2 + cn_stride);
161 __riscv_vse32_v_f32m4(c3, vout3, vl);
162 c3 = (float*) ((uintptr_t) c3 + cn_stride);
163 __riscv_vse32_v_f32m4(c4, vout4, vl);
164 c4 = (float*) ((uintptr_t) c4 + cn_stride);
165
166 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
167 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
168 a2 = (const int8_t*) ((uintptr_t) a2 - kc);
169 a3 = (const int8_t*) ((uintptr_t) a3 - kc);
170 a4 = (const int8_t*) ((uintptr_t) a4 - kc);
171 } while (nc != 0);
172 }