1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-igemm/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 <fp16.h>
13
14 #include <xnnpack/math.h>
15 #include <xnnpack/gemm.h>
16
17
xnn_qc8_igemm_minmax_fp32_ukernel_1x4__scalar_fmagic(size_t mr,size_t nc,size_t kc,size_t ks,const int8_t ** restrict a,const void * restrict w,int8_t * restrict c,size_t cm_stride,size_t cn_stride,size_t a_offset,const int8_t * zero,const union xnn_qs8_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_qc8_igemm_minmax_fp32_ukernel_1x4__scalar_fmagic(
19 size_t mr,
20 size_t nc,
21 size_t kc,
22 size_t ks,
23 const int8_t**restrict a,
24 const void*restrict w,
25 int8_t*restrict c,
26 size_t cm_stride,
27 size_t cn_stride,
28 size_t a_offset,
29 const int8_t* zero,
30 const union xnn_qs8_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
31 {
32 assert(mr != 0);
33 assert(mr <= 1);
34 assert(nc != 0);
35 assert(kc != 0);
36 assert(ks != 0);
37 assert(ks % (1 * sizeof(void*)) == 0);
38 assert(a != NULL);
39 assert(w != NULL);
40 assert(c != NULL);
41
42 int8_t* c0 = c;
43
44 do {
45 int32_t vacc0x0 = ((const int32_t*) w)[0];
46 int32_t vacc0x1 = ((const int32_t*) w)[1];
47 int32_t vacc0x2 = ((const int32_t*) w)[2];
48 int32_t vacc0x3 = ((const int32_t*) w)[3];
49 w = (const void*) ((const int32_t*) w + 4);
50
51 size_t p = ks;
52 do {
53 const int8_t* restrict a0 = a[0];
54 assert(a0 != NULL);
55 if XNN_UNPREDICTABLE(a0 != zero) {
56 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
57 }
58 a += 1;
59
60 size_t k = kc;
61 do {
62 const int32_t va0 = (int32_t) *a0++;
63
64 const int32_t vb0 = (int32_t) ((const int8_t*) w)[0];
65 const int32_t vb1 = (int32_t) ((const int8_t*) w)[1];
66 const int32_t vb2 = (int32_t) ((const int8_t*) w)[2];
67 const int32_t vb3 = (int32_t) ((const int8_t*) w)[3];
68 w = (const void*) ((const int8_t*) w + 4);
69
70 vacc0x0 += va0 * vb0;
71 vacc0x1 += va0 * vb1;
72 vacc0x2 += va0 * vb2;
73 vacc0x3 += va0 * vb3;
74
75 k -= sizeof(int8_t);
76 } while (k != 0);
77 p -= 1 * sizeof(void*);
78 } while (p != 0);
79
80 float vfpacc0x0 = (float) vacc0x0;
81 float vfpacc0x1 = (float) vacc0x1;
82 float vfpacc0x2 = (float) vacc0x2;
83 float vfpacc0x3 = (float) vacc0x3;
84
85 const float vscale0 = ((const float*) w)[0];
86 vfpacc0x0 *= vscale0;
87 const float vscale1 = ((const float*) w)[1];
88 vfpacc0x1 *= vscale1;
89 const float vscale2 = ((const float*) w)[2];
90 vfpacc0x2 *= vscale2;
91 const float vscale3 = ((const float*) w)[3];
92 vfpacc0x3 *= vscale3;
93 w = (const void*) ((const float*) w + 4);
94
95 const float voutput_min_less_zero_point = params->scalar_fmagic.output_min_less_zero_point;
96 vfpacc0x0 = math_max_f32(vfpacc0x0, voutput_min_less_zero_point);
97 vfpacc0x1 = math_max_f32(vfpacc0x1, voutput_min_less_zero_point);
98 vfpacc0x2 = math_max_f32(vfpacc0x2, voutput_min_less_zero_point);
99 vfpacc0x3 = math_max_f32(vfpacc0x3, voutput_min_less_zero_point);
100
101 const float voutput_max_less_zero_point = params->scalar_fmagic.output_max_less_zero_point;
102 vfpacc0x0 = math_min_f32(vfpacc0x0, voutput_max_less_zero_point);
103 vfpacc0x1 = math_min_f32(vfpacc0x1, voutput_max_less_zero_point);
104 vfpacc0x2 = math_min_f32(vfpacc0x2, voutput_max_less_zero_point);
105 vfpacc0x3 = math_min_f32(vfpacc0x3, voutput_max_less_zero_point);
106
107 const float vmagic_bias = params->scalar_fmagic.magic_bias;
108 vfpacc0x0 += vmagic_bias;
109 vfpacc0x1 += vmagic_bias;
110 vfpacc0x2 += vmagic_bias;
111 vfpacc0x3 += vmagic_bias;
112
113 const int32_t vmagic_bias_less_output_zero_point = params->scalar_fmagic.magic_bias_less_output_zero_point;
114 int32_t vout0x0 = (int32_t) fp32_to_bits(vfpacc0x0) - vmagic_bias_less_output_zero_point;
115 int32_t vout0x1 = (int32_t) fp32_to_bits(vfpacc0x1) - vmagic_bias_less_output_zero_point;
116 int32_t vout0x2 = (int32_t) fp32_to_bits(vfpacc0x2) - vmagic_bias_less_output_zero_point;
117 int32_t vout0x3 = (int32_t) fp32_to_bits(vfpacc0x3) - vmagic_bias_less_output_zero_point;
118
119 if XNN_LIKELY(nc >= 4) {
120 c0[0] = (int8_t) vout0x0;
121 c0[1] = (int8_t) vout0x1;
122 c0[2] = (int8_t) vout0x2;
123 c0[3] = (int8_t) vout0x3;
124
125 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
126
127 a = (const int8_t**restrict) ((uintptr_t) a - ks);
128 nc -= 4;
129 } else {
130 if (nc & 2) {
131 c0[0] = (int8_t) vout0x0;
132 c0[1] = (int8_t) vout0x1;
133 vout0x0 = vout0x2;
134 c0 += 2;
135 }
136 if (nc & 1) {
137 c0[0] = (int8_t) vout0x0;
138 }
139
140 nc = 0;
141 }
142 } while (nc != 0);
143 }
144