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