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_gemm_minmax_ukernel_5x8__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 union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_f32_gemm_minmax_ukernel_5x8__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 union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
28 {
29   assert(mr != 0);
30   assert(mr <= 5);
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   const float* a4 = (const float*) ((uintptr_t) a3 + a_stride);
59   float* c4 = (float*) ((uintptr_t) c3 + cm_stride);
60   if XNN_UNPREDICTABLE(mr <= 4) {
61     a4 = a3;
62     c4 = c3;
63   }
64 
65   do {
66     v128_t vacc0x0123 = wasm_v128_load(w + 0);
67     v128_t vacc0x4567 = wasm_v128_load(w + 4);
68     v128_t vacc1x0123 = vacc0x0123;
69     v128_t vacc1x4567 = vacc0x4567;
70     v128_t vacc2x0123 = vacc0x0123;
71     v128_t vacc2x4567 = vacc0x4567;
72     v128_t vacc3x0123 = vacc0x0123;
73     v128_t vacc3x4567 = vacc0x4567;
74     v128_t vacc4x0123 = vacc0x0123;
75     v128_t vacc4x4567 = vacc0x4567;
76     w += 8;
77 
78     size_t k = kc;
79     do {
80       const v128_t va0 = wasm_v32x4_load_splat(a0);
81       a0 += 1;
82       const v128_t va1 = wasm_v32x4_load_splat(a1);
83       a1 += 1;
84       const v128_t va2 = wasm_v32x4_load_splat(a2);
85       a2 += 1;
86       const v128_t va3 = wasm_v32x4_load_splat(a3);
87       a3 += 1;
88       const v128_t va4 = wasm_v32x4_load_splat(a4);
89       a4 += 1;
90 
91       const v128_t vb0123 = wasm_v128_load(w);
92       const v128_t vb4567 = wasm_v128_load(w + 4);
93       w += 8;
94 
95       vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(va0, vb0123));
96       vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(va1, vb0123));
97       vacc2x0123 = wasm_f32x4_add(vacc2x0123, wasm_f32x4_mul(va2, vb0123));
98       vacc3x0123 = wasm_f32x4_add(vacc3x0123, wasm_f32x4_mul(va3, vb0123));
99       vacc4x0123 = wasm_f32x4_add(vacc4x0123, wasm_f32x4_mul(va4, vb0123));
100       vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(va0, vb4567));
101       vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(va1, vb4567));
102       vacc2x4567 = wasm_f32x4_add(vacc2x4567, wasm_f32x4_mul(va2, vb4567));
103       vacc3x4567 = wasm_f32x4_add(vacc3x4567, wasm_f32x4_mul(va3, vb4567));
104       vacc4x4567 = wasm_f32x4_add(vacc4x4567, wasm_f32x4_mul(va4, vb4567));
105 
106       k -= sizeof(float);
107     } while (k != 0);
108 
109     const v128_t vmin = wasm_v32x4_load_splat(&params->scalar.min);
110     vacc0x0123 = wasm_v128_bitselect(vmin, vacc0x0123, wasm_f32x4_lt(vacc0x0123, vmin));
111     vacc1x0123 = wasm_v128_bitselect(vmin, vacc1x0123, wasm_f32x4_lt(vacc1x0123, vmin));
112     vacc2x0123 = wasm_v128_bitselect(vmin, vacc2x0123, wasm_f32x4_lt(vacc2x0123, vmin));
113     vacc3x0123 = wasm_v128_bitselect(vmin, vacc3x0123, wasm_f32x4_lt(vacc3x0123, vmin));
114     vacc4x0123 = wasm_v128_bitselect(vmin, vacc4x0123, wasm_f32x4_lt(vacc4x0123, vmin));
115     vacc0x4567 = wasm_v128_bitselect(vmin, vacc0x4567, wasm_f32x4_lt(vacc0x4567, vmin));
116     vacc1x4567 = wasm_v128_bitselect(vmin, vacc1x4567, wasm_f32x4_lt(vacc1x4567, vmin));
117     vacc2x4567 = wasm_v128_bitselect(vmin, vacc2x4567, wasm_f32x4_lt(vacc2x4567, vmin));
118     vacc3x4567 = wasm_v128_bitselect(vmin, vacc3x4567, wasm_f32x4_lt(vacc3x4567, vmin));
119     vacc4x4567 = wasm_v128_bitselect(vmin, vacc4x4567, wasm_f32x4_lt(vacc4x4567, vmin));
120 
121     const v128_t vmax = wasm_v32x4_load_splat(&params->scalar.max);
122     vacc0x0123 = wasm_v128_bitselect(vacc0x0123, vmax, wasm_f32x4_le(vacc0x0123, vmax));
123     vacc1x0123 = wasm_v128_bitselect(vacc1x0123, vmax, wasm_f32x4_le(vacc1x0123, vmax));
124     vacc2x0123 = wasm_v128_bitselect(vacc2x0123, vmax, wasm_f32x4_le(vacc2x0123, vmax));
125     vacc3x0123 = wasm_v128_bitselect(vacc3x0123, vmax, wasm_f32x4_le(vacc3x0123, vmax));
126     vacc4x0123 = wasm_v128_bitselect(vacc4x0123, vmax, wasm_f32x4_le(vacc4x0123, vmax));
127     vacc0x4567 = wasm_v128_bitselect(vacc0x4567, vmax, wasm_f32x4_le(vacc0x4567, vmax));
128     vacc1x4567 = wasm_v128_bitselect(vacc1x4567, vmax, wasm_f32x4_le(vacc1x4567, vmax));
129     vacc2x4567 = wasm_v128_bitselect(vacc2x4567, vmax, wasm_f32x4_le(vacc2x4567, vmax));
130     vacc3x4567 = wasm_v128_bitselect(vacc3x4567, vmax, wasm_f32x4_le(vacc3x4567, vmax));
131     vacc4x4567 = wasm_v128_bitselect(vacc4x4567, vmax, wasm_f32x4_le(vacc4x4567, vmax));
132 
133     if XNN_LIKELY(nc >= 8) {
134       wasm_v128_store(c4, vacc4x0123);
135       wasm_v128_store(c4 + 4, vacc4x4567);
136       c4 = (float*) ((uintptr_t) c4 + cn_stride);
137       wasm_v128_store(c3, vacc3x0123);
138       wasm_v128_store(c3 + 4, vacc3x4567);
139       c3 = (float*) ((uintptr_t) c3 + cn_stride);
140       wasm_v128_store(c2, vacc2x0123);
141       wasm_v128_store(c2 + 4, vacc2x4567);
142       c2 = (float*) ((uintptr_t) c2 + cn_stride);
143       wasm_v128_store(c1, vacc1x0123);
144       wasm_v128_store(c1 + 4, vacc1x4567);
145       c1 = (float*) ((uintptr_t) c1 + cn_stride);
146       wasm_v128_store(c0, vacc0x0123);
147       wasm_v128_store(c0 + 4, vacc0x4567);
148       c0 = (float*) ((uintptr_t) c0 + cn_stride);
149 
150       a4 = (const float*) ((uintptr_t) a4 - kc);
151       a3 = (const float*) ((uintptr_t) a3 - kc);
152       a2 = (const float*) ((uintptr_t) a2 - kc);
153       a1 = (const float*) ((uintptr_t) a1 - kc);
154       a0 = (const float*) ((uintptr_t) a0 - kc);
155 
156       nc -= 8;
157     } else {
158       if (nc & 4) {
159         wasm_v128_store(c4, vacc4x0123);
160         wasm_v128_store(c3, vacc3x0123);
161         wasm_v128_store(c2, vacc2x0123);
162         wasm_v128_store(c1, vacc1x0123);
163         wasm_v128_store(c0, vacc0x0123);
164 
165         vacc4x0123 = vacc4x4567;
166         vacc3x0123 = vacc3x4567;
167         vacc2x0123 = vacc2x4567;
168         vacc1x0123 = vacc1x4567;
169         vacc0x0123 = vacc0x4567;
170 
171         c4 += 4;
172         c3 += 4;
173         c2 += 4;
174         c1 += 4;
175         c0 += 4;
176       }
177       if (nc & 2) {
178         *((double*) c4) = wasm_f64x2_extract_lane(vacc4x0123, 0);
179         *((double*) c3) = wasm_f64x2_extract_lane(vacc3x0123, 0);
180         *((double*) c2) = wasm_f64x2_extract_lane(vacc2x0123, 0);
181         *((double*) c1) = wasm_f64x2_extract_lane(vacc1x0123, 0);
182         *((double*) c0) = wasm_f64x2_extract_lane(vacc0x0123, 0);
183 
184         vacc4x0123 = wasm_v32x4_shuffle(vacc4x0123, vacc4x0123, 2, 3, 2, 3);
185         vacc3x0123 = wasm_v32x4_shuffle(vacc3x0123, vacc3x0123, 2, 3, 2, 3);
186         vacc2x0123 = wasm_v32x4_shuffle(vacc2x0123, vacc2x0123, 2, 3, 2, 3);
187         vacc1x0123 = wasm_v32x4_shuffle(vacc1x0123, vacc1x0123, 2, 3, 2, 3);
188         vacc0x0123 = wasm_v32x4_shuffle(vacc0x0123, vacc0x0123, 2, 3, 2, 3);
189 
190         c4 += 2;
191         c3 += 2;
192         c2 += 2;
193         c1 += 2;
194         c0 += 2;
195       }
196       if (nc & 1) {
197         *c4 = wasm_f32x4_extract_lane(vacc4x0123, 0);
198         *c3 = wasm_f32x4_extract_lane(vacc3x0123, 0);
199         *c2 = wasm_f32x4_extract_lane(vacc2x0123, 0);
200         *c1 = wasm_f32x4_extract_lane(vacc1x0123, 0);
201         *c0 = wasm_f32x4_extract_lane(vacc0x0123, 0);
202       }
203 
204       nc = 0;
205     }
206   } while (nc != 0);
207 }
208