• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-igemm/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/igemm.h>
15 
16 
xnn_f32_igemm_ukernel_3x16s4__fma3_broadcast(size_t mr,size_t nc,size_t kc,size_t ks,const float ** restrict a,const float * restrict w,float * restrict c,size_t cm_stride,size_t cn_stride,size_t a_offset,const float * zero,const union xnn_f32_output_params params[restrict static1])17 void xnn_f32_igemm_ukernel_3x16s4__fma3_broadcast(
18     size_t mr,
19     size_t nc,
20     size_t kc,
21     size_t ks,
22     const float**restrict a,
23     const float*restrict w,
24     float*restrict c,
25     size_t cm_stride,
26     size_t cn_stride,
27     size_t a_offset,
28     const float* zero,
29     const union xnn_f32_output_params params[restrict static 1])
30 {
31   assert(mr != 0);
32   assert(mr <= 3);
33   assert(nc != 0);
34   assert(kc != 0);
35   assert(kc % sizeof(float) == 0);
36   assert(ks != 0);
37   assert(ks % (3 * sizeof(void*)) == 0);
38   assert(a_offset % sizeof(float) == 0);
39   assert(a != NULL);
40   assert(w != NULL);
41   assert(c != NULL);
42 
43   float* c0 = c;
44   float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
45   if XNN_UNPREDICTABLE(mr < 2) {
46     c1 = c0;
47   }
48   float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
49   if XNN_UNPREDICTABLE(mr <= 2) {
50     c2 = c1;
51   }
52 
53   do {
54     __m256 vacc0x01234567 = _mm256_load_ps(w);
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 p = ks;
63     do {
64       const float* restrict a0 = a[0];
65       assert(a0 != NULL);
66       if XNN_UNPREDICTABLE(a0 != zero) {
67         a0 = (const float*) ((uintptr_t) a0 + a_offset);
68       }
69       const float* restrict a1 = a[1];
70       assert(a1 != NULL);
71       if XNN_UNPREDICTABLE(a1 != zero) {
72         a1 = (const float*) ((uintptr_t) a1 + a_offset);
73       }
74       const float* restrict a2 = a[2];
75       assert(a2 != NULL);
76       if XNN_UNPREDICTABLE(a2 != zero) {
77         a2 = (const float*) ((uintptr_t) a2 + a_offset);
78       }
79       a += 3;
80 
81       size_t k = kc;
82       while (k >= 4 * sizeof(float)) {
83         __m256 va0 = _mm256_broadcast_ps((const __m128*) a0);
84         a0 += 4;
85         __m256 va1 = _mm256_broadcast_ps((const __m128*) a1);
86         a1 += 4;
87         __m256 va2 = _mm256_broadcast_ps((const __m128*) a2);
88         a2 += 4;
89 
90 
91         const __m256 vb01234567c0 = _mm256_load_ps(w + 0);
92         const __m256 vb89ABCDEFc0 = _mm256_load_ps(w + 8);
93 
94         vacc0x01234567 = _mm256_fmadd_ps(va0, vb01234567c0, vacc0x01234567);
95         vacc1x01234567 = _mm256_fmadd_ps(va1, vb01234567c0, vacc1x01234567);
96         vacc2x01234567 = _mm256_fmadd_ps(va2, vb01234567c0, vacc2x01234567);
97         vacc0x89ABCDEF = _mm256_fmadd_ps(va0, vb89ABCDEFc0, vacc0x89ABCDEF);
98         vacc1x89ABCDEF = _mm256_fmadd_ps(va1, vb89ABCDEFc0, vacc1x89ABCDEF);
99         vacc2x89ABCDEF = _mm256_fmadd_ps(va2, vb89ABCDEFc0, vacc2x89ABCDEF);
100 
101         va0 = _mm256_permute_ps(va0, _MM_SHUFFLE(0, 3, 2, 1));
102         va1 = _mm256_permute_ps(va1, _MM_SHUFFLE(0, 3, 2, 1));
103         va2 = _mm256_permute_ps(va2, _MM_SHUFFLE(0, 3, 2, 1));
104 
105         const __m256 vb01234567c1 = _mm256_load_ps(w + 16);
106         const __m256 vb89ABCDEFc1 = _mm256_load_ps(w + 24);
107 
108         vacc0x01234567 = _mm256_fmadd_ps(va0, vb01234567c1, vacc0x01234567);
109         vacc1x01234567 = _mm256_fmadd_ps(va1, vb01234567c1, vacc1x01234567);
110         vacc2x01234567 = _mm256_fmadd_ps(va2, vb01234567c1, vacc2x01234567);
111         vacc0x89ABCDEF = _mm256_fmadd_ps(va0, vb89ABCDEFc1, vacc0x89ABCDEF);
112         vacc1x89ABCDEF = _mm256_fmadd_ps(va1, vb89ABCDEFc1, vacc1x89ABCDEF);
113         vacc2x89ABCDEF = _mm256_fmadd_ps(va2, vb89ABCDEFc1, vacc2x89ABCDEF);
114 
115         va0 = _mm256_permute_ps(va0, _MM_SHUFFLE(0, 3, 2, 1));
116         va1 = _mm256_permute_ps(va1, _MM_SHUFFLE(0, 3, 2, 1));
117         va2 = _mm256_permute_ps(va2, _MM_SHUFFLE(0, 3, 2, 1));
118 
119         const __m256 vb01234567c2 = _mm256_load_ps(w + 32);
120         const __m256 vb89ABCDEFc2 = _mm256_load_ps(w + 40);
121 
122         vacc0x01234567 = _mm256_fmadd_ps(va0, vb01234567c2, vacc0x01234567);
123         vacc1x01234567 = _mm256_fmadd_ps(va1, vb01234567c2, vacc1x01234567);
124         vacc2x01234567 = _mm256_fmadd_ps(va2, vb01234567c2, vacc2x01234567);
125         vacc0x89ABCDEF = _mm256_fmadd_ps(va0, vb89ABCDEFc2, vacc0x89ABCDEF);
126         vacc1x89ABCDEF = _mm256_fmadd_ps(va1, vb89ABCDEFc2, vacc1x89ABCDEF);
127         vacc2x89ABCDEF = _mm256_fmadd_ps(va2, vb89ABCDEFc2, vacc2x89ABCDEF);
128 
129         va0 = _mm256_permute_ps(va0, _MM_SHUFFLE(0, 3, 2, 1));
130         va1 = _mm256_permute_ps(va1, _MM_SHUFFLE(0, 3, 2, 1));
131         va2 = _mm256_permute_ps(va2, _MM_SHUFFLE(0, 3, 2, 1));
132 
133         const __m256 vb01234567c3 = _mm256_load_ps(w + 48);
134         const __m256 vb89ABCDEFc3 = _mm256_load_ps(w + 56);
135 
136         vacc0x01234567 = _mm256_fmadd_ps(va0, vb01234567c3, vacc0x01234567);
137         vacc1x01234567 = _mm256_fmadd_ps(va1, vb01234567c3, vacc1x01234567);
138         vacc2x01234567 = _mm256_fmadd_ps(va2, vb01234567c3, vacc2x01234567);
139         vacc0x89ABCDEF = _mm256_fmadd_ps(va0, vb89ABCDEFc3, vacc0x89ABCDEF);
140         vacc1x89ABCDEF = _mm256_fmadd_ps(va1, vb89ABCDEFc3, vacc1x89ABCDEF);
141         vacc2x89ABCDEF = _mm256_fmadd_ps(va2, vb89ABCDEFc3, vacc2x89ABCDEF);
142 
143 
144         w += 64;
145         k -= 4 * sizeof(float);
146       }
147       if XNN_UNLIKELY(k != 0) {
148         do {
149           const __m256 va0 = _mm256_broadcast_ss(a0);
150           a0 += 1;
151           const __m256 va1 = _mm256_broadcast_ss(a1);
152           a1 += 1;
153           const __m256 va2 = _mm256_broadcast_ss(a2);
154           a2 += 1;
155 
156           const __m256 vb01234567 = _mm256_load_ps(w);
157           const __m256 vb89ABCDEF = _mm256_load_ps(w + 8);
158           w += 16;
159 
160           vacc0x01234567 = _mm256_fmadd_ps(va0, vb01234567, vacc0x01234567);
161           vacc1x01234567 = _mm256_fmadd_ps(va1, vb01234567, vacc1x01234567);
162           vacc2x01234567 = _mm256_fmadd_ps(va2, vb01234567, vacc2x01234567);
163           vacc0x89ABCDEF = _mm256_fmadd_ps(va0, vb89ABCDEF, vacc0x89ABCDEF);
164           vacc1x89ABCDEF = _mm256_fmadd_ps(va1, vb89ABCDEF, vacc1x89ABCDEF);
165           vacc2x89ABCDEF = _mm256_fmadd_ps(va2, vb89ABCDEF, vacc2x89ABCDEF);
166 
167           k -= sizeof(float);
168         } while (k != 0);
169       }
170       p -= 3 * sizeof(void*);
171     } while (p != 0);
172 
173     const __m256 vmax = _mm256_broadcast_ps((const __m128*) params->sse.max);
174     vacc0x01234567 = _mm256_min_ps(vacc0x01234567, vmax);
175     vacc1x01234567 = _mm256_min_ps(vacc1x01234567, vmax);
176     vacc2x01234567 = _mm256_min_ps(vacc2x01234567, vmax);
177     vacc0x89ABCDEF = _mm256_min_ps(vacc0x89ABCDEF, vmax);
178     vacc1x89ABCDEF = _mm256_min_ps(vacc1x89ABCDEF, vmax);
179     vacc2x89ABCDEF = _mm256_min_ps(vacc2x89ABCDEF, vmax);
180 
181     const __m256 vmin = _mm256_broadcast_ps((const __m128*) params->sse.min);
182     vacc0x01234567 = _mm256_max_ps(vacc0x01234567, vmin);
183     vacc1x01234567 = _mm256_max_ps(vacc1x01234567, vmin);
184     vacc2x01234567 = _mm256_max_ps(vacc2x01234567, vmin);
185     vacc0x89ABCDEF = _mm256_max_ps(vacc0x89ABCDEF, vmin);
186     vacc1x89ABCDEF = _mm256_max_ps(vacc1x89ABCDEF, vmin);
187     vacc2x89ABCDEF = _mm256_max_ps(vacc2x89ABCDEF, vmin);
188 
189     if XNN_LIKELY(nc >= 16) {
190       _mm256_storeu_ps(c2, vacc2x01234567);
191       _mm256_storeu_ps(c2 + 8, vacc2x89ABCDEF);
192       c2 = (float*) ((uintptr_t) c2 + cn_stride);
193       _mm256_storeu_ps(c1, vacc1x01234567);
194       _mm256_storeu_ps(c1 + 8, vacc1x89ABCDEF);
195       c1 = (float*) ((uintptr_t) c1 + cn_stride);
196       _mm256_storeu_ps(c0, vacc0x01234567);
197       _mm256_storeu_ps(c0 + 8, vacc0x89ABCDEF);
198       c0 = (float*) ((uintptr_t) c0 + cn_stride);
199 
200       a = (const float**restrict) ((uintptr_t) a - ks);
201       nc -= 16;
202     } else {
203       if (nc & 8) {
204         _mm256_storeu_ps(c2, vacc2x01234567);
205         _mm256_storeu_ps(c1, vacc1x01234567);
206         _mm256_storeu_ps(c0, vacc0x01234567);
207 
208         vacc2x01234567 = vacc2x89ABCDEF;
209         vacc1x01234567 = vacc1x89ABCDEF;
210         vacc0x01234567 = vacc0x89ABCDEF;
211 
212         c2 += 8;
213         c1 += 8;
214         c0 += 8;
215       }
216       __m128 vacc2x0123 = _mm256_castps256_ps128(vacc2x01234567);
217       __m128 vacc1x0123 = _mm256_castps256_ps128(vacc1x01234567);
218       __m128 vacc0x0123 = _mm256_castps256_ps128(vacc0x01234567);
219       if (nc & 4) {
220         _mm_storeu_ps(c2, vacc2x0123);
221         _mm_storeu_ps(c1, vacc1x0123);
222         _mm_storeu_ps(c0, vacc0x0123);
223 
224         vacc2x0123 = _mm256_extractf128_ps(vacc2x01234567, 1);
225         vacc1x0123 = _mm256_extractf128_ps(vacc1x01234567, 1);
226         vacc0x0123 = _mm256_extractf128_ps(vacc0x01234567, 1);
227 
228         c2 += 4;
229         c1 += 4;
230         c0 += 4;
231       }
232       if (nc & 2) {
233         _mm_storel_pi((__m64*) c2, vacc2x0123);
234         _mm_storel_pi((__m64*) c1, vacc1x0123);
235         _mm_storel_pi((__m64*) c0, vacc0x0123);
236 
237         vacc2x0123 = _mm_movehl_ps(vacc2x0123, vacc2x0123);
238         vacc1x0123 = _mm_movehl_ps(vacc1x0123, vacc1x0123);
239         vacc0x0123 = _mm_movehl_ps(vacc0x0123, vacc0x0123);
240 
241         c2 += 2;
242         c1 += 2;
243         c0 += 2;
244       }
245       if (nc & 1) {
246         _mm_store_ss(c2, vacc2x0123);
247         _mm_store_ss(c1, vacc1x0123);
248         _mm_store_ss(c0, vacc0x0123);
249       }
250 
251       nc = 0;
252     }
253   } while (nc != 0);
254 }
255