• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-prelu/wasmsimd-minmax.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/math.h>
15 #include <xnnpack/prelu.h>
16 
17 
xnn_f32_prelu_ukernel__wasmsimd_minmax_2x8(size_t rows,size_t channels,const float * restrict input,size_t input_stride,const float * restrict weights,float * restrict output,size_t output_stride)18 void xnn_f32_prelu_ukernel__wasmsimd_minmax_2x8(
19     size_t rows,
20     size_t channels,
21     const float*restrict input,
22     size_t input_stride,
23     const float*restrict weights,
24     float*restrict output,
25     size_t output_stride) XNN_DISABLE_TSAN
26 {
27   assert(rows != 0);
28   assert(channels != 0);
29   assert(channels % sizeof(float) == 0);
30 
31   const float* i0 = input;
32   float* o0 = output;
33   const float* i1 = (const float*) ((uintptr_t) i0 + input_stride);
34   float* o1 = (float*) ((uintptr_t) o0 + output_stride);
35   if XNN_UNPREDICTABLE(rows < 2) {
36     i1 = i0;
37     o1 = o0;
38   }
39 
40   const size_t input_increment = input_stride * 2 - channels;
41   const size_t output_increment = output_stride * 2 - channels;
42 
43   const v128_t vzero = wasm_f32x4_splat(0.0f);
44   do {
45     const float* w = weights;
46     size_t c = channels;
47     for (; c >= 8 * sizeof(float); c -= 8 * sizeof(float)) {
48       const v128_t vw0123 = wasm_v128_load(w);
49       const v128_t vw4567 = wasm_v128_load(w + 4);
50       w += 8;
51 
52       v128_t vi0x0123 = wasm_v128_load(i0);
53       v128_t vi0x4567 = wasm_v128_load(i0 + 4);
54       i0 += 8;
55       v128_t vi1x0123 = wasm_v128_load(i1);
56       v128_t vi1x4567 = wasm_v128_load(i1 + 4);
57       i1 += 8;
58 
59       v128_t vacc0x0123 = wasm_i32x4_max(vi0x0123, vzero);
60       vi0x0123 = wasm_i32x4_min(vi0x0123, vzero);
61       v128_t vacc0x4567 = wasm_i32x4_max(vi0x4567, vzero);
62       vi0x4567 = wasm_i32x4_min(vi0x4567, vzero);
63       v128_t vacc1x0123 = wasm_i32x4_max(vi1x0123, vzero);
64       vi1x0123 = wasm_i32x4_min(vi1x0123, vzero);
65       v128_t vacc1x4567 = wasm_i32x4_max(vi1x4567, vzero);
66       vi1x4567 = wasm_i32x4_min(vi1x4567, vzero);
67 
68       vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(vi0x0123, vw0123));
69       vacc0x4567 = wasm_f32x4_add(vacc0x4567, wasm_f32x4_mul(vi0x4567, vw4567));
70       vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(vi1x0123, vw0123));
71       vacc1x4567 = wasm_f32x4_add(vacc1x4567, wasm_f32x4_mul(vi1x4567, vw4567));
72 
73       wasm_v128_store(o0, vacc0x0123);
74       wasm_v128_store(o0 + 4, vacc0x4567);
75       o0 += 8;
76       wasm_v128_store(o1, vacc1x0123);
77       wasm_v128_store(o1 + 4, vacc1x4567);
78       o1 += 8;
79     }
80     for (; c >= 4 * sizeof(float); c -= 4 * sizeof(float)) {
81       const v128_t vw0123 = wasm_v128_load(w);
82       w += 4;
83 
84       v128_t vi0x0123 = wasm_v128_load(i0);
85       i0 += 4;
86       v128_t vi1x0123 = wasm_v128_load(i1);
87       i1 += 4;
88 
89       v128_t vacc0x0123 = wasm_i32x4_max(vi0x0123, vzero);
90       vi0x0123 = wasm_i32x4_min(vi0x0123, vzero);
91       v128_t vacc1x0123 = wasm_i32x4_max(vi1x0123, vzero);
92       vi1x0123 = wasm_i32x4_min(vi1x0123, vzero);
93 
94       vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(vi0x0123, vw0123));
95       vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(vi1x0123, vw0123));
96 
97       wasm_v128_store(o0, vacc0x0123);
98       o0 += 4;
99       wasm_v128_store(o1, vacc1x0123);
100       o1 += 4;
101     }
102     if XNN_UNLIKELY(c != 0) {
103       const v128_t vw0123 = wasm_v128_load(w);
104       w = (const float*) ((uintptr_t) w + c);
105 
106       v128_t vi0x0123 = wasm_v128_load(i0);
107       i0 = (const float*) ((uintptr_t) i0 + c);
108       v128_t vi1x0123 = wasm_v128_load(i1);
109       i1 = (const float*) ((uintptr_t) i1 + c);
110 
111       v128_t vacc0x0123 = wasm_i32x4_max(vi0x0123, vzero);
112       vi0x0123 = wasm_i32x4_min(vi0x0123, vzero);
113       v128_t vacc1x0123 = wasm_i32x4_max(vi1x0123, vzero);
114       vi1x0123 = wasm_i32x4_min(vi1x0123, vzero);
115 
116       vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(vi0x0123, vw0123));
117       vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(vi1x0123, vw0123));
118 
119       if (c & (2 * sizeof(float))) {
120         *((double*) o0) = wasm_f64x2_extract_lane(vacc0x0123, 0);
121         *((double*) o1) = wasm_f64x2_extract_lane(vacc1x0123, 0);
122 
123         vacc0x0123 = wasm_v32x4_shuffle(vacc0x0123, vacc0x0123, 2, 3, 2, 3);
124         vacc1x0123 = wasm_v32x4_shuffle(vacc1x0123, vacc1x0123, 2, 3, 2, 3);
125 
126         o0 += 2;
127         o1 += 2;
128       }
129       if (c & (1 * sizeof(float))) {
130         *o0 = wasm_f32x4_extract_lane(vacc0x0123, 0);
131         *o1 = wasm_f32x4_extract_lane(vacc1x0123, 0);
132 
133         o0 += 1;
134         o1 += 1;
135       }
136     }
137     i0 = (const float*) ((uintptr_t) i0 + input_increment);
138     o0 = (float*) ((uintptr_t) o0 + output_increment);
139     i1 = (const float*) ((uintptr_t) i1 + input_increment);
140     o1 = (float*) ((uintptr_t) o1 + output_increment);
141     if XNN_UNPREDICTABLE(rows < 4) {
142       i1 = i0;
143       o1 = o0;
144     }
145     rows = doz(rows, 2);
146   } while (rows != 0);
147 }
148