blob: 5f4d08d0f6cdf4e4ad972b4789c53d34ec08d56a [file] [log] [blame]
Gian Marco58c57942017-11-28 09:10:03 +00001/*
Pablo Marquez Tello20cfa452023-03-20 16:29:21 +00002 * Copyright (c) 2017-2020, 2023 Arm Limited.
Gian Marco58c57942017-11-28 09:10:03 +00003 *
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 Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_NEASYMM_H
25#define ARM_COMPUTE_NEASYMM_H
Gian Marco58c57942017-11-28 09:10:03 +000026
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010027#include "src/core/NEON/NEMath.h"
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +000028#include "src/core/NEON/wrapper/intrinsics/intrinsics.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029
Gian Marco58c57942017-11-28 09:10:03 +000030#include <arm_neon.h>
31
32namespace arm_compute
33{
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000034using qasymm8x8_t = uint8x8_t; /**< 8 bit quantized asymmetric vector with 8 elements */
35using qasymm8x8x2_t = uint8x8x2_t; /**< 8 bit quantized asymmetric vector with 16 elements */
36using qasymm8x8x3_t = uint8x8x3_t; /**< 8 bit quantized asymmetric vector with 24 elements */
37using qasymm8x8x4_t = uint8x8x4_t; /**< 8 bit quantized asymmetric vector with 32 elements */
38using qasymm8x16_t = uint8x16_t; /**< 8 bit quantized asymmetric vector with 16 elements */
39
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +000040using qasymm8x8_signed_t = int8x8_t; /**< 8 bit quantized signed asymmetric vector with 8 elements */
41using qasymm8x8x2_signed_t = int8x8x2_t; /**< 8 bit quantized signed asymmetric vector with 16 elements */
42using qasymm8x8x3_signed_t = int8x8x3_t; /**< 8 bit quantized signed asymmetric vector with 24 elements */
43using qasymm8x8x4_signed_t = int8x8x4_t; /**< 8 bit quantized signed asymmetric vector with 32 elements */
44using qasymm8x16_signed_t = int8x16_t; /**< 8 bit quantized signed asymmetric vector with 16 elements */
45
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000046/** 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.coma2561f02023-06-16 20:45:48 +010056template <RoundingPolicy round_policy = RoundingPolicy::TO_ZERO>
57qasymm8x16_t vmlaq_qasymm8(qasymm8x16_t vd, float32x4_t vs, float32x4_t vo);
Georgios Pinitasf72f9362018-01-12 16:29:45 +000058
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +000059/** 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.coma2561f02023-06-16 20:45:48 +010069template <RoundingPolicy round_policy = RoundingPolicy::TO_ZERO>
70qasymm8x16_signed_t vmlaq_qasymm8_signed(qasymm8x16_signed_t vd, float32x4_t vs, float32x4_t vo);
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +000071
Georgios Pinitasf72f9362018-01-12 16:29:45 +000072/** Performs final quantization step on 16 elements
73 *
Michalis Spyrou70d43a32020-06-22 17:05:43 +010074 * @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 Pinitasf72f9362018-01-12 16:29:45 +000081 *
82 * @return Quantized values
83 */
Michalis Spyrou70d43a32020-06-22 17:05:43 +010084inline 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 Pinitasf72f9362018-01-12 16:29:45 +000091{
92 const static int32x4_t zero_s32 = vdupq_n_s32(0);
93
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 if (result_shift < 0)
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +000095 {
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 Pinitasf72f9362018-01-12 16:29:45 +0000100
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000101 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 Pinitasf72f9362018-01-12 16:29:45 +0000120
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100134 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 Pinitasf72f9362018-01-12 16:29:45 +0000136
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140 if (is_bounded_relu)
Georgios Pinitasf72f9362018-01-12 16:29:45 +0000141 {
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 Tello54e98d92019-02-05 16:16:19 +0000148
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000149/** Performs final quantization step on 16 elements
150 *
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100151 * @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 Pinitas448a81f2019-11-21 14:10:25 +0000158 *
159 * @return Quantized values
160 */
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100161inline 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 Pinitas448a81f2019-11-21 14:10:25 +0000168{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100169 if (result_shift < 0)
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000170 {
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 Pinitas448a81f2019-11-21 14:10:25 +0000175
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000176 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 Pinitas448a81f2019-11-21 14:10:25 +0000195
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100203 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 Pinitas448a81f2019-11-21 14:10:25 +0000205
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100209 if (is_bounded_relu)
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000210 {
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 Pinitasdbdea0d2019-10-16 19:21:40 +0100218/** Performs final quantization step on 16 elements for symmetric quantization
219 *
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100220 * @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 Pinitasdbdea0d2019-10-16 19:21:40 +0100227 *
228 * @return Quantized values
229 */
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100230inline 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 Spyrou70d43a32020-06-22 17:05:43 +0100235 const int8x16_t &max_s8,
236 const bool is_bounded_relu)
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100237{
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000238 const static int32x4_t one_s32 = vdupq_n_s32(1);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100239
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000240 // Fixed point multiplication with vector saturating rounding doubling multiply high with scalar
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100241 int32x4x4_t res_shift_gt0 = {
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000242 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 Pinitasdbdea0d2019-10-16 19:21:40 +0100247 // Round to the nearest division by a power-of-two using result_shift_s32
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000248 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100253 int32x4x4_t res_shift_lt0 = {
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000254 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100265 const uint32x4x4_t mask_lt0 = {
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000266#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 Pinitasdbdea0d2019-10-16 19:21:40 +0100283
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100291 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 Pinitasdbdea0d2019-10-16 19:21:40 +0100293
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100297 if (is_bounded_relu)
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100298 {
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 Wort2d7e6832019-02-22 16:37:41 +0000306/** Performs final quantization step on single element
307 *
George Wort2d7e6832019-02-22 16:37:41 +0000308 * @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 Spyrou70d43a32020-06-22 17:05:43 +0100314 * @param[in] is_bounded_relu Specified if a fused bounded relu should be applied
George Wort2d7e6832019-02-22 16:37:41 +0000315 *
316 * @return Quantized value
317 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100318inline 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 Wort2d7e6832019-02-22 16:37:41 +0000325{
326 int32x4_t in_s32 = vdupq_n_s32(in_value);
327
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100328 if (result_shift < 0)
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000329 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100330 in_value = vgetq_lane_s32(
331 vqrdmulhq_n_s32(vmulq_n_s32(in_s32, (1 << (-result_shift))), result_fixedpoint_multiplier), 0);
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000332 }
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 Wort2d7e6832019-02-22 16:37:41 +0000340
341 // Add the offset term
342 in_value += result_offset_after_shift_s32;
343
344 // Bound the result
Georgios Pinitas6fa26382019-03-18 10:05:34 +0000345 uint8_t out_u8 = static_cast<uint8_t>(std::max<int32_t>(0, std::min<int32_t>(255, in_value)));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100346 if (is_bounded_relu)
George Wort2d7e6832019-02-22 16:37:41 +0000347 {
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 Pinitasdbdea0d2019-10-16 19:21:40 +0100354/** Performs final quantization step on single element
355 *
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100356 * @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 Spyrou70d43a32020-06-22 17:05:43 +0100362 * @param[in] is_bounded_relu Specified if a fused bounded relu should be applied
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100363 *
364 * @return Quantized value
365 */
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100366inline 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 Pinitasdbdea0d2019-10-16 19:21:40 +0100373{
374 int32x4_t in_s32 = vdupq_n_s32(in_value);
375
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100376 if (result_shift < 0)
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000377 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100378 in_value = vgetq_lane_s32(
379 vqrdmulhq_n_s32(vmulq_n_s32(in_s32, (1 << (-result_shift))), result_fixedpoint_multiplier), 0);
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000380 }
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 Pinitasdbdea0d2019-10-16 19:21:40 +0100385
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000386 // Shift value by result_shift_s32
387 in_value = rounding_divide_by_pow2(in_value, result_shift);
388 }
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100389
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100395 if (is_bounded_relu)
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100396 {
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 Pinitasd66094e2019-04-15 15:44:17 +0100403/** 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 Pinitas4c5469b2019-05-21 13:32:43 +0100410inline float32x4x2_t vdequantize(const uint8x8_t &qv, const UniformQuantizationInfo &qi)
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100411{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100412 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 Pinitasd66094e2019-04-15 15:44:17 +0100422 return vdequantized_input;
423}
424
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000425/** 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 */
432inline float32x4x2_t vdequantize(const int8x8_t &qv, const UniformQuantizationInfo &qi)
433{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100434 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 Spyrou8d4d1b82019-11-28 11:31:23 +0000442 return vdequantized_input;
443}
444
Pablo Tello54e98d92019-02-05 16:16:19 +0000445/** Dequantize a neon vector holding 16 quantized values.
446 *
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100447 * @param[in] qv Input values to be dequantized.
448 * @param[in] qi Quantization information to be used in the computation.
Pablo Tello54e98d92019-02-05 16:16:19 +0000449 *
450 * @return Dequantized values in a neon vector
451 */
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100452inline float32x4x4_t vdequantize(const uint8x16_t &qv, const UniformQuantizationInfo &qi)
Pablo Tello54e98d92019-02-05 16:16:19 +0000453{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100454 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 Tello54e98d92019-02-05 16:16:19 +0000472 return vdequantized_input;
473}
474
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000475/** 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 */
482inline float32x4x4_t vdequantize(const int8x16_t &qv, const UniformQuantizationInfo &qi)
483{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100484 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 Spyrou8d4d1b82019-11-28 11:31:23 +0000494 return vdequantized_input;
495}
496
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100497/** 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 */
505inline float32x4x4_t vdequantize(const uint8x16_t &qv, float scale, int32_t offset)
506{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100507 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 Pinitas3d13af82019-06-04 13:04:16 +0100523 return vdequantized_input;
524}
525
Sang-Hoon Parkd8176472019-12-04 09:46:28 +0000526/** 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 */
534inline float32x4x4_t vdequantize(const int8x16_t &qv, float scale, int32_t offset)
535{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100536 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 Parkd8176472019-12-04 09:46:28 +0000544 return vdequantized_input;
545}
546
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000547/** Dequantize following symmetric quantization scheme a neon vector holding 16 quantized values.
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100548 *
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000549 * @param[in] qv Input values to be dequantized.
550 * @param[in] vscale Vector containing quantization scaling factors.
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100551 *
552 * @return Dequantized values in a neon vector
553 */
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000554inline float32x4x4_t vdequantize(const int8x16_t &qv, const float32x4x4_t vscale)
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100555{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100556 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 Spyrou3f632f32019-08-22 16:52:00 +0100562 return vdequantized_input;
563}
564
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100565/** 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 */
572inline float32x4x4_t vdequantize(const int8x16_t &qv, float scale)
573{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100574 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 Pinitas3d13af82019-06-04 13:04:16 +0100581 return vdequantized_input;
582}
583
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100584/** 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 Pinitas4c5469b2019-05-21 13:32:43 +0100591inline uint8x8_t vquantize(const float32x4x2_t &qv, const UniformQuantizationInfo &qi)
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100592{
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100597 const int32x4x4_t rf = {{
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100598#ifdef __aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100599 vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)),
600 vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)),
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100601#else //__aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100602 vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)),
603 vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)),
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100604#endif //__aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100605 }};
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100606 return vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1])));
607}
608
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000609/** 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 */
616inline 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100622 const int32x4x4_t rf = {{
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000623#ifdef __aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100624 vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)),
625 vcvtnq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)),
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000626#else //__aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100627 vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[0], vinvscale)),
628 vcvtq_s32_f32(vmlaq_f32(voffset, qv.val[1], vinvscale)),
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000629#endif //__aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100630 }};
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000631 return vqmovn_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1])));
632}
633
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000634inline 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100638 const int32x4x4_t rf = {{
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000639#ifdef __aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100640 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 Parkadd8e812020-11-25 11:46:03 +0000644#else //__aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100645 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 Parkadd8e812020-11-25 11:46:03 +0000649#endif //__aarch64__
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100650 }};
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000651 return rf;
652}
653
Pablo Tello54e98d92019-02-05 16:16:19 +0000654/** Quantize a neon vector holding 16 floating point values.
655 *
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100656 * @param[in] qv Input values to be quantized.
657 * @param[in] qi Quantization information to be used in the computation.
Pablo Tello54e98d92019-02-05 16:16:19 +0000658 *
659 * @return A neon vector holding the quantized values
660 */
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100661inline uint8x16_t vquantize(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
Pablo Tello54e98d92019-02-05 16:16:19 +0000662{
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000663 auto rf = vquantize_internal(qv, qi.scale, qi.offset);
Pablo Tello54e98d92019-02-05 16:16:19 +0000664 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 Giorgiod64a46c2019-10-01 12:25:49 +0100668
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000669/** 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 Spyrou8d4d1b82019-11-28 11:31:23 +0000676inline int8x16_t vquantize_signed(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
677{
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000678 auto rf = vquantize_internal(qv, qi.scale, qi.offset);
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000679 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 Giorgiod64a46c2019-10-01 12:25:49 +0100684/** 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 */
691inline uint16x8x2_t vquantize_qasymm16(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
692{
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000693 auto rf = vquantize_internal(qv, qi.scale, qi.offset);
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100694 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100696 return {pa, pb};
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100697}
Pablo Marquez Tello20cfa452023-03-20 16:29:21 +0000698
Gian Marco58c57942017-11-28 09:10:03 +0000699} // namespace arm_compute
Georgios Pinitasddb93bb2020-10-02 16:38:59 +0100700#include "src/core/NEON/NEAsymm.inl"
Michalis Spyrouf4643372019-11-29 16:17:13 +0000701#endif // ARM_COMPUTE_NEASYMM_H