blob: 8839f8a9cd7bba5720e83f8ad9f82aaa106e71f0 [file] [log] [blame]
Marat Dukhan80fc9322019-09-29 21:06:36 -07001// Copyright (c) Facebook, Inc. and its affiliates.
2// All rights reserved.
3//
4// Copyright 2019 Google LLC
5//
6// This source code is licensed under the BSD-style license found in the
7// LICENSE file in the root directory of this source tree.
XNNPACK Teamb455b122019-09-27 18:10:33 -07008
9#include <assert.h>
10
11#include <arm_neon.h>
12
13#include <xnnpack/maxpool.h>
14
15
Marat Dukhan24d9a032020-04-14 18:49:54 -070016XNN_DISABLE_TSAN void xnn_u8_maxpool_minmax_ukernel_9p8x__neon_c16(
Marat Dukhan329da642019-11-19 21:44:39 -080017 size_t output_pixels,
18 size_t kernel_elements,
19 size_t channels,
XNNPACK Teamb455b122019-09-27 18:10:33 -070020 const uint8_t** input,
Marat Dukhan329da642019-11-19 21:44:39 -080021 size_t input_offset,
XNNPACK Teamb455b122019-09-27 18:10:33 -070022 uint8_t* output,
23 size_t input_increment,
24 size_t output_increment,
Marat Dukhanb2217dd2020-05-28 17:30:28 -070025 const union xnn_u8_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
XNNPACK Teamb455b122019-09-27 18:10:33 -070026{
Marat Dukhan329da642019-11-19 21:44:39 -080027 assert(output_pixels != 0);
28 assert(kernel_elements != 0);
29 assert(channels != 0);
XNNPACK Teamb455b122019-09-27 18:10:33 -070030
31 const uint8x16_t voutput_max = vld1q_dup_u8(&params->neon.max);
32 const uint8x16_t voutput_min = vld1q_dup_u8(&params->neon.min);
33 do {
34 uint8_t* o = output;
35 {
36 const uint8_t* i0 = *input++;
37 const uint8_t* i1 = *input++;
38 const uint8_t* i2 = *input++;
39 const uint8_t* i3 = *input++;
40 const uint8_t* i4 = *input++;
41 const uint8_t* i5 = *input++;
42 const uint8_t* i6 = *input++;
43 const uint8_t* i7 = *input++;
44 const uint8_t* i8 = *input++;
Marat Dukhan329da642019-11-19 21:44:39 -080045 i0 = (const uint8_t*) ((uintptr_t) i0 + input_offset);
46 i1 = (const uint8_t*) ((uintptr_t) i1 + input_offset);
47 i2 = (const uint8_t*) ((uintptr_t) i2 + input_offset);
48 i3 = (const uint8_t*) ((uintptr_t) i3 + input_offset);
49 i4 = (const uint8_t*) ((uintptr_t) i4 + input_offset);
50 i5 = (const uint8_t*) ((uintptr_t) i5 + input_offset);
51 i6 = (const uint8_t*) ((uintptr_t) i6 + input_offset);
52 i7 = (const uint8_t*) ((uintptr_t) i7 + input_offset);
53 i8 = (const uint8_t*) ((uintptr_t) i8 + input_offset);
54 if (kernel_elements < 2) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070055 i1 = i0;
56 }
Marat Dukhan329da642019-11-19 21:44:39 -080057 if (kernel_elements <= 2) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070058 i2 = i0;
59 }
Marat Dukhan329da642019-11-19 21:44:39 -080060 if (kernel_elements < 4) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070061 i3 = i0;
62 }
Marat Dukhan329da642019-11-19 21:44:39 -080063 if (kernel_elements <= 4) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070064 i4 = i0;
65 }
Marat Dukhan329da642019-11-19 21:44:39 -080066 if (kernel_elements < 6) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070067 i5 = i0;
68 }
Marat Dukhan329da642019-11-19 21:44:39 -080069 if (kernel_elements <= 6) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070070 i6 = i0;
71 }
Marat Dukhan329da642019-11-19 21:44:39 -080072 if (kernel_elements < 8) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070073 i7 = i0;
74 }
Marat Dukhan329da642019-11-19 21:44:39 -080075 if (kernel_elements <= 8) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070076 i8 = i0;
77 }
78
Marat Dukhan329da642019-11-19 21:44:39 -080079 size_t c = channels;
80 for (; c >= 16; c -= 16) {
XNNPACK Teamb455b122019-09-27 18:10:33 -070081 const uint8x16_t vi0 = vld1q_u8(i0); i0 += 16;
82 const uint8x16_t vi1 = vld1q_u8(i1); i1 += 16;
83 const uint8x16_t vi2 = vld1q_u8(i2); i2 += 16;
84 const uint8x16_t vi3 = vld1q_u8(i3); i3 += 16;
85 const uint8x16_t vi4 = vld1q_u8(i4); i4 += 16;
86 const uint8x16_t vi5 = vld1q_u8(i5); i5 += 16;
87 const uint8x16_t vi6 = vld1q_u8(i6); i6 += 16;
88 const uint8x16_t vi7 = vld1q_u8(i7); i7 += 16;
89 const uint8x16_t vi8 = vld1q_u8(i8); i8 += 16;
90
91 const uint8x16_t vmax018 = vmaxq_u8(vmaxq_u8(vi0, vi1), vi8);
92 const uint8x16_t vmax23 = vmaxq_u8(vi2, vi3);
93 const uint8x16_t vmax45 = vmaxq_u8(vi4, vi5);
94 const uint8x16_t vmax67 = vmaxq_u8(vi6, vi7);
95
96 const uint8x16_t vmax2345 = vmaxq_u8(vmax23, vmax45);
97 const uint8x16_t vmax01678 = vmaxq_u8(vmax018, vmax67);
98 const uint8x16_t vmax = vmaxq_u8(vmax2345, vmax01678);
99 const uint8x16_t vout = vmaxq_u8(vminq_u8(vmax, voutput_max), voutput_min);
100
101 vst1q_u8(o, vout); o += 16;
102 }
Marat Dukhan329da642019-11-19 21:44:39 -0800103 if (c != 0) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700104 const uint8x16_t vi0 = vld1q_u8(i0);
105 const uint8x16_t vi1 = vld1q_u8(i1);
106 const uint8x16_t vi2 = vld1q_u8(i2);
107 const uint8x16_t vi3 = vld1q_u8(i3);
108 const uint8x16_t vi4 = vld1q_u8(i4);
109 const uint8x16_t vi5 = vld1q_u8(i5);
110 const uint8x16_t vi6 = vld1q_u8(i6);
111 const uint8x16_t vi7 = vld1q_u8(i7);
112 const uint8x16_t vi8 = vld1q_u8(i8);
113
114 const uint8x16_t vmax018 = vmaxq_u8(vmaxq_u8(vi0, vi1), vi8);
115 const uint8x16_t vmax23 = vmaxq_u8(vi2, vi3);
116 const uint8x16_t vmax45 = vmaxq_u8(vi4, vi5);
117 const uint8x16_t vmax67 = vmaxq_u8(vi6, vi7);
118
119 const uint8x16_t vmax2345 = vmaxq_u8(vmax23, vmax45);
120 const uint8x16_t vmax01678 = vmaxq_u8(vmax018, vmax67);
121 const uint8x16_t vmax = vmaxq_u8(vmax2345, vmax01678);
122 const uint8x16_t vout = vmaxq_u8(vminq_u8(vmax, voutput_max), voutput_min);
123
124 uint8x8_t vout_lo = vget_low_u8(vout);
Marat Dukhan329da642019-11-19 21:44:39 -0800125 if (c & 8) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700126 vst1_u8(o, vout_lo); o += 8;
127 vout_lo = vget_high_u8(vout);
128 }
Marat Dukhan329da642019-11-19 21:44:39 -0800129 if (c & 4) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700130 vst1_lane_u32(__builtin_assume_aligned(o, 1), vreinterpret_u32_u8(vout_lo), 0); o += 4;
131 vout_lo = vext_u8(vout_lo, vout_lo, 4);
132 }
Marat Dukhan329da642019-11-19 21:44:39 -0800133 if (c & 2) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700134 vst1_lane_u16(__builtin_assume_aligned(o, 1), vreinterpret_u16_u8(vout_lo), 0); o += 2;
135 vout_lo = vext_u8(vout_lo, vout_lo, 2);
136 }
Marat Dukhan329da642019-11-19 21:44:39 -0800137 if (c & 1) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700138 vst1_lane_u8(o, vout_lo, 0); o += 1;
139 }
140 }
141 }
Marat Dukhane0df8312019-10-22 18:16:56 -0700142
Marat Dukhan329da642019-11-19 21:44:39 -0800143 for (ptrdiff_t k = (ptrdiff_t) kernel_elements - 9; k > 0; k -= 8) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700144 const uint8_t* i0 = *input++;
145 const uint8_t* i1 = *input++;
146 const uint8_t* i2 = *input++;
147 const uint8_t* i3 = *input++;
148 const uint8_t* i4 = *input++;
149 const uint8_t* i5 = *input++;
150 const uint8_t* i6 = *input++;
151 const uint8_t* i7 = *input++;
Marat Dukhan329da642019-11-19 21:44:39 -0800152 i0 = (const uint8_t*) ((uintptr_t) i0 + input_offset);
153 i1 = (const uint8_t*) ((uintptr_t) i1 + input_offset);
154 i2 = (const uint8_t*) ((uintptr_t) i2 + input_offset);
155 i3 = (const uint8_t*) ((uintptr_t) i3 + input_offset);
156 i4 = (const uint8_t*) ((uintptr_t) i4 + input_offset);
157 i5 = (const uint8_t*) ((uintptr_t) i5 + input_offset);
158 i6 = (const uint8_t*) ((uintptr_t) i6 + input_offset);
159 i7 = (const uint8_t*) ((uintptr_t) i7 + input_offset);
160 if (k < 2) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700161 i1 = i0;
162 }
Marat Dukhan329da642019-11-19 21:44:39 -0800163 if (k <= 2) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700164 i2 = i0;
165 }
Marat Dukhan329da642019-11-19 21:44:39 -0800166 if (k < 4) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700167 i3 = i0;
168 }
Marat Dukhan329da642019-11-19 21:44:39 -0800169 if (k <= 4) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700170 i4 = i0;
171 }
Marat Dukhan329da642019-11-19 21:44:39 -0800172 if (k < 6) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700173 i5 = i0;
174 }
Marat Dukhan329da642019-11-19 21:44:39 -0800175 if (k <= 6) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700176 i6 = i0;
177 }
Marat Dukhan329da642019-11-19 21:44:39 -0800178 if (k < 8) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700179 i7 = i0;
180 }
181
182 o = output;
Marat Dukhan329da642019-11-19 21:44:39 -0800183 size_t c = channels;
184 for (; c >= 16; c -= 16) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700185 const uint8x16_t vi0 = vld1q_u8(i0); i0 += 16;
186 const uint8x16_t vi1 = vld1q_u8(i1); i1 += 16;
187 const uint8x16_t vi2 = vld1q_u8(i2); i2 += 16;
188 const uint8x16_t vi3 = vld1q_u8(i3); i3 += 16;
189 const uint8x16_t vi4 = vld1q_u8(i4); i4 += 16;
190 const uint8x16_t vi5 = vld1q_u8(i5); i5 += 16;
191 const uint8x16_t vi6 = vld1q_u8(i6); i6 += 16;
192 const uint8x16_t vi7 = vld1q_u8(i7); i7 += 16;
193 const uint8x16_t vo = vld1q_u8(o);
194
195 const uint8x16_t vmax01 = vmaxq_u8(vmaxq_u8(vi0, vi1), vo);
196 const uint8x16_t vmax23 = vmaxq_u8(vi2, vi3);
197 const uint8x16_t vmax45 = vmaxq_u8(vi4, vi5);
198 const uint8x16_t vmax67 = vmaxq_u8(vi6, vi7);
199
200 const uint8x16_t vmax2345 = vmaxq_u8(vmax23, vmax45);
201 const uint8x16_t vmax0167 = vmaxq_u8(vmax01, vmax67);
202 const uint8x16_t vmax = vmaxq_u8(vmax2345, vmax0167);
203 const uint8x16_t vout = vmaxq_u8(vminq_u8(vmax, voutput_max), voutput_min);
204
205 vst1q_u8(o, vout); o += 16;
206 }
Marat Dukhan329da642019-11-19 21:44:39 -0800207 if (c != 0) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700208 const uint8x16_t vi0 = vld1q_u8(i0);
209 const uint8x16_t vi1 = vld1q_u8(i1);
210 const uint8x16_t vi2 = vld1q_u8(i2);
211 const uint8x16_t vi3 = vld1q_u8(i3);
212 const uint8x16_t vi4 = vld1q_u8(i4);
213 const uint8x16_t vi5 = vld1q_u8(i5);
214 const uint8x16_t vi6 = vld1q_u8(i6);
215 const uint8x16_t vi7 = vld1q_u8(i7);
216 const uint8x16_t vo = vld1q_u8(o);
217
218 const uint8x16_t vmax01 = vmaxq_u8(vmaxq_u8(vi0, vi1), vo);
219 const uint8x16_t vmax23 = vmaxq_u8(vi2, vi3);
220 const uint8x16_t vmax45 = vmaxq_u8(vi4, vi5);
221 const uint8x16_t vmax67 = vmaxq_u8(vi6, vi7);
222
223 const uint8x16_t vmax2345 = vmaxq_u8(vmax23, vmax45);
224 const uint8x16_t vmax0167 = vmaxq_u8(vmax01, vmax67);
225 const uint8x16_t vmax = vmaxq_u8(vmax2345, vmax0167);
226 const uint8x16_t vout = vmaxq_u8(vminq_u8(vmax, voutput_max), voutput_min);
227
228 uint8x8_t vout_lo = vget_low_u8(vout);
Marat Dukhan329da642019-11-19 21:44:39 -0800229 if (c & 8) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700230 vst1_u8(o, vout_lo); o += 8;
231 vout_lo = vget_high_u8(vout);
232 }
Marat Dukhan329da642019-11-19 21:44:39 -0800233 if (c & 4) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700234 vst1_lane_u32(__builtin_assume_aligned(o, 1), vreinterpret_u32_u8(vout_lo), 0); o += 4;
235 vout_lo = vext_u8(vout_lo, vout_lo, 4);
236 }
Marat Dukhan329da642019-11-19 21:44:39 -0800237 if (c & 2) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700238 vst1_lane_u16(__builtin_assume_aligned(o, 1), vreinterpret_u16_u8(vout_lo), 0); o += 2;
239 vout_lo = vext_u8(vout_lo, vout_lo, 2);
240 }
Marat Dukhan329da642019-11-19 21:44:39 -0800241 if (c & 1) {
XNNPACK Teamb455b122019-09-27 18:10:33 -0700242 vst1_lane_u8(o, vout_lo, 0); o += 1;
243 }
244 }
245 }
246 input = (const uint8_t**) ((uintptr_t) input + input_increment);
247 output = (uint8_t*) ((uintptr_t) o + output_increment);
Marat Dukhan329da642019-11-19 21:44:39 -0800248 } while (--output_pixels != 0);
XNNPACK Teamb455b122019-09-27 18:10:33 -0700249}