blob: 0557d1d775eb5a1b2cee8306d3b46163ad3b95a6 [file] [log] [blame]
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +01001/*
SiCongLib99e54e2022-01-05 12:18:03 +00002 * Copyright (c) 2017-2022 Arm Limited.
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +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 */
Matthew Bentham758b5ba2020-03-05 23:37:48 +000024#ifndef ARM_COMPUTE_SUPPORT_TOOLCHAINSUPPORT
25#define ARM_COMPUTE_SUPPORT_TOOLCHAINSUPPORT
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010026
Pablo Tello65f99822018-05-24 11:40:15 +010027#include <cassert>
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010028#include <cmath>
29#include <cstddef>
30#include <limits>
31#include <memory>
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010032#include <sstream>
33#include <string>
34#include <type_traits>
35
Giorgio Arenad93e2632019-10-15 11:09:33 +010036#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
37#include <arm_neon.h>
38#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
39
Georgios Pinitase8291ac2020-02-26 09:58:13 +000040#include "support/Bfloat16.h"
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +010041#include "support/Half.h"
42
SiCongLi410e21e2020-12-11 15:07:53 +000043#ifndef M_PI
44#define M_PI (3.14159265358979323846)
45#endif // M_PI
46
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010047namespace arm_compute
48{
49namespace support
50{
51namespace cpp11
52{
Michalis Spyrou07781ac2017-08-31 15:11:41 +010053#if(__ANDROID__ || BARE_METAL)
Michalis Spyroue6bcb5b2019-06-07 11:47:16 +010054template <typename T>
55inline T nearbyint(T value)
56{
Michalis Spyrou748a7c82019-10-07 13:00:44 +010057 return static_cast<T>(::nearbyint(value));
Michalis Spyroue6bcb5b2019-06-07 11:47:16 +010058}
59
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010060/** Round floating-point value with half value rounding away from zero.
61 *
62 * @note This function implements the same behaviour as std::round except that it doesn't
63 * support Integral type. The latter is not in the namespace std in some Android toolchains.
64 *
65 * @param[in] value floating-point value to be rounded.
66 *
67 * @return Floating-point value of rounded @p value.
68 */
69template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
70inline T round(T value)
71{
72 return ::round(value);
73}
74
Giorgio Arena433ea492021-05-26 15:32:50 +010075/** Round floating-point value with half value rounding away from zero and cast to long
76 *
77 * @note This function implements the same behaviour as std::lround except that it doesn't
78 * support Integral type. The latter is not in the namespace std in some Android toolchains.
79 *
80 * @param[in] value floating-point value to be rounded.
81 *
82 * @return Floating-point value of rounded @p value casted to long
83 */
84template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
85inline long lround(T value)
86{
87 return ::lround(value);
88}
89
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010090/** Truncate floating-point value.
91 *
92 * @note This function implements the same behaviour as std::truncate except that it doesn't
93 * support Integral type. The latter is not in the namespace std in some Android toolchains.
94 *
95 * @param[in] value floating-point value to be truncated.
96 *
97 * @return Floating-point value of truncated @p value.
98 */
99template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
100inline T trunc(T value)
101{
102 return ::trunc(value);
103}
104
105/** Composes a floating point value with the magnitude of @p x and the sign of @p y.
106 *
107 * @note This function implements the same behaviour as std::copysign except that it doesn't
108 * support Integral type. The latter is not in the namespace std in some Android toolchains.
109 *
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100110 * @param[in] x value that contains the magnitude to be used in constructing the result.
111 * @param[in] y value that contains the sign to be used in construct in the result.
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100112 *
113 * @return Floating-point value with magnitude of @p x and sign of @p y.
114 */
115template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
116inline T copysign(T x, T y)
117{
118 return ::copysign(x, y);
119}
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100120
Georgios Pinitas1c29ffc2019-08-01 15:03:00 +0100121/** Computes (x*y) + z as if to infinite precision and rounded only once to fit the result type.
122 *
123 * @note This function implements the same behaviour as std::fma except that it doesn't
124 * support Integral type. The latter is not in the namespace std in some Android toolchains.
125 *
126 * @param[in] x floating-point value
127 * @param[in] y floating-point value
128 * @param[in] z floating-point value
129 *
130 * @return Result floating point value equal to (x*y) + z.c
131 */
Giorgio Arenad93e2632019-10-15 11:09:33 +0100132template < typename T, typename = typename std::enable_if < std::is_floating_point<T>::value
133#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
134 || std::is_same<T, float16_t>::value
135#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
136 >::type >
Georgios Pinitas1c29ffc2019-08-01 15:03:00 +0100137inline T fma(T x, T y, T z)
138{
139 return ::fma(x, y, z);
140}
141
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100142/** Loads the data from the given location, converts them to character string equivalents
143 * and writes the result to a character string buffer.
144 *
145 * @param[in] s Pointer to a character string to write to
ramelg01b2eba7f2021-12-23 08:32:08 +0000146 * @param[in] n Up to buf_size - 1 characters may be written, plus the null ending character
147 * @param[in] fmt Pointer to a null-ended multibyte string specifying how to interpret the data.
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100148 * @param[in] args Arguments forwarded to snprintf.
149 *
150 * @return Number of characters that would have been written for a sufficiently large buffer
ramelg01b2eba7f2021-12-23 08:32:08 +0000151 * if successful (not including the ending null character), or a negative value if an error occurred.
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100152 */
153template <typename... Ts>
154inline int snprintf(char *s, size_t n, const char *fmt, Ts &&... args)
155{
156 return ::snprintf(s, n, fmt, std::forward<Ts>(args)...);
157}
Giorgio Arenad93e2632019-10-15 11:09:33 +0100158#else /* (__ANDROID__ || BARE_METAL) */
Michalis Spyroue6bcb5b2019-06-07 11:47:16 +0100159/** Rounds the floating-point argument arg to an integer value in floating-point format, using the current rounding mode.
160 *
161 * @note This function acts as a convenience wrapper around std::nearbyint. The
162 * latter is missing in some Android toolchains.
163 *
164 * @param[in] value Value to be rounded.
165 *
166 * @return The rounded value.
167 */
168template <typename T>
169inline T nearbyint(T value)
170{
Michalis Spyrou748a7c82019-10-07 13:00:44 +0100171 return static_cast<T>(std::nearbyint(value));
Michalis Spyroue6bcb5b2019-06-07 11:47:16 +0100172}
173
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100174/** Round floating-point value with half value rounding away from zero.
175 *
176 * @note This function implements the same behaviour as std::round except that it doesn't
177 * support Integral type. The latter is not in the namespace std in some Android toolchains.
178 *
179 * @param[in] value floating-point value to be rounded.
180 *
181 * @return Floating-point value of rounded @p value.
182 */
183template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
184inline T round(T value)
185{
Pablo Tello826c7692018-01-16 11:41:12 +0000186 //Workaround Valgrind's mismatches: when running from Valgrind the call to std::round(-4.500000) == -4.000000 instead of 5.00000
187 return (value < 0.f) ? static_cast<int>(value - 0.5f) : static_cast<int>(value + 0.5f);
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100188}
189
Giorgio Arena433ea492021-05-26 15:32:50 +0100190/** Round floating-point value with half value rounding away from zero and cast to long
191 *
192 * @note This function implements the same behaviour as std::lround except that it doesn't
193 * support Integral type. The latter is not in the namespace std in some Android toolchains.
194 *
195 * @param[in] value floating-point value to be rounded.
196 *
197 * @return Floating-point value of rounded @p value casted to long
198 */
199template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
200inline long lround(T value)
201{
202 return std::lround(value);
203}
204
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100205/** Truncate floating-point value.
206 *
207 * @note This function implements the same behaviour as std::truncate except that it doesn't
208 * support Integral type. The latter is not in the namespace std in some Android toolchains.
209 *
210 * @param[in] value floating-point value to be truncated.
211 *
212 * @return Floating-point value of truncated @p value.
213 */
214template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
215inline T trunc(T value)
216{
217 return std::trunc(value);
218}
219
220/** Composes a floating point value with the magnitude of @p x and the sign of @p y.
221 *
222 * @note This function implements the same behaviour as std::copysign except that it doesn't
223 * support Integral type. The latter is not in the namespace std in some Android toolchains.
224 *
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100225 * @param[in] x value that contains the magnitude to be used in constructing the result.
226 * @param[in] y value that contains the sign to be used in construct in the result.
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100227 *
228 * @return Floating-point value with magnitude of @p x and sign of @p y.
229 */
230template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
231inline T copysign(T x, T y)
232{
233 return std::copysign(x, y);
234}
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100235
Georgios Pinitas1c29ffc2019-08-01 15:03:00 +0100236/** Computes (x*y) + z as if to infinite precision and rounded only once to fit the result type.
237 *
238 * @note This function implements the same behaviour as std::fma except that it doesn't
239 * support Integral type. The latter is not in the namespace std in some Android toolchains.
240 *
241 * @param[in] x floating-point value
242 * @param[in] y floating-point value
243 * @param[in] z floating-point value
244 *
245 * @return Result floating point value equal to (x*y) + z.
246 */
Giorgio Arenad93e2632019-10-15 11:09:33 +0100247template < typename T, typename = typename std::enable_if < std::is_floating_point<T>::value
248#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
249 || std::is_same<T, float16_t>::value
250#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
251 >::type >
Georgios Pinitas1c29ffc2019-08-01 15:03:00 +0100252inline T fma(T x, T y, T z)
253{
254 return std::fma(x, y, z);
255}
256
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100257/** Loads the data from the given location, converts them to character string equivalents
258 * and writes the result to a character string buffer.
259 *
260 * @param[in] s Pointer to a character string to write to
ramelg01b2eba7f2021-12-23 08:32:08 +0000261 * @param[in] n Up to buf_size - 1 characters may be written, plus the null ending character
262 * @param[in] fmt Pointer to a null-ended multibyte string specifying how to interpret the data.
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100263 * @param[in] args Arguments forwarded to std::snprintf.
264 *
265 * @return Number of characters that would have been written for a sufficiently large buffer
ramelg01b2eba7f2021-12-23 08:32:08 +0000266 * if successful (not including the ending null character), or a negative value if an error occurred.
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100267 */
268template <typename... Ts>
269inline int snprintf(char *s, std::size_t n, const char *fmt, Ts &&... args)
270{
271 return std::snprintf(s, n, fmt, std::forward<Ts>(args)...);
272}
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100273#endif /* (__ANDROID__ || BARE_METAL) */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100274
Anthony Barbier3a6163e2018-08-10 17:36:36 +0100275// std::numeric_limits<T>::lowest
276template <typename T>
277inline T lowest()
278{
279 return std::numeric_limits<T>::lowest();
280}
281
282#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
283template <>
284inline __fp16 lowest<__fp16>()
285{
286 return std::numeric_limits<half_float::half>::lowest();
287}
288#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100289
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000290template <>
291inline bfloat16 lowest<bfloat16>()
292{
293 return bfloat16::lowest();
294}
295
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100296// std::isfinite
297template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
298inline bool isfinite(T value)
299{
Pablo Tello29cab362022-03-10 17:05:34 +0000300 return std::isfinite(static_cast<double>(value));
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100301}
302
303inline bool isfinite(half_float::half value)
304{
305 return half_float::isfinite(value);
306}
Georgios Pinitase8291ac2020-02-26 09:58:13 +0000307
308inline bool isfinite(bfloat16 value)
309{
310 return std::isfinite(float(value));
311}
SiCongLib99e54e2022-01-05 12:18:03 +0000312
313// std::signbit
314template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
315inline bool signbit(T value)
316{
Pablo Tello29cab362022-03-10 17:05:34 +0000317 return std::signbit(static_cast<double>(value));
SiCongLib99e54e2022-01-05 12:18:03 +0000318}
319
320inline bool signbit(half_float::half value)
321{
322 return half_float::signbit(value);
323}
324
325inline bool signbit(bfloat16 value)
326{
327 return std::signbit(float(value));
328}
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100329} // namespace cpp11
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100330} // namespace support
331} // namespace arm_compute
Matthew Bentham758b5ba2020-03-05 23:37:48 +0000332#endif /* ARM_COMPUTE_SUPPORT_TOOLCHAINSUPPORT */