xref: /aosp_15_r20/external/XNNPACK/src/f32-prelu/gen/wasmsimd-minmax-2x4.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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_2x4(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_2x4(
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_OOB_READS
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 
36   const size_t input_increment = input_stride * 2 - channels;
37   const size_t output_increment = output_stride * 2 - channels;
38 
39   const v128_t vzero = wasm_i32x4_const_splat(0);
40   do {
41     if XNN_UNPREDICTABLE(rows < 2) {
42       i1 = i0;
43       o1 = o0;
44     }
45 
46     const float* w = weights;
47     size_t c = channels;
48     for (; c >= 4 * sizeof(float); c -= 4 * sizeof(float)) {
49       const v128_t vw0123 = wasm_v128_load(w);
50       w += 4;
51 
52       v128_t vi0x0123 = wasm_v128_load(i0);
53       i0 += 4;
54       v128_t vi1x0123 = wasm_v128_load(i1);
55       i1 += 4;
56 
57       v128_t vacc0x0123 = wasm_i32x4_max(vi0x0123, vzero);
58       vi0x0123 = wasm_i32x4_min(vi0x0123, vzero);
59       v128_t vacc1x0123 = wasm_i32x4_max(vi1x0123, vzero);
60       vi1x0123 = wasm_i32x4_min(vi1x0123, vzero);
61 
62       vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(vi0x0123, vw0123));
63       vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(vi1x0123, vw0123));
64 
65       wasm_v128_store(o0, vacc0x0123);
66       o0 += 4;
67       wasm_v128_store(o1, vacc1x0123);
68       o1 += 4;
69     }
70     if XNN_UNLIKELY(c != 0) {
71       const v128_t vw0123 = wasm_v128_load(w);
72       w = (const float*) ((uintptr_t) w + c);
73 
74       v128_t vi0x0123 = wasm_v128_load(i0);
75       i0 = (const float*) ((uintptr_t) i0 + c);
76       v128_t vi1x0123 = wasm_v128_load(i1);
77       i1 = (const float*) ((uintptr_t) i1 + c);
78 
79       v128_t vacc0x0123 = wasm_i32x4_max(vi0x0123, vzero);
80       vi0x0123 = wasm_i32x4_min(vi0x0123, vzero);
81       v128_t vacc1x0123 = wasm_i32x4_max(vi1x0123, vzero);
82       vi1x0123 = wasm_i32x4_min(vi1x0123, vzero);
83 
84       vacc0x0123 = wasm_f32x4_add(vacc0x0123, wasm_f32x4_mul(vi0x0123, vw0123));
85       vacc1x0123 = wasm_f32x4_add(vacc1x0123, wasm_f32x4_mul(vi1x0123, vw0123));
86 
87       if (c & (2 * sizeof(float))) {
88         *((double*) o0) = wasm_f64x2_extract_lane(vacc0x0123, 0);
89         *((double*) o1) = wasm_f64x2_extract_lane(vacc1x0123, 0);
90 
91         vacc0x0123 = wasm_v32x4_shuffle(vacc0x0123, vacc0x0123, 2, 3, 2, 3);
92         vacc1x0123 = wasm_v32x4_shuffle(vacc1x0123, vacc1x0123, 2, 3, 2, 3);
93 
94         o0 += 2;
95         o1 += 2;
96       }
97       if (c & (1 * sizeof(float))) {
98         *o0 = wasm_f32x4_extract_lane(vacc0x0123, 0);
99         *o1 = wasm_f32x4_extract_lane(vacc1x0123, 0);
100 
101         o0 += 1;
102         o1 += 1;
103       }
104     }
105     i0 = (const float*) ((uintptr_t) i0 + input_increment);
106     o0 = (float*) ((uintptr_t) o0 + output_increment);
107     i1 = (const float*) ((uintptr_t) i1 + input_increment);
108     o1 = (float*) ((uintptr_t) o1 + output_increment);
109     rows = doz(rows, 2);
110   } while (rows != 0);
111 }
112