1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-igemm/sse-dup.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_4x8__sse_dup(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_4x8__sse_dup(
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         const __m128 va0 = _mm_loadu_ps(a0);
95         a0 += 4;
96         const __m128 va1 = _mm_loadu_ps(a1);
97         a1 += 4;
98         const __m128 va2 = _mm_loadu_ps(a2);
99         a2 += 4;
100         const __m128 va3 = _mm_loadu_ps(a3);
101         a3 += 4;
102 
103 
104         const __m128 va0c0000 = _mm_shuffle_ps(va0, va0, _MM_SHUFFLE(0, 0, 0, 0));
105         const __m128 va1c0000 = _mm_shuffle_ps(va1, va1, _MM_SHUFFLE(0, 0, 0, 0));
106         const __m128 va2c0000 = _mm_shuffle_ps(va2, va2, _MM_SHUFFLE(0, 0, 0, 0));
107         const __m128 va3c0000 = _mm_shuffle_ps(va3, va3, _MM_SHUFFLE(0, 0, 0, 0));
108 
109         const __m128 vb0123c0 = _mm_load_ps(w + 0);
110         const __m128 vb4567c0 = _mm_load_ps(w + 4);
111 
112         vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0c0000, vb0123c0));
113         vacc1x0123 = _mm_add_ps(vacc1x0123, _mm_mul_ps(va1c0000, vb0123c0));
114         vacc2x0123 = _mm_add_ps(vacc2x0123, _mm_mul_ps(va2c0000, vb0123c0));
115         vacc3x0123 = _mm_add_ps(vacc3x0123, _mm_mul_ps(va3c0000, vb0123c0));
116         vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0c0000, vb4567c0));
117         vacc1x4567 = _mm_add_ps(vacc1x4567, _mm_mul_ps(va1c0000, vb4567c0));
118         vacc2x4567 = _mm_add_ps(vacc2x4567, _mm_mul_ps(va2c0000, vb4567c0));
119         vacc3x4567 = _mm_add_ps(vacc3x4567, _mm_mul_ps(va3c0000, vb4567c0));
120 
121         const __m128 va0c1111 = _mm_shuffle_ps(va0, va0, _MM_SHUFFLE(1, 1, 1, 1));
122         const __m128 va1c1111 = _mm_shuffle_ps(va1, va1, _MM_SHUFFLE(1, 1, 1, 1));
123         const __m128 va2c1111 = _mm_shuffle_ps(va2, va2, _MM_SHUFFLE(1, 1, 1, 1));
124         const __m128 va3c1111 = _mm_shuffle_ps(va3, va3, _MM_SHUFFLE(1, 1, 1, 1));
125 
126         const __m128 vb0123c1 = _mm_load_ps(w + 8);
127         const __m128 vb4567c1 = _mm_load_ps(w + 12);
128 
129         vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0c1111, vb0123c1));
130         vacc1x0123 = _mm_add_ps(vacc1x0123, _mm_mul_ps(va1c1111, vb0123c1));
131         vacc2x0123 = _mm_add_ps(vacc2x0123, _mm_mul_ps(va2c1111, vb0123c1));
132         vacc3x0123 = _mm_add_ps(vacc3x0123, _mm_mul_ps(va3c1111, vb0123c1));
133         vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0c1111, vb4567c1));
134         vacc1x4567 = _mm_add_ps(vacc1x4567, _mm_mul_ps(va1c1111, vb4567c1));
135         vacc2x4567 = _mm_add_ps(vacc2x4567, _mm_mul_ps(va2c1111, vb4567c1));
136         vacc3x4567 = _mm_add_ps(vacc3x4567, _mm_mul_ps(va3c1111, vb4567c1));
137 
138         const __m128 va0c2222 = _mm_shuffle_ps(va0, va0, _MM_SHUFFLE(2, 2, 2, 2));
139         const __m128 va1c2222 = _mm_shuffle_ps(va1, va1, _MM_SHUFFLE(2, 2, 2, 2));
140         const __m128 va2c2222 = _mm_shuffle_ps(va2, va2, _MM_SHUFFLE(2, 2, 2, 2));
141         const __m128 va3c2222 = _mm_shuffle_ps(va3, va3, _MM_SHUFFLE(2, 2, 2, 2));
142 
143         const __m128 vb0123c2 = _mm_load_ps(w + 16);
144         const __m128 vb4567c2 = _mm_load_ps(w + 20);
145 
146         vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0c2222, vb0123c2));
147         vacc1x0123 = _mm_add_ps(vacc1x0123, _mm_mul_ps(va1c2222, vb0123c2));
148         vacc2x0123 = _mm_add_ps(vacc2x0123, _mm_mul_ps(va2c2222, vb0123c2));
149         vacc3x0123 = _mm_add_ps(vacc3x0123, _mm_mul_ps(va3c2222, vb0123c2));
150         vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0c2222, vb4567c2));
151         vacc1x4567 = _mm_add_ps(vacc1x4567, _mm_mul_ps(va1c2222, vb4567c2));
152         vacc2x4567 = _mm_add_ps(vacc2x4567, _mm_mul_ps(va2c2222, vb4567c2));
153         vacc3x4567 = _mm_add_ps(vacc3x4567, _mm_mul_ps(va3c2222, vb4567c2));
154 
155         const __m128 va0c3333 = _mm_shuffle_ps(va0, va0, _MM_SHUFFLE(3, 3, 3, 3));
156         const __m128 va1c3333 = _mm_shuffle_ps(va1, va1, _MM_SHUFFLE(3, 3, 3, 3));
157         const __m128 va2c3333 = _mm_shuffle_ps(va2, va2, _MM_SHUFFLE(3, 3, 3, 3));
158         const __m128 va3c3333 = _mm_shuffle_ps(va3, va3, _MM_SHUFFLE(3, 3, 3, 3));
159 
160         const __m128 vb0123c3 = _mm_load_ps(w + 24);
161         const __m128 vb4567c3 = _mm_load_ps(w + 28);
162 
163         vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0c3333, vb0123c3));
164         vacc1x0123 = _mm_add_ps(vacc1x0123, _mm_mul_ps(va1c3333, vb0123c3));
165         vacc2x0123 = _mm_add_ps(vacc2x0123, _mm_mul_ps(va2c3333, vb0123c3));
166         vacc3x0123 = _mm_add_ps(vacc3x0123, _mm_mul_ps(va3c3333, vb0123c3));
167         vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0c3333, vb4567c3));
168         vacc1x4567 = _mm_add_ps(vacc1x4567, _mm_mul_ps(va1c3333, vb4567c3));
169         vacc2x4567 = _mm_add_ps(vacc2x4567, _mm_mul_ps(va2c3333, vb4567c3));
170         vacc3x4567 = _mm_add_ps(vacc3x4567, _mm_mul_ps(va3c3333, vb4567c3));
171 
172         w += 32;
173         k -= 4 * sizeof(float);
174       }
175       if XNN_UNLIKELY(k != 0) {
176         do {
177           const __m128 vb0123 = _mm_load_ps(w);
178           const __m128 vb4567 = _mm_load_ps(w + 4);
179           w += 8;
180 
181           const __m128 va0 = _mm_load1_ps(a0);
182           a0 += 1;
183           const __m128 va1 = _mm_load1_ps(a1);
184           a1 += 1;
185           const __m128 va2 = _mm_load1_ps(a2);
186           a2 += 1;
187           const __m128 va3 = _mm_load1_ps(a3);
188           a3 += 1;
189 
190           vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0, vb0123));
191           vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0, vb4567));
192           vacc1x0123 = _mm_add_ps(vacc1x0123, _mm_mul_ps(va1, vb0123));
193           vacc1x4567 = _mm_add_ps(vacc1x4567, _mm_mul_ps(va1, vb4567));
194           vacc2x0123 = _mm_add_ps(vacc2x0123, _mm_mul_ps(va2, vb0123));
195           vacc2x4567 = _mm_add_ps(vacc2x4567, _mm_mul_ps(va2, vb4567));
196           vacc3x0123 = _mm_add_ps(vacc3x0123, _mm_mul_ps(va3, vb0123));
197           vacc3x4567 = _mm_add_ps(vacc3x4567, _mm_mul_ps(va3, vb4567));
198           k -= sizeof(float);
199         } while (k != 0);
200       }
201       p -= 4 * sizeof(void*);
202     } while (p != 0);
203 
204     const __m128 vmax = _mm_load_ps(params->sse.max);
205     vacc0x0123 = _mm_min_ps(vacc0x0123, vmax);
206     vacc1x0123 = _mm_min_ps(vacc1x0123, vmax);
207     vacc2x0123 = _mm_min_ps(vacc2x0123, vmax);
208     vacc3x0123 = _mm_min_ps(vacc3x0123, vmax);
209     vacc0x4567 = _mm_min_ps(vacc0x4567, vmax);
210     vacc1x4567 = _mm_min_ps(vacc1x4567, vmax);
211     vacc2x4567 = _mm_min_ps(vacc2x4567, vmax);
212     vacc3x4567 = _mm_min_ps(vacc3x4567, vmax);
213 
214     const __m128 vmin = _mm_load_ps(params->sse.min);
215     vacc0x0123 = _mm_max_ps(vacc0x0123, vmin);
216     vacc1x0123 = _mm_max_ps(vacc1x0123, vmin);
217     vacc2x0123 = _mm_max_ps(vacc2x0123, vmin);
218     vacc3x0123 = _mm_max_ps(vacc3x0123, vmin);
219     vacc0x4567 = _mm_max_ps(vacc0x4567, vmin);
220     vacc1x4567 = _mm_max_ps(vacc1x4567, vmin);
221     vacc2x4567 = _mm_max_ps(vacc2x4567, vmin);
222     vacc3x4567 = _mm_max_ps(vacc3x4567, vmin);
223 
224     if XNN_LIKELY(nc >= 8) {
225       _mm_storeu_ps(c3, vacc3x0123);
226       _mm_storeu_ps(c3 + 4, vacc3x4567);
227       c3 = (float*) ((uintptr_t) c3 + cn_stride);
228       _mm_storeu_ps(c2, vacc2x0123);
229       _mm_storeu_ps(c2 + 4, vacc2x4567);
230       c2 = (float*) ((uintptr_t) c2 + cn_stride);
231       _mm_storeu_ps(c1, vacc1x0123);
232       _mm_storeu_ps(c1 + 4, vacc1x4567);
233       c1 = (float*) ((uintptr_t) c1 + cn_stride);
234       _mm_storeu_ps(c0, vacc0x0123);
235       _mm_storeu_ps(c0 + 4, vacc0x4567);
236       c0 = (float*) ((uintptr_t) c0 + cn_stride);
237 
238       a = (const float**restrict) ((uintptr_t) a - ks);
239       nc -= 8;
240     } else {
241       if (nc & 4) {
242         _mm_storeu_ps(c3, vacc3x0123);
243         _mm_storeu_ps(c2, vacc2x0123);
244         _mm_storeu_ps(c1, vacc1x0123);
245         _mm_storeu_ps(c0, vacc0x0123);
246 
247         vacc3x0123 = vacc3x4567;
248         vacc2x0123 = vacc2x4567;
249         vacc1x0123 = vacc1x4567;
250         vacc0x0123 = vacc0x4567;
251 
252         c3 += 4;
253         c2 += 4;
254         c1 += 4;
255         c0 += 4;
256       }
257       if (nc & 2) {
258         _mm_storel_pi((__m64*) c3, vacc3x0123);
259         _mm_storel_pi((__m64*) c2, vacc2x0123);
260         _mm_storel_pi((__m64*) c1, vacc1x0123);
261         _mm_storel_pi((__m64*) c0, vacc0x0123);
262 
263         vacc3x0123 = _mm_movehl_ps(vacc3x0123, vacc3x0123);
264         vacc2x0123 = _mm_movehl_ps(vacc2x0123, vacc2x0123);
265         vacc1x0123 = _mm_movehl_ps(vacc1x0123, vacc1x0123);
266         vacc0x0123 = _mm_movehl_ps(vacc0x0123, vacc0x0123);
267 
268         c3 += 2;
269         c2 += 2;
270         c1 += 2;
271         c0 += 2;
272       }
273       if (nc & 1) {
274         _mm_store_ss(c3, vacc3x0123);
275         _mm_store_ss(c2, vacc2x0123);
276         _mm_store_ss(c1, vacc1x0123);
277         _mm_store_ss(c0, vacc0x0123);
278       }
279 
280       nc = 0;
281     }
282   } while (nc != 0);
283 }
284