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 | |
Marat Dukhan | c72fa1e | 2019-11-27 11:54:03 -0800 | [diff] [blame] | 13 | #include <xnnpack/common.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 14 | #include <xnnpack/gavgpool.h> |
| 15 | |
| 16 | |
Marat Dukhan | 08b7a97 | 2020-07-14 18:17:29 -0700 | [diff] [blame^] | 17 | void xnn_qu8_gavgpool_minmax_ukernel_7x__neon_c8( |
Marat Dukhan | a63a6fc | 2020-03-10 06:12:48 -0700 | [diff] [blame] | 18 | size_t rows, |
| 19 | size_t channels, |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 20 | const uint8_t* input, |
| 21 | size_t input_stride, |
| 22 | const uint8_t* zero, |
| 23 | uint8_t* output, |
Marat Dukhan | 08b7a97 | 2020-07-14 18:17:29 -0700 | [diff] [blame^] | 24 | const union xnn_qu8_avgpool_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 25 | { |
Marat Dukhan | a63a6fc | 2020-03-10 06:12:48 -0700 | [diff] [blame] | 26 | assert(rows != 0); |
| 27 | assert(rows <= 7); |
| 28 | assert(channels != 0); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 29 | |
| 30 | const uint8_t* i0 = input; |
| 31 | const uint8_t* i1 = (const uint8_t*) ((uintptr_t) i0 + input_stride); |
Marat Dukhan | a63a6fc | 2020-03-10 06:12:48 -0700 | [diff] [blame] | 32 | if (rows < 2) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 33 | i1 = zero; |
| 34 | } |
| 35 | const uint8_t* i2 = (const uint8_t*) ((uintptr_t) i1 + input_stride); |
Marat Dukhan | a63a6fc | 2020-03-10 06:12:48 -0700 | [diff] [blame] | 36 | if (rows <= 2) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 37 | i2 = zero; |
| 38 | } |
| 39 | const uint8_t* i3 = (const uint8_t*) ((uintptr_t) i2 + input_stride); |
Marat Dukhan | a63a6fc | 2020-03-10 06:12:48 -0700 | [diff] [blame] | 40 | if (rows < 4) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 41 | i3 = zero; |
| 42 | } |
| 43 | const uint8_t* i4 = (const uint8_t*) ((uintptr_t) i3 + input_stride); |
Marat Dukhan | a63a6fc | 2020-03-10 06:12:48 -0700 | [diff] [blame] | 44 | if (rows <= 4) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 45 | i4 = zero; |
| 46 | } |
| 47 | const uint8_t* i5 = (const uint8_t*) ((uintptr_t) i4 + input_stride); |
Marat Dukhan | a63a6fc | 2020-03-10 06:12:48 -0700 | [diff] [blame] | 48 | if (rows < 6) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 49 | i5 = zero; |
| 50 | } |
| 51 | const uint8_t* i6 = (const uint8_t*) ((uintptr_t) i5 + input_stride); |
Marat Dukhan | a63a6fc | 2020-03-10 06:12:48 -0700 | [diff] [blame] | 52 | if (rows <= 6) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 53 | i6 = zero; |
| 54 | } |
| 55 | |
| 56 | const int32x4_t vbias = vld1q_dup_s32(¶ms->neon.bias); |
Marat Dukhan | c72fa1e | 2019-11-27 11:54:03 -0800 | [diff] [blame] | 57 | #if XNN_ARCH_ARM64 |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 58 | const int32x4_t vmultiplier = vld1q_dup_s32(¶ms->neon.multiplier); |
| 59 | #else |
| 60 | const int32x2_t vmultiplier = vld1_dup_s32(¶ms->neon.multiplier); |
| 61 | #endif |
| 62 | const int64x2_t vleft_shift = vld1q_dup_s64(¶ms->neon.left_shift); |
| 63 | const int16x8_t voutput_zero_point = vld1q_dup_s16(¶ms->neon.output_zero_point); |
| 64 | const uint8x8_t voutput_min = vld1_dup_u8(¶ms->neon.output_min); |
| 65 | const uint8x8_t voutput_max = vld1_dup_u8(¶ms->neon.output_max); |
Marat Dukhan | a63a6fc | 2020-03-10 06:12:48 -0700 | [diff] [blame] | 66 | while (channels >= 8) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 67 | const uint8x8_t vi0 = vld1_u8(i0); i0 += 8; |
| 68 | const uint8x8_t vi1 = vld1_u8(i1); i1 += 8; |
| 69 | const uint8x8_t vi2 = vld1_u8(i2); i2 += 8; |
| 70 | const uint8x8_t vi3 = vld1_u8(i3); i3 += 8; |
| 71 | const uint8x8_t vi4 = vld1_u8(i4); i4 += 8; |
| 72 | const uint8x8_t vi5 = vld1_u8(i5); i5 += 8; |
| 73 | const uint8x8_t vi6 = vld1_u8(i6); i6 += 8; |
| 74 | |
| 75 | const uint16x8_t vsum01 = vaddl_u8(vi0, vi1); |
| 76 | const uint16x8_t vsum23 = vaddl_u8(vi2, vi3); |
| 77 | const uint16x8_t vsum45 = vaddl_u8(vi4, vi5); |
| 78 | |
| 79 | const uint16x8_t vsum016 = vaddw_u8(vsum01, vi6); |
| 80 | const uint16x8_t vsum2345 = vaddq_u16(vsum23, vsum45); |
| 81 | |
| 82 | const int16x8_t vsum = vreinterpretq_s16_u16(vaddq_u16(vsum016, vsum2345)); |
| 83 | int32x4_t vacc_lo = vaddw_s16(vbias, vget_low_s16(vsum)); |
| 84 | int32x4_t vacc_hi = vaddw_s16(vbias, vget_high_s16(vsum)); |
| 85 | |
| 86 | const int32x4_t vneg_mask_lo = vreinterpretq_s32_u32(vcltq_s32(vacc_lo, vmovq_n_s32(0))); |
| 87 | const int32x4_t vneg_mask_hi = vreinterpretq_s32_u32(vcltq_s32(vacc_hi, vmovq_n_s32(0))); |
| 88 | |
Marat Dukhan | c72fa1e | 2019-11-27 11:54:03 -0800 | [diff] [blame] | 89 | #if XNN_ARCH_ARM64 |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 90 | const int64x2_t vproduct01 = vmull_s32(vget_low_s32(vacc_lo), vget_low_s32(vmultiplier)); |
| 91 | const int64x2_t vproduct23 = vmull_high_s32(vacc_lo, vmultiplier); |
| 92 | const int64x2_t vproduct45 = vmull_s32(vget_low_s32(vacc_hi), vget_low_s32(vmultiplier)); |
| 93 | const int64x2_t vproduct67 = vmull_high_s32(vacc_hi, vmultiplier); |
| 94 | |
| 95 | const int64x2_t vadjusted_product01 = vaddw_s32(vproduct01, vget_low_s32(vneg_mask_lo)); |
| 96 | const int64x2_t vadjusted_product23 = vaddw_high_s32(vproduct23, vneg_mask_lo); |
| 97 | const int64x2_t vadjusted_product45 = vaddw_s32(vproduct45, vget_low_s32(vneg_mask_hi)); |
| 98 | const int64x2_t vadjusted_product67 = vaddw_high_s32(vproduct67, vneg_mask_hi); |
| 99 | #else |
| 100 | const int64x2_t vproduct01 = vmull_s32(vget_low_s32(vacc_lo), vmultiplier); |
| 101 | const int64x2_t vproduct23 = vmull_s32(vget_high_s32(vacc_lo), vmultiplier); |
| 102 | const int64x2_t vproduct45 = vmull_s32(vget_low_s32(vacc_hi), vmultiplier); |
| 103 | const int64x2_t vproduct67 = vmull_s32(vget_high_s32(vacc_hi), vmultiplier); |
| 104 | |
| 105 | const int64x2_t vadjusted_product01 = vaddw_s32(vproduct01, vget_low_s32(vneg_mask_lo)); |
| 106 | const int64x2_t vadjusted_product23 = vaddw_s32(vproduct23, vget_high_s32(vneg_mask_lo)); |
| 107 | const int64x2_t vadjusted_product45 = vaddw_s32(vproduct45, vget_low_s32(vneg_mask_hi)); |
| 108 | const int64x2_t vadjusted_product67 = vaddw_s32(vproduct67, vget_high_s32(vneg_mask_hi)); |
| 109 | #endif |
| 110 | |
| 111 | const int64x2_t vscaled_acc01 = vrshlq_s64(vadjusted_product01, vleft_shift); |
| 112 | const int64x2_t vscaled_acc23 = vrshlq_s64(vadjusted_product23, vleft_shift); |
| 113 | const int64x2_t vscaled_acc45 = vrshlq_s64(vadjusted_product45, vleft_shift); |
| 114 | const int64x2_t vscaled_acc67 = vrshlq_s64(vadjusted_product67, vleft_shift); |
| 115 | |
Marat Dukhan | c72fa1e | 2019-11-27 11:54:03 -0800 | [diff] [blame] | 116 | #if XNN_ARCH_ARM64 |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 117 | vacc_lo = vuzp1q_s32(vreinterpretq_s32_s64(vscaled_acc01), vreinterpretq_s32_s64(vscaled_acc23)); |
| 118 | vacc_hi = vuzp1q_s32(vreinterpretq_s32_s64(vscaled_acc45), vreinterpretq_s32_s64(vscaled_acc67)); |
| 119 | |
| 120 | const int16x8_t vacc = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc_lo), vacc_hi), voutput_zero_point); |
| 121 | #else |
| 122 | vacc_lo = vcombine_s32(vmovn_s64(vscaled_acc01), vmovn_s64(vscaled_acc23)); |
| 123 | vacc_hi = vcombine_s32(vmovn_s64(vscaled_acc45), vmovn_s64(vscaled_acc67)); |
| 124 | |
| 125 | const int16x8_t vacc = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc_lo), vqmovn_s32(vacc_hi)), voutput_zero_point); |
| 126 | #endif |
| 127 | |
| 128 | uint8x8_t vout = vqmovun_s16(vacc); |
| 129 | vout = vmax_u8(vout, voutput_min); |
| 130 | vout = vmin_u8(vout, voutput_max); |
| 131 | |
| 132 | vst1_u8(output, vout); output += 8; |
| 133 | |
Marat Dukhan | a63a6fc | 2020-03-10 06:12:48 -0700 | [diff] [blame] | 134 | channels -= 8; |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 135 | } |
Marat Dukhan | a63a6fc | 2020-03-10 06:12:48 -0700 | [diff] [blame] | 136 | if (channels != 0) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 137 | const uint8x8_t vi0 = vld1_u8(i0); |
| 138 | const uint8x8_t vi1 = vld1_u8(i1); |
| 139 | const uint8x8_t vi2 = vld1_u8(i2); |
| 140 | const uint8x8_t vi3 = vld1_u8(i3); |
| 141 | const uint8x8_t vi4 = vld1_u8(i4); |
| 142 | const uint8x8_t vi5 = vld1_u8(i5); |
| 143 | const uint8x8_t vi6 = vld1_u8(i6); |
| 144 | |
| 145 | const uint16x8_t vsum01 = vaddl_u8(vi0, vi1); |
| 146 | const uint16x8_t vsum23 = vaddl_u8(vi2, vi3); |
| 147 | const uint16x8_t vsum45 = vaddl_u8(vi4, vi5); |
| 148 | |
| 149 | const uint16x8_t vsum016 = vaddw_u8(vsum01, vi6); |
| 150 | const uint16x8_t vsum2345 = vaddq_u16(vsum23, vsum45); |
| 151 | |
| 152 | const int16x8_t vsum = vreinterpretq_s16_u16(vaddq_u16(vsum016, vsum2345)); |
| 153 | int32x4_t vacc_lo = vaddw_s16(vbias, vget_low_s16(vsum)); |
| 154 | int32x4_t vacc_hi = vaddw_s16(vbias, vget_high_s16(vsum)); |
| 155 | |
| 156 | const int32x4_t vneg_mask_lo = vreinterpretq_s32_u32(vcltq_s32(vacc_lo, vmovq_n_s32(0))); |
| 157 | const int32x4_t vneg_mask_hi = vreinterpretq_s32_u32(vcltq_s32(vacc_hi, vmovq_n_s32(0))); |
| 158 | |
Marat Dukhan | c72fa1e | 2019-11-27 11:54:03 -0800 | [diff] [blame] | 159 | #if XNN_ARCH_ARM64 |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 160 | const int64x2_t vproduct01 = vmull_s32(vget_low_s32(vacc_lo), vget_low_s32(vmultiplier)); |
| 161 | const int64x2_t vproduct23 = vmull_high_s32(vacc_lo, vmultiplier); |
| 162 | const int64x2_t vproduct45 = vmull_s32(vget_low_s32(vacc_hi), vget_low_s32(vmultiplier)); |
| 163 | const int64x2_t vproduct67 = vmull_high_s32(vacc_hi, vmultiplier); |
| 164 | |
| 165 | const int64x2_t vadjusted_product01 = vaddw_s32(vproduct01, vget_low_s32(vneg_mask_lo)); |
| 166 | const int64x2_t vadjusted_product23 = vaddw_high_s32(vproduct23, vneg_mask_lo); |
| 167 | const int64x2_t vadjusted_product45 = vaddw_s32(vproduct45, vget_low_s32(vneg_mask_hi)); |
| 168 | const int64x2_t vadjusted_product67 = vaddw_high_s32(vproduct67, vneg_mask_hi); |
| 169 | #else |
| 170 | const int64x2_t vproduct01 = vmull_s32(vget_low_s32(vacc_lo), vmultiplier); |
| 171 | const int64x2_t vproduct23 = vmull_s32(vget_high_s32(vacc_lo), vmultiplier); |
| 172 | const int64x2_t vproduct45 = vmull_s32(vget_low_s32(vacc_hi), vmultiplier); |
| 173 | const int64x2_t vproduct67 = vmull_s32(vget_high_s32(vacc_hi), vmultiplier); |
| 174 | |
| 175 | const int64x2_t vadjusted_product01 = vaddw_s32(vproduct01, vget_low_s32(vneg_mask_lo)); |
| 176 | const int64x2_t vadjusted_product23 = vaddw_s32(vproduct23, vget_high_s32(vneg_mask_lo)); |
| 177 | const int64x2_t vadjusted_product45 = vaddw_s32(vproduct45, vget_low_s32(vneg_mask_hi)); |
| 178 | const int64x2_t vadjusted_product67 = vaddw_s32(vproduct67, vget_high_s32(vneg_mask_hi)); |
| 179 | #endif |
| 180 | |
| 181 | const int64x2_t vscaled_acc01 = vrshlq_s64(vadjusted_product01, vleft_shift); |
| 182 | const int64x2_t vscaled_acc23 = vrshlq_s64(vadjusted_product23, vleft_shift); |
| 183 | const int64x2_t vscaled_acc45 = vrshlq_s64(vadjusted_product45, vleft_shift); |
| 184 | const int64x2_t vscaled_acc67 = vrshlq_s64(vadjusted_product67, vleft_shift); |
| 185 | |
Marat Dukhan | c72fa1e | 2019-11-27 11:54:03 -0800 | [diff] [blame] | 186 | #if XNN_ARCH_ARM64 |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 187 | vacc_lo = vuzp1q_s32(vreinterpretq_s32_s64(vscaled_acc01), vreinterpretq_s32_s64(vscaled_acc23)); |
| 188 | vacc_hi = vuzp1q_s32(vreinterpretq_s32_s64(vscaled_acc45), vreinterpretq_s32_s64(vscaled_acc67)); |
| 189 | |
| 190 | const int16x8_t vacc = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc_lo), vacc_hi), voutput_zero_point); |
| 191 | #else |
| 192 | vacc_lo = vcombine_s32(vmovn_s64(vscaled_acc01), vmovn_s64(vscaled_acc23)); |
| 193 | vacc_hi = vcombine_s32(vmovn_s64(vscaled_acc45), vmovn_s64(vscaled_acc67)); |
| 194 | |
| 195 | const int16x8_t vacc = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc_lo), vqmovn_s32(vacc_hi)), voutput_zero_point); |
| 196 | #endif |
| 197 | |
| 198 | uint8x8_t vout = vqmovun_s16(vacc); |
| 199 | vout = vmax_u8(vout, voutput_min); |
| 200 | vout = vmin_u8(vout, voutput_max); |
| 201 | |
Marat Dukhan | a63a6fc | 2020-03-10 06:12:48 -0700 | [diff] [blame] | 202 | if (channels & 4) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 203 | vst1_lane_u32(__builtin_assume_aligned(output, 1), vreinterpret_u32_u8(vout), 0); output += 4; |
| 204 | vout = vext_u8(vout, vout, 4); |
| 205 | } |
Marat Dukhan | a63a6fc | 2020-03-10 06:12:48 -0700 | [diff] [blame] | 206 | if (channels & 2) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 207 | vst1_lane_u16(__builtin_assume_aligned(output, 1), vreinterpret_u16_u8(vout), 0); output += 2; |
| 208 | vout = vext_u8(vout, vout, 2); |
| 209 | } |
Marat Dukhan | a63a6fc | 2020-03-10 06:12:48 -0700 | [diff] [blame] | 210 | if (channels & 1) { |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 211 | vst1_lane_u8(output, vout, 0); |
| 212 | } |
| 213 | } |
| 214 | } |