Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1 | /* |
Pablo Marquez Tello | 20cfa45 | 2023-03-20 16:29:21 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020, 2023 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 | |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 27 | #include "src/core/NEON/NEMath.h" |
Sang-Hoon Park | add8e81 | 2020-11-25 11:46:03 +0000 | [diff] [blame] | 28 | #include "src/core/NEON/wrapper/intrinsics/intrinsics.h" |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 29 | #include <arm_neon.h> |
| 30 | |
| 31 | namespace arm_compute |
| 32 | { |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 33 | using qasymm8x8_t = uint8x8_t; /**< 8 bit quantized asymmetric vector with 8 elements */ |
| 34 | using qasymm8x8x2_t = uint8x8x2_t; /**< 8 bit quantized asymmetric vector with 16 elements */ |
| 35 | using qasymm8x8x3_t = uint8x8x3_t; /**< 8 bit quantized asymmetric vector with 24 elements */ |
| 36 | using qasymm8x8x4_t = uint8x8x4_t; /**< 8 bit quantized asymmetric vector with 32 elements */ |
| 37 | using qasymm8x16_t = uint8x16_t; /**< 8 bit quantized asymmetric vector with 16 elements */ |
| 38 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 39 | using qasymm8x8_signed_t = int8x8_t; /**< 8 bit quantized signed asymmetric vector with 8 elements */ |
| 40 | using qasymm8x8x2_signed_t = int8x8x2_t; /**< 8 bit quantized signed asymmetric vector with 16 elements */ |
| 41 | using qasymm8x8x3_signed_t = int8x8x3_t; /**< 8 bit quantized signed asymmetric vector with 24 elements */ |
| 42 | using qasymm8x8x4_signed_t = int8x8x4_t; /**< 8 bit quantized signed asymmetric vector with 32 elements */ |
| 43 | using qasymm8x16_signed_t = int8x16_t; /**< 8 bit quantized signed asymmetric vector with 16 elements */ |
| 44 | |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 45 | /** Perform a multiply-accumulate on all 16 components of a QASYMM8 vector |
| 46 | * |
| 47 | * vd*vs + vo |
| 48 | * |
| 49 | * @param[in] vd Input vector value in QASYMM8 format |
| 50 | * @param[in] vs Vector multiplier in F32 format. The multiplier value must be duplicated across all four lanes. |
| 51 | * @param[in] vo Vector addend in F32 format. The addend value must be duplicated across all four lanes. |
| 52 | * |
| 53 | * @return A 16-component vector in QASYMM8 format, saturated to fit |
| 54 | */ |
ramy.elgammal@arm.com | a2561f0 | 2023-06-16 20:45:48 +0100 | [diff] [blame] | 55 | template <RoundingPolicy round_policy = RoundingPolicy::TO_ZERO> |
| 56 | qasymm8x16_t vmlaq_qasymm8(qasymm8x16_t vd, float32x4_t vs, float32x4_t vo); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 57 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 58 | /** Perform a multiply-accumulate on all 16 components of a QASYMM8_SIGNED vector |
| 59 | * |
| 60 | * vd*vs + vo |
| 61 | * |
| 62 | * @param[in] vd Input vector value in QASYMM8_SIGNED format |
| 63 | * @param[in] vs Vector multiplier in F32 format. The multiplier value must be duplicated across all four lanes. |
| 64 | * @param[in] vo Vector addend in F32 format. The addend value must be duplicated across all four lanes. |
| 65 | * |
| 66 | * @return A 16-component vector in QASYMM8_SIGNED format, saturated to fit |
| 67 | */ |
ramy.elgammal@arm.com | a2561f0 | 2023-06-16 20:45:48 +0100 | [diff] [blame] | 68 | template <RoundingPolicy round_policy = RoundingPolicy::TO_ZERO> |
| 69 | qasymm8x16_signed_t vmlaq_qasymm8_signed(qasymm8x16_signed_t vd, float32x4_t vs, float32x4_t vo); |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 70 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 71 | /** Performs final quantization step on 16 elements |
| 72 | * |
Michalis Spyrou | 70d43a3 | 2020-06-22 17:05:43 +0100 | [diff] [blame] | 73 | * @param[in] in_s32 Input to be quantized. |
| 74 | * @param[in] result_fixedpoint_multiplier Result multiplier parameter |
| 75 | * @param[in] result_shift Result shift parameter |
| 76 | * @param[in] result_offset_after_shift_s32 Result offset parameter |
| 77 | * @param[in] min_u8 Relu lower bound |
| 78 | * @param[in] max_u8 Relu upper bound |
| 79 | * @param[in] is_bounded_relu Specified if a fused bounded relu should be applied |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 80 | * |
| 81 | * @return Quantized values |
| 82 | */ |
Michalis Spyrou | 70d43a3 | 2020-06-22 17:05:43 +0100 | [diff] [blame] | 83 | inline uint8x16_t finalize_quantization(int32x4x4_t &in_s32, |
| 84 | int result_fixedpoint_multiplier, |
| 85 | int32_t result_shift, |
| 86 | int32x4_t result_offset_after_shift_s32, |
| 87 | uint8x16_t min_u8, |
| 88 | uint8x16_t max_u8, |
| 89 | bool is_bounded_relu) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 90 | { |
| 91 | const static int32x4_t zero_s32 = vdupq_n_s32(0); |
| 92 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 93 | if(result_shift < 0) |
| 94 | { |
| 95 | in_s32.val[0] = vmulq_n_s32(in_s32.val[0], (1 << (-result_shift))); |
| 96 | in_s32.val[1] = vmulq_n_s32(in_s32.val[1], (1 << (-result_shift))); |
| 97 | in_s32.val[2] = vmulq_n_s32(in_s32.val[2], (1 << (-result_shift))); |
| 98 | 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] | 99 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 100 | in_s32.val[0] = vqrdmulhq_n_s32(in_s32.val[0], result_fixedpoint_multiplier); |
| 101 | in_s32.val[1] = vqrdmulhq_n_s32(in_s32.val[1], result_fixedpoint_multiplier); |
| 102 | in_s32.val[2] = vqrdmulhq_n_s32(in_s32.val[2], result_fixedpoint_multiplier); |
| 103 | in_s32.val[3] = vqrdmulhq_n_s32(in_s32.val[3], result_fixedpoint_multiplier); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar |
| 108 | in_s32.val[0] = vqrdmulhq_n_s32(in_s32.val[0], result_fixedpoint_multiplier); |
| 109 | in_s32.val[1] = vqrdmulhq_n_s32(in_s32.val[1], result_fixedpoint_multiplier); |
| 110 | in_s32.val[2] = vqrdmulhq_n_s32(in_s32.val[2], result_fixedpoint_multiplier); |
| 111 | in_s32.val[3] = vqrdmulhq_n_s32(in_s32.val[3], result_fixedpoint_multiplier); |
| 112 | |
| 113 | // Round to the nearest division by a power-of-two using result_shift_s32 |
| 114 | in_s32.val[0] = rounding_divide_by_pow2(in_s32.val[0], result_shift); |
| 115 | in_s32.val[1] = rounding_divide_by_pow2(in_s32.val[1], result_shift); |
| 116 | in_s32.val[2] = rounding_divide_by_pow2(in_s32.val[2], result_shift); |
| 117 | in_s32.val[3] = rounding_divide_by_pow2(in_s32.val[3], result_shift); |
| 118 | } |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 119 | |
| 120 | // Add the offset terms |
| 121 | in_s32.val[0] = vaddq_s32(in_s32.val[0], result_offset_after_shift_s32); |
| 122 | in_s32.val[1] = vaddq_s32(in_s32.val[1], result_offset_after_shift_s32); |
| 123 | in_s32.val[2] = vaddq_s32(in_s32.val[2], result_offset_after_shift_s32); |
| 124 | in_s32.val[3] = vaddq_s32(in_s32.val[3], result_offset_after_shift_s32); |
| 125 | |
| 126 | // Saturate negative values |
| 127 | in_s32.val[0] = vmaxq_s32(in_s32.val[0], zero_s32); |
| 128 | in_s32.val[1] = vmaxq_s32(in_s32.val[1], zero_s32); |
| 129 | in_s32.val[2] = vmaxq_s32(in_s32.val[2], zero_s32); |
| 130 | in_s32.val[3] = vmaxq_s32(in_s32.val[3], zero_s32); |
| 131 | |
| 132 | // Convert S32 to S16 |
| 133 | const int16x8x2_t in_s16 = |
| 134 | { |
| 135 | { |
| 136 | vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])), |
| 137 | vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3])) |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | // Convert S16 to U8 |
| 142 | uint8x16_t out_u8 = vcombine_u8(vqmovun_s16(in_s16.val[0]), vqmovun_s16(in_s16.val[1])); |
| 143 | |
| 144 | if(is_bounded_relu) |
| 145 | { |
| 146 | out_u8 = vmaxq_u8(out_u8, min_u8); |
| 147 | out_u8 = vminq_u8(out_u8, max_u8); |
| 148 | } |
| 149 | |
| 150 | return out_u8; |
| 151 | } |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 152 | |
Georgios Pinitas | 448a81f | 2019-11-21 14:10:25 +0000 | [diff] [blame] | 153 | /** Performs final quantization step on 16 elements |
| 154 | * |
Michalis Spyrou | 70d43a3 | 2020-06-22 17:05:43 +0100 | [diff] [blame] | 155 | * @param[in] in_s32 Input to be quantized. |
| 156 | * @param[in] result_fixedpoint_multiplier Result multiplier parameter |
| 157 | * @param[in] result_shift Result shift parameter |
| 158 | * @param[in] result_offset_after_shift_s32 Result offset parameter |
| 159 | * @param[in] min_s8 Relu lower bound |
| 160 | * @param[in] max_s8 Relu upper bound |
| 161 | * @param[in] is_bounded_relu Specified if a fused bounded relu should be applied |
Georgios Pinitas | 448a81f | 2019-11-21 14:10:25 +0000 | [diff] [blame] | 162 | * |
| 163 | * @return Quantized values |
| 164 | */ |
Michalis Spyrou | 70d43a3 | 2020-06-22 17:05:43 +0100 | [diff] [blame] | 165 | inline 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 | bool is_bounded_relu) |
Georgios Pinitas | 448a81f | 2019-11-21 14:10:25 +0000 | [diff] [blame] | 172 | { |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 173 | if(result_shift < 0) |
| 174 | { |
| 175 | in_s32.val[0] = vmulq_n_s32(in_s32.val[0], (1 << (-result_shift))); |
| 176 | in_s32.val[1] = vmulq_n_s32(in_s32.val[1], (1 << (-result_shift))); |
| 177 | in_s32.val[2] = vmulq_n_s32(in_s32.val[2], (1 << (-result_shift))); |
| 178 | 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] | 179 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 180 | in_s32.val[0] = vqrdmulhq_n_s32(in_s32.val[0], result_fixedpoint_multiplier); |
| 181 | in_s32.val[1] = vqrdmulhq_n_s32(in_s32.val[1], result_fixedpoint_multiplier); |
| 182 | in_s32.val[2] = vqrdmulhq_n_s32(in_s32.val[2], result_fixedpoint_multiplier); |
| 183 | in_s32.val[3] = vqrdmulhq_n_s32(in_s32.val[3], result_fixedpoint_multiplier); |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar |
| 188 | in_s32.val[0] = vqrdmulhq_n_s32(in_s32.val[0], result_fixedpoint_multiplier); |
| 189 | in_s32.val[1] = vqrdmulhq_n_s32(in_s32.val[1], result_fixedpoint_multiplier); |
| 190 | in_s32.val[2] = vqrdmulhq_n_s32(in_s32.val[2], result_fixedpoint_multiplier); |
| 191 | in_s32.val[3] = vqrdmulhq_n_s32(in_s32.val[3], result_fixedpoint_multiplier); |
| 192 | |
| 193 | // Round to the nearest division by a power-of-two using result_shift_s32 |
| 194 | in_s32.val[0] = rounding_divide_by_pow2(in_s32.val[0], result_shift); |
| 195 | in_s32.val[1] = rounding_divide_by_pow2(in_s32.val[1], result_shift); |
| 196 | in_s32.val[2] = rounding_divide_by_pow2(in_s32.val[2], result_shift); |
| 197 | in_s32.val[3] = rounding_divide_by_pow2(in_s32.val[3], result_shift); |
| 198 | } |
Georgios Pinitas | 448a81f | 2019-11-21 14:10:25 +0000 | [diff] [blame] | 199 | |
| 200 | // Add the offset terms |
| 201 | in_s32.val[0] = vaddq_s32(in_s32.val[0], result_offset_after_shift_s32); |
| 202 | in_s32.val[1] = vaddq_s32(in_s32.val[1], result_offset_after_shift_s32); |
| 203 | in_s32.val[2] = vaddq_s32(in_s32.val[2], result_offset_after_shift_s32); |
| 204 | in_s32.val[3] = vaddq_s32(in_s32.val[3], result_offset_after_shift_s32); |
| 205 | |
| 206 | // Convert S32 to S16 |
| 207 | const int16x8x2_t in_s16 = |
| 208 | { |
| 209 | { |
| 210 | vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])), |
| 211 | vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3])) |
| 212 | } |
| 213 | }; |
| 214 | |
| 215 | // Convert S16 to S8 |
| 216 | int8x16_t out_s8 = vcombine_s8(vqmovn_s16(in_s16.val[0]), vqmovn_s16(in_s16.val[1])); |
| 217 | |
| 218 | if(is_bounded_relu) |
| 219 | { |
| 220 | out_s8 = vmaxq_s8(out_s8, min_s8); |
| 221 | out_s8 = vminq_s8(out_s8, max_s8); |
| 222 | } |
| 223 | |
| 224 | return out_s8; |
| 225 | } |
| 226 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 227 | /** Performs final quantization step on 16 elements for symmetric quantization |
| 228 | * |
Michalis Spyrou | 70d43a3 | 2020-06-22 17:05:43 +0100 | [diff] [blame] | 229 | * @param[in] in_s32 Input to be quantized. |
| 230 | * @param[in] result_fixedpoint_multiplier Result multiplier parameter |
| 231 | * @param[in] result_shift Result shift parameter |
| 232 | * @param[in] result_offset_after_shift_s32 Result offset parameter |
| 233 | * @param[in] min_s8 Relu lower bound |
| 234 | * @param[in] max_s8 Relu upper bound |
| 235 | * @param[in] is_bounded_relu Specified if a fused bounded relu should be applied |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 236 | * |
| 237 | * @return Quantized values |
| 238 | */ |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 239 | inline int8x16_t finalize_quantization_symm(int32x4x4_t &in_s32, |
| 240 | const int32x4x4_t &result_fixedpoint_multiplier, |
| 241 | const int32x4x4_t &result_shift, |
| 242 | const int32x4_t &result_offset_after_shift_s32, |
| 243 | const int8x16_t &min_s8, |
Michalis Spyrou | 70d43a3 | 2020-06-22 17:05:43 +0100 | [diff] [blame] | 244 | const int8x16_t &max_s8, |
| 245 | const bool is_bounded_relu) |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 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 | * |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 325 | * @param[in] in_value Input to be quantized. |
| 326 | * @param[in] result_fixedpoint_multiplier Result multiplier parameter |
| 327 | * @param[in] result_shift Result shift parameter |
| 328 | * @param[in] result_offset_after_shift_s32 Result offset parameter |
| 329 | * @param[in] min_u8 Relu lower bound |
| 330 | * @param[in] max_u8 Relu upper bound |
Michalis Spyrou | 70d43a3 | 2020-06-22 17:05:43 +0100 | [diff] [blame] | 331 | * @param[in] is_bounded_relu Specified if a fused bounded relu should be applied |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 332 | * |
| 333 | * @return Quantized value |
| 334 | */ |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 335 | inline uint8_t finalize_quantization(int32_t in_value, int result_fixedpoint_multiplier, |
| 336 | int32_t result_shift, int32_t result_offset_after_shift_s32, |
Michalis Spyrou | 70d43a3 | 2020-06-22 17:05:43 +0100 | [diff] [blame] | 337 | uint8_t min_u8, uint8_t max_u8, bool is_bounded_relu) |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 338 | { |
| 339 | int32x4_t in_s32 = vdupq_n_s32(in_value); |
| 340 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 341 | if(result_shift < 0) |
| 342 | { |
| 343 | in_value = vgetq_lane_s32(vqrdmulhq_n_s32(vmulq_n_s32(in_s32, (1 << (-result_shift))), result_fixedpoint_multiplier), 0); |
| 344 | } |
| 345 | else |
| 346 | { |
| 347 | // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar |
| 348 | in_value = vgetq_lane_s32(vqrdmulhq_n_s32(in_s32, result_fixedpoint_multiplier), 0); |
| 349 | // Shift value by result_shift_s32 |
| 350 | in_value = rounding_divide_by_pow2(in_value, result_shift); |
| 351 | } |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 352 | |
| 353 | // Add the offset term |
| 354 | in_value += result_offset_after_shift_s32; |
| 355 | |
| 356 | // Bound the result |
Georgios Pinitas | 6fa2638 | 2019-03-18 10:05:34 +0000 | [diff] [blame] | 357 | 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] | 358 | if(is_bounded_relu) |
| 359 | { |
| 360 | out_u8 = static_cast<uint8_t>(std::max(min_u8, std::min(max_u8, out_u8))); |
| 361 | } |
| 362 | |
| 363 | return out_u8; |
| 364 | } |
| 365 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 366 | /** Performs final quantization step on single element |
| 367 | * |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 368 | * @param[in] in_value Input to be quantized. |
| 369 | * @param[in] result_fixedpoint_multiplier Result multiplier parameter |
| 370 | * @param[in] result_shift Result shift parameter |
| 371 | * @param[in] result_offset_after_shift_s32 Result offset parameter |
| 372 | * @param[in] min_s8 Relu lower bound |
| 373 | * @param[in] max_s8 Relu upper bound |
Michalis Spyrou | 70d43a3 | 2020-06-22 17:05:43 +0100 | [diff] [blame] | 374 | * @param[in] is_bounded_relu Specified if a fused bounded relu should be applied |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 375 | * |
| 376 | * @return Quantized value |
| 377 | */ |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 378 | inline int8_t finalize_quantization(int32_t in_value, int result_fixedpoint_multiplier, |
| 379 | int32_t result_shift, int32_t result_offset_after_shift_s32, |
Michalis Spyrou | 70d43a3 | 2020-06-22 17:05:43 +0100 | [diff] [blame] | 380 | int8_t min_s8, int8_t max_s8, bool is_bounded_relu) |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 381 | { |
| 382 | int32x4_t in_s32 = vdupq_n_s32(in_value); |
| 383 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 384 | if(result_shift < 0) |
| 385 | { |
| 386 | in_value = vgetq_lane_s32(vqrdmulhq_n_s32(vmulq_n_s32(in_s32, (1 << (-result_shift))), result_fixedpoint_multiplier), 0); |
| 387 | } |
| 388 | else |
| 389 | { |
| 390 | // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar |
| 391 | 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] | 392 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 393 | // Shift value by result_shift_s32 |
| 394 | in_value = rounding_divide_by_pow2(in_value, result_shift); |
| 395 | } |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 396 | |
| 397 | // Add the offset term |
| 398 | in_value += result_offset_after_shift_s32; |
| 399 | |
| 400 | // Bound the result |
| 401 | int8_t out_s8 = static_cast<int8_t>(std::max<int32_t>(-128, std::min<int32_t>(127, in_value))); |
| 402 | if(is_bounded_relu) |
| 403 | { |
| 404 | out_s8 = static_cast<int8_t>(std::max(min_s8, std::min(max_s8, out_s8))); |
| 405 | } |
| 406 | |
| 407 | return out_s8; |
| 408 | } |
| 409 | |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 410 | /** Dequantize a neon vector holding 8 quantized values. |
| 411 | * |
| 412 | * @param[in] qv Input values to be dequantized. |
| 413 | * @param[in] qi Quantization information to be used in the computation. |
| 414 | * |
| 415 | * @return Dequantized values in a neon vector |
| 416 | */ |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 417 | inline float32x4x2_t vdequantize(const uint8x8_t &qv, const UniformQuantizationInfo &qi) |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 418 | { |
| 419 | const float scale = qi.scale; |
| 420 | const int offset = qi.offset; |
| 421 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 422 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 423 | const float32x4x2_t vdequantized_input = |
| 424 | { |
| 425 | { |
| 426 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(qv)))), voffset)), vscale), |
| 427 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(qv)))), voffset)), vscale), |
| 428 | } |
| 429 | }; |
| 430 | return vdequantized_input; |
| 431 | } |
| 432 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 433 | /** Dequantize a neon vector holding 8 singed quantized values. |
| 434 | * |
| 435 | * @param[in] qv Input values to be dequantized. |
| 436 | * @param[in] qi Quantization information to be used in the computation. |
| 437 | * |
| 438 | * @return Dequantized values in a neon vector |
| 439 | */ |
| 440 | inline float32x4x2_t vdequantize(const int8x8_t &qv, const UniformQuantizationInfo &qi) |
| 441 | { |
| 442 | const float scale = qi.scale; |
| 443 | const int offset = qi.offset; |
| 444 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 445 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 446 | const float32x4x2_t vdequantized_input = |
| 447 | { |
| 448 | { |
| 449 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(qv))), voffset)), vscale), |
| 450 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(qv))), voffset)), vscale), |
| 451 | } |
| 452 | }; |
| 453 | return vdequantized_input; |
| 454 | } |
| 455 | |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 456 | /** Dequantize a neon vector holding 16 quantized values. |
| 457 | * |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 458 | * @param[in] qv Input values to be dequantized. |
| 459 | * @param[in] qi Quantization information to be used in the computation. |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 460 | * |
| 461 | * @return Dequantized values in a neon vector |
| 462 | */ |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 463 | inline float32x4x4_t vdequantize(const uint8x16_t &qv, const UniformQuantizationInfo &qi) |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 464 | { |
| 465 | const float scale = qi.scale; |
| 466 | const int offset = qi.offset; |
| 467 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 468 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 469 | const float32x4x4_t vdequantized_input = |
| 470 | { |
| 471 | { |
| 472 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(qv))))), voffset)), vscale), |
| 473 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(qv))))), voffset)), vscale), |
| 474 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(qv))))), voffset)), vscale), |
| 475 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(qv))))), voffset)), vscale), |
| 476 | } |
| 477 | }; |
| 478 | return vdequantized_input; |
| 479 | } |
| 480 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 481 | /** Dequantize a neon vector holding 16 signed quantized values. |
| 482 | * |
| 483 | * @param[in] qv Input values to be dequantized. |
| 484 | * @param[in] qi Quantization information to be used in the computation. |
| 485 | * |
| 486 | * @return Dequantized values in a neon vector |
| 487 | */ |
| 488 | inline float32x4x4_t vdequantize(const int8x16_t &qv, const UniformQuantizationInfo &qi) |
| 489 | { |
| 490 | const float scale = qi.scale; |
| 491 | const int offset = qi.offset; |
| 492 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 493 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 494 | const float32x4x4_t vdequantized_input = |
| 495 | { |
| 496 | { |
| 497 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_low_s8(qv)))), voffset)), vscale), |
| 498 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_low_s8(qv)))), voffset)), vscale), |
| 499 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_high_s8(qv)))), voffset)), vscale), |
| 500 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_high_s8(qv)))), voffset)), vscale), |
| 501 | } |
| 502 | }; |
| 503 | return vdequantized_input; |
| 504 | } |
| 505 | |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 506 | /** Dequantize following an asymmetric quantization scheme a neon vector holding 16 quantized values. |
| 507 | * |
| 508 | * @param[in] qv Input values to be dequantized. |
| 509 | * @param[in] scale Quantization scaling factor. |
| 510 | * @param[in] offset Zero quantization offset. |
| 511 | * |
| 512 | * @return Dequantized values in a neon vector |
| 513 | */ |
| 514 | inline float32x4x4_t vdequantize(const uint8x16_t &qv, float scale, int32_t offset) |
| 515 | { |
| 516 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 517 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 518 | const float32x4x4_t vdequantized_input = |
| 519 | { |
| 520 | { |
| 521 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(qv))))), voffset)), vscale), |
| 522 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(qv))))), voffset)), vscale), |
| 523 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(qv))))), voffset)), vscale), |
| 524 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(qv))))), voffset)), vscale), |
| 525 | } |
| 526 | }; |
| 527 | return vdequantized_input; |
| 528 | } |
| 529 | |
Sang-Hoon Park | d817647 | 2019-12-04 09:46:28 +0000 | [diff] [blame] | 530 | /** Dequantize a vector of 16 values stored as signed asymmetric. |
| 531 | * |
| 532 | * @param[in] qv Input values to be dequantized. |
| 533 | * @param[in] scale Quantization scaling factor. |
| 534 | * @param[in] offset Zero quantization offset. |
| 535 | * |
| 536 | * @return Dequantized values in a neon vector |
| 537 | */ |
| 538 | inline float32x4x4_t vdequantize(const int8x16_t &qv, float scale, int32_t offset) |
| 539 | { |
| 540 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 541 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 542 | const float32x4x4_t vdequantized_input = |
| 543 | { |
| 544 | { |
| 545 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_low_s8(qv)))), voffset)), vscale), |
| 546 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_low_s8(qv)))), voffset)), vscale), |
| 547 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_high_s8(qv)))), voffset)), vscale), |
| 548 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_high_s8(qv)))), voffset)), vscale), |
| 549 | } |
| 550 | }; |
| 551 | return vdequantized_input; |
| 552 | } |
| 553 | |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 554 | /** Dequantize following symmetric quantization scheme a neon vector holding 16 quantized values. |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 555 | * |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 556 | * @param[in] qv Input values to be dequantized. |
| 557 | * @param[in] vscale Vector containing quantization scaling factors. |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 558 | * |
| 559 | * @return Dequantized values in a neon vector |
| 560 | */ |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 561 | inline float32x4x4_t vdequantize(const int8x16_t &qv, const float32x4x4_t vscale) |
Michalis Spyrou | 3f632f3 | 2019-08-22 16:52:00 +0100 | [diff] [blame] | 562 | { |
| 563 | const float32x4x4_t vdequantized_input = |
| 564 | { |
| 565 | { |
Georgios Pinitas | 8217c8e | 2019-11-11 18:24:22 +0000 | [diff] [blame] | 566 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_low_s8(qv))))), vscale.val[0]), |
| 567 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_low_s8(qv))))), vscale.val[1]), |
| 568 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_high_s8(qv))))), vscale.val[2]), |
| 569 | 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] | 570 | } |
| 571 | }; |
| 572 | return vdequantized_input; |
| 573 | } |
| 574 | |
Georgios Pinitas | 3d13af8 | 2019-06-04 13:04:16 +0100 | [diff] [blame] | 575 | /** Dequantize following a symmetric quantization scheme a neon vector holding 16 quantized values. |
| 576 | * |
| 577 | * @param[in] qv Input values to be dequantized. |
| 578 | * @param[in] scale Quantization scaling factor. |
| 579 | * |
| 580 | * @return Dequantized values in a neon vector |
| 581 | */ |
| 582 | inline float32x4x4_t vdequantize(const int8x16_t &qv, float scale) |
| 583 | { |
| 584 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 585 | const float32x4x4_t vdequantized_input = |
| 586 | { |
| 587 | { |
| 588 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_low_s8(qv))))), vscale), |
| 589 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_low_s8(qv))))), vscale), |
| 590 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_high_s8(qv))))), vscale), |
| 591 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_high_s8(qv))))), vscale), |
| 592 | } |
| 593 | }; |
| 594 | return vdequantized_input; |
| 595 | } |
| 596 | |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 597 | /** Quantize a neon vector holding 8 floating point values. |
| 598 | * |
| 599 | * @param[in] qv Input values to be quantized. |
| 600 | * @param[in] qi Quantization information to be used in the computation. |
| 601 | * |
| 602 | * @return A neon vector holding the quantized values |
| 603 | */ |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 604 | inline uint8x8_t vquantize(const float32x4x2_t &qv, const UniformQuantizationInfo &qi) |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 605 | { |
| 606 | const float scale = qi.scale; |
| 607 | const int offset = qi.offset; |
| 608 | const float32x4_t voffset = vdupq_n_f32(offset); |
| 609 | const float32x4_t vinvscale = vdupq_n_f32(1.f / scale); |
| 610 | const int32x4x4_t rf = |
| 611 | { |
| 612 | { |
| 613 | #ifdef __aarch64__ |
| 614 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 615 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 616 | #else //__aarch64__ |
| 617 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 618 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 619 | #endif //__aarch64__ |
| 620 | } |
| 621 | }; |
| 622 | return vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]))); |
| 623 | } |
| 624 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 625 | /** Quantize a neon vector holding 8 floating point values. |
| 626 | * |
| 627 | * @param[in] qv Input values to be quantized. |
| 628 | * @param[in] qi Quantization information to be used in the computation. |
| 629 | * |
| 630 | * @return A neon vector holding the singed quantized values |
| 631 | */ |
| 632 | inline int8x8_t vquantize_signed(const float32x4x2_t &qv, const UniformQuantizationInfo &qi) |
| 633 | { |
| 634 | const float scale = qi.scale; |
| 635 | const int offset = qi.offset; |
| 636 | const float32x4_t voffset = vdupq_n_f32(offset); |
| 637 | const float32x4_t vinvscale = vdupq_n_f32(1.f / scale); |
| 638 | const int32x4x4_t rf = |
| 639 | { |
| 640 | { |
| 641 | #ifdef __aarch64__ |
| 642 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 643 | vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 644 | #else //__aarch64__ |
| 645 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)), |
| 646 | vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)), |
| 647 | #endif //__aarch64__ |
| 648 | } |
| 649 | }; |
| 650 | return vqmovn_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]))); |
| 651 | } |
| 652 | |
Sang-Hoon Park | add8e81 | 2020-11-25 11:46:03 +0000 | [diff] [blame] | 653 | inline int32x4x4_t vquantize_internal(const float32x4x4_t &qv, float scale, int32_t offset) |
| 654 | { |
| 655 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 656 | const float32x4_t vinvscale = vdupq_n_f32(1.f / scale); |
| 657 | const int32x4x4_t rf = |
| 658 | { |
| 659 | { |
| 660 | #ifdef __aarch64__ |
| 661 | vaddq_s32(vcvtaq_s32_f32(vmulq_f32(qv.val[0], vinvscale)), voffset), |
| 662 | vaddq_s32(vcvtaq_s32_f32(vmulq_f32(qv.val[1], vinvscale)), voffset), |
| 663 | vaddq_s32(vcvtaq_s32_f32(vmulq_f32(qv.val[2], vinvscale)), voffset), |
| 664 | vaddq_s32(vcvtaq_s32_f32(vmulq_f32(qv.val[3], vinvscale)), voffset), |
| 665 | #else //__aarch64__ |
| 666 | vaddq_s32(vcvtq_s32_f32(vmulq_f32(qv.val[0], vinvscale)), voffset), |
| 667 | vaddq_s32(vcvtq_s32_f32(vmulq_f32(qv.val[1], vinvscale)), voffset), |
| 668 | vaddq_s32(vcvtq_s32_f32(vmulq_f32(qv.val[2], vinvscale)), voffset), |
| 669 | vaddq_s32(vcvtq_s32_f32(vmulq_f32(qv.val[3], vinvscale)), voffset), |
| 670 | #endif //__aarch64__ |
| 671 | } |
| 672 | }; |
| 673 | return rf; |
| 674 | } |
| 675 | |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 676 | /** Quantize a neon vector holding 16 floating point values. |
| 677 | * |
Georgios Pinitas | d66094e | 2019-04-15 15:44:17 +0100 | [diff] [blame] | 678 | * @param[in] qv Input values to be quantized. |
| 679 | * @param[in] qi Quantization information to be used in the computation. |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 680 | * |
| 681 | * @return A neon vector holding the quantized values |
| 682 | */ |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 683 | inline uint8x16_t vquantize(const float32x4x4_t &qv, const UniformQuantizationInfo &qi) |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 684 | { |
Sang-Hoon Park | add8e81 | 2020-11-25 11:46:03 +0000 | [diff] [blame] | 685 | auto rf = vquantize_internal(qv, qi.scale, qi.offset); |
Pablo Tello | 54e98d9 | 2019-02-05 16:16:19 +0000 | [diff] [blame] | 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 | */ |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 698 | inline int8x16_t vquantize_signed(const float32x4x4_t &qv, const UniformQuantizationInfo &qi) |
| 699 | { |
Sang-Hoon Park | add8e81 | 2020-11-25 11:46:03 +0000 | [diff] [blame] | 700 | auto rf = vquantize_internal(qv, qi.scale, qi.offset); |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 701 | const int8x8_t pa = vqmovn_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]))); |
| 702 | const int8x8_t pb = vqmovn_s16(vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3]))); |
| 703 | return vcombine_s8(pa, pb); |
| 704 | } |
| 705 | |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 706 | /** Quantize to QASYMM16 a neon vector holding 16 floating point values. |
| 707 | * |
| 708 | * @param[in] qv Input values to be quantized. |
| 709 | * @param[in] qi Quantization information to be used in the computation. |
| 710 | * |
| 711 | * @return A neon vector holding the quantized values |
| 712 | */ |
| 713 | inline uint16x8x2_t vquantize_qasymm16(const float32x4x4_t &qv, const UniformQuantizationInfo &qi) |
| 714 | { |
Sang-Hoon Park | add8e81 | 2020-11-25 11:46:03 +0000 | [diff] [blame] | 715 | auto rf = vquantize_internal(qv, qi.scale, qi.offset); |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 716 | const uint16x8_t pa = vcombine_u16(vqmovun_s32(rf.val[0]), vqmovun_s32(rf.val[1])); |
| 717 | const uint16x8_t pb = vcombine_u16(vqmovun_s32(rf.val[2]), vqmovun_s32(rf.val[3])); |
| 718 | return { pa, pb }; |
| 719 | } |
Pablo Marquez Tello | 20cfa45 | 2023-03-20 16:29:21 +0000 | [diff] [blame] | 720 | |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 721 | } // namespace arm_compute |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 722 | #include "src/core/NEON/NEAsymm.inl" |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 723 | #endif // ARM_COMPUTE_NEASYMM_H |