1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-gemm/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 <emmintrin.h>
13 
14 #include <xnnpack/gemm.h>
15 
16 
xnn_f32_gemm_minmax_ukernel_3x8__sse2_dup(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 union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_f32_gemm_minmax_ukernel_3x8__sse2_dup(
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 union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
28 {
29   assert(mr != 0);
30   assert(mr <= 3);
31   assert(nc != 0);
32   assert(kc != 0);
33   assert(kc % sizeof(float) == 0);
34   assert(a != NULL);
35   assert(w != NULL);
36   assert(c != NULL);
37 
38   const float* a0 = a;
39   float* c0 = c;
40   const float* a1 = (const float*) ((uintptr_t) a0 + a_stride);
41   float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
42   if XNN_UNPREDICTABLE(mr < 2) {
43     a1 = a0;
44     c1 = c0;
45   }
46   const float* a2 = (const float*) ((uintptr_t) a1 + a_stride);
47   float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
48   if XNN_UNPREDICTABLE(mr <= 2) {
49     a2 = a1;
50     c2 = c1;
51   }
52 
53   do {
54     __m128 vacc0x0123 = _mm_load_ps(w + 0);
55     __m128 vacc0x4567 = _mm_load_ps(w + 4);
56     __m128 vacc1x0123 = vacc0x0123;
57     __m128 vacc1x4567 = vacc0x4567;
58     __m128 vacc2x0123 = vacc0x0123;
59     __m128 vacc2x4567 = vacc0x4567;
60     w += 8;
61 
62     size_t k = kc;
63     while (k >= 4 * sizeof(float)) {
64       const __m128 va0 = _mm_loadu_ps(a0);
65       a0 += 4;
66       const __m128 va1 = _mm_loadu_ps(a1);
67       a1 += 4;
68       const __m128 va2 = _mm_loadu_ps(a2);
69       a2 += 4;
70 
71 
72       const __m128 va0c0000 = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(va0), _MM_SHUFFLE(0, 0, 0, 0)));
73       const __m128 va1c0000 = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(va1), _MM_SHUFFLE(0, 0, 0, 0)));
74       const __m128 va2c0000 = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(va2), _MM_SHUFFLE(0, 0, 0, 0)));
75 
76       const __m128 vb0123c0 = _mm_load_ps(w + 0);
77       const __m128 vb4567c0 = _mm_load_ps(w + 4);
78 
79       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0c0000, vb0123c0));
80       vacc1x0123 = _mm_add_ps(vacc1x0123, _mm_mul_ps(va1c0000, vb0123c0));
81       vacc2x0123 = _mm_add_ps(vacc2x0123, _mm_mul_ps(va2c0000, vb0123c0));
82       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0c0000, vb4567c0));
83       vacc1x4567 = _mm_add_ps(vacc1x4567, _mm_mul_ps(va1c0000, vb4567c0));
84       vacc2x4567 = _mm_add_ps(vacc2x4567, _mm_mul_ps(va2c0000, vb4567c0));
85 
86       const __m128 va0c1111 = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(va0), _MM_SHUFFLE(1, 1, 1, 1)));
87       const __m128 va1c1111 = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(va1), _MM_SHUFFLE(1, 1, 1, 1)));
88       const __m128 va2c1111 = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(va2), _MM_SHUFFLE(1, 1, 1, 1)));
89 
90       const __m128 vb0123c1 = _mm_load_ps(w + 8);
91       const __m128 vb4567c1 = _mm_load_ps(w + 12);
92 
93       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0c1111, vb0123c1));
94       vacc1x0123 = _mm_add_ps(vacc1x0123, _mm_mul_ps(va1c1111, vb0123c1));
95       vacc2x0123 = _mm_add_ps(vacc2x0123, _mm_mul_ps(va2c1111, vb0123c1));
96       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0c1111, vb4567c1));
97       vacc1x4567 = _mm_add_ps(vacc1x4567, _mm_mul_ps(va1c1111, vb4567c1));
98       vacc2x4567 = _mm_add_ps(vacc2x4567, _mm_mul_ps(va2c1111, vb4567c1));
99 
100       const __m128 va0c2222 = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(va0), _MM_SHUFFLE(2, 2, 2, 2)));
101       const __m128 va1c2222 = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(va1), _MM_SHUFFLE(2, 2, 2, 2)));
102       const __m128 va2c2222 = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(va2), _MM_SHUFFLE(2, 2, 2, 2)));
103 
104       const __m128 vb0123c2 = _mm_load_ps(w + 16);
105       const __m128 vb4567c2 = _mm_load_ps(w + 20);
106 
107       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0c2222, vb0123c2));
108       vacc1x0123 = _mm_add_ps(vacc1x0123, _mm_mul_ps(va1c2222, vb0123c2));
109       vacc2x0123 = _mm_add_ps(vacc2x0123, _mm_mul_ps(va2c2222, vb0123c2));
110       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0c2222, vb4567c2));
111       vacc1x4567 = _mm_add_ps(vacc1x4567, _mm_mul_ps(va1c2222, vb4567c2));
112       vacc2x4567 = _mm_add_ps(vacc2x4567, _mm_mul_ps(va2c2222, vb4567c2));
113 
114       const __m128 va0c3333 = _mm_shuffle_ps(va0, va0, _MM_SHUFFLE(3, 3, 3, 3));
115       const __m128 va1c3333 = _mm_shuffle_ps(va1, va1, _MM_SHUFFLE(3, 3, 3, 3));
116       const __m128 va2c3333 = _mm_shuffle_ps(va2, va2, _MM_SHUFFLE(3, 3, 3, 3));
117 
118       const __m128 vb0123c3 = _mm_load_ps(w + 24);
119       const __m128 vb4567c3 = _mm_load_ps(w + 28);
120 
121       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0c3333, vb0123c3));
122       vacc1x0123 = _mm_add_ps(vacc1x0123, _mm_mul_ps(va1c3333, vb0123c3));
123       vacc2x0123 = _mm_add_ps(vacc2x0123, _mm_mul_ps(va2c3333, vb0123c3));
124       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0c3333, vb4567c3));
125       vacc1x4567 = _mm_add_ps(vacc1x4567, _mm_mul_ps(va1c3333, vb4567c3));
126       vacc2x4567 = _mm_add_ps(vacc2x4567, _mm_mul_ps(va2c3333, vb4567c3));
127 
128       w += 32;
129       k -= 4 * sizeof(float);
130     }
131     if XNN_UNLIKELY(k != 0) {
132       do {
133         const __m128 va0 = _mm_load1_ps(a0);
134         a0 += 1;
135         const __m128 va1 = _mm_load1_ps(a1);
136         a1 += 1;
137         const __m128 va2 = _mm_load1_ps(a2);
138         a2 += 1;
139 
140         const __m128 vb0123 = _mm_load_ps(w);
141         const __m128 vb4567 = _mm_load_ps(w + 4);
142         w += 8;
143 
144         vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0, vb0123));
145         vacc1x0123 = _mm_add_ps(vacc1x0123, _mm_mul_ps(va1, vb0123));
146         vacc2x0123 = _mm_add_ps(vacc2x0123, _mm_mul_ps(va2, vb0123));
147         vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0, vb4567));
148         vacc1x4567 = _mm_add_ps(vacc1x4567, _mm_mul_ps(va1, vb4567));
149         vacc2x4567 = _mm_add_ps(vacc2x4567, _mm_mul_ps(va2, vb4567));
150 
151         k -= sizeof(float);
152       } while (k != 0);
153     }
154 
155     const __m128 vmax = _mm_load_ps(params->sse.max);
156     vacc0x0123 = _mm_min_ps(vacc0x0123, vmax);
157     vacc1x0123 = _mm_min_ps(vacc1x0123, vmax);
158     vacc2x0123 = _mm_min_ps(vacc2x0123, vmax);
159     vacc0x4567 = _mm_min_ps(vacc0x4567, vmax);
160     vacc1x4567 = _mm_min_ps(vacc1x4567, vmax);
161     vacc2x4567 = _mm_min_ps(vacc2x4567, vmax);
162 
163     const __m128 vmin = _mm_load_ps(params->sse.min);
164     vacc0x0123 = _mm_max_ps(vacc0x0123, vmin);
165     vacc1x0123 = _mm_max_ps(vacc1x0123, vmin);
166     vacc2x0123 = _mm_max_ps(vacc2x0123, vmin);
167     vacc0x4567 = _mm_max_ps(vacc0x4567, vmin);
168     vacc1x4567 = _mm_max_ps(vacc1x4567, vmin);
169     vacc2x4567 = _mm_max_ps(vacc2x4567, vmin);
170 
171     if XNN_LIKELY(nc >= 8) {
172       _mm_storeu_ps(c2, vacc2x0123);
173       _mm_storeu_ps(c2 + 4, vacc2x4567);
174       c2 = (float*) ((uintptr_t) c2 + cn_stride);
175       _mm_storeu_ps(c1, vacc1x0123);
176       _mm_storeu_ps(c1 + 4, vacc1x4567);
177       c1 = (float*) ((uintptr_t) c1 + cn_stride);
178       _mm_storeu_ps(c0, vacc0x0123);
179       _mm_storeu_ps(c0 + 4, vacc0x4567);
180       c0 = (float*) ((uintptr_t) c0 + cn_stride);
181 
182       a2 = (const float*) ((uintptr_t) a2 - kc);
183       a1 = (const float*) ((uintptr_t) a1 - kc);
184       a0 = (const float*) ((uintptr_t) a0 - kc);
185 
186       nc -= 8;
187     } else {
188       if (nc & 4) {
189         _mm_storeu_ps(c2, vacc2x0123);
190         _mm_storeu_ps(c1, vacc1x0123);
191         _mm_storeu_ps(c0, vacc0x0123);
192 
193         vacc2x0123 = vacc2x4567;
194         vacc1x0123 = vacc1x4567;
195         vacc0x0123 = vacc0x4567;
196 
197         c2 += 4;
198         c1 += 4;
199         c0 += 4;
200       }
201       if (nc & 2) {
202         _mm_storel_pi((__m64*) c2, vacc2x0123);
203         _mm_storel_pi((__m64*) c1, vacc1x0123);
204         _mm_storel_pi((__m64*) c0, vacc0x0123);
205 
206         vacc2x0123 = _mm_movehl_ps(vacc2x0123, vacc2x0123);
207         vacc1x0123 = _mm_movehl_ps(vacc1x0123, vacc1x0123);
208         vacc0x0123 = _mm_movehl_ps(vacc0x0123, vacc0x0123);
209 
210         c2 += 2;
211         c1 += 2;
212         c0 += 2;
213       }
214       if (nc & 1) {
215         _mm_store_ss(c2, vacc2x0123);
216         _mm_store_ss(c1, vacc1x0123);
217         _mm_store_ss(c0, vacc0x0123);
218       }
219 
220       nc = 0;
221     }
222   } while (nc != 0);
223 }
224