Debian Codegraph @

src/qd8-f32-qc4w-gemm/gen/qd8-f32-qc4w-gemm-1x4-minmax-wasm.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/scalar.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2021 Google LLC
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 "xnnpack/gemm.h"
13 #include "xnnpack/math.h"
14
15
16 void xnn_qd8_f32_qc4w_gemm_minmax_ukernel_1x4__wasm(
17 size_t mr,
18 size_t nc,
19 size_t kc,
20 const int8_t* restrict a,
21 size_t a_stride,
22 const void* restrict w,
23 float* restrict c,
24 size_t cm_stride,
25 size_t cn_stride,
26 const struct xnn_f32_qc4w_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
27 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)])
28 {
29 assert(mr != 0);
30 assert(mr <= 1);
31 assert(nc != 0);
32 assert(kc != 0);
33
34 const int8_t* a0 = a;
35 float* c0 = c;
36
37
38 kc = round_up_po2(kc, 2);
39 do {
40 const int32_t vksum0 = ((const int32_t*) w)[0];
41 const int32_t vksum1 = ((const int32_t*) w)[1];
42 const int32_t vksum2 = ((const int32_t*) w)[2];
43 const int32_t vksum3 = ((const int32_t*) w)[3];
44 const int32_t vinput_zero_point0 = quantization_params[0].zero_point;
45 int32_t vacc0x0 = vksum0 * vinput_zero_point0;
46 int32_t vacc0x1 = vksum1 * vinput_zero_point0;
47 int32_t vacc0x2 = vksum2 * vinput_zero_point0;
48 int32_t vacc0x3 = vksum3 * vinput_zero_point0;
49 w = (const int32_t*) w + 4;
50
51 size_t k = kc;
52 for (; k >= 2 * sizeof(uint8_t); k -= 2 * sizeof(uint8_t)) {
53 const int32_t va0c0 = (int32_t) a0[0];
54 const int32_t va0c1 = (int32_t) a0[1];
55 a0 += 2;
56
57 const uint8_t vbi0 = ((const uint8_t*) w)[0];
58 const uint8_t vbi1 = ((const uint8_t*) w)[1];
59 const uint8_t vbi2 = ((const uint8_t*) w)[2];
60 const uint8_t vbi3 = ((const uint8_t*) w)[3];
61 w = (const uint8_t*) w + 4;
62 const int32_t vb0c0 = (int32_t) (int8_t) (vbi0 << 4);
63 const int32_t vb0c1 = (int32_t) (int8_t) (vbi0 & 0xF0);
64 const int32_t vb1c0 = (int32_t) (int8_t) (vbi1 << 4);
65 const int32_t vb1c1 = (int32_t) (int8_t) (vbi1 & 0xF0);
66 const int32_t vb2c0 = (int32_t) (int8_t) (vbi2 << 4);
67 const int32_t vb2c1 = (int32_t) (int8_t) (vbi2 & 0xF0);
68 const int32_t vb3c0 = (int32_t) (int8_t) (vbi3 << 4);
69 const int32_t vb3c1 = (int32_t) (int8_t) (vbi3 & 0xF0);
70
71 vacc0x0 += va0c0 * vb0c0;
72 vacc0x1 += va0c0 * vb1c0;
73 vacc0x2 += va0c0 * vb2c0;
74 vacc0x3 += va0c0 * vb3c0;
75 vacc0x0 += va0c1 * vb0c1;
76 vacc0x1 += va0c1 * vb1c1;
77 vacc0x2 += va0c1 * vb2c1;
78 vacc0x3 += va0c1 * vb3c1;
79 }
80
81 float vout0x0 = (float) math_asr_s32(vacc0x0, 4);
82 float vout0x1 = (float) math_asr_s32(vacc0x1, 4);
83 float vout0x2 = (float) math_asr_s32(vacc0x2, 4);
84 float vout0x3 = (float) math_asr_s32(vacc0x3, 4);
85
86 const float vinput_scale0 = quantization_params[0].inv_scale;
87 vout0x0 *= vinput_scale0;
88 vout0x1 *= vinput_scale0;
89 vout0x2 *= vinput_scale0;
90 vout0x3 *= vinput_scale0;
91
92 const float vfilter_output_scale0 = ((const float*) w)[0];
93 vout0x0 *= vfilter_output_scale0;
94 const float vfilter_output_scale1 = ((const float*) w)[1];
95 vout0x1 *= vfilter_output_scale1;
96 const float vfilter_output_scale2 = ((const float*) w)[2];
97 vout0x2 *= vfilter_output_scale2;
98 const float vfilter_output_scale3 = ((const float*) w)[3];
99 vout0x3 *= vfilter_output_scale3;
100
101 const float vbias0 = ((const float*) w)[4];
102 vout0x0 += vbias0;
103 const float vbias1 = ((const float*) w)[5];
104 vout0x1 += vbias1;
105 const float vbias2 = ((const float*) w)[6];
106 vout0x2 += vbias2;
107 const float vbias3 = ((const float*) w)[7];
108 vout0x3 += vbias3;
109
110 w = (const float*) w + 8;
111
112 const float voutput_min = params->scalar.min;
113 vout0x0 = __builtin_wasm_max_f32(vout0x0, voutput_min);
114 vout0x1 = __builtin_wasm_max_f32(vout0x1, voutput_min);
115 vout0x2 = __builtin_wasm_max_f32(vout0x2, voutput_min);
116 vout0x3 = __builtin_wasm_max_f32(vout0x3, voutput_min);
117
118 const float voutput_max = params->scalar.max;
119 vout0x0 = __builtin_wasm_min_f32(vout0x0, voutput_max);
120 vout0x1 = __builtin_wasm_min_f32(vout0x1, voutput_max);
121 vout0x2 = __builtin_wasm_min_f32(vout0x2, voutput_max);
122 vout0x3 = __builtin_wasm_min_f32(vout0x3, voutput_max);
123
124 if XNN_LIKELY(nc >= 4) {
125 c0[0] = vout0x0;
126 c0[1] = vout0x1;
127 c0[2] = vout0x2;
128 c0[3] = vout0x3;
129
130 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
131
132 c0 = (float*) ((uintptr_t) c0 + cn_stride);
133
134 nc -= 4;
135 } else {
136 if (nc & 2) {
137 c0[0] = vout0x0;
138 c0[1] = vout0x1;
139 vout0x0 = vout0x2;
140 c0 += 2;
141 }
142 if (nc & 1) {
143 c0[0] = vout0x0;
144 }
145
146 nc = 0;
147 }
148 } while (nc != 0);
149 }