• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-igemm/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/igemm.h>
15 
16 
xnn_f32_igemm_minmax_ukernel_4x2c4__wasmsimd_arm(size_t mr,size_t nc,size_t kc,size_t ks,const float ** restrict a,const float * restrict w,float * restrict c,size_t cm_stride,size_t cn_stride,size_t a_offset,const float * zero,const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_f32_igemm_minmax_ukernel_4x2c4__wasmsimd_arm(
18     size_t mr,
19     size_t nc,
20     size_t kc,
21     size_t ks,
22     const float**restrict a,
23     const float*restrict w,
24     float*restrict c,
25     size_t cm_stride,
26     size_t cn_stride,
27     size_t a_offset,
28     const float* zero,
29     const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
30 {
31   assert(mr != 0);
32   assert(mr <= 4);
33   assert(nc != 0);
34   assert(kc != 0);
35   assert(kc % sizeof(float) == 0);
36   assert(ks != 0);
37   assert(ks % (4 * sizeof(void*)) == 0);
38   assert(a_offset % sizeof(float) == 0);
39   assert(a != NULL);
40   assert(w != NULL);
41   assert(c != NULL);
42 
43   float* c0 = c;
44   float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
45   if XNN_UNPREDICTABLE(mr < 2) {
46     c1 = c0;
47   }
48   float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
49   if XNN_UNPREDICTABLE(mr <= 2) {
50     c2 = c1;
51   }
52   float* c3 = (float*) ((uintptr_t) c2 + cm_stride);
53   if XNN_UNPREDICTABLE(mr != 4) {
54     c3 = c2;
55   }
56 
57   const v128_t vmin = wasm_v32x4_load_splat(&params->scalar.min);
58   const v128_t vmax = wasm_v32x4_load_splat(&params->scalar.max);
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 p = ks;
71     do {
72       const float* restrict a0 = a[0];
73       assert(a0 != NULL);
74       if XNN_UNPREDICTABLE(a0 != zero) {
75         a0 = (const float*) ((uintptr_t) a0 + a_offset);
76       }
77       const float* restrict a1 = a[1];
78       assert(a1 != NULL);
79       if XNN_UNPREDICTABLE(a1 != zero) {
80         a1 = (const float*) ((uintptr_t) a1 + a_offset);
81       }
82       const float* restrict a2 = a[2];
83       assert(a2 != NULL);
84       if XNN_UNPREDICTABLE(a2 != zero) {
85         a2 = (const float*) ((uintptr_t) a2 + a_offset);
86       }
87       const float* restrict a3 = a[3];
88       assert(a3 != NULL);
89       if XNN_UNPREDICTABLE(a3 != zero) {
90         a3 = (const float*) ((uintptr_t) a3 + a_offset);
91       }
92       a += 4;
93 
94       size_t k = kc;
95       for (; k >= 4 * sizeof(float); k -= 4 * sizeof(float)) {
96         const v128_t va0 = wasm_v128_load(a0);
97         a0 += 4;
98         const v128_t va1 = wasm_v128_load(a1);
99         a1 += 4;
100         const v128_t va2 = wasm_v128_load(a2);
101         a2 += 4;
102         const v128_t va3 = wasm_v128_load(a3);
103         a3 += 4;
104 
105         const v128_t vb0 = wasm_v128_load(w);
106         const v128_t vb1 = wasm_v128_load(w + 4);
107         w += 8;
108 
109         vacc0x0c4 = wasm_f32x4_add(vacc0x0c4, wasm_f32x4_mul(va0, vb0));
110         vacc0x1c4 = wasm_f32x4_add(vacc0x1c4, wasm_f32x4_mul(va0, vb1));
111         vacc1x0c4 = wasm_f32x4_add(vacc1x0c4, wasm_f32x4_mul(va1, vb0));
112         vacc1x1c4 = wasm_f32x4_add(vacc1x1c4, wasm_f32x4_mul(va1, vb1));
113         vacc2x0c4 = wasm_f32x4_add(vacc2x0c4, wasm_f32x4_mul(va2, vb0));
114         vacc2x1c4 = wasm_f32x4_add(vacc2x1c4, wasm_f32x4_mul(va2, vb1));
115         vacc3x0c4 = wasm_f32x4_add(vacc3x0c4, wasm_f32x4_mul(va3, vb0));
116         vacc3x1c4 = wasm_f32x4_add(vacc3x1c4, wasm_f32x4_mul(va3, vb1));
117       }
118       if XNN_UNLIKELY(k != 0) {
119         const v128_t va0 = wasm_v128_load(a0);
120         const v128_t va1 = wasm_v128_load(a1);
121         const v128_t va2 = wasm_v128_load(a2);
122         const v128_t va3 = wasm_v128_load(a3);
123 
124         const v128_t vb0 = wasm_v128_load(w);
125         const v128_t vb1 = wasm_v128_load(w + 4);
126         w += 8;
127 
128         const v128_t vzero = wasm_f32x4_splat(0.0f);
129         const v128_t vmask0 = wasm_f32x4_eq(vb0, vzero);
130         const v128_t vmask1 = wasm_f32x4_eq(vb1, vzero);
131 
132         vacc0x0c4 = wasm_f32x4_add(vacc0x0c4, wasm_f32x4_mul(wasm_v128_andnot(va0, vmask0), vb0));
133         vacc0x1c4 = wasm_f32x4_add(vacc0x1c4, wasm_f32x4_mul(wasm_v128_andnot(va0, vmask1), vb1));
134         vacc1x0c4 = wasm_f32x4_add(vacc1x0c4, wasm_f32x4_mul(wasm_v128_andnot(va1, vmask0), vb0));
135         vacc1x1c4 = wasm_f32x4_add(vacc1x1c4, wasm_f32x4_mul(wasm_v128_andnot(va1, vmask1), vb1));
136         vacc2x0c4 = wasm_f32x4_add(vacc2x0c4, wasm_f32x4_mul(wasm_v128_andnot(va2, vmask0), vb0));
137         vacc2x1c4 = wasm_f32x4_add(vacc2x1c4, wasm_f32x4_mul(wasm_v128_andnot(va2, vmask1), vb1));
138         vacc3x0c4 = wasm_f32x4_add(vacc3x0c4, wasm_f32x4_mul(wasm_v128_andnot(va3, vmask0), vb0));
139         vacc3x1c4 = wasm_f32x4_add(vacc3x1c4, wasm_f32x4_mul(wasm_v128_andnot(va3, vmask1), vb1));
140       }
141       p -= 4 * sizeof(void*);
142     } while (p != 0);
143 
144     const v128_t vacc0x01c2 = wasm_f32x4_add(
145       wasm_v32x4_shuffle(vacc0x0c4, vacc0x1c4, 0, 4, 1, 5),
146       wasm_v32x4_shuffle(vacc0x0c4, vacc0x1c4, 2, 6, 3, 7));
147     const v128_t vacc1x01c2 = wasm_f32x4_add(
148       wasm_v32x4_shuffle(vacc1x0c4, vacc1x1c4, 0, 4, 1, 5),
149       wasm_v32x4_shuffle(vacc1x0c4, vacc1x1c4, 2, 6, 3, 7));
150     const v128_t vacc2x01c2 = wasm_f32x4_add(
151       wasm_v32x4_shuffle(vacc2x0c4, vacc2x1c4, 0, 4, 1, 5),
152       wasm_v32x4_shuffle(vacc2x0c4, vacc2x1c4, 2, 6, 3, 7));
153     const v128_t vacc3x01c2 = wasm_f32x4_add(
154       wasm_v32x4_shuffle(vacc3x0c4, vacc3x1c4, 0, 4, 1, 5),
155       wasm_v32x4_shuffle(vacc3x0c4, vacc3x1c4, 2, 6, 3, 7));
156 
157     v128_t vacc01x01 = wasm_f32x4_add(
158       wasm_v32x4_shuffle(vacc0x01c2, vacc1x01c2, 0, 1, 4, 5),
159       wasm_v32x4_shuffle(vacc0x01c2, vacc1x01c2, 2, 3, 6, 7));
160     v128_t vacc23x01 = wasm_f32x4_add(
161       wasm_v32x4_shuffle(vacc2x01c2, vacc3x01c2, 0, 1, 4, 5),
162       wasm_v32x4_shuffle(vacc2x01c2, vacc3x01c2, 2, 3, 6, 7));
163 
164     vacc01x01 = wasm_f32x4_max(vacc01x01, vmin);
165     vacc23x01 = wasm_f32x4_max(vacc23x01, vmin);
166 
167     vacc01x01 = wasm_f32x4_min(vacc01x01, vmax);
168     vacc23x01 = wasm_f32x4_min(vacc23x01, vmax);
169 
170     if XNN_LIKELY(nc >= 2) {
171       *((double*) c3) = wasm_f64x2_extract_lane(vacc23x01, 1);
172       c3 = (float*) ((uintptr_t) c3 + cn_stride);
173       *((double*) c2) = wasm_f64x2_extract_lane(vacc23x01, 0);
174       c2 = (float*) ((uintptr_t) c2 + cn_stride);
175       *((double*) c1) = wasm_f64x2_extract_lane(vacc01x01, 1);
176       c1 = (float*) ((uintptr_t) c1 + cn_stride);
177       *((double*) c0) = wasm_f64x2_extract_lane(vacc01x01, 0);
178       c0 = (float*) ((uintptr_t) c0 + cn_stride);
179 
180       a = (const float**restrict) ((uintptr_t) a - ks);
181       nc -= 2;
182     } else {
183       assert(nc == 1);
184       *c3 = wasm_f32x4_extract_lane(vacc23x01, 2);
185       *c2 = wasm_f32x4_extract_lane(vacc23x01, 0);
186       *c1 = wasm_f32x4_extract_lane(vacc01x01, 2);
187       *c0 = wasm_f32x4_extract_lane(vacc01x01, 0);
188 
189       nc = 0;
190     }
191   } while (nc != 0);
192 }
193