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 | */ |
| 24 | #ifndef __ARM_COMPUTE_NEASYMM_H__ |
| 25 | #define __ARM_COMPUTE_NEASYMM_H__ |
| 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 | |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 38 | /** Perform a multiply-accumulate on all 16 components of a QASYMM8 vector |
| 39 | * |
| 40 | * vd*vs + vo |
| 41 | * |
| 42 | * @param[in] vd Input vector value in QASYMM8 format |
| 43 | * @param[in] vs Vector multiplier in F32 format. The multiplier value must be duplicated across all four lanes. |
| 44 | * @param[in] vo Vector addend in F32 format. The addend value must be duplicated across all four lanes. |
| 45 | * |
| 46 | * @return A 16-component vector in QASYMM8 format, saturated to fit |
| 47 | */ |
| 48 | 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] | 49 | |
| 50 | /** Performs final quantization step on 16 elements |
| 51 | * |
| 52 | * @tparam is_bounded_relu Specified if a fused bounded relu should be applied |
| 53 | * |
| 54 | * @param in_s32 Input to be quantized. |
| 55 | * @param result_fixedpoint_multiplier Result multiplier parameter |
| 56 | * @param result_shift Result shift parameter |
| 57 | * @param result_offset_after_shift_s32 Result offset parameter |
| 58 | * @param min_u8 Relu lower bound |
| 59 | * @param max_u8 Relu upper bound |
| 60 | * |
| 61 | * @return Quantized values |
| 62 | */ |
| 63 | template <bool is_bounded_relu> |
| 64 | uint8x16_t finalize_quantization(int32x4x4_t &in_s32, |
| 65 | int result_fixedpoint_multiplier, |
| 66 | int32_t result_shift, |
| 67 | int32x4_t result_offset_after_shift_s32, |
| 68 | uint8x16_t min_u8, |
| 69 | uint8x16_t max_u8) |
| 70 | { |
| 71 | const static int32x4_t zero_s32 = vdupq_n_s32(0); |
| 72 | |
| 73 | // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar |
| 74 | in_s32.val[0] = vqrdmulhq_n_s32(in_s32.val[0], result_fixedpoint_multiplier); |
| 75 | in_s32.val[1] = vqrdmulhq_n_s32(in_s32.val[1], result_fixedpoint_multiplier); |
| 76 | in_s32.val[2] = vqrdmulhq_n_s32(in_s32.val[2], result_fixedpoint_multiplier); |
| 77 | in_s32.val[3] = vqrdmulhq_n_s32(in_s32.val[3], result_fixedpoint_multiplier); |
| 78 | |
| 79 | // Round to the nearest division by a power-of-two using result_shift_s32 |
| 80 | in_s32.val[0] = rounding_divide_by_pow2(in_s32.val[0], result_shift); |
| 81 | in_s32.val[1] = rounding_divide_by_pow2(in_s32.val[1], result_shift); |
| 82 | in_s32.val[2] = rounding_divide_by_pow2(in_s32.val[2], result_shift); |
| 83 | in_s32.val[3] = rounding_divide_by_pow2(in_s32.val[3], result_shift); |
| 84 | |
| 85 | // Add the offset terms |
| 86 | in_s32.val[0] = vaddq_s32(in_s32.val[0], result_offset_after_shift_s32); |
| 87 | in_s32.val[1] = vaddq_s32(in_s32.val[1], result_offset_after_shift_s32); |
| 88 | in_s32.val[2] = vaddq_s32(in_s32.val[2], result_offset_after_shift_s32); |
| 89 | in_s32.val[3] = vaddq_s32(in_s32.val[3], result_offset_after_shift_s32); |
| 90 | |
| 91 | // Saturate negative values |
| 92 | in_s32.val[0] = vmaxq_s32(in_s32.val[0], zero_s32); |
| 93 | in_s32.val[1] = vmaxq_s32(in_s32.val[1], zero_s32); |
| 94 | in_s32.val[2] = vmaxq_s32(in_s32.val[2], zero_s32); |
| 95 | in_s32.val[3] = vmaxq_s32(in_s32.val[3], zero_s32); |
| 96 | |
| 97 | // Convert S32 to S16 |
| 98 | const int16x8x2_t in_s16 = |
| 99 | { |
| 100 | { |
| 101 | vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])), |
| 102 | vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3])) |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | // Convert S16 to U8 |
| 107 | uint8x16_t out_u8 = vcombine_u8(vqmovun_s16(in_s16.val[0]), vqmovun_s16(in_s16.val[1])); |
| 108 | |
| 109 | if(is_bounded_relu) |
| 110 | { |
| 111 | out_u8 = vmaxq_u8(out_u8, min_u8); |
| 112 | out_u8 = vminq_u8(out_u8, max_u8); |
| 113 | } |
| 114 | |
| 115 | return out_u8; |
| 116 | } |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 117 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 118 | /** Performs final quantization step on 16 elements for symmetric quantization |
| 119 | * |
| 120 | * @tparam is_bounded_relu Specified if a fused bounded relu should be applied |
| 121 | * |
| 122 | * @param in_s32 Input to be quantized. |
| 123 | * @param result_fixedpoint_multiplier Result multiplier parameter |
| 124 | * @param result_shift Result shift parameter |
| 125 | * @param result_offset_after_shift_s32 Result offset parameter |
| 126 | * @param min_s8 Relu lower bound |
| 127 | * @param max_s8 Relu upper bound |
| 128 | * |
| 129 | * @return Quantized values |
| 130 | */ |
| 131 | template <bool is_bounded_relu> |
| 132 | inline int8x16_t finalize_quantization_symm(int32x4x4_t &in_s32, |
| 133 | const int32x4x4_t &result_fixedpoint_multiplier, |
| 134 | const int32x4x4_t &result_shift, |
| 135 | const int32x4_t &result_offset_after_shift_s32, |
| 136 | const int8x16_t &min_s8, |
| 137 | const int8x16_t &max_s8) |
| 138 | { |
| 139 | // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar |
| 140 | in_s32.val[0] = vqrdmulhq_s32(in_s32.val[0], result_fixedpoint_multiplier.val[0]); |
| 141 | in_s32.val[1] = vqrdmulhq_s32(in_s32.val[1], result_fixedpoint_multiplier.val[1]); |
| 142 | in_s32.val[2] = vqrdmulhq_s32(in_s32.val[2], result_fixedpoint_multiplier.val[2]); |
| 143 | in_s32.val[3] = vqrdmulhq_s32(in_s32.val[3], result_fixedpoint_multiplier.val[3]); |
| 144 | |
| 145 | // Round to the nearest division by a power-of-two using result_shift_s32 |
| 146 | in_s32.val[0] = rounding_divide_by_pow2(in_s32.val[0], result_shift.val[0]); |
| 147 | in_s32.val[1] = rounding_divide_by_pow2(in_s32.val[1], result_shift.val[1]); |
| 148 | in_s32.val[2] = rounding_divide_by_pow2(in_s32.val[2], result_shift.val[2]); |
| 149 | in_s32.val[3] = rounding_divide_by_pow2(in_s32.val[3], result_shift.val[3]); |
| 150 | |
| 151 | // Add the offset terms |
| 152 | in_s32.val[0] = vaddq_s32(in_s32.val[0], result_offset_after_shift_s32); |
| 153 | in_s32.val[1] = vaddq_s32(in_s32.val[1], result_offset_after_shift_s32); |
| 154 | in_s32.val[2] = vaddq_s32(in_s32.val[2], result_offset_after_shift_s32); |
| 155 | in_s32.val[3] = vaddq_s32(in_s32.val[3], result_offset_after_shift_s32); |
| 156 | |
| 157 | // Convert S32 to S16 |
| 158 | const int16x8x2_t in_s16 = |
| 159 | { |
| 160 | { |
| 161 | vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])), |
| 162 | vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3])) |
| 163 | } |
| 164 | }; |
| 165 | |
| 166 | // Convert S16 to S8 |
| 167 | int8x16_t out_s8 = vcombine_s8(vqmovn_s16(in_s16.val[0]), vqmovn_s16(in_s16.val[1])); |
| 168 | |
| 169 | if(is_bounded_relu) |
| 170 | { |
| 171 | out_s8 = vmaxq_s8(out_s8, min_s8); |
| 172 | out_s8 = vminq_s8(out_s8, max_s8); |
| 173 | } |
| 174 | |
| 175 | return out_s8; |
| 176 | } |
| 177 | |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 178 | /** Performs final quantization step on single element |
| 179 | * |
| 180 | * @tparam is_bounded_relu Specified if a fused bounded relu should be applied |
| 181 | * |
| 182 | * @param[in] in_value Input to be quantized. |
| 183 | * @param[in] result_fixedpoint_multiplier Result multiplier parameter |
| 184 | * @param[in] result_shift Result shift parameter |
| 185 | * @param[in] result_offset_after_shift_s32 Result offset parameter |
| 186 | * @param[in] min_u8 Relu lower bound |
| 187 | * @param[in] max_u8 Relu upper bound |
| 188 | * |
| 189 | * @return Quantized value |
| 190 | */ |
| 191 | template <bool is_bounded_relu> |
| 192 | inline uint8_t finalize_quantization(int32_t in_value, int result_fixedpoint_multiplier, |
| 193 | int32_t result_shift, int32_t result_offset_after_shift_s32, |
| 194 | uint8_t min_u8, uint8_t max_u8) |
| 195 | { |
| 196 | int32x4_t in_s32 = vdupq_n_s32(in_value); |
| 197 | |
| 198 | // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar |
| 199 | in_value = vgetq_lane_s32(vqrdmulhq_n_s32(in_s32, result_fixedpoint_multiplier), 0); |
| 200 | |
| 201 | // Shift value by result_shift_s32 |
| 202 | in_value = rounding_divide_by_pow2(in_value, result_shift); |
| 203 | |
| 204 | // Add the offset term |
| 205 | in_value += result_offset_after_shift_s32; |
| 206 | |
| 207 | // Bound the result |
Georgios Pinitas | 6fa2638 | 2019-03-18 10:05:34 +0000 | [diff] [blame] | 208 | 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] | 209 | if(is_bounded_relu) |
| 210 | { |
| 211 | out_u8 = static_cast<uint8_t>(std::max(min_u8, std::min(max_u8, out_u8))); |
| 212 | } |
| 213 | |
| 214 | return out_u8; |
| 215 | } |
| 216 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 217 | /** Performs final quantization step on single element |
| 218 | * |
| 219 | * @tparam is_bounded_relu Specified if a fused bounded relu should be applied |
| 220 | * |
| 221 | * @param[in] in_value Input to be quantized. |
| 222 | * @param[in] result_fixedpoint_multiplier Result multiplier parameter |
| 223 | * @param[in] result_shift Result shift parameter |
| 224 | * @param[in] result_offset_after_shift_s32 Result offset parameter |
| 225 | * @param[in] min_s8 Relu lower bound |
| 226 | * @param[in] max_s8 Relu upper bound |
| 227 | * |
| 228 | * @return Quantized value |
| 229 | */ |
| 230 | template <bool is_bounded_relu> |
| 231 | inline int8_t finalize_quantization(int32_t in_value, int result_fixedpoint_multiplier, |
| 232 | int32_t result_shift, int32_t result_offset_after_shift_s32, |
| 233 | int8_t min_s8, int8_t max_s8) |
| 234 | { |
| 235 | int32x4_t in_s32 = vdupq_n_s32(in_value); |
| 236 | |
| 237 | // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar |
| 238 | in_value = vgetq_lane_s32(vqrdmulhq_n_s32(in_s32, result_fixedpoint_multiplier), 0); |
| 239 | |
| 240 | // Shift value by result_shift_s32 |
| 241 | in_value = rounding_divide_by_pow2(in_value, result_shift); |
| 242 | |
| 243 | // Add the offset term |
| 244 | in_value += result_offset_after_shift_s32; |
| 245 | |
| 246 | // Bound the result |
| 247 | int8_t out_s8 = static_cast<int8_t>(std::max<int32_t>(-128, std::min<int32_t>(127, in_value))); |
| 248 | if(is_bounded_relu) |
| 249 | { |
| 250 | out_s8 = static_cast<int8_t>(std::max(min_s8, std::min(max_s8, out_s8))); |
| 251 | } |
| 252 | |
| 253 | return out_s8; |
| 254 | } |
| 255 | |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 256 | /** Dequantize a neon vector holding 8 quantized values. |
| 257 | * |
| 258 | * @param[in] qv Input values to be dequantized. |
| 259 | * @param[in] qi Quantization information to be used in the computation. |
| 260 | * |
| 261 | * @return Dequantized values in a neon vector |
| 262 | */ |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 263 | inline float32x4x2_t vdequantize(const uint8x8_t &qv, const UniformQuantizationInfo &qi) |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 264 | { |
| 265 | const float scale = qi.scale; |
| 266 | const int offset = qi.offset; |
| 267 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 268 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 269 | const float32x4x2_t vdequantized_input = |
| 270 | { |
| 271 | { |
| 272 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(qv)))), voffset)), vscale), |
| 273 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(qv)))), voffset)), vscale), |
| 274 | } |
| 275 | }; |
| 276 | return vdequantized_input; |
| 277 | } |
| 278 | |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 279 | /** Dequantize a neon vector holding 16 quantized values. |
| 280 | * |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 281 | * @param[in] qv Input values to be dequantized. |
| 282 | * @param[in] qi Quantization information to be used in the computation. |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 283 | * |
| 284 | * @return Dequantized values in a neon vector |
| 285 | */ |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 286 | inline float32x4x4_t vdequantize(const uint8x16_t &qv, const UniformQuantizationInfo &qi) |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 287 | { |
| 288 | const float scale = qi.scale; |
| 289 | const int offset = qi.offset; |
| 290 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 291 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 292 | const float32x4x4_t vdequantized_input = |
| 293 | { |
| 294 | { |
| 295 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(qv))))), voffset)), vscale), |
| 296 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(qv))))), voffset)), vscale), |
| 297 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(qv))))), voffset)), vscale), |
| 298 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(qv))))), voffset)), vscale), |
| 299 | } |
| 300 | }; |
| 301 | return vdequantized_input; |
| 302 | } |
| 303 | |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 304 | /** Dequantize following an asymmetric quantization scheme a neon vector holding 16 quantized values. |
| 305 | * |
| 306 | * @param[in] qv Input values to be dequantized. |
| 307 | * @param[in] scale Quantization scaling factor. |
| 308 | * @param[in] offset Zero quantization offset. |
| 309 | * |
| 310 | * @return Dequantized values in a neon vector |
| 311 | */ |
| 312 | inline float32x4x4_t vdequantize(const uint8x16_t &qv, float scale, int32_t offset) |
| 313 | { |
| 314 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 315 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 316 | const float32x4x4_t vdequantized_input = |
| 317 | { |
| 318 | { |
| 319 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(qv))))), voffset)), vscale), |
| 320 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(qv))))), voffset)), vscale), |
| 321 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(qv))))), voffset)), vscale), |
| 322 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(qv))))), voffset)), vscale), |
| 323 | } |
| 324 | }; |
| 325 | return vdequantized_input; |
| 326 | } |
| 327 | |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 328 | /** Dequantize following symmetric quantization scheme a neon vector holding 16 quantized values. |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 329 | * |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 330 | * @param[in] qv Input values to be dequantized. |
| 331 | * @param[in] vscale Vector containing quantization scaling factors. |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 332 | * |
| 333 | * @return Dequantized values in a neon vector |
| 334 | */ |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 335 | inline float32x4x4_t vdequantize(const int8x16_t &qv, const float32x4x4_t vscale) |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 336 | { |
| 337 | const float32x4x4_t vdequantized_input = |
| 338 | { |
| 339 | { |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 340 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_low_s8(qv))))), vscale.val[0]), |
| 341 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_low_s8(qv))))), vscale.val[1]), |
| 342 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_high_s8(qv))))), vscale.val[2]), |
| 343 | 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] | 344 | } |
| 345 | }; |
| 346 | return vdequantized_input; |
| 347 | } |
| 348 | |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 349 | /** Dequantize following a symmetric quantization scheme a neon vector holding 16 quantized values. |
| 350 | * |
| 351 | * @param[in] qv Input values to be dequantized. |
| 352 | * @param[in] scale Quantization scaling factor. |
| 353 | * |
| 354 | * @return Dequantized values in a neon vector |
| 355 | */ |
| 356 | inline float32x4x4_t vdequantize(const int8x16_t &qv, float scale) |
| 357 | { |
| 358 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 359 | const float32x4x4_t vdequantized_input = |
| 360 | { |
| 361 | { |
| 362 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_low_s8(qv))))), vscale), |
| 363 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_low_s8(qv))))), vscale), |
| 364 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_high_s8(qv))))), vscale), |
| 365 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_high_s8(qv))))), vscale), |
| 366 | } |
| 367 | }; |
| 368 | return vdequantized_input; |
| 369 | } |
| 370 | |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 371 | /** Quantize a neon vector holding 8 floating point values. |
| 372 | * |
| 373 | * @param[in] qv Input values to be quantized. |
| 374 | * @param[in] qi Quantization information to be used in the computation. |
| 375 | * |
| 376 | * @return A neon vector holding the quantized values |
| 377 | */ |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 378 | inline uint8x8_t vquantize(const float32x4x2_t &qv, const UniformQuantizationInfo &qi) |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 379 | { |
| 380 | const float scale = qi.scale; |
| 381 | const int offset = qi.offset; |
| 382 | const float32x4_t voffset = vdupq_n_f32(offset); |
| 383 | const float32x4_t vinvscale = vdupq_n_f32(1.f / scale); |
| 384 | const int32x4x4_t rf = |
| 385 | { |
| 386 | { |
| 387 | #ifdef __aarch64__ |
| 388 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 389 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 390 | #else //__aarch64__ |
| 391 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 392 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 393 | #endif //__aarch64__ |
| 394 | } |
| 395 | }; |
| 396 | return vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]))); |
| 397 | } |
| 398 | |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 399 | /** Quantize a neon vector holding 16 floating point values. |
| 400 | * |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 401 | * @param[in] qv Input values to be quantized. |
| 402 | * @param[in] qi Quantization information to be used in the computation. |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 403 | * |
| 404 | * @return A neon vector holding the quantized values |
| 405 | */ |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 406 | inline uint8x16_t vquantize(const float32x4x4_t &qv, const UniformQuantizationInfo &qi) |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 407 | { |
| 408 | const float scale = qi.scale; |
| 409 | const int offset = qi.offset; |
| 410 | const float32x4_t voffset = vdupq_n_f32(offset); |
| 411 | const float32x4_t vinvscale = vdupq_n_f32(1.f / scale); |
| 412 | const int32x4x4_t rf = |
| 413 | { |
| 414 | { |
| 415 | #ifdef __aarch64__ |
| 416 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 417 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 418 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[2], vinvscale)), |
| 419 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[3], vinvscale)), |
| 420 | #else //__aarch64__ |
| 421 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 422 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 423 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[2], vinvscale)), |
| 424 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[3], vinvscale)), |
| 425 | #endif //__aarch64__ |
| 426 | } |
| 427 | }; |
| 428 | const uint8x8_t pa = vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]))); |
| 429 | const uint8x8_t pb = vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3]))); |
| 430 | return vcombine_u8(pa, pb); |
| 431 | } |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 432 | |
| 433 | /** Quantize to QASYMM16 a neon vector holding 16 floating point values. |
| 434 | * |
| 435 | * @param[in] qv Input values to be quantized. |
| 436 | * @param[in] qi Quantization information to be used in the computation. |
| 437 | * |
| 438 | * @return A neon vector holding the quantized values |
| 439 | */ |
| 440 | inline uint16x8x2_t vquantize_qasymm16(const float32x4x4_t &qv, const UniformQuantizationInfo &qi) |
| 441 | { |
| 442 | const float scale = qi.scale; |
| 443 | const int offset = qi.offset; |
| 444 | const float32x4_t voffset = vdupq_n_f32(offset); |
| 445 | const float32x4_t vinvscale = vdupq_n_f32(1.f / scale); |
| 446 | const int32x4x4_t rf = |
| 447 | { |
| 448 | { |
| 449 | #ifdef __aarch64__ |
| 450 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 451 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 452 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[2], vinvscale)), |
| 453 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[3], vinvscale)), |
| 454 | #else //__aarch64__ |
| 455 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 456 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 457 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[2], vinvscale)), |
| 458 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[3], vinvscale)), |
| 459 | #endif //__aarch64__ |
| 460 | } |
| 461 | }; |
| 462 | const uint16x8_t pa = vcombine_u16(vqmovun_s32(rf.val[0]), vqmovun_s32(rf.val[1])); |
| 463 | const uint16x8_t pb = vcombine_u16(vqmovun_s32(rf.val[2]), vqmovun_s32(rf.val[3])); |
| 464 | return { pa, pb }; |
| 465 | } |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 466 | } // namespace arm_compute |
| 467 | #include "arm_compute/core/NEON/NEAsymm.inl" |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 468 | #endif // __ARM_COMPUTE_NEASYMM_H__ |