1 // Copyright 2020 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5
6 #include <assert.h>
7
8 #include <wasm_simd128.h>
9
10 #include <xnnpack/pavgpool.h>
11
12
xnn_f32_pavgpool_minmax_ukernel_9x__wasmsimd_x86_c4(size_t output_pixels,size_t kernel_elements,size_t channels,const float ** input,size_t input_offset,const float * zero,const float * multiplier,float * output,size_t input_increment,size_t output_increment,const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])13 void xnn_f32_pavgpool_minmax_ukernel_9x__wasmsimd_x86_c4(
14 size_t output_pixels,
15 size_t kernel_elements,
16 size_t channels,
17 const float** input,
18 size_t input_offset,
19 const float* zero,
20 const float* multiplier,
21 float* output,
22 size_t input_increment,
23 size_t output_increment,
24 const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
25 {
26 assert(output_pixels != 0);
27 assert(kernel_elements != 0);
28 assert(kernel_elements <= 9);
29 assert(channels != 0);
30
31 const v128_t vmin = wasm_v32x4_load_splat(¶ms->scalar.min);
32 const v128_t vmax = wasm_v32x4_load_splat(¶ms->scalar.max);
33
34 do {
35 const float* i0 = input[0];
36 assert(i0 != NULL);
37 const float* i1 = input[1];
38 const float* i2 = input[2];
39 const float* i3 = input[3];
40 const float* i4 = input[4];
41 const float* i5 = input[5];
42 const float* i6 = input[6];
43 const float* i7 = input[7];
44 const float* i8 = input[8];
45 input = (const float**) ((uintptr_t) input + input_increment);
46 if (kernel_elements < 2) {
47 i1 = zero;
48 }
49 assert(i1 != NULL);
50 if (kernel_elements <= 2) {
51 i2 = zero;
52 }
53 assert(i2 != NULL);
54 if (kernel_elements < 4) {
55 i3 = zero;
56 }
57 assert(i3 != NULL);
58 if (kernel_elements <= 4) {
59 i4 = zero;
60 }
61 assert(i4 != NULL);
62 if (kernel_elements < 6) {
63 i5 = zero;
64 }
65 assert(i5 != NULL);
66 if (kernel_elements <= 6) {
67 i6 = zero;
68 }
69 assert(i6 != NULL);
70 if (kernel_elements < 8) {
71 i7 = zero;
72 }
73 assert(i7 != NULL);
74 if (kernel_elements <= 8) {
75 i8 = zero;
76 }
77 assert(i8 != NULL);
78 if XNN_UNPREDICTABLE(i0 != zero) {
79 i0 = (const float*) ((uintptr_t) i0 + input_offset);
80 }
81 if XNN_UNPREDICTABLE(i1 != zero) {
82 i1 = (const float*) ((uintptr_t) i1 + input_offset);
83 }
84 if XNN_UNPREDICTABLE(i2 != zero) {
85 i2 = (const float*) ((uintptr_t) i2 + input_offset);
86 }
87 if XNN_UNPREDICTABLE(i3 != zero) {
88 i3 = (const float*) ((uintptr_t) i3 + input_offset);
89 }
90 if XNN_UNPREDICTABLE(i4 != zero) {
91 i4 = (const float*) ((uintptr_t) i4 + input_offset);
92 }
93 if XNN_UNPREDICTABLE(i5 != zero) {
94 i5 = (const float*) ((uintptr_t) i5 + input_offset);
95 }
96 if XNN_UNPREDICTABLE(i6 != zero) {
97 i6 = (const float*) ((uintptr_t) i6 + input_offset);
98 }
99 if XNN_UNPREDICTABLE(i7 != zero) {
100 i7 = (const float*) ((uintptr_t) i7 + input_offset);
101 }
102 if XNN_UNPREDICTABLE(i8 != zero) {
103 i8 = (const float*) ((uintptr_t) i8 + input_offset);
104 }
105
106 const v128_t vmultiplier = wasm_v32x4_load_splat(multiplier);
107 multiplier += 1;
108
109 size_t c = channels;
110 while (c >= 4) {
111 const v128_t vi0 = wasm_v128_load(i0);
112 i0 += 4;
113 const v128_t vi1 = wasm_v128_load(i1);
114 i1 += 4;
115 const v128_t vi2 = wasm_v128_load(i2);
116 i2 += 4;
117 const v128_t vi3 = wasm_v128_load(i3);
118 i3 += 4;
119 const v128_t vi4 = wasm_v128_load(i4);
120 i4 += 4;
121 const v128_t vi5 = wasm_v128_load(i5);
122 i5 += 4;
123 const v128_t vi6 = wasm_v128_load(i6);
124 i6 += 4;
125 const v128_t vi7 = wasm_v128_load(i7);
126 i7 += 4;
127 const v128_t vi8 = wasm_v128_load(i8);
128 i8 += 4;
129
130 const v128_t vsum018 = wasm_f32x4_add(wasm_f32x4_add(vi0, vi1), vi8);
131 const v128_t vsum23 = wasm_f32x4_add(vi2, vi3);
132 const v128_t vsum45 = wasm_f32x4_add(vi4, vi5);
133 const v128_t vsum67 = wasm_f32x4_add(vi6, vi7);
134
135 const v128_t vsum2345 = wasm_f32x4_add(vsum23, vsum45);
136 const v128_t vsum01678 = wasm_f32x4_add(vsum018, vsum67);
137 const v128_t vsum = wasm_f32x4_add(vsum2345, vsum01678);
138
139 v128_t vout = wasm_f32x4_mul(vsum, vmultiplier);
140 const v128_t vufmask = wasm_f32x4_lt(vout, vmin);
141 const v128_t vofmask = wasm_f32x4_le(vmax, vout);
142 vout = wasm_v128_bitselect(vmin, vout, vufmask);
143 vout = wasm_v128_bitselect(vmax, vout, vofmask);
144
145 wasm_v128_store(output, vout);
146 output += 4;
147
148 c -= 4;
149 }
150 if (c != 0) {
151 const v128_t vi0 = wasm_v128_load(i0);
152 const v128_t vi1 = wasm_v128_load(i1);
153 const v128_t vi2 = wasm_v128_load(i2);
154 const v128_t vi3 = wasm_v128_load(i3);
155 const v128_t vi4 = wasm_v128_load(i4);
156 const v128_t vi5 = wasm_v128_load(i5);
157 const v128_t vi6 = wasm_v128_load(i6);
158 const v128_t vi7 = wasm_v128_load(i7);
159 const v128_t vi8 = wasm_v128_load(i8);
160
161 const v128_t vsum01 = wasm_f32x4_add(vi0, vi1);
162 const v128_t vsum23 = wasm_f32x4_add(vi2, vi3);
163 const v128_t vsum45 = wasm_f32x4_add(vi4, vi5);
164 const v128_t vsum67 = wasm_f32x4_add(vi6, vi7);
165 const v128_t vsum018 = wasm_f32x4_add(vsum01, vi8);
166 const v128_t vsum2345 = wasm_f32x4_add(vsum23, vsum45);
167 const v128_t vsum01678 = wasm_f32x4_add(vsum018, vsum67);
168 const v128_t vsum = wasm_f32x4_add(vsum2345, vsum01678);
169
170 v128_t vout = wasm_f32x4_mul(vsum, vmultiplier);
171 const v128_t vufmask = wasm_f32x4_lt(vout, vmin);
172 const v128_t vofmask = wasm_f32x4_le(vmax, vout);
173 vout = wasm_v128_bitselect(vmin, vout, vufmask);
174 vout = wasm_v128_bitselect(vmax, vout, vofmask);
175
176 if (c & 2) {
177 *((double*) output) = wasm_f64x2_extract_lane(vout, 0);
178 output += 2;
179 vout = wasm_v32x4_shuffle(vout, vout, 2, 3, 2, 3);
180 }
181 if (c & 1) {
182 *output++ = wasm_f32x4_extract_lane(vout, 0);
183 }
184 }
185 output = (float*) ((uintptr_t) output + output_increment);
186 } while (--output_pixels != 0);
187 }
188