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