• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-gemm/sse-shuffle.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 <xmmintrin.h>
13 
14 #include <xnnpack/gemm.h>
15 
16 
xnn_f32_gemminc_ukernel_4x8s4__sse(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_4x8s4__sse(
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 <= 4);
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   const float* a3 = (const float*) ((uintptr_t) a2 + a_stride);
55   float* c3 = (float*) ((uintptr_t) c2 + cm_stride);
56   if XNN_UNPREDICTABLE(mr != 4) {
57     a3 = a2;
58     c3 = c2;
59   }
60 
61   do {
62     __m128 vacc0x0123 = _mm_load_ps(acc + 0);
63     __m128 vacc0x4567 = _mm_load_ps(acc + 4);
64     __m128 vacc1x0123 = _mm_load_ps(acc + 8);
65     __m128 vacc1x4567 = _mm_load_ps(acc + 12);
66     __m128 vacc2x0123 = _mm_load_ps(acc + 16);
67     __m128 vacc2x4567 = _mm_load_ps(acc + 20);
68     __m128 vacc3x0123 = _mm_load_ps(acc + 24);
69     __m128 vacc3x4567 = _mm_load_ps(acc + 28);
70     acc += 32;
71 
72     size_t k = kc;
73     while (k >= 4 * sizeof(float)) {
74       __m128 va0 = _mm_loadu_ps(a0);
75       a0 += 4;
76       __m128 va1 = _mm_loadu_ps(a1);
77       a1 += 4;
78       __m128 va2 = _mm_loadu_ps(a2);
79       a2 += 4;
80       __m128 va3 = _mm_loadu_ps(a3);
81       a3 += 4;
82 
83 
84       const __m128 vb0123c0 = _mm_load_ps(w + 0);
85       const __m128 vb4567c0 = _mm_load_ps(w + 4);
86 
87       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0, vb0123c0));
88       vacc1x0123 = _mm_add_ps(vacc1x0123, _mm_mul_ps(va1, vb0123c0));
89       vacc2x0123 = _mm_add_ps(vacc2x0123, _mm_mul_ps(va2, vb0123c0));
90       vacc3x0123 = _mm_add_ps(vacc3x0123, _mm_mul_ps(va3, vb0123c0));
91       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0, vb4567c0));
92       vacc1x4567 = _mm_add_ps(vacc1x4567, _mm_mul_ps(va1, vb4567c0));
93       vacc2x4567 = _mm_add_ps(vacc2x4567, _mm_mul_ps(va2, vb4567c0));
94       vacc3x4567 = _mm_add_ps(vacc3x4567, _mm_mul_ps(va3, vb4567c0));
95 
96       va0 = _mm_shuffle_ps(va0, va0, _MM_SHUFFLE(0, 3, 2, 1));
97       va1 = _mm_shuffle_ps(va1, va1, _MM_SHUFFLE(0, 3, 2, 1));
98       va2 = _mm_shuffle_ps(va2, va2, _MM_SHUFFLE(0, 3, 2, 1));
99       va3 = _mm_shuffle_ps(va3, va3, _MM_SHUFFLE(0, 3, 2, 1));
100 
101       const __m128 vb0123c1 = _mm_load_ps(w + 8);
102       const __m128 vb4567c1 = _mm_load_ps(w + 12);
103 
104       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0, vb0123c1));
105       vacc1x0123 = _mm_add_ps(vacc1x0123, _mm_mul_ps(va1, vb0123c1));
106       vacc2x0123 = _mm_add_ps(vacc2x0123, _mm_mul_ps(va2, vb0123c1));
107       vacc3x0123 = _mm_add_ps(vacc3x0123, _mm_mul_ps(va3, vb0123c1));
108       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0, vb4567c1));
109       vacc1x4567 = _mm_add_ps(vacc1x4567, _mm_mul_ps(va1, vb4567c1));
110       vacc2x4567 = _mm_add_ps(vacc2x4567, _mm_mul_ps(va2, vb4567c1));
111       vacc3x4567 = _mm_add_ps(vacc3x4567, _mm_mul_ps(va3, vb4567c1));
112 
113       va0 = _mm_shuffle_ps(va0, va0, _MM_SHUFFLE(0, 3, 2, 1));
114       va1 = _mm_shuffle_ps(va1, va1, _MM_SHUFFLE(0, 3, 2, 1));
115       va2 = _mm_shuffle_ps(va2, va2, _MM_SHUFFLE(0, 3, 2, 1));
116       va3 = _mm_shuffle_ps(va3, va3, _MM_SHUFFLE(0, 3, 2, 1));
117 
118       const __m128 vb0123c2 = _mm_load_ps(w + 16);
119       const __m128 vb4567c2 = _mm_load_ps(w + 20);
120 
121       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0, vb0123c2));
122       vacc1x0123 = _mm_add_ps(vacc1x0123, _mm_mul_ps(va1, vb0123c2));
123       vacc2x0123 = _mm_add_ps(vacc2x0123, _mm_mul_ps(va2, vb0123c2));
124       vacc3x0123 = _mm_add_ps(vacc3x0123, _mm_mul_ps(va3, vb0123c2));
125       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0, vb4567c2));
126       vacc1x4567 = _mm_add_ps(vacc1x4567, _mm_mul_ps(va1, vb4567c2));
127       vacc2x4567 = _mm_add_ps(vacc2x4567, _mm_mul_ps(va2, vb4567c2));
128       vacc3x4567 = _mm_add_ps(vacc3x4567, _mm_mul_ps(va3, vb4567c2));
129 
130       va0 = _mm_shuffle_ps(va0, va0, _MM_SHUFFLE(0, 3, 2, 1));
131       va1 = _mm_shuffle_ps(va1, va1, _MM_SHUFFLE(0, 3, 2, 1));
132       va2 = _mm_shuffle_ps(va2, va2, _MM_SHUFFLE(0, 3, 2, 1));
133       va3 = _mm_shuffle_ps(va3, va3, _MM_SHUFFLE(0, 3, 2, 1));
134 
135       const __m128 vb0123c3 = _mm_load_ps(w + 24);
136       const __m128 vb4567c3 = _mm_load_ps(w + 28);
137 
138       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0, vb0123c3));
139       vacc1x0123 = _mm_add_ps(vacc1x0123, _mm_mul_ps(va1, vb0123c3));
140       vacc2x0123 = _mm_add_ps(vacc2x0123, _mm_mul_ps(va2, vb0123c3));
141       vacc3x0123 = _mm_add_ps(vacc3x0123, _mm_mul_ps(va3, vb0123c3));
142       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0, vb4567c3));
143       vacc1x4567 = _mm_add_ps(vacc1x4567, _mm_mul_ps(va1, vb4567c3));
144       vacc2x4567 = _mm_add_ps(vacc2x4567, _mm_mul_ps(va2, vb4567c3));
145       vacc3x4567 = _mm_add_ps(vacc3x4567, _mm_mul_ps(va3, vb4567c3));
146 
147 
148       w += 32;
149       k -= 4 * sizeof(float);
150     }
151     if XNN_UNLIKELY(k != 0) {
152       do {
153         const __m128 va0 = _mm_load1_ps(a0);
154         a0 += 1;
155         const __m128 va1 = _mm_load1_ps(a1);
156         a1 += 1;
157         const __m128 va2 = _mm_load1_ps(a2);
158         a2 += 1;
159         const __m128 va3 = _mm_load1_ps(a3);
160         a3 += 1;
161 
162         const __m128 vb0123 = _mm_load_ps(w);
163         const __m128 vb4567 = _mm_load_ps(w + 4);
164         w += 8;
165 
166         vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0, vb0123));
167         vacc1x0123 = _mm_add_ps(vacc1x0123, _mm_mul_ps(va1, vb0123));
168         vacc2x0123 = _mm_add_ps(vacc2x0123, _mm_mul_ps(va2, vb0123));
169         vacc3x0123 = _mm_add_ps(vacc3x0123, _mm_mul_ps(va3, vb0123));
170         vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0, vb4567));
171         vacc1x4567 = _mm_add_ps(vacc1x4567, _mm_mul_ps(va1, vb4567));
172         vacc2x4567 = _mm_add_ps(vacc2x4567, _mm_mul_ps(va2, vb4567));
173         vacc3x4567 = _mm_add_ps(vacc3x4567, _mm_mul_ps(va3, vb4567));
174 
175         k -= sizeof(float);
176       } while (k != 0);
177     }
178 
179     const __m128 vmax = _mm_load_ps(params->sse.max);
180     vacc0x0123 = _mm_min_ps(vacc0x0123, vmax);
181     vacc1x0123 = _mm_min_ps(vacc1x0123, vmax);
182     vacc2x0123 = _mm_min_ps(vacc2x0123, vmax);
183     vacc3x0123 = _mm_min_ps(vacc3x0123, vmax);
184     vacc0x4567 = _mm_min_ps(vacc0x4567, vmax);
185     vacc1x4567 = _mm_min_ps(vacc1x4567, vmax);
186     vacc2x4567 = _mm_min_ps(vacc2x4567, vmax);
187     vacc3x4567 = _mm_min_ps(vacc3x4567, vmax);
188 
189     const __m128 vmin = _mm_load_ps(params->sse.min);
190     vacc0x0123 = _mm_max_ps(vacc0x0123, vmin);
191     vacc1x0123 = _mm_max_ps(vacc1x0123, vmin);
192     vacc2x0123 = _mm_max_ps(vacc2x0123, vmin);
193     vacc3x0123 = _mm_max_ps(vacc3x0123, vmin);
194     vacc0x4567 = _mm_max_ps(vacc0x4567, vmin);
195     vacc1x4567 = _mm_max_ps(vacc1x4567, vmin);
196     vacc2x4567 = _mm_max_ps(vacc2x4567, vmin);
197     vacc3x4567 = _mm_max_ps(vacc3x4567, vmin);
198 
199     if XNN_LIKELY(nc >= 8) {
200       _mm_storeu_ps(c3, vacc3x0123);
201       _mm_storeu_ps(c3 + 4, vacc3x4567);
202       c3 = (float*) ((uintptr_t) c3 + cn_stride);
203       _mm_storeu_ps(c2, vacc2x0123);
204       _mm_storeu_ps(c2 + 4, vacc2x4567);
205       c2 = (float*) ((uintptr_t) c2 + cn_stride);
206       _mm_storeu_ps(c1, vacc1x0123);
207       _mm_storeu_ps(c1 + 4, vacc1x4567);
208       c1 = (float*) ((uintptr_t) c1 + cn_stride);
209       _mm_storeu_ps(c0, vacc0x0123);
210       _mm_storeu_ps(c0 + 4, vacc0x4567);
211       c0 = (float*) ((uintptr_t) c0 + cn_stride);
212 
213       a3 = (const float*) ((uintptr_t) a3 - kc);
214       a2 = (const float*) ((uintptr_t) a2 - kc);
215       a1 = (const float*) ((uintptr_t) a1 - kc);
216       a0 = (const float*) ((uintptr_t) a0 - kc);
217 
218       nc -= 8;
219     } else {
220       if (nc & 4) {
221         _mm_storeu_ps(c3, vacc3x0123);
222         _mm_storeu_ps(c2, vacc2x0123);
223         _mm_storeu_ps(c1, vacc1x0123);
224         _mm_storeu_ps(c0, vacc0x0123);
225 
226         vacc3x0123 = vacc3x4567;
227         vacc2x0123 = vacc2x4567;
228         vacc1x0123 = vacc1x4567;
229         vacc0x0123 = vacc0x4567;
230 
231         c3 += 4;
232         c2 += 4;
233         c1 += 4;
234         c0 += 4;
235       }
236       if (nc & 2) {
237         _mm_storel_pi((__m64*) c3, vacc3x0123);
238         _mm_storel_pi((__m64*) c2, vacc2x0123);
239         _mm_storel_pi((__m64*) c1, vacc1x0123);
240         _mm_storel_pi((__m64*) c0, vacc0x0123);
241 
242         vacc3x0123 = _mm_movehl_ps(vacc3x0123, vacc3x0123);
243         vacc2x0123 = _mm_movehl_ps(vacc2x0123, vacc2x0123);
244         vacc1x0123 = _mm_movehl_ps(vacc1x0123, vacc1x0123);
245         vacc0x0123 = _mm_movehl_ps(vacc0x0123, vacc0x0123);
246 
247         c3 += 2;
248         c2 += 2;
249         c1 += 2;
250         c0 += 2;
251       }
252       if (nc & 1) {
253         _mm_store_ss(c3, vacc3x0123);
254         _mm_store_ss(c2, vacc2x0123);
255         _mm_store_ss(c1, vacc1x0123);
256         _mm_store_ss(c0, vacc0x0123);
257       }
258 
259       nc = 0;
260     }
261   } while (nc != 0);
262 }
263