Debian Codegraph @

src/qd8-f32-qc4w-gemm/gen/qd8-f32-qc4w-gemm-2x2-minmax-scalar.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 #include "xnnpack/unaligned.h"
15
16
17 void xnn_qd8_f32_qc4w_gemm_minmax_ukernel_2x2__scalar(
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 struct xnn_f32_qc4w_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 <= 2);
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
44
45 kc = round_up_po2(kc, 2);
46 do {
47 const int32_t vksum0 = unaligned_indexed_load_s32(w, 0);
48 const int32_t vksum1 = unaligned_indexed_load_s32(w, 1);
49 const int32_t vinput_zero_point0 = quantization_params[0].zero_point;
50 int32_t vacc0x0 = vksum0 * vinput_zero_point0;
51 int32_t vacc0x1 = vksum1 * vinput_zero_point0;
52 const int32_t vinput_zero_point1 = quantization_params[1].zero_point;
53 int32_t vacc1x0 = vksum0 * vinput_zero_point1;
54 int32_t vacc1x1 = vksum1 * vinput_zero_point1;
55 w = (const int32_t*) w + 2;
56
57 size_t k = kc;
58 for (; k >= 2 * sizeof(uint8_t); k -= 2 * sizeof(uint8_t)) {
59 const int32_t va0c0 = (int32_t) a0[0];
60 const int32_t va0c1 = (int32_t) a0[1];
61 a0 += 2;
62 const int32_t va1c0 = (int32_t) a1[0];
63 const int32_t va1c1 = (int32_t) a1[1];
64 a1 += 2;
65
66 const uint8_t vbi0 = ((const uint8_t*) w)[0];
67 const uint8_t vbi1 = ((const uint8_t*) w)[1];
68 w = (const uint8_t*) w + 2;
69 const int32_t vb0c0 = (int32_t) (int8_t) (vbi0 << 4);
70 const int32_t vb0c1 = (int32_t) (int8_t) (vbi0 & 0xF0);
71 const int32_t vb1c0 = (int32_t) (int8_t) (vbi1 << 4);
72 const int32_t vb1c1 = (int32_t) (int8_t) (vbi1 & 0xF0);
73
74 vacc0x0 += va0c0 * vb0c0;
75 vacc0x1 += va0c0 * vb1c0;
76 vacc1x0 += va1c0 * vb0c0;
77 vacc1x1 += va1c0 * vb1c0;
78 vacc0x0 += va0c1 * vb0c1;
79 vacc0x1 += va0c1 * vb1c1;
80 vacc1x0 += va1c1 * vb0c1;
81 vacc1x1 += va1c1 * vb1c1;
82 }
83
84 float vout0x0 = (float) math_asr_s32(vacc0x0, 4);
85 float vout0x1 = (float) math_asr_s32(vacc0x1, 4);
86 float vout1x0 = (float) math_asr_s32(vacc1x0, 4);
87 float vout1x1 = (float) math_asr_s32(vacc1x1, 4);
88
89 const float vinput_scale0 = quantization_params[0].inv_scale;
90 vout0x0 *= vinput_scale0;
91 vout0x1 *= vinput_scale0;
92 const float vinput_scale1 = quantization_params[1].inv_scale;
93 vout1x0 *= vinput_scale1;
94 vout1x1 *= vinput_scale1;
95
96 const float vfilter_output_scale0 = unaligned_indexed_load_f32(w, 0);
97 vout0x0 *= vfilter_output_scale0;
98 vout1x0 *= vfilter_output_scale0;
99 const float vfilter_output_scale1 = unaligned_indexed_load_f32(w, 1);
100 vout0x1 *= vfilter_output_scale1;
101 vout1x1 *= vfilter_output_scale1;
102
103 const float vbias0 = unaligned_indexed_load_f32(w, 2);
104 vout0x0 += vbias0;
105 vout1x0 += vbias0;
106 const float vbias1 = unaligned_indexed_load_f32(w, 3);
107 vout0x1 += vbias1;
108 vout1x1 += vbias1;
109
110 w = (const float*) w + 4;
111
112 const float voutput_min = params->scalar.min;
113 vout0x0 = math_max_f32(vout0x0, voutput_min);
114 vout1x0 = math_max_f32(vout1x0, voutput_min);
115 vout0x1 = math_max_f32(vout0x1, voutput_min);
116 vout1x1 = math_max_f32(vout1x1, voutput_min);
117
118 const float voutput_max = params->scalar.max;
119 vout0x0 = math_min_f32(vout0x0, voutput_max);
120 vout1x0 = math_min_f32(vout1x0, voutput_max);
121 vout0x1 = math_min_f32(vout0x1, voutput_max);
122 vout1x1 = math_min_f32(vout1x1, voutput_max);
123
124 if XNN_LIKELY(nc >= 2) {
125 c0[0] = vout0x0;
126 c0[1] = vout0x1;
127 c1[0] = vout1x0;
128 c1[1] = vout1x1;
129
130 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
131 a1 = (const int8_t*) ((uintptr_t) a1 - kc);
132
133 c0 = (float*) ((uintptr_t) c0 + cn_stride);
134 c1 = (float*) ((uintptr_t) c1 + cn_stride);
135
136 nc -= 2;
137 } else {
138 if (nc & 1) {
139 c0[0] = vout0x0;
140 c1[0] = vout1x0;
141 }
142
143 nc = 0;
144 }
145 } while (nc != 0);
146 }