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