1 // Auto-generated file. Do not edit!
2 // Template: src/f32-gemm/wasmsimd-s4.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2020 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 <wasm_simd128.h>
13
14 #include <xnnpack/gemm.h>
15
16
xnn_f32_gemm_minmax_ukernel_3x8s4__wasmsimd_x86(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_3x8s4__wasmsimd_x86(
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 v128_t vacc0x0123 = wasm_v128_load(w + 0);
55 v128_t vacc0x4567 = wasm_v128_load(w + 4);
56 v128_t vacc1x0123 = vacc0x0123;
57 v128_t vacc1x4567 = vacc0x4567;
58 v128_t vacc2x0123 = vacc0x0123;
59 v128_t vacc2x4567 = vacc0x4567;
60 w += 8;
61
62 size_t k = kc;
63 while (k >= 4 * sizeof(float)) {
64 v128_t va0 = wasm_v128_load(a0);
65 a0 += 4;
66 v128_t va1 = wasm_v128_load(a1);
67 a1 += 4;
68 v128_t va2 = wasm_v128_load(a2);
69 a2 += 4;
70
71
72 const v128_t vb0123c0 = wasm_v128_load(w + 0);
73 const v128_t vb4567c0 = wasm_v128_load(w + 4);
74
75 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c0));
76 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c0));
77 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c0));
78 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c0));
79 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c0));
80 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c0));
81
82 va0 = wasm_v32x4_shuffle(va0, va0, 1, 2, 3, 0);
83 va1 = wasm_v32x4_shuffle(va1, va1, 1, 2, 3, 0);
84 va2 = wasm_v32x4_shuffle(va2, va2, 1, 2, 3, 0);
85
86 const v128_t vb0123c1 = wasm_v128_load(w + 8);
87 const v128_t vb4567c1 = wasm_v128_load(w + 12);
88
89 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c1));
90 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c1));
91 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c1));
92 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c1));
93 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c1));
94 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c1));
95
96 va0 = wasm_v32x4_shuffle(va0, va0, 1, 2, 3, 0);
97 va1 = wasm_v32x4_shuffle(va1, va1, 1, 2, 3, 0);
98 va2 = wasm_v32x4_shuffle(va2, va2, 1, 2, 3, 0);
99
100 const v128_t vb0123c2 = wasm_v128_load(w + 16);
101 const v128_t vb4567c2 = wasm_v128_load(w + 20);
102
103 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c2));
104 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c2));
105 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c2));
106 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c2));
107 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c2));
108 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c2));
109
110 va0 = wasm_v32x4_shuffle(va0, va0, 1, 2, 3, 0);
111 va1 = wasm_v32x4_shuffle(va1, va1, 1, 2, 3, 0);
112 va2 = wasm_v32x4_shuffle(va2, va2, 1, 2, 3, 0);
113
114 const v128_t vb0123c3 = wasm_v128_load(w + 24);
115 const v128_t vb4567c3 = wasm_v128_load(w + 28);
116
117 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123c3));
118 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123c3));
119 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123c3));
120 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567c3));
121 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567c3));
122 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567c3));
123
124
125 w += 32;
126 k -= 4 * sizeof(float);
127 }
128 if XNN_UNLIKELY(k != 0) {
129 do {
130 const v128_t va0 = wasm_v32x4_load_splat(a0);
131 a0 += 1;
132 const v128_t va1 = wasm_v32x4_load_splat(a1);
133 a1 += 1;
134 const v128_t va2 = wasm_v32x4_load_splat(a2);
135 a2 += 1;
136
137 const v128_t vb0123 = wasm_v128_load(w);
138 const v128_t vb4567 = wasm_v128_load(w + 4);
139 w += 8;
140
141 vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123));
142 vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123));
143 vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123));
144 vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567));
145 vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567));
146 vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567));
147
148 k -= sizeof(float);
149 } while (k != 0);
150 }
151
152 const v128_t vmin = wasm_v32x4_load_splat(¶ms->scalar.min);
153 vacc0x0123 = wasm_v128_bitselect(vmin, vacc0x0123, wasm_f32x4_lt(vacc0x0123, vmin));
154 vacc1x0123 = wasm_v128_bitselect(vmin, vacc1x0123, wasm_f32x4_lt(vacc1x0123, vmin));
155 vacc2x0123 = wasm_v128_bitselect(vmin, vacc2x0123, wasm_f32x4_lt(vacc2x0123, vmin));
156 vacc0x4567 = wasm_v128_bitselect(vmin, vacc0x4567, wasm_f32x4_lt(vacc0x4567, vmin));
157 vacc1x4567 = wasm_v128_bitselect(vmin, vacc1x4567, wasm_f32x4_lt(vacc1x4567, vmin));
158 vacc2x4567 = wasm_v128_bitselect(vmin, vacc2x4567, wasm_f32x4_lt(vacc2x4567, vmin));
159
160 const v128_t vmax = wasm_v32x4_load_splat(¶ms->scalar.max);
161 vacc0x0123 = wasm_v128_bitselect(vacc0x0123, vmax, wasm_f32x4_le(vacc0x0123, vmax));
162 vacc1x0123 = wasm_v128_bitselect(vacc1x0123, vmax, wasm_f32x4_le(vacc1x0123, vmax));
163 vacc2x0123 = wasm_v128_bitselect(vacc2x0123, vmax, wasm_f32x4_le(vacc2x0123, vmax));
164 vacc0x4567 = wasm_v128_bitselect(vacc0x4567, vmax, wasm_f32x4_le(vacc0x4567, vmax));
165 vacc1x4567 = wasm_v128_bitselect(vacc1x4567, vmax, wasm_f32x4_le(vacc1x4567, vmax));
166 vacc2x4567 = wasm_v128_bitselect(vacc2x4567, vmax, wasm_f32x4_le(vacc2x4567, vmax));
167
168 if XNN_LIKELY(nc >= 8) {
169 wasm_v128_store(c2, vacc2x0123);
170 wasm_v128_store(c2 + 4, vacc2x4567);
171 c2 = (float*) ((uintptr_t) c2 + cn_stride);
172 wasm_v128_store(c1, vacc1x0123);
173 wasm_v128_store(c1 + 4, vacc1x4567);
174 c1 = (float*) ((uintptr_t) c1 + cn_stride);
175 wasm_v128_store(c0, vacc0x0123);
176 wasm_v128_store(c0 + 4, vacc0x4567);
177 c0 = (float*) ((uintptr_t) c0 + cn_stride);
178
179 a2 = (const float*) ((uintptr_t) a2 - kc);
180 a1 = (const float*) ((uintptr_t) a1 - kc);
181 a0 = (const float*) ((uintptr_t) a0 - kc);
182
183 nc -= 8;
184 } else {
185 if (nc & 4) {
186 wasm_v128_store(c2, vacc2x0123);
187 wasm_v128_store(c1, vacc1x0123);
188 wasm_v128_store(c0, vacc0x0123);
189
190 vacc2x0123 = vacc2x4567;
191 vacc1x0123 = vacc1x4567;
192 vacc0x0123 = vacc0x4567;
193
194 c2 += 4;
195 c1 += 4;
196 c0 += 4;
197 }
198 if (nc & 2) {
199 *((double*) c2) = wasm_f64x2_extract_lane(vacc2x0123, 0);
200 *((double*) c1) = wasm_f64x2_extract_lane(vacc1x0123, 0);
201 *((double*) c0) = wasm_f64x2_extract_lane(vacc0x0123, 0);
202
203 vacc2x0123 = wasm_v32x4_shuffle(vacc2x0123, vacc2x0123, 2, 3, 2, 3);
204 vacc1x0123 = wasm_v32x4_shuffle(vacc1x0123, vacc1x0123, 2, 3, 2, 3);
205 vacc0x0123 = wasm_v32x4_shuffle(vacc0x0123, vacc0x0123, 2, 3, 2, 3);
206
207 c2 += 2;
208 c1 += 2;
209 c0 += 2;
210 }
211 if (nc & 1) {
212 *c2 = wasm_f32x4_extract_lane(vacc2x0123, 0);
213 *c1 = wasm_f32x4_extract_lane(vacc1x0123, 0);
214 *c0 = wasm_f32x4_extract_lane(vacc0x0123, 0);
215 }
216
217 nc = 0;
218 }
219 } while (nc != 0);
220 }
221