Marat Dukhan | 80fc932 | 2019-09-29 21:06:36 -0700 | [diff] [blame] | 1 | // 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 Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 8 | |
| 9 | #include <assert.h> |
| 10 | |
| 11 | #include <arm_neon.h> |
| 12 | |
| 13 | #include <xnnpack/maxpool.h> |
| 14 | |
| 15 | |
Marat Dukhan | 24d9a03 | 2020-04-14 18:49:54 -0700 | [diff] [blame] | 16 | XNN_DISABLE_TSAN void xnn_u8_maxpool_minmax_ukernel_9p8x__neon_c16( |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 17 | size_t output_pixels, |
| 18 | size_t kernel_elements, |
| 19 | size_t channels, |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 20 | const uint8_t** input, |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 21 | size_t input_offset, |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 22 | uint8_t* output, |
| 23 | size_t input_increment, |
| 24 | size_t output_increment, |
Marat Dukhan | b2217dd | 2020-05-28 17:30:28 -0700 | [diff] [blame] | 25 | const union xnn_u8_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 26 | { |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 27 | assert(output_pixels != 0); |
| 28 | assert(kernel_elements != 0); |
| 29 | assert(channels != 0); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 30 | |
| 31 | const uint8x16_t voutput_max = vld1q_dup_u8(¶ms->neon.max); |
| 32 | const uint8x16_t voutput_min = vld1q_dup_u8(¶ms->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 Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 45 | 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 Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 55 | i1 = i0; |
| 56 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 57 | if (kernel_elements <= 2) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 58 | i2 = i0; |
| 59 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 60 | if (kernel_elements < 4) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 61 | i3 = i0; |
| 62 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 63 | if (kernel_elements <= 4) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 64 | i4 = i0; |
| 65 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 66 | if (kernel_elements < 6) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 67 | i5 = i0; |
| 68 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 69 | if (kernel_elements <= 6) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 70 | i6 = i0; |
| 71 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 72 | if (kernel_elements < 8) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 73 | i7 = i0; |
| 74 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 75 | if (kernel_elements <= 8) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 76 | i8 = i0; |
| 77 | } |
| 78 | |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 79 | size_t c = channels; |
| 80 | for (; c >= 16; c -= 16) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 81 | 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 Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 103 | if (c != 0) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 104 | 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 Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 125 | if (c & 8) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 126 | vst1_u8(o, vout_lo); o += 8; |
| 127 | vout_lo = vget_high_u8(vout); |
| 128 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 129 | if (c & 4) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 130 | 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 Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 133 | if (c & 2) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 134 | 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 Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 137 | if (c & 1) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 138 | vst1_lane_u8(o, vout_lo, 0); o += 1; |
| 139 | } |
| 140 | } |
| 141 | } |
Marat Dukhan | e0df831 | 2019-10-22 18:16:56 -0700 | [diff] [blame] | 142 | |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 143 | for (ptrdiff_t k = (ptrdiff_t) kernel_elements - 9; k > 0; k -= 8) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 144 | 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 Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 152 | 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 Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 161 | i1 = i0; |
| 162 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 163 | if (k <= 2) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 164 | i2 = i0; |
| 165 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 166 | if (k < 4) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 167 | i3 = i0; |
| 168 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 169 | if (k <= 4) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 170 | i4 = i0; |
| 171 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 172 | if (k < 6) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 173 | i5 = i0; |
| 174 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 175 | if (k <= 6) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 176 | i6 = i0; |
| 177 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 178 | if (k < 8) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 179 | i7 = i0; |
| 180 | } |
| 181 | |
| 182 | o = output; |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 183 | size_t c = channels; |
| 184 | for (; c >= 16; c -= 16) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 185 | 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 Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 207 | if (c != 0) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 208 | 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 Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 229 | if (c & 8) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 230 | vst1_u8(o, vout_lo); o += 8; |
| 231 | vout_lo = vget_high_u8(vout); |
| 232 | } |
Marat Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 233 | if (c & 4) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 234 | 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 Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 237 | if (c & 2) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 238 | 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 Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 241 | if (c & 1) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 242 | 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 Dukhan | 329da64 | 2019-11-19 21:44:39 -0800 | [diff] [blame] | 248 | } while (--output_pixels != 0); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 249 | } |