blob: 9e81c38ad854927bec6395524a79353117acb0ad [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Murray Kornelsen926f5022022-07-13 21:22:39 -04002 * Copyright (c) 2016-2022 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Manuel Bottini21079dd2019-10-29 17:20:09 +000024#ifndef ARM_COMPUTE_NEMATH_H
25#define ARM_COMPUTE_NEMATH_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include <arm_neon.h>
morgolock3155f772020-05-11 16:00:04 +010028#include <array>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029
30namespace arm_compute
31{
Georgios Pinitasd8e765b2017-08-02 13:44:33 +010032/** Calculate floor of a vector.
33 *
34 * @param[in] val Input vector value in F32 format.
35 *
36 * @return The calculated floor vector.
37 */
38float32x4_t vfloorq_f32(float32x4_t val);
39
Usama Arif0a5a57a2019-05-23 14:20:33 +010040/** Calculate round value of a vector to nearest with ties to even.
41 *
42 * @param[in] val Input vector value in F32 format.
43 *
44 * @return The calculated round vector.
45 */
46float32x4_t vroundq_rte_f32(float32x4_t val);
47
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048/** Calculate inverse square root.
49 *
50 * @param[in] x Input value.
51 *
52 * @return The calculated inverse square root.
53 */
Georgios Pinitascdf51452017-08-31 14:21:36 +010054float32x2_t vinvsqrt_f32(float32x2_t x);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055
Pablo Tello8fda1cb2017-07-05 15:20:38 +010056/** Calculate inverse square root.
57 *
58 * @param[in] x Input value.
59 *
60 * @return The calculated inverse square root.
61 */
Georgios Pinitascdf51452017-08-31 14:21:36 +010062float32x4_t vinvsqrtq_f32(float32x4_t x);
63
64/** Calculate reciprocal.
65 *
66 * @param[in] x Input value.
67 *
68 * @return The calculated reciprocal.
69 */
70float32x2_t vinv_f32(float32x2_t x);
Pablo Tello8fda1cb2017-07-05 15:20:38 +010071
Anthony Barbier6ff3b192017-09-04 18:44:23 +010072/** Calculate reciprocal.
73 *
74 * @param[in] x Input value.
75 *
76 * @return The calculated reciprocal.
77 */
78float32x4_t vinvq_f32(float32x4_t x);
79
80/** Perform a 7th degree polynomial approximation using Estrin's method.
81 *
82 * @param[in] x Input vector value in F32 format.
83 * @param[in] coeffs Polynomial coefficients table.
84 *
85 * @return The calculated approximation.
86 */
87float32x4_t vtaylor_polyq_f32(float32x4_t x, const std::array<float32x4_t, 8> &coeffs);
88
89/** Calculate exponential
90 *
91 * @param[in] x Input vector value in F32 format.
92 *
93 * @return The calculated exponent.
94 */
95float32x4_t vexpq_f32(float32x4_t x);
96
Murray Kornelsen926f5022022-07-13 21:22:39 -040097/** Calculate error function
98 *
99 * @param[in] x Input vector in F32 format.
100 *
101 * @return The calculated erf.
102 */
103float32x4_t verfq_f32(float32x4_t x);
104
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105/** Calculate logarithm
106 *
107 * @param[in] x Input vector value in F32 format.
108 *
109 * @return The calculated logarithm.
110 */
111float32x4_t vlogq_f32(float32x4_t x);
112
113/** Calculate hyperbolic tangent.
114 *
115 * tanh(x) = (e^2x - 1)/(e^2x + 1)
116 *
117 * @note We clamp x to [-5,5] to avoid overflowing issues.
118 *
119 * @param[in] val Input vector value in F32 format.
120 *
121 * @return The calculated Hyperbolic Tangent.
122 */
123float32x4_t vtanhq_f32(float32x4_t val);
124
125/** Calculate n power of a number.
126 *
127 * pow(x,n) = e^(n*log(x))
128 *
129 * @param[in] val Input vector value in F32 format.
130 * @param[in] n Powers to raise the input to.
131 *
132 * @return The calculated power.
133 */
134float32x4_t vpowq_f32(float32x4_t val, float32x4_t n);
Pablo Tellodf246182017-07-03 16:25:09 +0100135
Manuel Bottini7bb56c62019-06-26 15:17:09 +0100136/** Round to the nearest division by a power-of-two using exponent
137 *
138 * @note This function calculates the following expression: (x + 2^n -1 ) / 2^n where n = exponent
139 *
140 * @param[in] x Vector of 4 elements
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100141 * @param[in] exponent Vector of 4 elements with integer value used to round to nearest division by a power-of-two
142 *
143 * @return the nearest division by a power-of-two using exponent
144 */
145int32x4_t rounding_divide_by_pow2(int32x4_t x, int32x4_t exponent);
146
147/** Round to the nearest division by a power-of-two using exponent
148 *
149 * @note This function calculates the following expression: (x + 2^n -1 ) / 2^n where n = exponent
150 *
151 * @param[in] x Vector of 4 elements
Manuel Bottini7bb56c62019-06-26 15:17:09 +0100152 * @param[in] exponent Integer value used to round to nearest division by a power-of-two
153 *
154 * @return the nearest division by a power-of-two using exponent
155 */
156int32x4_t rounding_divide_by_pow2(int32x4_t x, int exponent);
157
158/** Round to the nearest division by a power-of-two using exponent
159 *
160 * @note This function calculates the following expression: (x + 2^n -1 ) / 2^n where n = exponent
161 *
162 * @param[in] x Element to divide.
163 * @param[in] exponent Integer value used to round to nearest division by a power-of-two
164 *
165 * @return the nearest division by a power-of-two using exponent
166 */
167int32_t rounding_divide_by_pow2(int32_t x, int exponent);
168
Manuel Bottini21079dd2019-10-29 17:20:09 +0000169/** Converts from uint8x16 to float32x4x4_t
170 *
171 * @param[in] in Vector of uint8 to be converted
172 *
173 * @return Converted vector of float
174 */
175float32x4x4_t convert_uint8x16_to_float32x4x4(const uint8x16_t &in);
176
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000177/** Converts from int8x16 to float32x4x4_t
178 *
179 * @param[in] in Vector of int8 to be converted
180 *
181 * @return Converted vector of float
182 */
183float32x4x4_t convert_int8x16_to_float32x4x4(const int8x16_t &in);
184
Manuel Bottini4370cff2020-02-07 16:31:59 +0000185/** Converts to float32x4x4_t from the specified templated 16 elements vectors
186 *
187 * @param[in] in Vector of float to be converted
188 *
189 * @return Converted vector of float
190 */
191template <typename T>
192float32x4x4_t convert_to_float32x4x4(const T &in);
193
Manuel Bottini21079dd2019-10-29 17:20:09 +0000194/** Converts from two float32x4x3_t to just one uint8x8x3_t
195 *
196 * @param[in] in1 First input vector of float to be converted
197 * @param[in] in2 Second input vector of float to be converted
198 * @param[out] out Converted output vector uint8 to store the result
199 */
200void convert_float32x4x3_to_uint8x8x3(const float32x4x3_t &in1, const float32x4x3_t &in2, uint8x8x3_t &out);
201
202/** Converts from two float32x4x4_t to just one uint8x16_t
203 *
204 * @param[in] in Vector of float to be converted
205 * @param[out] out Converted vector of uint8 to store the result
206 */
Sang-Hoon Parkc3a74202019-11-22 16:05:46 +0000207void convert_float32x4x4_to_uint8x16(const float32x4x4_t &in, uint8x16_t &out);
208
209/** Converts from float32x4x4_t to just one int8x16_t
210 *
211 * @param[in] in Vector of float to be converted
212 * @param[out] out Converted vector of uint8 to store the result
213 */
214void convert_float32x4x4_to_int8x16(const float32x4x4_t &in, int8x16_t &out);
Manuel Bottini21079dd2019-10-29 17:20:09 +0000215
Sang-Hoon Parkdcf3c7e2021-03-04 17:03:46 +0000216/** Converts from float vector to integer vector
217 *
218 * @param[in] in Float vector to converted
219 *
220 * @return The converted integer vector
221 */
222template <typename float_vec_type, typename int_vec_type>
223int_vec_type convert_float_to_int(const float_vec_type &in);
224
225/** Converts from integer vector to float vector
226 *
227 * @param[in] in Integer vector to converted
228 *
229 * @return The converted float vector
230 */
231template <typename float_vec_type, typename int_vec_type>
232float_vec_type convert_int_to_float(const int_vec_type &in);
233
Manuel Bottinied753262019-05-15 15:30:47 +0100234/** Calculate sine.
235 *
236 * @param[in] val Input vector value in radians, F32 format.
237 *
238 * @return The calculated sine.
239 */
240float32x4_t vsinq_f32(float32x4_t val);
241
242/** Calculate sine.
243 *
244 * @param[in] val Input vector value in radians, F32 format.
245 *
246 * @return The calculated sine.
247 */
248float32x2_t vsin_f32(float32x2_t val);
249
Sheri Zhang5dda2172021-10-15 19:54:17 +0100250/** Reduce a vector to be a scalar by accumulating all lanes in the vector
251 *
252 * @param[in] v Vector to be reduced.
253 *
254 * @return the wrapped-around number.
255 */
256float vreduce(const float32x4_t &v);
257
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000258#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tello91654c42017-07-05 11:32:17 +0100259/** Calculate hyperbolic tangent.
260 *
261 * tanh(x) = (e^2x - 1)/(e^2x + 1)
262 *
263 * @note We clamp x to [-5,5] to avoid overflowing issues.
264 *
Usama Arif0a5a57a2019-05-23 14:20:33 +0100265 * @param[in] val Input vector value in F16 format.
Pablo Tello91654c42017-07-05 11:32:17 +0100266 *
267 * @return The calculated Hyperbolic Tangent.
268 */
269float16x8_t vtanhq_f16(float16x8_t val);
Georgios Pinitascdf51452017-08-31 14:21:36 +0100270
Usama Arif0a5a57a2019-05-23 14:20:33 +0100271/** Calculate round value of a vector to nearest with ties to even.
272 *
273 * @param[in] val Input vector value in F16 format.
274 *
275 * @return The calculated round vector.
276 */
277float16x8_t vroundq_rte_f16(float16x8_t val);
278
Georgios Pinitascdf51452017-08-31 14:21:36 +0100279/** Calculate reciprocal.
280 *
281 * @param[in] x Input value.
282 *
283 * @return The calculated reciprocal.
284 */
285float16x4_t vinv_f16(float16x4_t x);
286
287/** Calculate reciprocal.
288 *
289 * @param[in] x Input value.
290 *
291 * @return The calculated reciprocal.
292 */
293float16x8_t vinvq_f16(float16x8_t x);
294
295/** Calculate inverse square root.
296 *
297 * @param[in] x Input value.
298 *
299 * @return The calculated inverse square root.
300 */
301float16x4_t vinvsqrt_f16(float16x4_t x);
302
Pablo Tello91654c42017-07-05 11:32:17 +0100303/** Calculate inverse square root.
304 *
305 * @param[in] x Input value.
306 *
307 * @return The calculated inverse square root.
308 */
309float16x8_t vinvsqrtq_f16(float16x8_t x);
Georgios Pinitascdf51452017-08-31 14:21:36 +0100310
Pablo Tellodf246182017-07-03 16:25:09 +0100311/** Calculate exponential
312 *
313 * @param[in] x Input vector value in F16 format.
314 *
315 * @return The calculated exponent.
316 */
317float16x8_t vexpq_f16(float16x8_t x);
Georgios Pinitascdf51452017-08-31 14:21:36 +0100318
Murray Kornelsen926f5022022-07-13 21:22:39 -0400319/** Calculate error function
320 *
321 * @param[in] x Input vector in F16 format.
322 *
323 * @return The calculated erf.
324 */
325float16x8_t verfq_f16(float16x8_t x);
326
Pablo Tellodf246182017-07-03 16:25:09 +0100327/** Calculate n power of a number.
328 *
329 * pow(x,n) = e^(n*log(x))
330 *
331 * @param[in] val Input vector value in F16 format.
332 * @param[in] n Powers to raise the input to.
333 *
334 * @return The calculated power.
335 */
336float16x8_t vpowq_f16(float16x8_t val, float16x8_t n);
Manuel Bottinied753262019-05-15 15:30:47 +0100337
338/** Calculate sine.
339 *
340 * @param[in] val Input vector value in radians, F16 format.
341 *
342 * @return The calculated sine.
343 */
344float16x8_t vsinq_f16(float16x8_t val);
345
Sheri Zhang5dda2172021-10-15 19:54:17 +0100346/** Reduce a vector to be a scalar by accumulating all lanes in the vector
347 *
348 * @param[in] v Vector to be reduced.
349 *
350 * @return the wrapped-around number.
351 */
352float16_t vreduce(const float16x8_t &v);
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000353#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100354} // namespace arm_compute
Georgios Pinitasddb93bb2020-10-02 16:38:59 +0100355#include "src/core/NEON/NEMath.inl"
Manuel Bottini21079dd2019-10-29 17:20:09 +0000356#endif /* ARM_COMPUTE_NEMATH_H */