1 // Auto-generated file. Do not edit!
2 // Template: src/f32-gemm/avx-shuffle4.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2019 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 <immintrin.h>
13
14 #include <xnnpack/gemm.h>
15
16
xnn_f32_gemm_minmax_ukernel_5x16s4__fma3_broadcast(size_t mr,size_t nc,size_t kc,const float * restrict a,size_t a_stride,const float * restrict w,float * restrict c,size_t cm_stride,size_t cn_stride,const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_f32_gemm_minmax_ukernel_5x16s4__fma3_broadcast(
18 size_t mr,
19 size_t nc,
20 size_t kc,
21 const float*restrict a,
22 size_t a_stride,
23 const float*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 {
29 assert(mr != 0);
30 assert(mr <= 5);
31 assert(nc != 0);
32 assert(kc != 0);
33 assert(kc % sizeof(float) == 0);
34 assert(a != NULL);
35 assert(w != NULL);
36 assert(c != NULL);
37
38 const float* a0 = a;
39 float* c0 = c;
40 const float* a1 = (const float*) ((uintptr_t) a0 + a_stride);
41 float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
42 if XNN_UNPREDICTABLE(mr < 2) {
43 a1 = a0;
44 c1 = c0;
45 }
46 const float* a2 = (const float*) ((uintptr_t) a1 + a_stride);
47 float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
48 if XNN_UNPREDICTABLE(mr <= 2) {
49 a2 = a1;
50 c2 = c1;
51 }
52 const float* a3 = (const float*) ((uintptr_t) a2 + a_stride);
53 float* c3 = (float*) ((uintptr_t) c2 + cm_stride);
54 if XNN_UNPREDICTABLE(mr < 4) {
55 a3 = a2;
56 c3 = c2;
57 }
58 const float* a4 = (const float*) ((uintptr_t) a3 + a_stride);
59 float* c4 = (float*) ((uintptr_t) c3 + cm_stride);
60 if XNN_UNPREDICTABLE(mr <= 4) {
61 a4 = a3;
62 c4 = c3;
63 }
64
65 do {
66 __m256 vacc0x01234567 = _mm256_load_ps(w + 0);
67 __m256 vacc0x89ABCDEF = _mm256_load_ps(w + 8);
68 __m256 vacc1x01234567 = vacc0x01234567;
69 __m256 vacc1x89ABCDEF = vacc0x89ABCDEF;
70 __m256 vacc2x01234567 = vacc0x01234567;
71 __m256 vacc2x89ABCDEF = vacc0x89ABCDEF;
72 __m256 vacc3x01234567 = vacc0x01234567;
73 __m256 vacc3x89ABCDEF = vacc0x89ABCDEF;
74 __m256 vacc4x01234567 = vacc0x01234567;
75 __m256 vacc4x89ABCDEF = vacc0x89ABCDEF;
76 w += 16;
77
78 size_t k = kc;
79 while (k >= 4 * sizeof(float)) {
80 __m256 va0 = _mm256_broadcast_ps((const __m128*) a0);
81 a0 += 4;
82 __m256 va1 = _mm256_broadcast_ps((const __m128*) a1);
83 a1 += 4;
84 __m256 va2 = _mm256_broadcast_ps((const __m128*) a2);
85 a2 += 4;
86 __m256 va3 = _mm256_broadcast_ps((const __m128*) a3);
87 a3 += 4;
88 __m256 va4 = _mm256_broadcast_ps((const __m128*) a4);
89 a4 += 4;
90
91
92 const __m256 vb01234567c0 = _mm256_load_ps(w + 0);
93 const __m256 vb89ABCDEFc0 = _mm256_load_ps(w + 8);
94
95 vacc0x01234567 = _mm256_fmadd_ps(va0, vb01234567c0, vacc0x01234567);
96 vacc1x01234567 = _mm256_fmadd_ps(va1, vb01234567c0, vacc1x01234567);
97 vacc2x01234567 = _mm256_fmadd_ps(va2, vb01234567c0, vacc2x01234567);
98 vacc3x01234567 = _mm256_fmadd_ps(va3, vb01234567c0, vacc3x01234567);
99 vacc4x01234567 = _mm256_fmadd_ps(va4, vb01234567c0, vacc4x01234567);
100 vacc0x89ABCDEF = _mm256_fmadd_ps(va0, vb89ABCDEFc0, vacc0x89ABCDEF);
101 vacc1x89ABCDEF = _mm256_fmadd_ps(va1, vb89ABCDEFc0, vacc1x89ABCDEF);
102 vacc2x89ABCDEF = _mm256_fmadd_ps(va2, vb89ABCDEFc0, vacc2x89ABCDEF);
103 vacc3x89ABCDEF = _mm256_fmadd_ps(va3, vb89ABCDEFc0, vacc3x89ABCDEF);
104 vacc4x89ABCDEF = _mm256_fmadd_ps(va4, vb89ABCDEFc0, vacc4x89ABCDEF);
105
106 va0 = _mm256_permute_ps(va0, _MM_SHUFFLE(0, 3, 2, 1));
107 va1 = _mm256_permute_ps(va1, _MM_SHUFFLE(0, 3, 2, 1));
108 va2 = _mm256_permute_ps(va2, _MM_SHUFFLE(0, 3, 2, 1));
109 va3 = _mm256_permute_ps(va3, _MM_SHUFFLE(0, 3, 2, 1));
110 va4 = _mm256_permute_ps(va4, _MM_SHUFFLE(0, 3, 2, 1));
111
112 const __m256 vb01234567c1 = _mm256_load_ps(w + 16);
113 const __m256 vb89ABCDEFc1 = _mm256_load_ps(w + 24);
114
115 vacc0x01234567 = _mm256_fmadd_ps(va0, vb01234567c1, vacc0x01234567);
116 vacc1x01234567 = _mm256_fmadd_ps(va1, vb01234567c1, vacc1x01234567);
117 vacc2x01234567 = _mm256_fmadd_ps(va2, vb01234567c1, vacc2x01234567);
118 vacc3x01234567 = _mm256_fmadd_ps(va3, vb01234567c1, vacc3x01234567);
119 vacc4x01234567 = _mm256_fmadd_ps(va4, vb01234567c1, vacc4x01234567);
120 vacc0x89ABCDEF = _mm256_fmadd_ps(va0, vb89ABCDEFc1, vacc0x89ABCDEF);
121 vacc1x89ABCDEF = _mm256_fmadd_ps(va1, vb89ABCDEFc1, vacc1x89ABCDEF);
122 vacc2x89ABCDEF = _mm256_fmadd_ps(va2, vb89ABCDEFc1, vacc2x89ABCDEF);
123 vacc3x89ABCDEF = _mm256_fmadd_ps(va3, vb89ABCDEFc1, vacc3x89ABCDEF);
124 vacc4x89ABCDEF = _mm256_fmadd_ps(va4, vb89ABCDEFc1, vacc4x89ABCDEF);
125
126 va0 = _mm256_permute_ps(va0, _MM_SHUFFLE(0, 3, 2, 1));
127 va1 = _mm256_permute_ps(va1, _MM_SHUFFLE(0, 3, 2, 1));
128 va2 = _mm256_permute_ps(va2, _MM_SHUFFLE(0, 3, 2, 1));
129 va3 = _mm256_permute_ps(va3, _MM_SHUFFLE(0, 3, 2, 1));
130 va4 = _mm256_permute_ps(va4, _MM_SHUFFLE(0, 3, 2, 1));
131
132 const __m256 vb01234567c2 = _mm256_load_ps(w + 32);
133 const __m256 vb89ABCDEFc2 = _mm256_load_ps(w + 40);
134
135 vacc0x01234567 = _mm256_fmadd_ps(va0, vb01234567c2, vacc0x01234567);
136 vacc1x01234567 = _mm256_fmadd_ps(va1, vb01234567c2, vacc1x01234567);
137 vacc2x01234567 = _mm256_fmadd_ps(va2, vb01234567c2, vacc2x01234567);
138 vacc3x01234567 = _mm256_fmadd_ps(va3, vb01234567c2, vacc3x01234567);
139 vacc4x01234567 = _mm256_fmadd_ps(va4, vb01234567c2, vacc4x01234567);
140 vacc0x89ABCDEF = _mm256_fmadd_ps(va0, vb89ABCDEFc2, vacc0x89ABCDEF);
141 vacc1x89ABCDEF = _mm256_fmadd_ps(va1, vb89ABCDEFc2, vacc1x89ABCDEF);
142 vacc2x89ABCDEF = _mm256_fmadd_ps(va2, vb89ABCDEFc2, vacc2x89ABCDEF);
143 vacc3x89ABCDEF = _mm256_fmadd_ps(va3, vb89ABCDEFc2, vacc3x89ABCDEF);
144 vacc4x89ABCDEF = _mm256_fmadd_ps(va4, vb89ABCDEFc2, vacc4x89ABCDEF);
145
146 va0 = _mm256_permute_ps(va0, _MM_SHUFFLE(0, 3, 2, 1));
147 va1 = _mm256_permute_ps(va1, _MM_SHUFFLE(0, 3, 2, 1));
148 va2 = _mm256_permute_ps(va2, _MM_SHUFFLE(0, 3, 2, 1));
149 va3 = _mm256_permute_ps(va3, _MM_SHUFFLE(0, 3, 2, 1));
150 va4 = _mm256_permute_ps(va4, _MM_SHUFFLE(0, 3, 2, 1));
151
152 const __m256 vb01234567c3 = _mm256_load_ps(w + 48);
153 const __m256 vb89ABCDEFc3 = _mm256_load_ps(w + 56);
154
155 vacc0x01234567 = _mm256_fmadd_ps(va0, vb01234567c3, vacc0x01234567);
156 vacc1x01234567 = _mm256_fmadd_ps(va1, vb01234567c3, vacc1x01234567);
157 vacc2x01234567 = _mm256_fmadd_ps(va2, vb01234567c3, vacc2x01234567);
158 vacc3x01234567 = _mm256_fmadd_ps(va3, vb01234567c3, vacc3x01234567);
159 vacc4x01234567 = _mm256_fmadd_ps(va4, vb01234567c3, vacc4x01234567);
160 vacc0x89ABCDEF = _mm256_fmadd_ps(va0, vb89ABCDEFc3, vacc0x89ABCDEF);
161 vacc1x89ABCDEF = _mm256_fmadd_ps(va1, vb89ABCDEFc3, vacc1x89ABCDEF);
162 vacc2x89ABCDEF = _mm256_fmadd_ps(va2, vb89ABCDEFc3, vacc2x89ABCDEF);
163 vacc3x89ABCDEF = _mm256_fmadd_ps(va3, vb89ABCDEFc3, vacc3x89ABCDEF);
164 vacc4x89ABCDEF = _mm256_fmadd_ps(va4, vb89ABCDEFc3, vacc4x89ABCDEF);
165
166
167 w += 64;
168 k -= 4 * sizeof(float);
169 }
170 if XNN_UNLIKELY(k != 0) {
171 do {
172 const __m256 va0 = _mm256_broadcast_ss(a0);
173 a0 += 1;
174 const __m256 va1 = _mm256_broadcast_ss(a1);
175 a1 += 1;
176 const __m256 va2 = _mm256_broadcast_ss(a2);
177 a2 += 1;
178 const __m256 va3 = _mm256_broadcast_ss(a3);
179 a3 += 1;
180 const __m256 va4 = _mm256_broadcast_ss(a4);
181 a4 += 1;
182
183 const __m256 vb01234567 = _mm256_load_ps(w);
184 const __m256 vb89ABCDEF = _mm256_load_ps(w + 8);
185 w += 16;
186
187 vacc0x01234567 = _mm256_fmadd_ps(va0, vb01234567, vacc0x01234567);
188 vacc1x01234567 = _mm256_fmadd_ps(va1, vb01234567, vacc1x01234567);
189 vacc2x01234567 = _mm256_fmadd_ps(va2, vb01234567, vacc2x01234567);
190 vacc3x01234567 = _mm256_fmadd_ps(va3, vb01234567, vacc3x01234567);
191 vacc4x01234567 = _mm256_fmadd_ps(va4, vb01234567, vacc4x01234567);
192 vacc0x89ABCDEF = _mm256_fmadd_ps(va0, vb89ABCDEF, vacc0x89ABCDEF);
193 vacc1x89ABCDEF = _mm256_fmadd_ps(va1, vb89ABCDEF, vacc1x89ABCDEF);
194 vacc2x89ABCDEF = _mm256_fmadd_ps(va2, vb89ABCDEF, vacc2x89ABCDEF);
195 vacc3x89ABCDEF = _mm256_fmadd_ps(va3, vb89ABCDEF, vacc3x89ABCDEF);
196 vacc4x89ABCDEF = _mm256_fmadd_ps(va4, vb89ABCDEF, vacc4x89ABCDEF);
197
198 k -= sizeof(float);
199 } while (k != 0);
200 }
201
202 const __m256 vmax = _mm256_broadcast_ps((const __m128*) params->sse.max);
203 vacc0x01234567 = _mm256_min_ps(vacc0x01234567, vmax);
204 vacc1x01234567 = _mm256_min_ps(vacc1x01234567, vmax);
205 vacc2x01234567 = _mm256_min_ps(vacc2x01234567, vmax);
206 vacc3x01234567 = _mm256_min_ps(vacc3x01234567, vmax);
207 vacc4x01234567 = _mm256_min_ps(vacc4x01234567, vmax);
208 vacc0x89ABCDEF = _mm256_min_ps(vacc0x89ABCDEF, vmax);
209 vacc1x89ABCDEF = _mm256_min_ps(vacc1x89ABCDEF, vmax);
210 vacc2x89ABCDEF = _mm256_min_ps(vacc2x89ABCDEF, vmax);
211 vacc3x89ABCDEF = _mm256_min_ps(vacc3x89ABCDEF, vmax);
212 vacc4x89ABCDEF = _mm256_min_ps(vacc4x89ABCDEF, vmax);
213
214 const __m256 vmin = _mm256_broadcast_ps((const __m128*) params->sse.min);
215 vacc0x01234567 = _mm256_max_ps(vacc0x01234567, vmin);
216 vacc1x01234567 = _mm256_max_ps(vacc1x01234567, vmin);
217 vacc2x01234567 = _mm256_max_ps(vacc2x01234567, vmin);
218 vacc3x01234567 = _mm256_max_ps(vacc3x01234567, vmin);
219 vacc4x01234567 = _mm256_max_ps(vacc4x01234567, vmin);
220 vacc0x89ABCDEF = _mm256_max_ps(vacc0x89ABCDEF, vmin);
221 vacc1x89ABCDEF = _mm256_max_ps(vacc1x89ABCDEF, vmin);
222 vacc2x89ABCDEF = _mm256_max_ps(vacc2x89ABCDEF, vmin);
223 vacc3x89ABCDEF = _mm256_max_ps(vacc3x89ABCDEF, vmin);
224 vacc4x89ABCDEF = _mm256_max_ps(vacc4x89ABCDEF, vmin);
225
226 if XNN_LIKELY(nc >= 16) {
227 _mm256_storeu_ps(c4, vacc4x01234567);
228 _mm256_storeu_ps(c4 + 8, vacc4x89ABCDEF);
229 c4 = (float*) ((uintptr_t) c4 + cn_stride);
230 _mm256_storeu_ps(c3, vacc3x01234567);
231 _mm256_storeu_ps(c3 + 8, vacc3x89ABCDEF);
232 c3 = (float*) ((uintptr_t) c3 + cn_stride);
233 _mm256_storeu_ps(c2, vacc2x01234567);
234 _mm256_storeu_ps(c2 + 8, vacc2x89ABCDEF);
235 c2 = (float*) ((uintptr_t) c2 + cn_stride);
236 _mm256_storeu_ps(c1, vacc1x01234567);
237 _mm256_storeu_ps(c1 + 8, vacc1x89ABCDEF);
238 c1 = (float*) ((uintptr_t) c1 + cn_stride);
239 _mm256_storeu_ps(c0, vacc0x01234567);
240 _mm256_storeu_ps(c0 + 8, vacc0x89ABCDEF);
241 c0 = (float*) ((uintptr_t) c0 + cn_stride);
242
243 a4 = (const float*) ((uintptr_t) a4 - kc);
244 a3 = (const float*) ((uintptr_t) a3 - kc);
245 a2 = (const float*) ((uintptr_t) a2 - kc);
246 a1 = (const float*) ((uintptr_t) a1 - kc);
247 a0 = (const float*) ((uintptr_t) a0 - kc);
248
249 nc -= 16;
250 } else {
251 if (nc & 8) {
252 _mm256_storeu_ps(c4, vacc4x01234567);
253 _mm256_storeu_ps(c3, vacc3x01234567);
254 _mm256_storeu_ps(c2, vacc2x01234567);
255 _mm256_storeu_ps(c1, vacc1x01234567);
256 _mm256_storeu_ps(c0, vacc0x01234567);
257
258 vacc4x01234567 = vacc4x89ABCDEF;
259 vacc3x01234567 = vacc3x89ABCDEF;
260 vacc2x01234567 = vacc2x89ABCDEF;
261 vacc1x01234567 = vacc1x89ABCDEF;
262 vacc0x01234567 = vacc0x89ABCDEF;
263
264 c4 += 8;
265 c3 += 8;
266 c2 += 8;
267 c1 += 8;
268 c0 += 8;
269 }
270 __m128 vacc4x0123 = _mm256_castps256_ps128(vacc4x01234567);
271 __m128 vacc3x0123 = _mm256_castps256_ps128(vacc3x01234567);
272 __m128 vacc2x0123 = _mm256_castps256_ps128(vacc2x01234567);
273 __m128 vacc1x0123 = _mm256_castps256_ps128(vacc1x01234567);
274 __m128 vacc0x0123 = _mm256_castps256_ps128(vacc0x01234567);
275 if (nc & 4) {
276 _mm_storeu_ps(c4, vacc4x0123);
277 _mm_storeu_ps(c3, vacc3x0123);
278 _mm_storeu_ps(c2, vacc2x0123);
279 _mm_storeu_ps(c1, vacc1x0123);
280 _mm_storeu_ps(c0, vacc0x0123);
281
282 vacc4x0123 = _mm256_extractf128_ps(vacc4x01234567, 1);
283 vacc3x0123 = _mm256_extractf128_ps(vacc3x01234567, 1);
284 vacc2x0123 = _mm256_extractf128_ps(vacc2x01234567, 1);
285 vacc1x0123 = _mm256_extractf128_ps(vacc1x01234567, 1);
286 vacc0x0123 = _mm256_extractf128_ps(vacc0x01234567, 1);
287
288 c4 += 4;
289 c3 += 4;
290 c2 += 4;
291 c1 += 4;
292 c0 += 4;
293 }
294 if (nc & 2) {
295 _mm_storel_pi((__m64*) c4, vacc4x0123);
296 _mm_storel_pi((__m64*) c3, vacc3x0123);
297 _mm_storel_pi((__m64*) c2, vacc2x0123);
298 _mm_storel_pi((__m64*) c1, vacc1x0123);
299 _mm_storel_pi((__m64*) c0, vacc0x0123);
300
301 vacc4x0123 = _mm_movehl_ps(vacc4x0123, vacc4x0123);
302 vacc3x0123 = _mm_movehl_ps(vacc3x0123, vacc3x0123);
303 vacc2x0123 = _mm_movehl_ps(vacc2x0123, vacc2x0123);
304 vacc1x0123 = _mm_movehl_ps(vacc1x0123, vacc1x0123);
305 vacc0x0123 = _mm_movehl_ps(vacc0x0123, vacc0x0123);
306
307 c4 += 2;
308 c3 += 2;
309 c2 += 2;
310 c1 += 2;
311 c0 += 2;
312 }
313 if (nc & 1) {
314 _mm_store_ss(c4, vacc4x0123);
315 _mm_store_ss(c3, vacc3x0123);
316 _mm_store_ss(c2, vacc2x0123);
317 _mm_store_ss(c1, vacc1x0123);
318 _mm_store_ss(c0, vacc0x0123);
319 }
320
321 nc = 0;
322 }
323 } while (nc != 0);
324 }
325