xref: /aosp_15_r20/external/ComputeLibrary/src/core/NEON/SVESymm.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2020-2022 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_SVESYMM_H
25 #define ARM_COMPUTE_SVESYMM_H
26 
27 #include "arm_compute/core/utils/quantization/AsymmHelpers.h"
28 
29 #if defined(ARM_COMPUTE_ENABLE_SVE2)
30 #include "src/core/NEON/SVEMath.h"
31 #include <arm_sve.h>
32 
33 namespace arm_compute
34 {
35 /** Dequantize an sve vector holding 16-bit quantized values.
36  *
37  * @param[in] pg    Predicate value.
38  * @param[in] qv    Input values to be dequantized.
39  * @param[in] scale Quantization scale
40  *
41  * @return Dequantized values in an sve vector
42  */
svdequantize_qsymm16_z(svbool_t pg,const svint16_t & qv,float scale)43 inline svfloat32x2_t svdequantize_qsymm16_z(svbool_t pg, const svint16_t &qv, float scale)
44 {
45     const auto          vscale             = svdup_n_f32(scale);
46     const svfloat32x2_t vdequantized_input = svcreate2_f32(svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlb_s32(qv)), vscale), svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlt_s32(qv)), vscale));
47     return vdequantized_input;
48 }
49 
50 /** Quantize an sve vector holding 8 floating point values.
51  *
52  * @param[in] pg    Predicate value.
53  * @param[in] qv    Input values to be quantized.
54  * @param[in] scale Quantization scale
55  *
56  * @return An sve vector holding the quantized values
57  */
svquantize_qsymm16_z(svbool_t pg,const svfloat32x2_t qv,float scale)58 inline svint16_t svquantize_qsymm16_z(svbool_t pg, const svfloat32x2_t qv, float scale)
59 {
60     const svfloat32_t vinvscale = svdup_n_f32(1.f / scale);
61 
62     const auto rf_0 = svcvt_s32_f32_z(pg, svmul_f32_z(pg, svget2_f32(qv, 0), vinvscale));
63     const auto rf_1 = svcvt_s32_f32_z(pg, svmul_f32_z(pg, svget2_f32(qv, 1), vinvscale));
64     const auto pa   = svqxtnt_s32(svqxtnb_s32(rf_0), rf_1);
65 
66     return pa;
67 }
68 
69 /** Dequantize an sve vector holding 16 16-bit quantized values.
70  *
71  * @param[in] pg Predicate value.
72  * @param[in] qv Input values to be dequantized.
73  * @param[in] qi Quantization information to be used in the computation.
74  *
75  * @return Dequantized values in an sve vector
76  */
svdequantize_z(svbool_t pg,const svint16x2_t qv,const UniformQuantizationInfo & qi)77 inline svfloat32x4_t svdequantize_z(svbool_t pg, const svint16x2_t qv, const UniformQuantizationInfo &qi)
78 {
79     const float         scale              = qi.scale;
80     const auto          vscale             = svdup_n_f32(scale);
81     const svfloat32x4_t vdequantized_input = svcreate4_f32(
82                                                  svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlb_s32(svget2_s16(qv, 0))), vscale),
83                                                  svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlt_s32(svget2_s16(qv, 0))), vscale),
84                                                  svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlb_s32(svget2_s16(qv, 1))), vscale),
85                                                  svmul_f32_z(pg, svcvt_f32_s32_z(pg, svmovlt_s32(svget2_s16(qv, 1))), vscale));
86     return vdequantized_input;
87 }
88 
89 /** Quantize an sve vector holding 16 floating point values.
90  *
91  * @param[in] pg Predicate value.
92  * @param[in] qv Input values to be quantized.
93  * @param[in] qi Quantization information to be used in the computation.
94  *
95  * @return An sve vector holding the quantized values
96  */
svquantize_qsymm16_z(svbool_t pg,const svfloat32x4_t qv,const UniformQuantizationInfo & qi)97 inline svint16x2_t svquantize_qsymm16_z(svbool_t pg, const svfloat32x4_t qv, const UniformQuantizationInfo &qi)
98 {
99     const float scale = qi.scale;
100     ARM_COMPUTE_ERROR_ON(scale == 0.f);
101     const auto vinvscale = svdup_n_f32(1.f / scale);
102     const auto rf_0      = svcvt_s32_f32_z(pg, svmul_f32_z(pg, svget4_f32(qv, 0), vinvscale));
103     const auto rf_1      = svcvt_s32_f32_z(pg, svmul_f32_z(pg, svget4_f32(qv, 1), vinvscale));
104     const auto rf_2      = svcvt_s32_f32_z(pg, svmul_f32_z(pg, svget4_f32(qv, 2), vinvscale));
105     const auto rf_3      = svcvt_s32_f32_z(pg, svmul_f32_z(pg, svget4_f32(qv, 3), vinvscale));
106 
107     const auto pa = svqxtnt_s32(svqxtnb_s32(rf_0), rf_1);
108     const auto pb = svqxtnt_s32(svqxtnb_s32(rf_2), rf_3);
109 
110     return svcreate2_s16(pa, pb);
111 }
112 
113 } // namespace arm_compute
114 #endif /* defined(ARM_COMPUTE_ENABLE_SVE2) */
115 #endif // ARM_COMPUTE_NESYMM_H