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