Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1 | /* |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_NEASYMM_H |
| 25 | #define ARM_COMPUTE_NEASYMM_H |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 26 | |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/NEON/NEMath.h" |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 28 | #include <arm_neon.h> |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 32 | using qasymm8x8_t = uint8x8_t; /**< 8 bit quantized asymmetric vector with 8 elements */ |
| 33 | using qasymm8x8x2_t = uint8x8x2_t; /**< 8 bit quantized asymmetric vector with 16 elements */ |
| 34 | using qasymm8x8x3_t = uint8x8x3_t; /**< 8 bit quantized asymmetric vector with 24 elements */ |
| 35 | using qasymm8x8x4_t = uint8x8x4_t; /**< 8 bit quantized asymmetric vector with 32 elements */ |
| 36 | using qasymm8x16_t = uint8x16_t; /**< 8 bit quantized asymmetric vector with 16 elements */ |
| 37 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 38 | using qasymm8x8_signed_t = int8x8_t; /**< 8 bit quantized signed asymmetric vector with 8 elements */ |
| 39 | using qasymm8x8x2_signed_t = int8x8x2_t; /**< 8 bit quantized signed asymmetric vector with 16 elements */ |
| 40 | using qasymm8x8x3_signed_t = int8x8x3_t; /**< 8 bit quantized signed asymmetric vector with 24 elements */ |
| 41 | using qasymm8x8x4_signed_t = int8x8x4_t; /**< 8 bit quantized signed asymmetric vector with 32 elements */ |
| 42 | using qasymm8x16_signed_t = int8x16_t; /**< 8 bit quantized signed asymmetric vector with 16 elements */ |
| 43 | |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 44 | /** Perform a multiply-accumulate on all 16 components of a QASYMM8 vector |
| 45 | * |
| 46 | * vd*vs + vo |
| 47 | * |
| 48 | * @param[in] vd Input vector value in QASYMM8 format |
| 49 | * @param[in] vs Vector multiplier in F32 format. The multiplier value must be duplicated across all four lanes. |
| 50 | * @param[in] vo Vector addend in F32 format. The addend value must be duplicated across all four lanes. |
| 51 | * |
| 52 | * @return A 16-component vector in QASYMM8 format, saturated to fit |
| 53 | */ |
| 54 | uint8x16_t vmlaq_qasymm8(qasymm8x16_t vd, float32x4_t vs, float32x4_t vo); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 55 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 56 | /** Perform a multiply-accumulate on all 16 components of a QASYMM8_SIGNED vector |
| 57 | * |
| 58 | * vd*vs + vo |
| 59 | * |
| 60 | * @param[in] vd Input vector value in QASYMM8_SIGNED format |
| 61 | * @param[in] vs Vector multiplier in F32 format. The multiplier value must be duplicated across all four lanes. |
| 62 | * @param[in] vo Vector addend in F32 format. The addend value must be duplicated across all four lanes. |
| 63 | * |
| 64 | * @return A 16-component vector in QASYMM8_SIGNED format, saturated to fit |
| 65 | */ |
| 66 | int8x16_t vmlaq_qasymm8_signed(qasymm8x16_signed_t vd, float32x4_t vs, float32x4_t vo); |
| 67 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 68 | /** Performs final quantization step on 16 elements |
| 69 | * |
| 70 | * @tparam is_bounded_relu Specified if a fused bounded relu should be applied |
| 71 | * |
| 72 | * @param in_s32 Input to be quantized. |
| 73 | * @param result_fixedpoint_multiplier Result multiplier parameter |
| 74 | * @param result_shift Result shift parameter |
| 75 | * @param result_offset_after_shift_s32 Result offset parameter |
| 76 | * @param min_u8 Relu lower bound |
| 77 | * @param max_u8 Relu upper bound |
| 78 | * |
| 79 | * @return Quantized values |
| 80 | */ |
| 81 | template <bool is_bounded_relu> |
| 82 | uint8x16_t finalize_quantization(int32x4x4_t &in_s32, |
| 83 | int result_fixedpoint_multiplier, |
| 84 | int32_t result_shift, |
| 85 | int32x4_t result_offset_after_shift_s32, |
| 86 | uint8x16_t min_u8, |
| 87 | uint8x16_t max_u8) |
| 88 | { |
| 89 | const static int32x4_t zero_s32 = vdupq_n_s32(0); |
| 90 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame^] | 91 | if(result_shift < 0) |
| 92 | { |
| 93 | in_s32.val[0] = vmulq_n_s32(in_s32.val[0], (1 << (-result_shift))); |
| 94 | in_s32.val[1] = vmulq_n_s32(in_s32.val[1], (1 << (-result_shift))); |
| 95 | in_s32.val[2] = vmulq_n_s32(in_s32.val[2], (1 << (-result_shift))); |
| 96 | in_s32.val[3] = vmulq_n_s32(in_s32.val[3], (1 << (-result_shift))); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 97 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame^] | 98 | in_s32.val[0] = vqrdmulhq_n_s32(in_s32.val[0], result_fixedpoint_multiplier); |
| 99 | in_s32.val[1] = vqrdmulhq_n_s32(in_s32.val[1], result_fixedpoint_multiplier); |
| 100 | in_s32.val[2] = vqrdmulhq_n_s32(in_s32.val[2], result_fixedpoint_multiplier); |
| 101 | in_s32.val[3] = vqrdmulhq_n_s32(in_s32.val[3], result_fixedpoint_multiplier); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar |
| 106 | in_s32.val[0] = vqrdmulhq_n_s32(in_s32.val[0], result_fixedpoint_multiplier); |
| 107 | in_s32.val[1] = vqrdmulhq_n_s32(in_s32.val[1], result_fixedpoint_multiplier); |
| 108 | in_s32.val[2] = vqrdmulhq_n_s32(in_s32.val[2], result_fixedpoint_multiplier); |
| 109 | in_s32.val[3] = vqrdmulhq_n_s32(in_s32.val[3], result_fixedpoint_multiplier); |
| 110 | |
| 111 | // Round to the nearest division by a power-of-two using result_shift_s32 |
| 112 | in_s32.val[0] = rounding_divide_by_pow2(in_s32.val[0], result_shift); |
| 113 | in_s32.val[1] = rounding_divide_by_pow2(in_s32.val[1], result_shift); |
| 114 | in_s32.val[2] = rounding_divide_by_pow2(in_s32.val[2], result_shift); |
| 115 | in_s32.val[3] = rounding_divide_by_pow2(in_s32.val[3], result_shift); |
| 116 | } |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 117 | |
| 118 | // Add the offset terms |
| 119 | in_s32.val[0] = vaddq_s32(in_s32.val[0], result_offset_after_shift_s32); |
| 120 | in_s32.val[1] = vaddq_s32(in_s32.val[1], result_offset_after_shift_s32); |
| 121 | in_s32.val[2] = vaddq_s32(in_s32.val[2], result_offset_after_shift_s32); |
| 122 | in_s32.val[3] = vaddq_s32(in_s32.val[3], result_offset_after_shift_s32); |
| 123 | |
| 124 | // Saturate negative values |
| 125 | in_s32.val[0] = vmaxq_s32(in_s32.val[0], zero_s32); |
| 126 | in_s32.val[1] = vmaxq_s32(in_s32.val[1], zero_s32); |
| 127 | in_s32.val[2] = vmaxq_s32(in_s32.val[2], zero_s32); |
| 128 | in_s32.val[3] = vmaxq_s32(in_s32.val[3], zero_s32); |
| 129 | |
| 130 | // Convert S32 to S16 |
| 131 | const int16x8x2_t in_s16 = |
| 132 | { |
| 133 | { |
| 134 | vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])), |
| 135 | vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3])) |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | // Convert S16 to U8 |
| 140 | uint8x16_t out_u8 = vcombine_u8(vqmovun_s16(in_s16.val[0]), vqmovun_s16(in_s16.val[1])); |
| 141 | |
| 142 | if(is_bounded_relu) |
| 143 | { |
| 144 | out_u8 = vmaxq_u8(out_u8, min_u8); |
| 145 | out_u8 = vminq_u8(out_u8, max_u8); |
| 146 | } |
| 147 | |
| 148 | return out_u8; |
| 149 | } |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 150 | |
Georgios Pinitas | 448a81f | 2019-11-21 14:10:25 +0000 | [diff] [blame] | 151 | /** Performs final quantization step on 16 elements |
| 152 | * |
| 153 | * @tparam is_bounded_relu Specified if a fused bounded relu should be applied |
| 154 | * |
| 155 | * @param in_s32 Input to be quantized. |
| 156 | * @param result_fixedpoint_multiplier Result multiplier parameter |
| 157 | * @param result_shift Result shift parameter |
| 158 | * @param result_offset_after_shift_s32 Result offset parameter |
| 159 | * @param min_s8 Relu lower bound |
| 160 | * @param max_s8 Relu upper bound |
| 161 | * |
| 162 | * @return Quantized values |
| 163 | */ |
| 164 | template <bool is_bounded_relu> |
| 165 | int8x16_t finalize_quantization(int32x4x4_t &in_s32, |
| 166 | int result_fixedpoint_multiplier, |
| 167 | int32_t result_shift, |
| 168 | int32x4_t result_offset_after_shift_s32, |
| 169 | int8x16_t min_s8, |
| 170 | int8x16_t max_s8) |
| 171 | { |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame^] | 172 | if(result_shift < 0) |
| 173 | { |
| 174 | in_s32.val[0] = vmulq_n_s32(in_s32.val[0], (1 << (-result_shift))); |
| 175 | in_s32.val[1] = vmulq_n_s32(in_s32.val[1], (1 << (-result_shift))); |
| 176 | in_s32.val[2] = vmulq_n_s32(in_s32.val[2], (1 << (-result_shift))); |
| 177 | in_s32.val[3] = vmulq_n_s32(in_s32.val[3], (1 << (-result_shift))); |
Georgios Pinitas | 448a81f | 2019-11-21 14:10:25 +0000 | [diff] [blame] | 178 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame^] | 179 | in_s32.val[0] = vqrdmulhq_n_s32(in_s32.val[0], result_fixedpoint_multiplier); |
| 180 | in_s32.val[1] = vqrdmulhq_n_s32(in_s32.val[1], result_fixedpoint_multiplier); |
| 181 | in_s32.val[2] = vqrdmulhq_n_s32(in_s32.val[2], result_fixedpoint_multiplier); |
| 182 | in_s32.val[3] = vqrdmulhq_n_s32(in_s32.val[3], result_fixedpoint_multiplier); |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar |
| 187 | in_s32.val[0] = vqrdmulhq_n_s32(in_s32.val[0], result_fixedpoint_multiplier); |
| 188 | in_s32.val[1] = vqrdmulhq_n_s32(in_s32.val[1], result_fixedpoint_multiplier); |
| 189 | in_s32.val[2] = vqrdmulhq_n_s32(in_s32.val[2], result_fixedpoint_multiplier); |
| 190 | in_s32.val[3] = vqrdmulhq_n_s32(in_s32.val[3], result_fixedpoint_multiplier); |
| 191 | |
| 192 | // Round to the nearest division by a power-of-two using result_shift_s32 |
| 193 | in_s32.val[0] = rounding_divide_by_pow2(in_s32.val[0], result_shift); |
| 194 | in_s32.val[1] = rounding_divide_by_pow2(in_s32.val[1], result_shift); |
| 195 | in_s32.val[2] = rounding_divide_by_pow2(in_s32.val[2], result_shift); |
| 196 | in_s32.val[3] = rounding_divide_by_pow2(in_s32.val[3], result_shift); |
| 197 | } |
Georgios Pinitas | 448a81f | 2019-11-21 14:10:25 +0000 | [diff] [blame] | 198 | |
| 199 | // Add the offset terms |
| 200 | in_s32.val[0] = vaddq_s32(in_s32.val[0], result_offset_after_shift_s32); |
| 201 | in_s32.val[1] = vaddq_s32(in_s32.val[1], result_offset_after_shift_s32); |
| 202 | in_s32.val[2] = vaddq_s32(in_s32.val[2], result_offset_after_shift_s32); |
| 203 | in_s32.val[3] = vaddq_s32(in_s32.val[3], result_offset_after_shift_s32); |
| 204 | |
| 205 | // Convert S32 to S16 |
| 206 | const int16x8x2_t in_s16 = |
| 207 | { |
| 208 | { |
| 209 | vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])), |
| 210 | vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3])) |
| 211 | } |
| 212 | }; |
| 213 | |
| 214 | // Convert S16 to S8 |
| 215 | int8x16_t out_s8 = vcombine_s8(vqmovn_s16(in_s16.val[0]), vqmovn_s16(in_s16.val[1])); |
| 216 | |
| 217 | if(is_bounded_relu) |
| 218 | { |
| 219 | out_s8 = vmaxq_s8(out_s8, min_s8); |
| 220 | out_s8 = vminq_s8(out_s8, max_s8); |
| 221 | } |
| 222 | |
| 223 | return out_s8; |
| 224 | } |
| 225 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 226 | /** Performs final quantization step on 16 elements for symmetric quantization |
| 227 | * |
| 228 | * @tparam is_bounded_relu Specified if a fused bounded relu should be applied |
| 229 | * |
| 230 | * @param in_s32 Input to be quantized. |
| 231 | * @param result_fixedpoint_multiplier Result multiplier parameter |
| 232 | * @param result_shift Result shift parameter |
| 233 | * @param result_offset_after_shift_s32 Result offset parameter |
| 234 | * @param min_s8 Relu lower bound |
| 235 | * @param max_s8 Relu upper bound |
| 236 | * |
| 237 | * @return Quantized values |
| 238 | */ |
| 239 | template <bool is_bounded_relu> |
| 240 | inline int8x16_t finalize_quantization_symm(int32x4x4_t &in_s32, |
| 241 | const int32x4x4_t &result_fixedpoint_multiplier, |
| 242 | const int32x4x4_t &result_shift, |
| 243 | const int32x4_t &result_offset_after_shift_s32, |
| 244 | const int8x16_t &min_s8, |
| 245 | const int8x16_t &max_s8) |
| 246 | { |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame^] | 247 | const static int32x4_t one_s32 = vdupq_n_s32(1); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 248 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame^] | 249 | // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar |
| 250 | int32x4x4_t res_shift_gt0 = |
| 251 | { |
| 252 | vqrdmulhq_s32(in_s32.val[0], result_fixedpoint_multiplier.val[0]), |
| 253 | vqrdmulhq_s32(in_s32.val[1], result_fixedpoint_multiplier.val[1]), |
| 254 | vqrdmulhq_s32(in_s32.val[2], result_fixedpoint_multiplier.val[2]), |
| 255 | vqrdmulhq_s32(in_s32.val[3], result_fixedpoint_multiplier.val[3]), |
| 256 | }; |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 257 | // Round to the nearest division by a power-of-two using result_shift_s32 |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame^] | 258 | res_shift_gt0.val[0] = rounding_divide_by_pow2(res_shift_gt0.val[0], result_shift.val[0]); |
| 259 | res_shift_gt0.val[1] = rounding_divide_by_pow2(res_shift_gt0.val[1], result_shift.val[1]); |
| 260 | res_shift_gt0.val[2] = rounding_divide_by_pow2(res_shift_gt0.val[2], result_shift.val[2]); |
| 261 | res_shift_gt0.val[3] = rounding_divide_by_pow2(res_shift_gt0.val[3], result_shift.val[3]); |
| 262 | |
| 263 | int32x4x4_t res_shift_lt0 = |
| 264 | { |
| 265 | vmulq_s32(in_s32.val[0], vshlq_s32(one_s32, vnegq_s32(result_shift.val[0]))), |
| 266 | vmulq_s32(in_s32.val[1], vshlq_s32(one_s32, vnegq_s32(result_shift.val[1]))), |
| 267 | vmulq_s32(in_s32.val[2], vshlq_s32(one_s32, vnegq_s32(result_shift.val[2]))), |
| 268 | vmulq_s32(in_s32.val[3], vshlq_s32(one_s32, vnegq_s32(result_shift.val[3]))), |
| 269 | }; |
| 270 | res_shift_lt0.val[0] = vqrdmulhq_s32(res_shift_lt0.val[0], result_fixedpoint_multiplier.val[0]); |
| 271 | res_shift_lt0.val[1] = vqrdmulhq_s32(res_shift_lt0.val[1], result_fixedpoint_multiplier.val[1]); |
| 272 | res_shift_lt0.val[2] = vqrdmulhq_s32(res_shift_lt0.val[2], result_fixedpoint_multiplier.val[2]); |
| 273 | res_shift_lt0.val[3] = vqrdmulhq_s32(res_shift_lt0.val[3], result_fixedpoint_multiplier.val[3]); |
| 274 | |
| 275 | // Select result depending on shift value |
| 276 | const uint32x4x4_t mask_lt0 = |
| 277 | { |
| 278 | #ifdef __aarch64__ |
| 279 | vcltzq_s32(result_shift.val[0]), |
| 280 | vcltzq_s32(result_shift.val[1]), |
| 281 | vcltzq_s32(result_shift.val[2]), |
| 282 | vcltzq_s32(result_shift.val[3]), |
| 283 | #else //__aarch64__ |
| 284 | vcltq_s32(result_shift.val[0], vdupq_n_s32(0)), |
| 285 | vcltq_s32(result_shift.val[1], vdupq_n_s32(0)), |
| 286 | vcltq_s32(result_shift.val[2], vdupq_n_s32(0)), |
| 287 | vcltq_s32(result_shift.val[3], vdupq_n_s32(0)), |
| 288 | #endif //__aarch64__ |
| 289 | }; |
| 290 | |
| 291 | in_s32.val[0] = vbslq_s32(mask_lt0.val[0], res_shift_lt0.val[0], res_shift_gt0.val[0]); |
| 292 | in_s32.val[1] = vbslq_s32(mask_lt0.val[1], res_shift_lt0.val[1], res_shift_gt0.val[1]); |
| 293 | in_s32.val[2] = vbslq_s32(mask_lt0.val[2], res_shift_lt0.val[2], res_shift_gt0.val[2]); |
| 294 | in_s32.val[3] = vbslq_s32(mask_lt0.val[3], res_shift_lt0.val[3], res_shift_gt0.val[3]); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 295 | |
| 296 | // Add the offset terms |
| 297 | in_s32.val[0] = vaddq_s32(in_s32.val[0], result_offset_after_shift_s32); |
| 298 | in_s32.val[1] = vaddq_s32(in_s32.val[1], result_offset_after_shift_s32); |
| 299 | in_s32.val[2] = vaddq_s32(in_s32.val[2], result_offset_after_shift_s32); |
| 300 | in_s32.val[3] = vaddq_s32(in_s32.val[3], result_offset_after_shift_s32); |
| 301 | |
| 302 | // Convert S32 to S16 |
| 303 | const int16x8x2_t in_s16 = |
| 304 | { |
| 305 | { |
| 306 | vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])), |
| 307 | vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3])) |
| 308 | } |
| 309 | }; |
| 310 | |
| 311 | // Convert S16 to S8 |
| 312 | int8x16_t out_s8 = vcombine_s8(vqmovn_s16(in_s16.val[0]), vqmovn_s16(in_s16.val[1])); |
| 313 | |
| 314 | if(is_bounded_relu) |
| 315 | { |
| 316 | out_s8 = vmaxq_s8(out_s8, min_s8); |
| 317 | out_s8 = vminq_s8(out_s8, max_s8); |
| 318 | } |
| 319 | |
| 320 | return out_s8; |
| 321 | } |
| 322 | |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 323 | /** Performs final quantization step on single element |
| 324 | * |
| 325 | * @tparam is_bounded_relu Specified if a fused bounded relu should be applied |
| 326 | * |
| 327 | * @param[in] in_value Input to be quantized. |
| 328 | * @param[in] result_fixedpoint_multiplier Result multiplier parameter |
| 329 | * @param[in] result_shift Result shift parameter |
| 330 | * @param[in] result_offset_after_shift_s32 Result offset parameter |
| 331 | * @param[in] min_u8 Relu lower bound |
| 332 | * @param[in] max_u8 Relu upper bound |
| 333 | * |
| 334 | * @return Quantized value |
| 335 | */ |
| 336 | template <bool is_bounded_relu> |
| 337 | inline uint8_t finalize_quantization(int32_t in_value, int result_fixedpoint_multiplier, |
| 338 | int32_t result_shift, int32_t result_offset_after_shift_s32, |
| 339 | uint8_t min_u8, uint8_t max_u8) |
| 340 | { |
| 341 | int32x4_t in_s32 = vdupq_n_s32(in_value); |
| 342 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame^] | 343 | if(result_shift < 0) |
| 344 | { |
| 345 | in_value = vgetq_lane_s32(vqrdmulhq_n_s32(vmulq_n_s32(in_s32, (1 << (-result_shift))), result_fixedpoint_multiplier), 0); |
| 346 | } |
| 347 | else |
| 348 | { |
| 349 | // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar |
| 350 | in_value = vgetq_lane_s32(vqrdmulhq_n_s32(in_s32, result_fixedpoint_multiplier), 0); |
| 351 | // Shift value by result_shift_s32 |
| 352 | in_value = rounding_divide_by_pow2(in_value, result_shift); |
| 353 | } |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 354 | |
| 355 | // Add the offset term |
| 356 | in_value += result_offset_after_shift_s32; |
| 357 | |
| 358 | // Bound the result |
Georgios Pinitas | 6fa2638 | 2019-03-18 10:05:34 +0000 | [diff] [blame] | 359 | uint8_t out_u8 = static_cast<uint8_t>(std::max<int32_t>(0, std::min<int32_t>(255, in_value))); |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 360 | if(is_bounded_relu) |
| 361 | { |
| 362 | out_u8 = static_cast<uint8_t>(std::max(min_u8, std::min(max_u8, out_u8))); |
| 363 | } |
| 364 | |
| 365 | return out_u8; |
| 366 | } |
| 367 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 368 | /** Performs final quantization step on single element |
| 369 | * |
| 370 | * @tparam is_bounded_relu Specified if a fused bounded relu should be applied |
| 371 | * |
| 372 | * @param[in] in_value Input to be quantized. |
| 373 | * @param[in] result_fixedpoint_multiplier Result multiplier parameter |
| 374 | * @param[in] result_shift Result shift parameter |
| 375 | * @param[in] result_offset_after_shift_s32 Result offset parameter |
| 376 | * @param[in] min_s8 Relu lower bound |
| 377 | * @param[in] max_s8 Relu upper bound |
| 378 | * |
| 379 | * @return Quantized value |
| 380 | */ |
| 381 | template <bool is_bounded_relu> |
| 382 | inline int8_t finalize_quantization(int32_t in_value, int result_fixedpoint_multiplier, |
| 383 | int32_t result_shift, int32_t result_offset_after_shift_s32, |
| 384 | int8_t min_s8, int8_t max_s8) |
| 385 | { |
| 386 | int32x4_t in_s32 = vdupq_n_s32(in_value); |
| 387 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame^] | 388 | if(result_shift < 0) |
| 389 | { |
| 390 | in_value = vgetq_lane_s32(vqrdmulhq_n_s32(vmulq_n_s32(in_s32, (1 << (-result_shift))), result_fixedpoint_multiplier), 0); |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar |
| 395 | in_value = vgetq_lane_s32(vqrdmulhq_n_s32(in_s32, result_fixedpoint_multiplier), 0); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 396 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame^] | 397 | // Shift value by result_shift_s32 |
| 398 | in_value = rounding_divide_by_pow2(in_value, result_shift); |
| 399 | } |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 400 | |
| 401 | // Add the offset term |
| 402 | in_value += result_offset_after_shift_s32; |
| 403 | |
| 404 | // Bound the result |
| 405 | int8_t out_s8 = static_cast<int8_t>(std::max<int32_t>(-128, std::min<int32_t>(127, in_value))); |
| 406 | if(is_bounded_relu) |
| 407 | { |
| 408 | out_s8 = static_cast<int8_t>(std::max(min_s8, std::min(max_s8, out_s8))); |
| 409 | } |
| 410 | |
| 411 | return out_s8; |
| 412 | } |
| 413 | |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 414 | /** Dequantize a neon vector holding 8 quantized values. |
| 415 | * |
| 416 | * @param[in] qv Input values to be dequantized. |
| 417 | * @param[in] qi Quantization information to be used in the computation. |
| 418 | * |
| 419 | * @return Dequantized values in a neon vector |
| 420 | */ |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 421 | inline float32x4x2_t vdequantize(const uint8x8_t &qv, const UniformQuantizationInfo &qi) |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 422 | { |
| 423 | const float scale = qi.scale; |
| 424 | const int offset = qi.offset; |
| 425 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 426 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 427 | const float32x4x2_t vdequantized_input = |
| 428 | { |
| 429 | { |
| 430 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(qv)))), voffset)), vscale), |
| 431 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(qv)))), voffset)), vscale), |
| 432 | } |
| 433 | }; |
| 434 | return vdequantized_input; |
| 435 | } |
| 436 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 437 | /** Dequantize a neon vector holding 8 singed quantized values. |
| 438 | * |
| 439 | * @param[in] qv Input values to be dequantized. |
| 440 | * @param[in] qi Quantization information to be used in the computation. |
| 441 | * |
| 442 | * @return Dequantized values in a neon vector |
| 443 | */ |
| 444 | inline float32x4x2_t vdequantize(const int8x8_t &qv, const UniformQuantizationInfo &qi) |
| 445 | { |
| 446 | const float scale = qi.scale; |
| 447 | const int offset = qi.offset; |
| 448 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 449 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 450 | const float32x4x2_t vdequantized_input = |
| 451 | { |
| 452 | { |
| 453 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(qv))), voffset)), vscale), |
| 454 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(qv))), voffset)), vscale), |
| 455 | } |
| 456 | }; |
| 457 | return vdequantized_input; |
| 458 | } |
| 459 | |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 460 | /** Dequantize a neon vector holding 16 quantized values. |
| 461 | * |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 462 | * @param[in] qv Input values to be dequantized. |
| 463 | * @param[in] qi Quantization information to be used in the computation. |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 464 | * |
| 465 | * @return Dequantized values in a neon vector |
| 466 | */ |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 467 | inline float32x4x4_t vdequantize(const uint8x16_t &qv, const UniformQuantizationInfo &qi) |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 468 | { |
| 469 | const float scale = qi.scale; |
| 470 | const int offset = qi.offset; |
| 471 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 472 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 473 | const float32x4x4_t vdequantized_input = |
| 474 | { |
| 475 | { |
| 476 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(qv))))), voffset)), vscale), |
| 477 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(qv))))), voffset)), vscale), |
| 478 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(qv))))), voffset)), vscale), |
| 479 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(qv))))), voffset)), vscale), |
| 480 | } |
| 481 | }; |
| 482 | return vdequantized_input; |
| 483 | } |
| 484 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 485 | /** Dequantize a neon vector holding 16 signed quantized values. |
| 486 | * |
| 487 | * @param[in] qv Input values to be dequantized. |
| 488 | * @param[in] qi Quantization information to be used in the computation. |
| 489 | * |
| 490 | * @return Dequantized values in a neon vector |
| 491 | */ |
| 492 | inline float32x4x4_t vdequantize(const int8x16_t &qv, const UniformQuantizationInfo &qi) |
| 493 | { |
| 494 | const float scale = qi.scale; |
| 495 | const int offset = qi.offset; |
| 496 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 497 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 498 | const float32x4x4_t vdequantized_input = |
| 499 | { |
| 500 | { |
| 501 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_low_s8(qv)))), voffset)), vscale), |
| 502 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_low_s8(qv)))), voffset)), vscale), |
| 503 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_high_s8(qv)))), voffset)), vscale), |
| 504 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_high_s8(qv)))), voffset)), vscale), |
| 505 | } |
| 506 | }; |
| 507 | return vdequantized_input; |
| 508 | } |
| 509 | |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 510 | /** Dequantize following an asymmetric quantization scheme a neon vector holding 16 quantized values. |
| 511 | * |
| 512 | * @param[in] qv Input values to be dequantized. |
| 513 | * @param[in] scale Quantization scaling factor. |
| 514 | * @param[in] offset Zero quantization offset. |
| 515 | * |
| 516 | * @return Dequantized values in a neon vector |
| 517 | */ |
| 518 | inline float32x4x4_t vdequantize(const uint8x16_t &qv, float scale, int32_t offset) |
| 519 | { |
| 520 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 521 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 522 | const float32x4x4_t vdequantized_input = |
| 523 | { |
| 524 | { |
| 525 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(qv))))), voffset)), vscale), |
| 526 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(qv))))), voffset)), vscale), |
| 527 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(qv))))), voffset)), vscale), |
| 528 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(qv))))), voffset)), vscale), |
| 529 | } |
| 530 | }; |
| 531 | return vdequantized_input; |
| 532 | } |
| 533 | |
Sang-Hoon Park | d817647 | 2019-12-04 09:46:28 +0000 | [diff] [blame] | 534 | /** Dequantize a vector of 16 values stored as signed asymmetric. |
| 535 | * |
| 536 | * @param[in] qv Input values to be dequantized. |
| 537 | * @param[in] scale Quantization scaling factor. |
| 538 | * @param[in] offset Zero quantization offset. |
| 539 | * |
| 540 | * @return Dequantized values in a neon vector |
| 541 | */ |
| 542 | inline float32x4x4_t vdequantize(const int8x16_t &qv, float scale, int32_t offset) |
| 543 | { |
| 544 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 545 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 546 | const float32x4x4_t vdequantized_input = |
| 547 | { |
| 548 | { |
| 549 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_low_s8(qv)))), voffset)), vscale), |
| 550 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_low_s8(qv)))), voffset)), vscale), |
| 551 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_high_s8(qv)))), voffset)), vscale), |
| 552 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_high_s8(qv)))), voffset)), vscale), |
| 553 | } |
| 554 | }; |
| 555 | return vdequantized_input; |
| 556 | } |
| 557 | |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 558 | /** Dequantize following symmetric quantization scheme a neon vector holding 16 quantized values. |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 559 | * |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 560 | * @param[in] qv Input values to be dequantized. |
| 561 | * @param[in] vscale Vector containing quantization scaling factors. |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 562 | * |
| 563 | * @return Dequantized values in a neon vector |
| 564 | */ |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 565 | inline float32x4x4_t vdequantize(const int8x16_t &qv, const float32x4x4_t vscale) |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 566 | { |
| 567 | const float32x4x4_t vdequantized_input = |
| 568 | { |
| 569 | { |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 570 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_low_s8(qv))))), vscale.val[0]), |
| 571 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_low_s8(qv))))), vscale.val[1]), |
| 572 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_high_s8(qv))))), vscale.val[2]), |
| 573 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_high_s8(qv))))), vscale.val[3]), |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 574 | } |
| 575 | }; |
| 576 | return vdequantized_input; |
| 577 | } |
| 578 | |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 579 | /** Dequantize following a symmetric quantization scheme a neon vector holding 16 quantized values. |
| 580 | * |
| 581 | * @param[in] qv Input values to be dequantized. |
| 582 | * @param[in] scale Quantization scaling factor. |
| 583 | * |
| 584 | * @return Dequantized values in a neon vector |
| 585 | */ |
| 586 | inline float32x4x4_t vdequantize(const int8x16_t &qv, float scale) |
| 587 | { |
| 588 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 589 | const float32x4x4_t vdequantized_input = |
| 590 | { |
| 591 | { |
| 592 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_low_s8(qv))))), vscale), |
| 593 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_low_s8(qv))))), vscale), |
| 594 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_high_s8(qv))))), vscale), |
| 595 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_high_s8(qv))))), vscale), |
| 596 | } |
| 597 | }; |
| 598 | return vdequantized_input; |
| 599 | } |
| 600 | |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 601 | /** Quantize a neon vector holding 8 floating point values. |
| 602 | * |
| 603 | * @param[in] qv Input values to be quantized. |
| 604 | * @param[in] qi Quantization information to be used in the computation. |
| 605 | * |
| 606 | * @return A neon vector holding the quantized values |
| 607 | */ |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 608 | inline uint8x8_t vquantize(const float32x4x2_t &qv, const UniformQuantizationInfo &qi) |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 609 | { |
| 610 | const float scale = qi.scale; |
| 611 | const int offset = qi.offset; |
| 612 | const float32x4_t voffset = vdupq_n_f32(offset); |
| 613 | const float32x4_t vinvscale = vdupq_n_f32(1.f / scale); |
| 614 | const int32x4x4_t rf = |
| 615 | { |
| 616 | { |
| 617 | #ifdef __aarch64__ |
| 618 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 619 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 620 | #else //__aarch64__ |
| 621 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 622 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 623 | #endif //__aarch64__ |
| 624 | } |
| 625 | }; |
| 626 | return vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]))); |
| 627 | } |
| 628 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 629 | /** Quantize a neon vector holding 8 floating point values. |
| 630 | * |
| 631 | * @param[in] qv Input values to be quantized. |
| 632 | * @param[in] qi Quantization information to be used in the computation. |
| 633 | * |
| 634 | * @return A neon vector holding the singed quantized values |
| 635 | */ |
| 636 | inline int8x8_t vquantize_signed(const float32x4x2_t &qv, const UniformQuantizationInfo &qi) |
| 637 | { |
| 638 | const float scale = qi.scale; |
| 639 | const int offset = qi.offset; |
| 640 | const float32x4_t voffset = vdupq_n_f32(offset); |
| 641 | const float32x4_t vinvscale = vdupq_n_f32(1.f / scale); |
| 642 | const int32x4x4_t rf = |
| 643 | { |
| 644 | { |
| 645 | #ifdef __aarch64__ |
| 646 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 647 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 648 | #else //__aarch64__ |
| 649 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 650 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 651 | #endif //__aarch64__ |
| 652 | } |
| 653 | }; |
| 654 | return vqmovn_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]))); |
| 655 | } |
| 656 | |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 657 | /** Quantize a neon vector holding 16 floating point values. |
| 658 | * |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 659 | * @param[in] qv Input values to be quantized. |
| 660 | * @param[in] qi Quantization information to be used in the computation. |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 661 | * |
| 662 | * @return A neon vector holding the quantized values |
| 663 | */ |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 664 | inline uint8x16_t vquantize(const float32x4x4_t &qv, const UniformQuantizationInfo &qi) |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 665 | { |
| 666 | const float scale = qi.scale; |
| 667 | const int offset = qi.offset; |
| 668 | const float32x4_t voffset = vdupq_n_f32(offset); |
| 669 | const float32x4_t vinvscale = vdupq_n_f32(1.f / scale); |
| 670 | const int32x4x4_t rf = |
| 671 | { |
| 672 | { |
| 673 | #ifdef __aarch64__ |
| 674 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 675 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 676 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[2], vinvscale)), |
| 677 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[3], vinvscale)), |
| 678 | #else //__aarch64__ |
| 679 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 680 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 681 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[2], vinvscale)), |
| 682 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[3], vinvscale)), |
| 683 | #endif //__aarch64__ |
| 684 | } |
| 685 | }; |
| 686 | const uint8x8_t pa = vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]))); |
| 687 | const uint8x8_t pb = vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3]))); |
| 688 | return vcombine_u8(pa, pb); |
| 689 | } |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 690 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 691 | /** Signed quantize a neon vector holding 16 floating point values. |
| 692 | * |
| 693 | * @param[in] qv Input values to be quantized. |
| 694 | * @param[in] qi Quantization information to be used in the computation. |
| 695 | * |
| 696 | * @return A neon vector holding the quantized values |
| 697 | */ |
| 698 | |
| 699 | inline int8x16_t vquantize_signed(const float32x4x4_t &qv, const UniformQuantizationInfo &qi) |
| 700 | { |
| 701 | const float scale = qi.scale; |
| 702 | const int offset = qi.offset; |
| 703 | const float32x4_t voffset = vdupq_n_f32(offset); |
| 704 | const float32x4_t vinvscale = vdupq_n_f32(1.f / scale); |
| 705 | const int32x4x4_t rf = |
| 706 | { |
| 707 | { |
| 708 | #ifdef __aarch64__ |
| 709 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 710 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 711 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[2], vinvscale)), |
| 712 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[3], vinvscale)), |
| 713 | #else //__aarch64__ |
| 714 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 715 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 716 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[2], vinvscale)), |
| 717 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[3], vinvscale)), |
| 718 | #endif //__aarch64__ |
| 719 | |
| 720 | } |
| 721 | }; |
| 722 | const int8x8_t pa = vqmovn_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]))); |
| 723 | const int8x8_t pb = vqmovn_s16(vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3]))); |
| 724 | return vcombine_s8(pa, pb); |
| 725 | } |
| 726 | |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 727 | /** Quantize to QASYMM16 a neon vector holding 16 floating point values. |
| 728 | * |
| 729 | * @param[in] qv Input values to be quantized. |
| 730 | * @param[in] qi Quantization information to be used in the computation. |
| 731 | * |
| 732 | * @return A neon vector holding the quantized values |
| 733 | */ |
| 734 | inline uint16x8x2_t vquantize_qasymm16(const float32x4x4_t &qv, const UniformQuantizationInfo &qi) |
| 735 | { |
| 736 | const float scale = qi.scale; |
| 737 | const int offset = qi.offset; |
| 738 | const float32x4_t voffset = vdupq_n_f32(offset); |
| 739 | const float32x4_t vinvscale = vdupq_n_f32(1.f / scale); |
| 740 | const int32x4x4_t rf = |
| 741 | { |
| 742 | { |
| 743 | #ifdef __aarch64__ |
| 744 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 745 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 746 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[2], vinvscale)), |
| 747 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[3], vinvscale)), |
| 748 | #else //__aarch64__ |
| 749 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 750 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 751 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[2], vinvscale)), |
| 752 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[3], vinvscale)), |
| 753 | #endif //__aarch64__ |
| 754 | } |
| 755 | }; |
| 756 | const uint16x8_t pa = vcombine_u16(vqmovun_s32(rf.val[0]), vqmovun_s32(rf.val[1])); |
| 757 | const uint16x8_t pb = vcombine_u16(vqmovun_s32(rf.val[2]), vqmovun_s32(rf.val[3])); |
| 758 | return { pa, pb }; |
| 759 | } |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 760 | } // namespace arm_compute |
| 761 | #include "arm_compute/core/NEON/NEAsymm.inl" |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 762 | #endif // ARM_COMPUTE_NEASYMM_H |