1 // Copyright 2019 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 <arm_neon.h>
9
10 #include <xnnpack/maxpool.h>
11
12
xnn_f32_maxpool_minmax_ukernel_9p8x__neon_c4(size_t output_pixels,size_t kernel_elements,size_t channels,const float ** input,size_t input_offset,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_maxpool_minmax_ukernel_9p8x__neon_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 float* output,
20 size_t input_increment,
21 size_t output_increment,
22 const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
23 {
24 assert(output_pixels != 0);
25 assert(kernel_elements != 0);
26 assert(channels != 0);
27
28 const float32x4_t voutput_max = vld1q_dup_f32(¶ms->scalar.max);
29 const float32x4_t voutput_min = vld1q_dup_f32(¶ms->scalar.min);
30 do {
31 float* o = output;
32 {
33 const float* i0 = *input++;
34 const float* i1 = *input++;
35 const float* i2 = *input++;
36 const float* i3 = *input++;
37 const float* i4 = *input++;
38 const float* i5 = *input++;
39 const float* i6 = *input++;
40 const float* i7 = *input++;
41 const float* i8 = *input++;
42 i0 = (const float*) ((uintptr_t) i0 + input_offset);
43 i1 = (const float*) ((uintptr_t) i1 + input_offset);
44 i2 = (const float*) ((uintptr_t) i2 + input_offset);
45 i3 = (const float*) ((uintptr_t) i3 + input_offset);
46 i4 = (const float*) ((uintptr_t) i4 + input_offset);
47 i5 = (const float*) ((uintptr_t) i5 + input_offset);
48 i6 = (const float*) ((uintptr_t) i6 + input_offset);
49 i7 = (const float*) ((uintptr_t) i7 + input_offset);
50 i8 = (const float*) ((uintptr_t) i8 + input_offset);
51 if (kernel_elements < 2) {
52 i1 = i0;
53 }
54 if (kernel_elements <= 2) {
55 i2 = i0;
56 }
57 if (kernel_elements < 4) {
58 i3 = i0;
59 }
60 if (kernel_elements <= 4) {
61 i4 = i0;
62 }
63 if (kernel_elements < 6) {
64 i5 = i0;
65 }
66 if (kernel_elements <= 6) {
67 i6 = i0;
68 }
69 if (kernel_elements < 8) {
70 i7 = i0;
71 }
72 if (kernel_elements <= 8) {
73 i8 = i0;
74 }
75
76 size_t c = channels;
77 for (; c >= 4; c -= 4) {
78 const float32x4_t vi0 = vld1q_f32(i0); i0 += 4;
79 const float32x4_t vi1 = vld1q_f32(i1); i1 += 4;
80 const float32x4_t vi2 = vld1q_f32(i2); i2 += 4;
81 const float32x4_t vi3 = vld1q_f32(i3); i3 += 4;
82 const float32x4_t vi4 = vld1q_f32(i4); i4 += 4;
83 const float32x4_t vi5 = vld1q_f32(i5); i5 += 4;
84 const float32x4_t vi6 = vld1q_f32(i6); i6 += 4;
85 const float32x4_t vi7 = vld1q_f32(i7); i7 += 4;
86 const float32x4_t vi8 = vld1q_f32(i8); i8 += 4;
87
88 const float32x4_t vmax018 = vmaxq_f32(vmaxq_f32(vi0, vi1), vi8);
89 const float32x4_t vmax23 = vmaxq_f32(vi2, vi3);
90 const float32x4_t vmax45 = vmaxq_f32(vi4, vi5);
91 const float32x4_t vmax67 = vmaxq_f32(vi6, vi7);
92
93 const float32x4_t vmax2345 = vmaxq_f32(vmax23, vmax45);
94 const float32x4_t vmax01678 = vmaxq_f32(vmax018, vmax67);
95 const float32x4_t vmax = vmaxq_f32(vmax2345, vmax01678);
96 const float32x4_t vout = vmaxq_f32(vminq_f32(vmax, voutput_max), voutput_min);
97
98 vst1q_f32(o, vout); o += 4;
99 }
100 if (c != 0) {
101 const float32x4_t vi0 = vld1q_f32(i0); i0 += 4;
102 const float32x4_t vi1 = vld1q_f32(i1); i1 += 4;
103 const float32x4_t vi2 = vld1q_f32(i2); i2 += 4;
104 const float32x4_t vi3 = vld1q_f32(i3); i3 += 4;
105 const float32x4_t vi4 = vld1q_f32(i4); i4 += 4;
106 const float32x4_t vi5 = vld1q_f32(i5); i5 += 4;
107 const float32x4_t vi6 = vld1q_f32(i6); i6 += 4;
108 const float32x4_t vi7 = vld1q_f32(i7); i7 += 4;
109 const float32x4_t vi8 = vld1q_f32(i8); i8 += 4;
110
111 const float32x4_t vmax018 = vmaxq_f32(vmaxq_f32(vi0, vi1), vi8);
112 const float32x4_t vmax23 = vmaxq_f32(vi2, vi3);
113 const float32x4_t vmax45 = vmaxq_f32(vi4, vi5);
114 const float32x4_t vmax67 = vmaxq_f32(vi6, vi7);
115
116 const float32x4_t vmax2345 = vmaxq_f32(vmax23, vmax45);
117 const float32x4_t vmax01678 = vmaxq_f32(vmax018, vmax67);
118 const float32x4_t vmax = vmaxq_f32(vmax2345, vmax01678);
119 float32x4_t vout = vmaxq_f32(vminq_f32(vmax, voutput_max), voutput_min);
120
121 float32x2_t vout_lo = vget_low_f32(vout);
122 if (c & 2) {
123 vst1_f32(o, vout_lo); o += 2;
124 vout_lo = vget_high_f32(vout);
125 }
126 if (c & 1) {
127 vst1_lane_f32(o, vout_lo, 0); o += 1;
128 }
129 }
130 }
131
132 for (ptrdiff_t k = (ptrdiff_t) kernel_elements - 9; k > 0; k -= 8) {
133 const float* i0 = *input++;
134 const float* i1 = *input++;
135 const float* i2 = *input++;
136 const float* i3 = *input++;
137 const float* i4 = *input++;
138 const float* i5 = *input++;
139 const float* i6 = *input++;
140 const float* i7 = *input++;
141 i0 = (const float*) ((uintptr_t) i0 + input_offset);
142 i1 = (const float*) ((uintptr_t) i1 + input_offset);
143 i2 = (const float*) ((uintptr_t) i2 + input_offset);
144 i3 = (const float*) ((uintptr_t) i3 + input_offset);
145 i4 = (const float*) ((uintptr_t) i4 + input_offset);
146 i5 = (const float*) ((uintptr_t) i5 + input_offset);
147 i6 = (const float*) ((uintptr_t) i6 + input_offset);
148 i7 = (const float*) ((uintptr_t) i7 + input_offset);
149 if (k < 2) {
150 i1 = i0;
151 }
152 if (k <= 2) {
153 i2 = i0;
154 }
155 if (k < 4) {
156 i3 = i0;
157 }
158 if (k <= 4) {
159 i4 = i0;
160 }
161 if (k < 6) {
162 i5 = i0;
163 }
164 if (k <= 6) {
165 i6 = i0;
166 }
167 if (k < 8) {
168 i7 = i0;
169 }
170
171 o = output;
172 size_t c = channels;
173 for (; c >= 4; c -= 4) {
174 const float32x4_t vi0 = vld1q_f32(i0); i0 += 4;
175 const float32x4_t vi1 = vld1q_f32(i1); i1 += 4;
176 const float32x4_t vi2 = vld1q_f32(i2); i2 += 4;
177 const float32x4_t vi3 = vld1q_f32(i3); i3 += 4;
178 const float32x4_t vi4 = vld1q_f32(i4); i4 += 4;
179 const float32x4_t vi5 = vld1q_f32(i5); i5 += 4;
180 const float32x4_t vi6 = vld1q_f32(i6); i6 += 4;
181 const float32x4_t vi7 = vld1q_f32(i7); i7 += 4;
182 const float32x4_t vo = vld1q_f32(o);
183
184 const float32x4_t vmax01 = vmaxq_f32(vmaxq_f32(vi0, vi1), vo);
185 const float32x4_t vmax23 = vmaxq_f32(vi2, vi3);
186 const float32x4_t vmax45 = vmaxq_f32(vi4, vi5);
187 const float32x4_t vmax67 = vmaxq_f32(vi6, vi7);
188
189 const float32x4_t vmax2345 = vmaxq_f32(vmax23, vmax45);
190 const float32x4_t vmax0167 = vmaxq_f32(vmax01, vmax67);
191 const float32x4_t vmax = vmaxq_f32(vmax2345, vmax0167);
192 const float32x4_t vout = vmaxq_f32(vminq_f32(vmax, voutput_max), voutput_min);
193
194 vst1q_f32(o, vout); o += 4;
195 }
196 if (c != 0) {
197 const float32x4_t vi0 = vld1q_f32(i0);
198 const float32x4_t vi1 = vld1q_f32(i1);
199 const float32x4_t vi2 = vld1q_f32(i2);
200 const float32x4_t vi3 = vld1q_f32(i3);
201 const float32x4_t vi4 = vld1q_f32(i4);
202 const float32x4_t vi5 = vld1q_f32(i5);
203 const float32x4_t vi6 = vld1q_f32(i6);
204 const float32x4_t vi7 = vld1q_f32(i7);
205 const float32x4_t vo = vld1q_f32(o);
206
207 const float32x4_t vmax01 = vmaxq_f32(vmaxq_f32(vi0, vi1), vo);
208 const float32x4_t vmax23 = vmaxq_f32(vi2, vi3);
209 const float32x4_t vmax45 = vmaxq_f32(vi4, vi5);
210 const float32x4_t vmax67 = vmaxq_f32(vi6, vi7);
211
212 const float32x4_t vmax2345 = vmaxq_f32(vmax23, vmax45);
213 const float32x4_t vmax0167 = vmaxq_f32(vmax01, vmax67);
214 const float32x4_t vmax = vmaxq_f32(vmax2345, vmax0167);
215 float32x4_t vout = vmaxq_f32(vminq_f32(vmax, voutput_max), voutput_min);
216
217 float32x2_t vout_lo = vget_low_f32(vout);
218 if (c & 2) {
219 vst1_f32(o, vout_lo); o += 2;
220 vout_lo = vget_high_f32(vout);
221 }
222 if (c & 1) {
223 vst1_lane_f32(o, vout_lo, 0); o += 1;
224 }
225 }
226 }
227 input = (const float**) ((uintptr_t) input + input_increment);
228 output = (float*) ((uintptr_t) o + output_increment);
229 } while (--output_pixels != 0);
230 }
231