1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-gemm/MRx2c4-wasmsimd.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_4x2c4__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_4x2c4__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)]) XNN_DISABLE_TSAN
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     v128_t vacc0x0c4 = wasm_f32x4_replace_lane(wasm_f32x4_splat(0.0f), 0, w[0]);
61     v128_t vacc0x1c4 = wasm_f32x4_replace_lane(vacc0x0c4, 0, w[1]);
62     v128_t vacc1x0c4 = vacc0x0c4;
63     v128_t vacc1x1c4 = vacc0x1c4;
64     v128_t vacc2x0c4 = vacc0x0c4;
65     v128_t vacc2x1c4 = vacc0x1c4;
66     v128_t vacc3x0c4 = vacc0x0c4;
67     v128_t vacc3x1c4 = vacc0x1c4;
68     w += 2;
69 
70     size_t k = kc;
71     for (; k >= 4 * sizeof(float); k -= 4 * sizeof(float)) {
72       const v128_t va0 = wasm_v128_load(a0);
73       a0 += 4;
74       const v128_t va1 = wasm_v128_load(a1);
75       a1 += 4;
76       const v128_t va2 = wasm_v128_load(a2);
77       a2 += 4;
78       const v128_t va3 = wasm_v128_load(a3);
79       a3 += 4;
80 
81       const v128_t vb0 = wasm_v128_load(w);
82       const v128_t vb1 = wasm_v128_load(w + 4);
83       w += 8;
84 
85       vacc0x0c4 = wasm_f32x4_add(vacc0x0c4, wasm_f32x4_mul(va0, vb0));
86       vacc0x1c4 = wasm_f32x4_add(vacc0x1c4, wasm_f32x4_mul(va0, vb1));
87       vacc1x0c4 = wasm_f32x4_add(vacc1x0c4, wasm_f32x4_mul(va1, vb0));
88       vacc1x1c4 = wasm_f32x4_add(vacc1x1c4, wasm_f32x4_mul(va1, vb1));
89       vacc2x0c4 = wasm_f32x4_add(vacc2x0c4, wasm_f32x4_mul(va2, vb0));
90       vacc2x1c4 = wasm_f32x4_add(vacc2x1c4, wasm_f32x4_mul(va2, vb1));
91       vacc3x0c4 = wasm_f32x4_add(vacc3x0c4, wasm_f32x4_mul(va3, vb0));
92       vacc3x1c4 = wasm_f32x4_add(vacc3x1c4, wasm_f32x4_mul(va3, vb1));
93     }
94     if XNN_UNLIKELY(k != 0) {
95       const v128_t va0 = wasm_v128_load(a0);
96       a0 = (const float*) ((uintptr_t) a0 + k);
97       const v128_t va1 = wasm_v128_load(a1);
98       a1 = (const float*) ((uintptr_t) a1 + k);
99       const v128_t va2 = wasm_v128_load(a2);
100       a2 = (const float*) ((uintptr_t) a2 + k);
101       const v128_t va3 = wasm_v128_load(a3);
102       a3 = (const float*) ((uintptr_t) a3 + k);
103 
104       const v128_t vb0 = wasm_v128_load(w);
105       const v128_t vb1 = wasm_v128_load(w + 4);
106       w += 8;
107 
108       const v128_t vzero = wasm_f32x4_splat(0.0f);
109       const v128_t vmask0 = wasm_f32x4_eq(vb0, vzero);
110       const v128_t vmask1 = wasm_f32x4_eq(vb1, vzero);
111 
112       vacc0x0c4 = wasm_f32x4_add(vacc0x0c4, wasm_f32x4_mul(wasm_v128_andnot(va0, vmask0), vb0));
113       vacc0x1c4 = wasm_f32x4_add(vacc0x1c4, wasm_f32x4_mul(wasm_v128_andnot(va0, vmask1), vb1));
114       vacc1x0c4 = wasm_f32x4_add(vacc1x0c4, wasm_f32x4_mul(wasm_v128_andnot(va1, vmask0), vb0));
115       vacc1x1c4 = wasm_f32x4_add(vacc1x1c4, wasm_f32x4_mul(wasm_v128_andnot(va1, vmask1), vb1));
116       vacc2x0c4 = wasm_f32x4_add(vacc2x0c4, wasm_f32x4_mul(wasm_v128_andnot(va2, vmask0), vb0));
117       vacc2x1c4 = wasm_f32x4_add(vacc2x1c4, wasm_f32x4_mul(wasm_v128_andnot(va2, vmask1), vb1));
118       vacc3x0c4 = wasm_f32x4_add(vacc3x0c4, wasm_f32x4_mul(wasm_v128_andnot(va3, vmask0), vb0));
119       vacc3x1c4 = wasm_f32x4_add(vacc3x1c4, wasm_f32x4_mul(wasm_v128_andnot(va3, vmask1), vb1));
120     }
121 
122     const v128_t vacc0x01c2 = wasm_f32x4_add(
123       wasm_v32x4_shuffle(vacc0x0c4, vacc0x1c4, 0, 4, 1, 5),
124       wasm_v32x4_shuffle(vacc0x0c4, vacc0x1c4, 2, 6, 3, 7));
125     const v128_t vacc1x01c2 = wasm_f32x4_add(
126       wasm_v32x4_shuffle(vacc1x0c4, vacc1x1c4, 0, 4, 1, 5),
127       wasm_v32x4_shuffle(vacc1x0c4, vacc1x1c4, 2, 6, 3, 7));
128     const v128_t vacc2x01c2 = wasm_f32x4_add(
129       wasm_v32x4_shuffle(vacc2x0c4, vacc2x1c4, 0, 4, 1, 5),
130       wasm_v32x4_shuffle(vacc2x0c4, vacc2x1c4, 2, 6, 3, 7));
131     const v128_t vacc3x01c2 = wasm_f32x4_add(
132       wasm_v32x4_shuffle(vacc3x0c4, vacc3x1c4, 0, 4, 1, 5),
133       wasm_v32x4_shuffle(vacc3x0c4, vacc3x1c4, 2, 6, 3, 7));
134 
135     v128_t vacc01x01 = wasm_f32x4_add(
136       wasm_v32x4_shuffle(vacc0x01c2, vacc1x01c2, 0, 1, 4, 5),
137       wasm_v32x4_shuffle(vacc0x01c2, vacc1x01c2, 2, 3, 6, 7));
138     v128_t vacc23x01 = wasm_f32x4_add(
139       wasm_v32x4_shuffle(vacc2x01c2, vacc3x01c2, 0, 1, 4, 5),
140       wasm_v32x4_shuffle(vacc2x01c2, vacc3x01c2, 2, 3, 6, 7));
141 
142     const v128_t vmin = wasm_v32x4_load_splat(&params->scalar.min);
143     vacc01x01 = wasm_v128_bitselect(vmin, vacc01x01, wasm_f32x4_lt(vacc01x01, vmin));
144     vacc23x01 = wasm_v128_bitselect(vmin, vacc23x01, wasm_f32x4_lt(vacc23x01, vmin));
145 
146     const v128_t vmax = wasm_v32x4_load_splat(&params->scalar.max);
147     vacc01x01 = wasm_v128_bitselect(vacc01x01, vmax, wasm_f32x4_le(vacc01x01, vmax));
148     vacc23x01 = wasm_v128_bitselect(vacc23x01, vmax, wasm_f32x4_le(vacc23x01, vmax));
149 
150     if XNN_LIKELY(nc >= 2) {
151       *((double*) c2) = wasm_f64x2_extract_lane(vacc23x01, 0);
152       c2 = (float*) ((uintptr_t) c2 + cn_stride);
153       a2 = (const float*) ((uintptr_t) a2 - kc);
154       *((double*) c3) = wasm_f64x2_extract_lane(vacc23x01, 1);
155       c3 = (float*) ((uintptr_t) c3 + cn_stride);
156       a3 = (const float*) ((uintptr_t) a3 - kc);
157       *((double*) c0) = wasm_f64x2_extract_lane(vacc01x01, 0);
158       c0 = (float*) ((uintptr_t) c0 + cn_stride);
159       a0 = (const float*) ((uintptr_t) a0 - kc);
160       *((double*) c1) = wasm_f64x2_extract_lane(vacc01x01, 1);
161       c1 = (float*) ((uintptr_t) c1 + cn_stride);
162       a1 = (const float*) ((uintptr_t) a1 - kc);
163 
164       nc -= 2;
165     } else {
166       assert(nc == 1);
167       *c2 = wasm_f32x4_extract_lane(vacc23x01, 0);
168       *c3 = wasm_f32x4_extract_lane(vacc23x01, 2);
169       *c0 = wasm_f32x4_extract_lane(vacc01x01, 0);
170       *c1 = wasm_f32x4_extract_lane(vacc01x01, 2);
171 
172       nc = 0;
173     }
174   } while (nc != 0);
175 }
176