1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-gemm/wasmsimd-loadsplat.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_4x8__wasmsimd_x86_loadsplat(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_4x8__wasmsimd_x86_loadsplat(
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 <= 4);
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   const float* a3 = (const float*) ((uintptr_t) a2 + a_stride);
55   float* c3 = (float*) ((uintptr_t) c2 + cm_stride);
56   if XNN_UNPREDICTABLE(mr != 4) {
57     a3 = a2;
58     c3 = c2;
59   }
60 
61   do {
62     v128_t vacc0x0123 = wasm_v128_load(acc + 0);
63     v128_t vacc0x4567 = wasm_v128_load(acc + 4);
64     v128_t vacc1x0123 = wasm_v128_load(acc + 8);
65     v128_t vacc1x4567 = wasm_v128_load(acc + 12);
66     v128_t vacc2x0123 = wasm_v128_load(acc + 16);
67     v128_t vacc2x4567 = wasm_v128_load(acc + 20);
68     v128_t vacc3x0123 = wasm_v128_load(acc + 24);
69     v128_t vacc3x4567 = wasm_v128_load(acc + 28);
70     acc += 32;
71 
72     size_t k = kc;
73     do {
74       const v128_t va0 = wasm_v32x4_load_splat(a0);
75       a0 += 1;
76       const v128_t va1 = wasm_v32x4_load_splat(a1);
77       a1 += 1;
78       const v128_t va2 = wasm_v32x4_load_splat(a2);
79       a2 += 1;
80       const v128_t va3 = wasm_v32x4_load_splat(a3);
81       a3 += 1;
82 
83       const v128_t vb0123 = wasm_v128_load(w);
84       const v128_t vb4567 = wasm_v128_load(w + 4);
85       w += 8;
86 
87       vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123));
88       vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123));
89       vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123));
90       vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3, vb0123));
91       vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567));
92       vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567));
93       vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567));
94       vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3, vb4567));
95 
96       k -= sizeof(float);
97     } while (k != 0);
98 
99     const v128_t vmin = wasm_v32x4_load_splat(&params->scalar.min);
100     vacc0x0123 = wasm_v128_bitselect(vmin, vacc0x0123, wasm_f32x4_lt(vacc0x0123, vmin));
101     vacc1x0123 = wasm_v128_bitselect(vmin, vacc1x0123, wasm_f32x4_lt(vacc1x0123, vmin));
102     vacc2x0123 = wasm_v128_bitselect(vmin, vacc2x0123, wasm_f32x4_lt(vacc2x0123, vmin));
103     vacc3x0123 = wasm_v128_bitselect(vmin, vacc3x0123, wasm_f32x4_lt(vacc3x0123, vmin));
104     vacc0x4567 = wasm_v128_bitselect(vmin, vacc0x4567, wasm_f32x4_lt(vacc0x4567, vmin));
105     vacc1x4567 = wasm_v128_bitselect(vmin, vacc1x4567, wasm_f32x4_lt(vacc1x4567, vmin));
106     vacc2x4567 = wasm_v128_bitselect(vmin, vacc2x4567, wasm_f32x4_lt(vacc2x4567, vmin));
107     vacc3x4567 = wasm_v128_bitselect(vmin, vacc3x4567, wasm_f32x4_lt(vacc3x4567, vmin));
108 
109     const v128_t vmax = wasm_v32x4_load_splat(&params->scalar.max);
110     vacc0x0123 = wasm_v128_bitselect(vacc0x0123, vmax, wasm_f32x4_le(vacc0x0123, vmax));
111     vacc1x0123 = wasm_v128_bitselect(vacc1x0123, vmax, wasm_f32x4_le(vacc1x0123, vmax));
112     vacc2x0123 = wasm_v128_bitselect(vacc2x0123, vmax, wasm_f32x4_le(vacc2x0123, vmax));
113     vacc3x0123 = wasm_v128_bitselect(vacc3x0123, vmax, wasm_f32x4_le(vacc3x0123, vmax));
114     vacc0x4567 = wasm_v128_bitselect(vacc0x4567, vmax, wasm_f32x4_le(vacc0x4567, vmax));
115     vacc1x4567 = wasm_v128_bitselect(vacc1x4567, vmax, wasm_f32x4_le(vacc1x4567, vmax));
116     vacc2x4567 = wasm_v128_bitselect(vacc2x4567, vmax, wasm_f32x4_le(vacc2x4567, vmax));
117     vacc3x4567 = wasm_v128_bitselect(vacc3x4567, vmax, wasm_f32x4_le(vacc3x4567, vmax));
118 
119     if XNN_LIKELY(nc >= 8) {
120       wasm_v128_store(c3, vacc3x0123);
121       wasm_v128_store(c3 + 4, vacc3x4567);
122       c3 = (float*) ((uintptr_t) c3 + cn_stride);
123       wasm_v128_store(c2, vacc2x0123);
124       wasm_v128_store(c2 + 4, vacc2x4567);
125       c2 = (float*) ((uintptr_t) c2 + cn_stride);
126       wasm_v128_store(c1, vacc1x0123);
127       wasm_v128_store(c1 + 4, vacc1x4567);
128       c1 = (float*) ((uintptr_t) c1 + cn_stride);
129       wasm_v128_store(c0, vacc0x0123);
130       wasm_v128_store(c0 + 4, vacc0x4567);
131       c0 = (float*) ((uintptr_t) c0 + cn_stride);
132 
133       a3 = (const float*) ((uintptr_t) a3 - kc);
134       a2 = (const float*) ((uintptr_t) a2 - kc);
135       a1 = (const float*) ((uintptr_t) a1 - kc);
136       a0 = (const float*) ((uintptr_t) a0 - kc);
137 
138       nc -= 8;
139     } else {
140       if (nc & 4) {
141         wasm_v128_store(c3, vacc3x0123);
142         wasm_v128_store(c2, vacc2x0123);
143         wasm_v128_store(c1, vacc1x0123);
144         wasm_v128_store(c0, vacc0x0123);
145 
146         vacc3x0123 = vacc3x4567;
147         vacc2x0123 = vacc2x4567;
148         vacc1x0123 = vacc1x4567;
149         vacc0x0123 = vacc0x4567;
150 
151         c3 += 4;
152         c2 += 4;
153         c1 += 4;
154         c0 += 4;
155       }
156       if (nc & 2) {
157         *((double*) c3) = wasm_f64x2_extract_lane(vacc3x0123, 0);
158         *((double*) c2) = wasm_f64x2_extract_lane(vacc2x0123, 0);
159         *((double*) c1) = wasm_f64x2_extract_lane(vacc1x0123, 0);
160         *((double*) c0) = wasm_f64x2_extract_lane(vacc0x0123, 0);
161 
162         vacc3x0123 = wasm_v32x4_shuffle(vacc3x0123, vacc3x0123, 2, 3, 2, 3);
163         vacc2x0123 = wasm_v32x4_shuffle(vacc2x0123, vacc2x0123, 2, 3, 2, 3);
164         vacc1x0123 = wasm_v32x4_shuffle(vacc1x0123, vacc1x0123, 2, 3, 2, 3);
165         vacc0x0123 = wasm_v32x4_shuffle(vacc0x0123, vacc0x0123, 2, 3, 2, 3);
166 
167         c3 += 2;
168         c2 += 2;
169         c1 += 2;
170         c0 += 2;
171       }
172       if (nc & 1) {
173         *c3 = wasm_f32x4_extract_lane(vacc3x0123, 0);
174         *c2 = wasm_f32x4_extract_lane(vacc2x0123, 0);
175         *c1 = wasm_f32x4_extract_lane(vacc1x0123, 0);
176         *c0 = wasm_f32x4_extract_lane(vacc0x0123, 0);
177       }
178 
179       nc = 0;
180     }
181   } while (nc != 0);
182 }
183