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_3x16s4__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_3x16s4__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 <= 3);
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
53 do {
54 __m256 vacc0x01234567 = _mm256_load_ps(w + 0);
55 __m256 vacc0x89ABCDEF = _mm256_load_ps(w + 8);
56 __m256 vacc1x01234567 = vacc0x01234567;
57 __m256 vacc1x89ABCDEF = vacc0x89ABCDEF;
58 __m256 vacc2x01234567 = vacc0x01234567;
59 __m256 vacc2x89ABCDEF = vacc0x89ABCDEF;
60 w += 16;
61
62 size_t k = kc;
63 while (k >= 4 * sizeof(float)) {
64 __m256 va0 = _mm256_broadcast_ps((const __m128*) a0);
65 a0 += 4;
66 __m256 va1 = _mm256_broadcast_ps((const __m128*) a1);
67 a1 += 4;
68 __m256 va2 = _mm256_broadcast_ps((const __m128*) a2);
69 a2 += 4;
70
71
72 const __m256 vb01234567c0 = _mm256_load_ps(w + 0);
73 const __m256 vb89ABCDEFc0 = _mm256_load_ps(w + 8);
74
75 vacc0x01234567 = _mm256_fmadd_ps(va0, vb01234567c0, vacc0x01234567);
76 vacc1x01234567 = _mm256_fmadd_ps(va1, vb01234567c0, vacc1x01234567);
77 vacc2x01234567 = _mm256_fmadd_ps(va2, vb01234567c0, vacc2x01234567);
78 vacc0x89ABCDEF = _mm256_fmadd_ps(va0, vb89ABCDEFc0, vacc0x89ABCDEF);
79 vacc1x89ABCDEF = _mm256_fmadd_ps(va1, vb89ABCDEFc0, vacc1x89ABCDEF);
80 vacc2x89ABCDEF = _mm256_fmadd_ps(va2, vb89ABCDEFc0, vacc2x89ABCDEF);
81
82 va0 = _mm256_permute_ps(va0, _MM_SHUFFLE(0, 3, 2, 1));
83 va1 = _mm256_permute_ps(va1, _MM_SHUFFLE(0, 3, 2, 1));
84 va2 = _mm256_permute_ps(va2, _MM_SHUFFLE(0, 3, 2, 1));
85
86 const __m256 vb01234567c1 = _mm256_load_ps(w + 16);
87 const __m256 vb89ABCDEFc1 = _mm256_load_ps(w + 24);
88
89 vacc0x01234567 = _mm256_fmadd_ps(va0, vb01234567c1, vacc0x01234567);
90 vacc1x01234567 = _mm256_fmadd_ps(va1, vb01234567c1, vacc1x01234567);
91 vacc2x01234567 = _mm256_fmadd_ps(va2, vb01234567c1, vacc2x01234567);
92 vacc0x89ABCDEF = _mm256_fmadd_ps(va0, vb89ABCDEFc1, vacc0x89ABCDEF);
93 vacc1x89ABCDEF = _mm256_fmadd_ps(va1, vb89ABCDEFc1, vacc1x89ABCDEF);
94 vacc2x89ABCDEF = _mm256_fmadd_ps(va2, vb89ABCDEFc1, vacc2x89ABCDEF);
95
96 va0 = _mm256_permute_ps(va0, _MM_SHUFFLE(0, 3, 2, 1));
97 va1 = _mm256_permute_ps(va1, _MM_SHUFFLE(0, 3, 2, 1));
98 va2 = _mm256_permute_ps(va2, _MM_SHUFFLE(0, 3, 2, 1));
99
100 const __m256 vb01234567c2 = _mm256_load_ps(w + 32);
101 const __m256 vb89ABCDEFc2 = _mm256_load_ps(w + 40);
102
103 vacc0x01234567 = _mm256_fmadd_ps(va0, vb01234567c2, vacc0x01234567);
104 vacc1x01234567 = _mm256_fmadd_ps(va1, vb01234567c2, vacc1x01234567);
105 vacc2x01234567 = _mm256_fmadd_ps(va2, vb01234567c2, vacc2x01234567);
106 vacc0x89ABCDEF = _mm256_fmadd_ps(va0, vb89ABCDEFc2, vacc0x89ABCDEF);
107 vacc1x89ABCDEF = _mm256_fmadd_ps(va1, vb89ABCDEFc2, vacc1x89ABCDEF);
108 vacc2x89ABCDEF = _mm256_fmadd_ps(va2, vb89ABCDEFc2, vacc2x89ABCDEF);
109
110 va0 = _mm256_permute_ps(va0, _MM_SHUFFLE(0, 3, 2, 1));
111 va1 = _mm256_permute_ps(va1, _MM_SHUFFLE(0, 3, 2, 1));
112 va2 = _mm256_permute_ps(va2, _MM_SHUFFLE(0, 3, 2, 1));
113
114 const __m256 vb01234567c3 = _mm256_load_ps(w + 48);
115 const __m256 vb89ABCDEFc3 = _mm256_load_ps(w + 56);
116
117 vacc0x01234567 = _mm256_fmadd_ps(va0, vb01234567c3, vacc0x01234567);
118 vacc1x01234567 = _mm256_fmadd_ps(va1, vb01234567c3, vacc1x01234567);
119 vacc2x01234567 = _mm256_fmadd_ps(va2, vb01234567c3, vacc2x01234567);
120 vacc0x89ABCDEF = _mm256_fmadd_ps(va0, vb89ABCDEFc3, vacc0x89ABCDEF);
121 vacc1x89ABCDEF = _mm256_fmadd_ps(va1, vb89ABCDEFc3, vacc1x89ABCDEF);
122 vacc2x89ABCDEF = _mm256_fmadd_ps(va2, vb89ABCDEFc3, vacc2x89ABCDEF);
123
124
125 w += 64;
126 k -= 4 * sizeof(float);
127 }
128 if XNN_UNLIKELY(k != 0) {
129 do {
130 const __m256 va0 = _mm256_broadcast_ss(a0);
131 a0 += 1;
132 const __m256 va1 = _mm256_broadcast_ss(a1);
133 a1 += 1;
134 const __m256 va2 = _mm256_broadcast_ss(a2);
135 a2 += 1;
136
137 const __m256 vb01234567 = _mm256_load_ps(w);
138 const __m256 vb89ABCDEF = _mm256_load_ps(w + 8);
139 w += 16;
140
141 vacc0x01234567 = _mm256_fmadd_ps(va0, vb01234567, vacc0x01234567);
142 vacc1x01234567 = _mm256_fmadd_ps(va1, vb01234567, vacc1x01234567);
143 vacc2x01234567 = _mm256_fmadd_ps(va2, vb01234567, vacc2x01234567);
144 vacc0x89ABCDEF = _mm256_fmadd_ps(va0, vb89ABCDEF, vacc0x89ABCDEF);
145 vacc1x89ABCDEF = _mm256_fmadd_ps(va1, vb89ABCDEF, vacc1x89ABCDEF);
146 vacc2x89ABCDEF = _mm256_fmadd_ps(va2, vb89ABCDEF, vacc2x89ABCDEF);
147
148 k -= sizeof(float);
149 } while (k != 0);
150 }
151
152 const __m256 vmax = _mm256_broadcast_ps((const __m128*) params->sse.max);
153 vacc0x01234567 = _mm256_min_ps(vacc0x01234567, vmax);
154 vacc1x01234567 = _mm256_min_ps(vacc1x01234567, vmax);
155 vacc2x01234567 = _mm256_min_ps(vacc2x01234567, vmax);
156 vacc0x89ABCDEF = _mm256_min_ps(vacc0x89ABCDEF, vmax);
157 vacc1x89ABCDEF = _mm256_min_ps(vacc1x89ABCDEF, vmax);
158 vacc2x89ABCDEF = _mm256_min_ps(vacc2x89ABCDEF, vmax);
159
160 const __m256 vmin = _mm256_broadcast_ps((const __m128*) params->sse.min);
161 vacc0x01234567 = _mm256_max_ps(vacc0x01234567, vmin);
162 vacc1x01234567 = _mm256_max_ps(vacc1x01234567, vmin);
163 vacc2x01234567 = _mm256_max_ps(vacc2x01234567, vmin);
164 vacc0x89ABCDEF = _mm256_max_ps(vacc0x89ABCDEF, vmin);
165 vacc1x89ABCDEF = _mm256_max_ps(vacc1x89ABCDEF, vmin);
166 vacc2x89ABCDEF = _mm256_max_ps(vacc2x89ABCDEF, vmin);
167
168 if XNN_LIKELY(nc >= 16) {
169 _mm256_storeu_ps(c2, vacc2x01234567);
170 _mm256_storeu_ps(c2 + 8, vacc2x89ABCDEF);
171 c2 = (float*) ((uintptr_t) c2 + cn_stride);
172 _mm256_storeu_ps(c1, vacc1x01234567);
173 _mm256_storeu_ps(c1 + 8, vacc1x89ABCDEF);
174 c1 = (float*) ((uintptr_t) c1 + cn_stride);
175 _mm256_storeu_ps(c0, vacc0x01234567);
176 _mm256_storeu_ps(c0 + 8, vacc0x89ABCDEF);
177 c0 = (float*) ((uintptr_t) c0 + cn_stride);
178
179 a2 = (const float*) ((uintptr_t) a2 - kc);
180 a1 = (const float*) ((uintptr_t) a1 - kc);
181 a0 = (const float*) ((uintptr_t) a0 - kc);
182
183 nc -= 16;
184 } else {
185 if (nc & 8) {
186 _mm256_storeu_ps(c2, vacc2x01234567);
187 _mm256_storeu_ps(c1, vacc1x01234567);
188 _mm256_storeu_ps(c0, vacc0x01234567);
189
190 vacc2x01234567 = vacc2x89ABCDEF;
191 vacc1x01234567 = vacc1x89ABCDEF;
192 vacc0x01234567 = vacc0x89ABCDEF;
193
194 c2 += 8;
195 c1 += 8;
196 c0 += 8;
197 }
198 __m128 vacc2x0123 = _mm256_castps256_ps128(vacc2x01234567);
199 __m128 vacc1x0123 = _mm256_castps256_ps128(vacc1x01234567);
200 __m128 vacc0x0123 = _mm256_castps256_ps128(vacc0x01234567);
201 if (nc & 4) {
202 _mm_storeu_ps(c2, vacc2x0123);
203 _mm_storeu_ps(c1, vacc1x0123);
204 _mm_storeu_ps(c0, vacc0x0123);
205
206 vacc2x0123 = _mm256_extractf128_ps(vacc2x01234567, 1);
207 vacc1x0123 = _mm256_extractf128_ps(vacc1x01234567, 1);
208 vacc0x0123 = _mm256_extractf128_ps(vacc0x01234567, 1);
209
210 c2 += 4;
211 c1 += 4;
212 c0 += 4;
213 }
214 if (nc & 2) {
215 _mm_storel_pi((__m64*) c2, vacc2x0123);
216 _mm_storel_pi((__m64*) c1, vacc1x0123);
217 _mm_storel_pi((__m64*) c0, vacc0x0123);
218
219 vacc2x0123 = _mm_movehl_ps(vacc2x0123, vacc2x0123);
220 vacc1x0123 = _mm_movehl_ps(vacc1x0123, vacc1x0123);
221 vacc0x0123 = _mm_movehl_ps(vacc0x0123, vacc0x0123);
222
223 c2 += 2;
224 c1 += 2;
225 c0 += 2;
226 }
227 if (nc & 1) {
228 _mm_store_ss(c2, vacc2x0123);
229 _mm_store_ss(c1, vacc1x0123);
230 _mm_store_ss(c0, vacc0x0123);
231 }
232
233 nc = 0;
234 }
235 } while (nc != 0);
236 }
237