blob: a448cde4754190bbc2a82497c1e38b3ac0849b19 [file] [log] [blame]
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +00001/*
Michalis Spyrou168d6a82022-05-03 17:15:42 +01002 * Copyright (c) 2020-2022 Arm Limited.
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +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 */
24#ifndef ARM_COMPUTE_SVEASYMM_H
25#define ARM_COMPUTE_SVEASYMM_H
26
Michalis Spyrou20fca522021-06-07 14:23:57 +010027#if defined(ARM_COMPUTE_ENABLE_SVE2)
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +000028#include "src/core/NEON/SVEMath.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010029
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +000030#include <arm_sve.h>
31
32namespace arm_compute
33{
34/** Perform a multiply-accumulate on all components of a QASYMM8 vector
35 *
36 * vd*vs + vo
37 *
38 * @param[in] pg Predicate value.
39 * @param[in] vd Input vector value in QASYMM8 format
40 * @param[in] vs Vector multiplier in F32 format. The multiplier value must be duplicated across all four lanes.
41 * @param[in] vo Vector addend in F32 format. The addend value must be duplicated across all four lanes.
42 *
43 * @return A vector in QASYMM8 format, saturated to fit
44 */
45svuint8_t svmla_qasymm8_z(svbool_t pg, svuint8_t vd, svfloat32_t vs, svfloat32_t vo);
46
47/** Perform a multiply-accumulate on all components of a QASYMM8_SIGNED vector
48 *
49 * vd*vs + vo
50 *
51 * @param[in] pg Predicate value.
52 * @param[in] vd Input vector value in QASYMM8_SIGNED format
53 * @param[in] vs Vector multiplier in F32 format. The multiplier value must be duplicated across all four lanes.
54 * @param[in] vo Vector addend in F32 format. The addend value must be duplicated across all four lanes.
55 *
56 * @return A vector in QASYMM8_SIGNED format, saturated to fit
57 */
58svint8_t svmla_qasymm8_signed_z(svbool_t pg, svint8_t vd, svfloat32_t vs, svfloat32_t vo);
59
60/** Dequantize following an asymmetric quantization scheme a sve vector.
61 *
62 * @param[in] pg Predicate value.
63 * @param[in] qv Input values to be dequantized.
64 * @param[in] scale Quantization scaling factor.
65 * @param[in] offset Zero quantization offset.
66 *
67 * @return Dequantized values in an sve vector
68 */
69inline svfloat32x4_t svdequantize_z(svbool_t pg, const svuint8_t &qv, float scale, int32_t offset)
70{
Michalis Spyrou168d6a82022-05-03 17:15:42 +010071 const auto voffset = svdup_n_s32(offset);
72 const auto vscale = svdup_n_f32(scale);
73 const svfloat32x4_t vdequantized_input = svcreate4_f32(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010074 svmul_f32_z(pg,
75 svcvt_f32_s32_z(pg, svsub_s32_z(pg, svreinterpret_s32_u32(svmovlb_u32(svmovlb_u16(qv))), voffset)),
76 vscale),
77 svmul_f32_z(pg,
78 svcvt_f32_s32_z(pg, svsub_s32_z(pg, svreinterpret_s32_u32(svmovlt_u32(svmovlb_u16(qv))), voffset)),
79 vscale),
80 svmul_f32_z(pg,
81 svcvt_f32_s32_z(pg, svsub_s32_z(pg, svreinterpret_s32_u32(svmovlb_u32(svmovlt_u16(qv))), voffset)),
82 vscale),
83 svmul_f32_z(pg,
84 svcvt_f32_s32_z(pg, svsub_s32_z(pg, svreinterpret_s32_u32(svmovlt_u32(svmovlt_u16(qv))), voffset)),
85 vscale));
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +000086 return vdequantized_input;
87}
88
89/** Dequantize an sve vector
90 *
91 * @param[in] pg Predicate value.
92 * @param[in] qv Input values to be dequantized.
93 * @param[in] qi Quantization information to be used in the computation.
94 *
95 * @return Dequantized values in an sve vector
96 */
97inline svfloat32x4_t svdequantize_z(svbool_t pg, const svuint8_t &qv, const UniformQuantizationInfo &qi)
98{
99 return svdequantize_z(pg, qv, qi.scale, qi.offset);
100}
101
102/** Dequantize an sve vector stored as signed asymmetric.
103 *
104 * @param[in] pg Predicate value.
105 * @param[in] qv Input values to be dequantized.
106 * @param[in] scale Quantization scaling factor.
107 * @param[in] offset Zero quantization offset.
108 *
109 * @return Dequantized values in a sve vector
110 */
111inline svfloat32x4_t svdequantize_z(svbool_t pg, const svint8_t &qv, float scale, int32_t offset)
112{
Michalis Spyrou168d6a82022-05-03 17:15:42 +0100113 const auto voffset = svdup_n_s32(offset);
114 const auto vscale = svdup_n_f32(scale);
115 const svfloat32x4_t vdequantized_input = svcreate4_f32(
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100116 svmul_f32_z(pg, svcvt_f32_s32_z(pg, svsub_s32_z(pg, svmovlb_s32(svmovlb_s16(qv)), voffset)), vscale),
117 svmul_f32_z(pg, svcvt_f32_s32_z(pg, svsub_s32_z(pg, svmovlt_s32(svmovlb_s16(qv)), voffset)), vscale),
118 svmul_f32_z(pg, svcvt_f32_s32_z(pg, svsub_s32_z(pg, svmovlb_s32(svmovlt_s16(qv)), voffset)), vscale),
119 svmul_f32_z(pg, svcvt_f32_s32_z(pg, svsub_s32_z(pg, svmovlt_s32(svmovlt_s16(qv)), voffset)), vscale));
Michalis Spyrou168d6a82022-05-03 17:15:42 +0100120
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +0000121 return vdequantized_input;
122}
123
124/** Dequantize an sve vector.
125 *
126 * @param[in] pg Predicate value.
127 * @param[in] qv Input values to be dequantized.
128 * @param[in] qi Quantization information to be used in the computation.
129 *
130 * @return Dequantized values in an sve vector
131 */
132inline svfloat32x4_t svdequantize_z(svbool_t pg, const svint8_t &qv, const UniformQuantizationInfo &qi)
133{
134 return svdequantize_z(pg, qv, qi.scale, qi.offset);
135}
136
137/** Dequantize following symmetric quantization scheme on an sve vector.
138 *
139 * @param[in] pg Predicate value.
140 * @param[in] qv Input values to be dequantized.
141 * @param[in] vscale Vector containing quantization scaling factors.
142 *
143 * @return Dequantized values in a sve vector
144 */
145inline svfloat32x4_t svdequantize_z(svbool_t pg, const svint8_t &qv, const svfloat32x4_t vscale)
146{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100147 const svfloat32x4_t vdequantized_input =
148 svcreate4_f32(svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlb_s32(svmovlb_s16(qv))), svget4_f32(vscale, 0)),
149 svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlt_s32(svmovlb_s16(qv))), svget4_f32(vscale, 1)),
150 svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlb_s32(svmovlt_s16(qv))), svget4_f32(vscale, 2)),
151 svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlt_s32(svmovlt_s16(qv))), svget4_f32(vscale, 3)));
Michalis Spyrou168d6a82022-05-03 17:15:42 +0100152
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +0000153 return vdequantized_input;
154}
155
156/** Dequantize following a symmetric quantization scheme an sve vector.
157 *
158 * @param[in] qv Input values to be dequantized.
159 * @param[in] scale Quantization scaling factor.
160 *
161 * @return Dequantized values in a sve vector
162 */
163inline svfloat32x4_t svdequantize_z(svbool_t pg, const svint8_t &qv, float scale)
164{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100165 const auto vscale = svdup_n_f32(scale);
166 const svfloat32x4_t vdequantized_input =
167 svcreate4_f32(svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlb_s32(svmovlb_s16(qv))), vscale),
168 svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlt_s32(svmovlb_s16(qv))), vscale),
169 svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlb_s32(svmovlt_s16(qv))), vscale),
170 svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlt_s32(svmovlt_s16(qv))), vscale));
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +0000171 return vdequantized_input;
172}
173
174/** Quantize an sve vector holding floating point values.
175 *
176 * @param[in] pg Predicate value.
177 * @param[in] qv Input values to be quantized.
178 * @param[in] qi Quantization information to be used in the computation.
179 *
180 * @return An sve vector holding the quantized values
181 */
182inline svuint8_t svquantize_z(svbool_t pg, const svfloat32x4_t qv, const UniformQuantizationInfo &qi)
183{
184 const float scale = qi.scale;
185 const int offset = qi.offset;
186 const auto voffset = svdup_n_f32(offset);
187 const auto vinvscale = svdup_n_f32(1.f / scale);
188
189 const auto rf_0 = svcvt_u32_f32_z(pg, svmla_f32_z(pg, voffset, svget4_f32(qv, 0), vinvscale));
190 const auto rf_1 = svcvt_u32_f32_z(pg, svmla_f32_z(pg, voffset, svget4_f32(qv, 1), vinvscale));
191 const auto rf_2 = svcvt_u32_f32_z(pg, svmla_f32_z(pg, voffset, svget4_f32(qv, 2), vinvscale));
192 const auto rf_3 = svcvt_u32_f32_z(pg, svmla_f32_z(pg, voffset, svget4_f32(qv, 3), vinvscale));
193
194 const auto pa = svqxtnt_u32(svqxtnb_u32(rf_0), rf_1);
195 const auto pb = svqxtnt_u32(svqxtnb_u32(rf_2), rf_3);
196
197 return svqxtnt_u16(svqxtnb_u16(pa), pb);
198}
199
200/** Signed quantize an sve vector holding floating point values.
201 *
202 * @param[in] pg Predicate value.
203 * @param[in] qv Input values to be quantized.
204 * @param[in] qi Quantization information to be used in the computation.
205 *
206 * @return An sve vector holding the quantized values
207 */
208inline svint8_t svquantize_signed_z(svbool_t pg, const svfloat32x4_t qv, const UniformQuantizationInfo &qi)
209{
210 const float scale = qi.scale;
211 const int offset = qi.offset;
212 const auto voffset = svdup_n_f32(offset);
213 const auto vinvscale = svdup_n_f32(1.f / scale);
214 const auto rf_0 = svcvt_s32_f32_z(pg, svmla_f32_z(pg, voffset, svget4_f32(qv, 0), vinvscale));
215 const auto rf_1 = svcvt_s32_f32_z(pg, svmla_f32_z(pg, voffset, svget4_f32(qv, 1), vinvscale));
216 const auto rf_2 = svcvt_s32_f32_z(pg, svmla_f32_z(pg, voffset, svget4_f32(qv, 2), vinvscale));
217 const auto rf_3 = svcvt_s32_f32_z(pg, svmla_f32_z(pg, voffset, svget4_f32(qv, 3), vinvscale));
218
219 const auto pa = svqxtnt_s32(svqxtnb_s32(rf_0), rf_1);
220 const auto pb = svqxtnt_s32(svqxtnb_s32(rf_2), rf_3);
221
222 return svqxtnt_s16(svqxtnb_s16(pa), pb);
223}
224
225/** Quantize to QASYMM16 an sve vector holding 16 floating point values.
226 *
227 * @param[in] pg Predicate value.
228 * @param[in] qv Input values to be quantized.
229 * @param[in] qi Quantization information to be used in the computation.
230 *
231 * @return An sve vector holding the quantized values
232 */
233inline svuint16x2_t svquantize_qasymm16_z(svbool_t pg, const svfloat32x4_t qv, const UniformQuantizationInfo &qi)
234{
235 const float scale = qi.scale;
236 const int offset = qi.offset;
237 const auto voffset = svdup_n_f32(offset);
238 const auto vinvscale = svdup_n_f32(1.f / scale);
239
240 const auto rf_0 = svcvt_u32_f32_z(pg, svmla_f32_z(pg, voffset, svget4_f32(qv, 0), vinvscale));
241 const auto rf_1 = svcvt_u32_f32_z(pg, svmla_f32_z(pg, voffset, svget4_f32(qv, 1), vinvscale));
242 const auto rf_2 = svcvt_u32_f32_z(pg, svmla_f32_z(pg, voffset, svget4_f32(qv, 2), vinvscale));
243 const auto rf_3 = svcvt_u32_f32_z(pg, svmla_f32_z(pg, voffset, svget4_f32(qv, 3), vinvscale));
244
245 const auto pa = svqxtnt_u32(svqxtnb_u32(rf_0), rf_1);
246 const auto pb = svqxtnt_u32(svqxtnb_u32(rf_2), rf_3);
247
248 return svcreate2_u16(pa, pb);
249}
250} // namespace arm_compute
251#include "src/core/NEON/SVEAsymm.inl"
Michalis Spyrou20fca522021-06-07 14:23:57 +0100252#endif /* defined(ARM_COMPUTE_ENABLE_SVE2) */
Michalis Spyrouaa51a5b2020-11-22 00:49:42 +0000253#endif // ARM_COMPUTE_NEASYMM_H