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