1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/c8-neon-mull.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 <arm_neon.h>
13
14 #include <xnnpack/gemm.h>
15 #include <xnnpack/intrinsics-polyfill.h>
16 #include <xnnpack/math.h>
17
18
xnn_qs8_gemm_minmax_fp32_ukernel_1x8c8__neonv8_mlal(size_t mr,size_t nc,size_t kc,const int8_t * restrict a,size_t a_stride,const void * restrict w,int8_t * restrict c,size_t cm_stride,size_t cn_stride,const union xnn_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_qs8_gemm_minmax_fp32_ukernel_1x8c8__neonv8_mlal(
20 size_t mr,
21 size_t nc,
22 size_t kc,
23 const int8_t* restrict a,
24 size_t a_stride,
25 const void* restrict w,
26 int8_t* restrict c,
27 size_t cm_stride,
28 size_t cn_stride,
29 const union xnn_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
30 {
31 assert(mr != 0);
32 assert(mr <= 1);
33 assert(nc != 0);
34 assert(kc != 0);
35 assert(kc % sizeof(int8_t) == 0);
36 assert(a != NULL);
37 assert(w != NULL);
38 assert(c != NULL);
39
40 kc = round_up_po2(kc, 8 * sizeof(int8_t));
41 const int8_t* a0 = a;
42 int8_t* c0 = c;
43
44 do {
45 int32x4_t vacc0x0 = vld1q_lane_s32(w, vmovq_n_s32(0), 0); w = (const void*) ((uintptr_t) w + sizeof(int32_t));
46 int32x4_t vacc0x1 = vld1q_lane_s32(w, vmovq_n_s32(0), 0); w = (const void*) ((uintptr_t) w + sizeof(int32_t));
47 int32x4_t vacc0x2 = vld1q_lane_s32(w, vmovq_n_s32(0), 0); w = (const void*) ((uintptr_t) w + sizeof(int32_t));
48 int32x4_t vacc0x3 = vld1q_lane_s32(w, vmovq_n_s32(0), 0); w = (const void*) ((uintptr_t) w + sizeof(int32_t));
49 int32x4_t vacc0x4 = vld1q_lane_s32(w, vmovq_n_s32(0), 0); w = (const void*) ((uintptr_t) w + sizeof(int32_t));
50 int32x4_t vacc0x5 = vld1q_lane_s32(w, vmovq_n_s32(0), 0); w = (const void*) ((uintptr_t) w + sizeof(int32_t));
51 int32x4_t vacc0x6 = vld1q_lane_s32(w, vmovq_n_s32(0), 0); w = (const void*) ((uintptr_t) w + sizeof(int32_t));
52 int32x4_t vacc0x7 = vld1q_lane_s32(w, vmovq_n_s32(0), 0); w = (const void*) ((uintptr_t) w + sizeof(int32_t));
53
54 size_t k = kc;
55 // 2x partial unrolled loop to load 16 bytes at a time using MLA.
56 while (k >= 16 * sizeof(int8_t)) {
57 const int8x8_t va0x0 = vld1_s8(a0); a0 += 8;
58 const int8x8_t va0x1 = vld1_s8(a0); a0 += 8;
59
60 const int8x8_t vb0x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
61 const int8x8_t vb1x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
62 const int8x8_t vb2x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
63 const int8x8_t vb3x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
64 const int8x8_t vb4x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
65 const int8x8_t vb5x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
66 const int8x8_t vb6x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
67 const int8x8_t vb7x0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
68
69 const int8x8_t vb0x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
70 int16x8_t vprod0x0 = vmull_s8(vb0x0, va0x0);
71 vprod0x0 = vmlal_s8(vprod0x0, vb0x1, va0x1);
72 vacc0x0 = vpadalq_s16(vacc0x0, vprod0x0);
73 const int8x8_t vb1x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
74 int16x8_t vprod0x1 = vmull_s8(vb1x0, va0x0);
75 vprod0x1 = vmlal_s8(vprod0x1, vb1x1, va0x1);
76 vacc0x1 = vpadalq_s16(vacc0x1, vprod0x1);
77 const int8x8_t vb2x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
78 int16x8_t vprod0x2 = vmull_s8(vb2x0, va0x0);
79 vprod0x2 = vmlal_s8(vprod0x2, vb2x1, va0x1);
80 vacc0x2 = vpadalq_s16(vacc0x2, vprod0x2);
81 const int8x8_t vb3x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
82 int16x8_t vprod0x3 = vmull_s8(vb3x0, va0x0);
83 vprod0x3 = vmlal_s8(vprod0x3, vb3x1, va0x1);
84 vacc0x3 = vpadalq_s16(vacc0x3, vprod0x3);
85 const int8x8_t vb4x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
86 int16x8_t vprod0x4 = vmull_s8(vb4x0, va0x0);
87 vprod0x4 = vmlal_s8(vprod0x4, vb4x1, va0x1);
88 vacc0x4 = vpadalq_s16(vacc0x4, vprod0x4);
89 const int8x8_t vb5x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
90 int16x8_t vprod0x5 = vmull_s8(vb5x0, va0x0);
91 vprod0x5 = vmlal_s8(vprod0x5, vb5x1, va0x1);
92 vacc0x5 = vpadalq_s16(vacc0x5, vprod0x5);
93 const int8x8_t vb6x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
94 int16x8_t vprod0x6 = vmull_s8(vb6x0, va0x0);
95 vprod0x6 = vmlal_s8(vprod0x6, vb6x1, va0x1);
96 vacc0x6 = vpadalq_s16(vacc0x6, vprod0x6);
97 const int8x8_t vb7x1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof( int8_t));
98 int16x8_t vprod0x7 = vmull_s8(vb7x0, va0x0);
99 vprod0x7 = vmlal_s8(vprod0x7, vb7x1, va0x1);
100 vacc0x7 = vpadalq_s16(vacc0x7, vprod0x7);
101
102 k -= 16 * sizeof(int8_t);
103 }
104
105 // Handle 8 bytes at a time using MUL.
106 if (k != 0) {
107 const int8x8_t va0 = vld1_s8(a0); a0 += 8;
108
109 const int8x8_t vb0 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
110 const int16x8_t vprod0x0 = vmull_s8(vb0, va0);
111 vacc0x0 = vpadalq_s16(vacc0x0, vprod0x0);
112 const int8x8_t vb1 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
113 const int16x8_t vprod0x1 = vmull_s8(vb1, va0);
114 vacc0x1 = vpadalq_s16(vacc0x1, vprod0x1);
115 const int8x8_t vb2 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
116 const int16x8_t vprod0x2 = vmull_s8(vb2, va0);
117 vacc0x2 = vpadalq_s16(vacc0x2, vprod0x2);
118 const int8x8_t vb3 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
119 const int16x8_t vprod0x3 = vmull_s8(vb3, va0);
120 vacc0x3 = vpadalq_s16(vacc0x3, vprod0x3);
121 const int8x8_t vb4 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
122 const int16x8_t vprod0x4 = vmull_s8(vb4, va0);
123 vacc0x4 = vpadalq_s16(vacc0x4, vprod0x4);
124 const int8x8_t vb5 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
125 const int16x8_t vprod0x5 = vmull_s8(vb5, va0);
126 vacc0x5 = vpadalq_s16(vacc0x5, vprod0x5);
127 const int8x8_t vb6 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
128 const int16x8_t vprod0x6 = vmull_s8(vb6, va0);
129 vacc0x6 = vpadalq_s16(vacc0x6, vprod0x6);
130 const int8x8_t vb7 = vld1_s8(w); w = (const void*) ((uintptr_t) w + 8 * sizeof(int8_t));
131 const int16x8_t vprod0x7 = vmull_s8(vb7, va0);
132 vacc0x7 = vpadalq_s16(vacc0x7, vprod0x7);
133
134 k -= 8 * sizeof(int8_t);
135 }
136
137 #if XNN_ARCH_ARM64
138 const int32x4_t vsum0x01 = vpaddq_s32(vacc0x0, vacc0x1);
139 const int32x4_t vsum0x23 = vpaddq_s32(vacc0x2, vacc0x3);
140 const int32x4_t vsum0x45 = vpaddq_s32(vacc0x4, vacc0x5);
141 const int32x4_t vsum0x67 = vpaddq_s32(vacc0x6, vacc0x7);
142
143 int32x4_t vacc0x0123 = vpaddq_s32(vsum0x01, vsum0x23);
144 int32x4_t vacc0x4567 = vpaddq_s32(vsum0x45, vsum0x67);
145 #else
146 const int32x2_t vpsum0x0 = vadd_s32(vget_low_s32(vacc0x0), vget_high_s32(vacc0x0));
147 const int32x2_t vpsum0x1 = vadd_s32(vget_low_s32(vacc0x1), vget_high_s32(vacc0x1));
148 const int32x2_t vpsum0x2 = vadd_s32(vget_low_s32(vacc0x2), vget_high_s32(vacc0x2));
149 const int32x2_t vpsum0x3 = vadd_s32(vget_low_s32(vacc0x3), vget_high_s32(vacc0x3));
150 const int32x2_t vsum0x01 = vpadd_s32(vpsum0x0, vpsum0x1);
151 const int32x2_t vsum0x23 = vpadd_s32(vpsum0x2, vpsum0x3);
152 int32x4_t vacc0x0123 = vcombine_s32(vsum0x01, vsum0x23 );
153 const int32x2_t vpsum0x4 = vadd_s32(vget_low_s32(vacc0x4), vget_high_s32(vacc0x4));
154 const int32x2_t vpsum0x5 = vadd_s32(vget_low_s32(vacc0x5), vget_high_s32(vacc0x5));
155 const int32x2_t vpsum0x6 = vadd_s32(vget_low_s32(vacc0x6), vget_high_s32(vacc0x6));
156 const int32x2_t vpsum0x7 = vadd_s32(vget_low_s32(vacc0x7), vget_high_s32(vacc0x7));
157 const int32x2_t vsum0x45 = vpadd_s32(vpsum0x4, vpsum0x5);
158 const int32x2_t vsum0x67 = vpadd_s32(vpsum0x6, vpsum0x7);
159 int32x4_t vacc0x4567 = vcombine_s32(vsum0x45, vsum0x67 );
160 #endif
161
162 float32x4_t vfpacc0x0123 = vcvtq_f32_s32(vacc0x0123);
163 float32x4_t vfpacc0x4567 = vcvtq_f32_s32(vacc0x4567);
164
165 const float32x4_t vscale = vld1q_dup_f32(¶ms->fp32_neonv8.scale);
166 vfpacc0x0123 = vmulq_f32(vfpacc0x0123, vscale);
167 vfpacc0x4567 = vmulq_f32(vfpacc0x4567, vscale);
168
169 vacc0x0123 = vcvtnq_s32_f32(vfpacc0x0123);
170 vacc0x4567 = vcvtnq_s32_f32(vfpacc0x4567);
171
172 const int16x8_t voutput_zero_point = vld1q_dup_s16(¶ms->fp32_neonv8.output_zero_point);
173 #if XNN_ARCH_ARM64
174 int16x8_t vacc0x01234567 = vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567);
175
176 vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
177
178 int8x8_t vout0x01234567 = vqmovn_s16(vacc0x01234567);
179 #else
180 int16x8_t vacc0x01234567 = vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567));
181
182 vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
183
184 int8x8_t vout0x01234567 = vqmovn_s16(vacc0x01234567);
185 #endif
186
187 const int8x8_t voutput_min = vld1_dup_s8(¶ms->fp32_neonv8.output_min);
188 vout0x01234567 = vmax_s8(vout0x01234567, voutput_min);
189
190 const int8x8_t voutput_max = vld1_dup_s8(¶ms->fp32_neonv8.output_max);
191 vout0x01234567 = vmin_s8(vout0x01234567, voutput_max);
192
193 if (nc >= 8) {
194 vst1_s8(c0 + 0, vout0x01234567);
195
196 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
197
198 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
199
200 nc -= 8;
201 } else {
202 // Final case where not all of the 8 columns fit in the destination.
203 if (nc & 4) {
204 vst1_lane_u32((void*) c0, vreinterpret_u32_s8(vout0x01234567), 0); c0 += 4;
205 vout0x01234567 = vext_s8(vout0x01234567, vout0x01234567, 4);
206 }
207 if (nc & 2) {
208 vst1_lane_u16((void*) c0, vreinterpret_u16_s8(vout0x01234567), 0); c0 += 2;
209 vout0x01234567 = vext_s8(vout0x01234567, vout0x01234567, 2);
210 }
211 if (nc & 1) {
212 vst1_lane_s8(c0, vout0x01234567, 0);
213 }
214
215 nc = 0;
216 }
217 } while (nc != 0);
218 }
219