blob: e6d0e532c846eefd02e940db7d773ad56d278d2d [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"
Gian Marco58c57942017-11-28 09:10:03 +000029#include <arm_neon.h>
30
31namespace arm_compute
32{
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000033using qasymm8x8_t = uint8x8_t; /**< 8 bit quantized asymmetric vector with 8 elements */
34using qasymm8x8x2_t = uint8x8x2_t; /**< 8 bit quantized asymmetric vector with 16 elements */
35using qasymm8x8x3_t = uint8x8x3_t; /**< 8 bit quantized asymmetric vector with 24 elements */
36using qasymm8x8x4_t = uint8x8x4_t; /**< 8 bit quantized asymmetric vector with 32 elements */
37using qasymm8x16_t = uint8x16_t; /**< 8 bit quantized asymmetric vector with 16 elements */
38
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +000039using qasymm8x8_signed_t = int8x8_t; /**< 8 bit quantized signed asymmetric vector with 8 elements */
40using qasymm8x8x2_signed_t = int8x8x2_t; /**< 8 bit quantized signed asymmetric vector with 16 elements */
41using qasymm8x8x3_signed_t = int8x8x3_t; /**< 8 bit quantized signed asymmetric vector with 24 elements */
42using qasymm8x8x4_signed_t = int8x8x4_t; /**< 8 bit quantized signed asymmetric vector with 32 elements */
43using qasymm8x16_signed_t = int8x16_t; /**< 8 bit quantized signed asymmetric vector with 16 elements */
44
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000045/** 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.coma2561f02023-06-16 20:45:48 +010055template <RoundingPolicy round_policy = RoundingPolicy::TO_ZERO>
56qasymm8x16_t vmlaq_qasymm8(qasymm8x16_t vd, float32x4_t vs, float32x4_t vo);
Georgios Pinitasf72f9362018-01-12 16:29:45 +000057
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +000058/** 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.coma2561f02023-06-16 20:45:48 +010068template <RoundingPolicy round_policy = RoundingPolicy::TO_ZERO>
69qasymm8x16_signed_t vmlaq_qasymm8_signed(qasymm8x16_signed_t vd, float32x4_t vs, float32x4_t vo);
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +000070
Georgios Pinitasf72f9362018-01-12 16:29:45 +000071/** Performs final quantization step on 16 elements
72 *
Michalis Spyrou70d43a32020-06-22 17:05:43 +010073 * @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 Pinitasf72f9362018-01-12 16:29:45 +000080 *
81 * @return Quantized values
82 */
Michalis Spyrou70d43a32020-06-22 17:05:43 +010083inline 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 Pinitasf72f9362018-01-12 16:29:45 +000090{
91 const static int32x4_t zero_s32 = vdupq_n_s32(0);
92
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +000093 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 Pinitasf72f9362018-01-12 16:29:45 +000099
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000100 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 Pinitasf72f9362018-01-12 16:29:45 +0000119
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 Tello54e98d92019-02-05 16:16:19 +0000152
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000153/** Performs final quantization step on 16 elements
154 *
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100155 * @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 Pinitas448a81f2019-11-21 14:10:25 +0000162 *
163 * @return Quantized values
164 */
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100165inline 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 Pinitas448a81f2019-11-21 14:10:25 +0000172{
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000173 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 Pinitas448a81f2019-11-21 14:10:25 +0000179
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000180 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 Pinitas448a81f2019-11-21 14:10:25 +0000199
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 Pinitasdbdea0d2019-10-16 19:21:40 +0100227/** Performs final quantization step on 16 elements for symmetric quantization
228 *
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100229 * @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 Pinitasdbdea0d2019-10-16 19:21:40 +0100236 *
237 * @return Quantized values
238 */
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100239inline 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 Spyrou70d43a32020-06-22 17:05:43 +0100244 const int8x16_t &max_s8,
245 const bool is_bounded_relu)
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100246{
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000247 const static int32x4_t one_s32 = vdupq_n_s32(1);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100248
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000249 // 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 Pinitasdbdea0d2019-10-16 19:21:40 +0100257 // Round to the nearest division by a power-of-two using result_shift_s32
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000258 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 Pinitasdbdea0d2019-10-16 19:21:40 +0100295
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 Wort2d7e6832019-02-22 16:37:41 +0000323/** Performs final quantization step on single element
324 *
George Wort2d7e6832019-02-22 16:37:41 +0000325 * @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 Spyrou70d43a32020-06-22 17:05:43 +0100331 * @param[in] is_bounded_relu Specified if a fused bounded relu should be applied
George Wort2d7e6832019-02-22 16:37:41 +0000332 *
333 * @return Quantized value
334 */
George Wort2d7e6832019-02-22 16:37:41 +0000335inline 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 Spyrou70d43a32020-06-22 17:05:43 +0100337 uint8_t min_u8, uint8_t max_u8, bool is_bounded_relu)
George Wort2d7e6832019-02-22 16:37:41 +0000338{
339 int32x4_t in_s32 = vdupq_n_s32(in_value);
340
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000341 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 Wort2d7e6832019-02-22 16:37:41 +0000352
353 // Add the offset term
354 in_value += result_offset_after_shift_s32;
355
356 // Bound the result
Georgios Pinitas6fa26382019-03-18 10:05:34 +0000357 uint8_t out_u8 = static_cast<uint8_t>(std::max<int32_t>(0, std::min<int32_t>(255, in_value)));
George Wort2d7e6832019-02-22 16:37:41 +0000358 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 Pinitasdbdea0d2019-10-16 19:21:40 +0100366/** Performs final quantization step on single element
367 *
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100368 * @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 Spyrou70d43a32020-06-22 17:05:43 +0100374 * @param[in] is_bounded_relu Specified if a fused bounded relu should be applied
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100375 *
376 * @return Quantized value
377 */
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100378inline 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 Spyrou70d43a32020-06-22 17:05:43 +0100380 int8_t min_s8, int8_t max_s8, bool is_bounded_relu)
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100381{
382 int32x4_t in_s32 = vdupq_n_s32(in_value);
383
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000384 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 Pinitasdbdea0d2019-10-16 19:21:40 +0100392
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000393 // Shift value by result_shift_s32
394 in_value = rounding_divide_by_pow2(in_value, result_shift);
395 }
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100396
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 Pinitasd66094e2019-04-15 15:44:17 +0100410/** 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 Pinitas4c5469b2019-05-21 13:32:43 +0100417inline float32x4x2_t vdequantize(const uint8x8_t &qv, const UniformQuantizationInfo &qi)
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100418{
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 Spyrou8d4d1b82019-11-28 11:31:23 +0000433/** 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 */
440inline 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 Tello54e98d92019-02-05 16:16:19 +0000456/** Dequantize a neon vector holding 16 quantized values.
457 *
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100458 * @param[in] qv Input values to be dequantized.
459 * @param[in] qi Quantization information to be used in the computation.
Pablo Tello54e98d92019-02-05 16:16:19 +0000460 *
461 * @return Dequantized values in a neon vector
462 */
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100463inline float32x4x4_t vdequantize(const uint8x16_t &qv, const UniformQuantizationInfo &qi)
Pablo Tello54e98d92019-02-05 16:16:19 +0000464{
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 Spyrou8d4d1b82019-11-28 11:31:23 +0000481/** 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 */
488inline 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 Pinitas3d13af82019-06-04 13:04:16 +0100506/** 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 */
514inline 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 Parkd8176472019-12-04 09:46:28 +0000530/** 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 */
538inline 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 Pinitas8217c8e2019-11-11 18:24:22 +0000554/** Dequantize following symmetric quantization scheme a neon vector holding 16 quantized values.
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100555 *
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000556 * @param[in] qv Input values to be dequantized.
557 * @param[in] vscale Vector containing quantization scaling factors.
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100558 *
559 * @return Dequantized values in a neon vector
560 */
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000561inline float32x4x4_t vdequantize(const int8x16_t &qv, const float32x4x4_t vscale)
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100562{
563 const float32x4x4_t vdequantized_input =
564 {
565 {
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000566 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 Spyrou3f632f32019-08-22 16:52:00 +0100570 }
571 };
572 return vdequantized_input;
573}
574
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100575/** 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 */
582inline 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 Pinitasd66094e2019-04-15 15:44:17 +0100597/** 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 Pinitas4c5469b2019-05-21 13:32:43 +0100604inline uint8x8_t vquantize(const float32x4x2_t &qv, const UniformQuantizationInfo &qi)
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100605{
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 Spyrou8d4d1b82019-11-28 11:31:23 +0000625/** 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 */
632inline 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 Parkadd8e812020-11-25 11:46:03 +0000653inline 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 Tello54e98d92019-02-05 16:16:19 +0000676/** Quantize a neon vector holding 16 floating point values.
677 *
Georgios Pinitasd66094e2019-04-15 15:44:17 +0100678 * @param[in] qv Input values to be quantized.
679 * @param[in] qi Quantization information to be used in the computation.
Pablo Tello54e98d92019-02-05 16:16:19 +0000680 *
681 * @return A neon vector holding the quantized values
682 */
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100683inline uint8x16_t vquantize(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
Pablo Tello54e98d92019-02-05 16:16:19 +0000684{
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000685 auto rf = vquantize_internal(qv, qi.scale, qi.offset);
Pablo Tello54e98d92019-02-05 16:16:19 +0000686 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 Giorgiod64a46c2019-10-01 12:25:49 +0100690
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000691/** 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 Spyrou8d4d1b82019-11-28 11:31:23 +0000698inline int8x16_t vquantize_signed(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
699{
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000700 auto rf = vquantize_internal(qv, qi.scale, qi.offset);
Michalis Spyrou8d4d1b82019-11-28 11:31:23 +0000701 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 Giorgiod64a46c2019-10-01 12:25:49 +0100706/** 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 */
713inline uint16x8x2_t vquantize_qasymm16(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
714{
Sang-Hoon Parkadd8e812020-11-25 11:46:03 +0000715 auto rf = vquantize_internal(qv, qi.scale, qi.offset);
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100716 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 Tello20cfa452023-03-20 16:29:21 +0000720
Gian Marco58c57942017-11-28 09:10:03 +0000721} // namespace arm_compute
Georgios Pinitasddb93bb2020-10-02 16:38:59 +0100722#include "src/core/NEON/NEAsymm.inl"
Michalis Spyrouf4643372019-11-29 16:17:13 +0000723#endif // ARM_COMPUTE_NEASYMM_H