xref: /aosp_15_r20/external/ComputeLibrary/src/core/NEON/SVEMath.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2020-2021 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_SVEMATH_H
25 #define ARM_COMPUTE_SVEMATH_H
26 
27 #if defined(ARM_COMPUTE_ENABLE_SVE)
28 #include "src/core/NEON/wrapper/intrinsics/svcvt.h"
29 #include "src/core/NEON/wrapper/intrinsics/svdup_n.h"
30 #include "src/core/NEON/wrapper/intrinsics/svreinterpret.h"
31 #include <arm_sve.h>
32 #include <array>
33 
34 namespace arm_compute
35 {
36 /** Calculate exponent.
37  *
38  * @param[in] pg  Input predicate.
39  * @param[in] val Input vector value in F32 format.
40  *
41  * @return The calculated exponent.
42  */
43 svfloat32_t svexp_f32_z(svbool_t pg, svfloat32_t val);
44 
45 /** Calculate reciprocal.
46  *
47  * @param[in] pg Input predicate.
48  * @param[in] x  Input value.
49  *
50  * @return The calculated reciprocal.
51  */
52 svfloat32_t svinv_f32_z(svbool_t pg, svfloat32_t x);
53 
54 /** Calculate logarithm
55  *
56  * @param[in] pg Input predicate.
57  * @param[in] x  Input vector value in F32 format.
58  *
59  * @return The calculated logarithm.
60  */
61 svfloat32_t svlog_f32_z(svbool_t pg, svfloat32_t x);
62 
63 /** Calculate hyperbolic tangent.
64  *
65  * tanh(x) = (e^2x - 1)/(e^2x + 1)
66  *
67  * @note We clamp x to [-5,5] to avoid overflowing issues.
68  *
69  * @param[in] pg  Input predicate.
70  * @param[in] val Input vector value in F32 format.
71  *
72  * @return The calculated Hyperbolic Tangent.
73  */
74 svfloat32_t svtanh_f32_z(svbool_t pg, svfloat32_t val);
75 
76 /** Calculate hyperbolic tangent.
77  *
78  * tanh(x) = (e^2x - 1)/(e^2x + 1)
79  *
80  * @note We clamp x to [-5,5] to avoid overflowing issues.
81  *
82  * @param[in] pg  Input predicate.
83  * @param[in] val Input vector value in F16 format.
84  *
85  * @return The calculated Hyperbolic Tangent.
86  */
87 svfloat16_t svtanh_f16_z(svbool_t pg, svfloat16_t val);
88 
89 /** Calculate exponential
90  *
91  * @param[in] pg Input predicate.
92  * @param[in] x  Input vector value in F16 format.
93  *
94  * @return The calculated exponent.
95  */
96 svfloat16_t svexp_f16_z(svbool_t pg, svfloat16_t x);
97 
98 /** Calculate reciprocal.
99  *
100  * @param[in] pg Input predicate.
101  * @param[in] x  Input value.
102  *
103  * @return The calculated reciprocal.
104  */
105 svfloat16_t svinv_f16_z(svbool_t pg, svfloat16_t x);
106 
107 /** Calculate logarithm
108  *
109  * @param[in] pg Input predicate.
110  * @param[in] x  Input vector value in F32 format.
111  *
112  * @return The calculated logarithm.
113  */
114 svfloat16_t svlog_f16_z(svbool_t pg, svfloat16_t x);
115 
116 /** Calculate inverse square root.
117  *
118  * @param[in] pg  Input predicate.
119  * @param[in] val Input value.
120  *
121  * @return The calculated inverse square root.
122  */
123 template <typename VectorType>
svinvsqrt(svbool_t pg,VectorType val)124 inline VectorType svinvsqrt(svbool_t pg, VectorType val)
125 {
126     auto sqrt_reciprocal = svrsqrte(val);
127     sqrt_reciprocal      = svmul_z(pg, svrsqrts(svmul_z(pg, val, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
128     sqrt_reciprocal      = svmul_z(pg, svrsqrts(svmul_z(pg, val, sqrt_reciprocal), sqrt_reciprocal), sqrt_reciprocal);
129     return sqrt_reciprocal;
130 }
131 
132 /** Calculate sine.
133  *
134  * @param[in] pg  Input predicate.
135  * @param[in] val Input vector value in radians, F32 format.
136  *
137  * @return The calculated sine.
138  */
139 svfloat32_t svsin_f32_z(svbool_t pg, svfloat32_t val);
140 
141 /** Calculate sine.
142  *
143  * @param[in] pg  Input predicate.
144  * @param[in] val Input vector value in radians, F16 format.
145  *
146  * @return The calculated sine.
147  */
148 svfloat16_t svsin_f16_z(svbool_t pg, svfloat16_t val);
149 
150 /** Calculate n power of a number.
151  *
152  * pow(x,n) = e^(n*log(x))
153  *
154  * @param[in] pg Input predicate.
155  * @param[in] a  Input vector value in F32 format.
156  * @param[in] b  Powers to raise the input to.
157  *
158  * @return The calculated power.
159  */
160 svfloat32_t svpow_f32_z(svbool_t pg, svfloat32_t a, svfloat32_t b);
161 
162 /** Calculate n power of a number.
163  *
164  * pow(x,n) = e^(n*log(x))
165  *
166  * @param[in] pg Input predicate.
167  * @param[in] a  Input vector value in F16 format.
168  * @param[in] b  Powers to raise the input to.
169  *
170  * @return The calculated power.
171  */
172 svfloat16_t svpow_f16_z(svbool_t pg, svfloat16_t a, svfloat16_t b);
173 
174 /** Convert and pack four 32-bit float vectors into an 8-bit integer vector
175  *
176  * @param[in] in_0 The first float vector
177  * @param[in] in_1 The second float vector
178  * @param[in] in_2 The third float vector
179  * @param[in] in_3 The fourth float vector
180  *
181  * @return The converted integer vector
182  */
183 template <typename int_vec_type>
184 int_vec_type convert_float_to_int(const svfloat32_t &in_0, const svfloat32_t &in_1, const svfloat32_t &in_2, const svfloat32_t &in_3);
185 
186 } // namespace arm_compute
187 #include "src/core/NEON/SVEMath.inl"
188 #endif /* defined(ARM_COMPUTE_ENABLE_SVE) */
189 #endif /* ARM_COMPUTE_SVEMATH_H */