• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017-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_SUPPORT_TOOLCHAINSUPPORT
25 #define ARM_COMPUTE_SUPPORT_TOOLCHAINSUPPORT
26 
27 #include <cassert>
28 #include <cmath>
29 #include <cstddef>
30 #include <limits>
31 #include <memory>
32 #include <sstream>
33 #include <string>
34 #include <type_traits>
35 
36 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
37 #include <arm_neon.h>
38 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
39 
40 #include "support/Bfloat16.h"
41 #include "support/Half.h"
42 
43 #ifndef M_PI
44 #define M_PI (3.14159265358979323846)
45 #endif // M_PI
46 
47 namespace arm_compute
48 {
49 namespace support
50 {
51 namespace cpp11
52 {
53 #if(__ANDROID__ || BARE_METAL)
54 template <typename T>
nearbyint(T value)55 inline T nearbyint(T value)
56 {
57     return static_cast<T>(::nearbyint(value));
58 }
59 
60 /** 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  */
69 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
round(T value)70 inline T round(T value)
71 {
72     return ::round(value);
73 }
74 
75 /** 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  */
84 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
lround(T value)85 inline long lround(T value)
86 {
87     return ::lround(value);
88 }
89 
90 /** 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  */
99 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
trunc(T value)100 inline 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  *
110  * @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.
112  *
113  * @return Floating-point value with magnitude of @p x and sign of @p y.
114  */
115 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
copysign(T x,T y)116 inline T copysign(T x, T y)
117 {
118     return ::copysign(x, y);
119 }
120 
121 /** 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  */
132 template < 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 >
fma(T x,T y,T z)137 inline T fma(T x, T y, T z)
138 {
139     return ::fma(x, y, z);
140 }
141 
142 /** 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
146  * @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.
148  * @param[in] args Arguments forwarded to snprintf.
149  *
150  * @return  Number of characters that would have been written for a sufficiently large buffer
151  *          if successful (not including the ending null character), or a negative value if an error occurred.
152  */
153 template <typename... Ts>
snprintf(char * s,size_t n,const char * fmt,Ts &&...args)154 inline int snprintf(char *s, size_t n, const char *fmt, Ts &&... args)
155 {
156     return ::snprintf(s, n, fmt, std::forward<Ts>(args)...);
157 }
158 #else /* (__ANDROID__ || BARE_METAL) */
159 /** 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  */
168 template <typename T>
169 inline T nearbyint(T value)
170 {
171     return static_cast<T>(std::nearbyint(value));
172 }
173 
174 /** 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  */
183 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
184 inline T round(T value)
185 {
186     //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);
188 }
189 
190 /** 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  */
199 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
200 inline long lround(T value)
201 {
202     return std::lround(value);
203 }
204 
205 /** 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  */
214 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
215 inline 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  *
225  * @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.
227  *
228  * @return Floating-point value with magnitude of @p x and sign of @p y.
229  */
230 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
231 inline T copysign(T x, T y)
232 {
233     return std::copysign(x, y);
234 }
235 
236 /** 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  */
247 template < 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 >
252 inline T fma(T x, T y, T z)
253 {
254     return std::fma(x, y, z);
255 }
256 
257 /** 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
261  * @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.
263  * @param[in] args Arguments forwarded to std::snprintf.
264  *
265  * @return  Number of characters that would have been written for a sufficiently large buffer
266  *          if successful (not including the ending null character), or a negative value if an error occurred.
267  */
268 template <typename... Ts>
269 inline 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 }
273 #endif /* (__ANDROID__ || BARE_METAL) */
274 
275 // std::numeric_limits<T>::lowest
276 template <typename T>
lowest()277 inline T lowest()
278 {
279     return std::numeric_limits<T>::lowest();
280 }
281 
282 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
283 template <>
284 inline __fp16 lowest<__fp16>()
285 {
286     return std::numeric_limits<half_float::half>::lowest();
287 }
288 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
289 
290 template <>
291 inline bfloat16 lowest<bfloat16>()
292 {
293     return bfloat16::lowest();
294 }
295 
296 // std::isfinite
297 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
isfinite(T value)298 inline bool isfinite(T value)
299 {
300     return std::isfinite(static_cast<double>(value));
301 }
302 
isfinite(half_float::half value)303 inline bool isfinite(half_float::half value)
304 {
305     return half_float::isfinite(value);
306 }
307 
isfinite(bfloat16 value)308 inline bool isfinite(bfloat16 value)
309 {
310     return std::isfinite(float(value));
311 }
312 
313 // std::signbit
314 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
signbit(T value)315 inline bool signbit(T value)
316 {
317     return std::signbit(static_cast<double>(value));
318 }
319 
signbit(half_float::half value)320 inline bool signbit(half_float::half value)
321 {
322     return half_float::signbit(value);
323 }
324 
signbit(bfloat16 value)325 inline bool signbit(bfloat16 value)
326 {
327     return std::signbit(float(value));
328 }
329 } // namespace cpp11
330 } // namespace support
331 } // namespace arm_compute
332 #endif /* ARM_COMPUTE_SUPPORT_TOOLCHAINSUPPORT */
333