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