Compute Library
 21.02
ToolchainSupport.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 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>
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>
70 inline T round(T value)
71 {
72  return ::round(value);
73 }
74 
75 /** Truncate floating-point value.
76  *
77  * @note This function implements the same behaviour as std::truncate 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 truncated.
81  *
82  * @return Floating-point value of truncated @p value.
83  */
84 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
85 inline T trunc(T value)
86 {
87  return ::trunc(value);
88 }
89 
90 /** Composes a floating point value with the magnitude of @p x and the sign of @p y.
91  *
92  * @note This function implements the same behaviour as std::copysign 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] x value that contains the magnitude to be used in constructing the result.
96  * @param[in] y value that contains the sign to be used in construct in the result.
97  *
98  * @return Floating-point value with magnitude of @p x and sign of @p y.
99  */
100 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
101 inline T copysign(T x, T y)
102 {
103  return ::copysign(x, y);
104 }
105 
106 /** Computes (x*y) + z as if to infinite precision and rounded only once to fit the result type.
107  *
108  * @note This function implements the same behaviour as std::fma except that it doesn't
109  * support Integral type. The latter is not in the namespace std in some Android toolchains.
110  *
111  * @param[in] x floating-point value
112  * @param[in] y floating-point value
113  * @param[in] z floating-point value
114  *
115  * @return Result floating point value equal to (x*y) + z.c
116  */
117 template < typename T, typename = typename std::enable_if < std::is_floating_point<T>::value
118 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
119  || std::is_same<T, float16_t>::value
120 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
121  >::type >
122 inline T fma(T x, T y, T z)
123 {
124  return ::fma(x, y, z);
125 }
126 
127 /** Loads the data from the given location, converts them to character string equivalents
128  * and writes the result to a character string buffer.
129  *
130  * @param[in] s Pointer to a character string to write to
131  * @param[in] n Up to buf_size - 1 characters may be written, plus the null terminator
132  * @param[in] fmt Pointer to a null-terminated multibyte string specifying how to interpret the data.
133  * @param[in] args Arguments forwarded to snprintf.
134  *
135  * @return Number of characters that would have been written for a sufficiently large buffer
136  * if successful (not including the terminating null character), or a negative value if an error occurred.
137  */
138 template <typename... Ts>
139 inline int snprintf(char *s, size_t n, const char *fmt, Ts &&... args)
140 {
141  return ::snprintf(s, n, fmt, std::forward<Ts>(args)...);
142 }
143 #else /* (__ANDROID__ || BARE_METAL) */
144 /** Rounds the floating-point argument arg to an integer value in floating-point format, using the current rounding mode.
145  *
146  * @note This function acts as a convenience wrapper around std::nearbyint. The
147  * latter is missing in some Android toolchains.
148  *
149  * @param[in] value Value to be rounded.
150  *
151  * @return The rounded value.
152  */
153 template <typename T>
154 inline T nearbyint(T value)
155 {
156  return static_cast<T>(std::nearbyint(value));
157 }
158 
159 /** Round floating-point value with half value rounding away from zero.
160  *
161  * @note This function implements the same behaviour as std::round except that it doesn't
162  * support Integral type. The latter is not in the namespace std in some Android toolchains.
163  *
164  * @param[in] value floating-point value to be rounded.
165  *
166  * @return Floating-point value of rounded @p value.
167  */
168 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
169 inline T round(T value)
170 {
171  //Workaround Valgrind's mismatches: when running from Valgrind the call to std::round(-4.500000) == -4.000000 instead of 5.00000
172  return (value < 0.f) ? static_cast<int>(value - 0.5f) : static_cast<int>(value + 0.5f);
173 }
174 
175 /** Truncate floating-point value.
176  *
177  * @note This function implements the same behaviour as std::truncate except that it doesn't
178  * support Integral type. The latter is not in the namespace std in some Android toolchains.
179  *
180  * @param[in] value floating-point value to be truncated.
181  *
182  * @return Floating-point value of truncated @p value.
183  */
184 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
185 inline T trunc(T value)
186 {
187  return std::trunc(value);
188 }
189 
190 /** Composes a floating point value with the magnitude of @p x and the sign of @p y.
191  *
192  * @note This function implements the same behaviour as std::copysign 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] x value that contains the magnitude to be used in constructing the result.
196  * @param[in] y value that contains the sign to be used in construct in the result.
197  *
198  * @return Floating-point value with magnitude of @p x and sign of @p y.
199  */
200 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
201 inline T copysign(T x, T y)
202 {
203  return std::copysign(x, y);
204 }
205 
206 /** Computes (x*y) + z as if to infinite precision and rounded only once to fit the result type.
207  *
208  * @note This function implements the same behaviour as std::fma except that it doesn't
209  * support Integral type. The latter is not in the namespace std in some Android toolchains.
210  *
211  * @param[in] x floating-point value
212  * @param[in] y floating-point value
213  * @param[in] z floating-point value
214  *
215  * @return Result floating point value equal to (x*y) + z.
216  */
217 template < typename T, typename = typename std::enable_if < std::is_floating_point<T>::value
218 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
219  || std::is_same<T, float16_t>::value
220 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
221  >::type >
222 inline T fma(T x, T y, T z)
223 {
224  return std::fma(x, y, z);
225 }
226 
227 /** Loads the data from the given location, converts them to character string equivalents
228  * and writes the result to a character string buffer.
229  *
230  * @param[in] s Pointer to a character string to write to
231  * @param[in] n Up to buf_size - 1 characters may be written, plus the null terminator
232  * @param[in] fmt Pointer to a null-terminated multibyte string specifying how to interpret the data.
233  * @param[in] args Arguments forwarded to std::snprintf.
234  *
235  * @return Number of characters that would have been written for a sufficiently large buffer
236  * if successful (not including the terminating null character), or a negative value if an error occurred.
237  */
238 template <typename... Ts>
239 inline int snprintf(char *s, std::size_t n, const char *fmt, Ts &&... args)
240 {
241  return std::snprintf(s, n, fmt, std::forward<Ts>(args)...);
242 }
243 #endif /* (__ANDROID__ || BARE_METAL) */
244 
245 // std::numeric_limits<T>::lowest
246 template <typename T>
247 inline T lowest()
248 {
250 }
251 
252 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
253 template <>
254 inline __fp16 lowest<__fp16>()
255 {
257 }
258 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
259 
260 template <>
262 {
263  return bfloat16::lowest();
264 }
265 
266 // std::isfinite
267 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
268 inline bool isfinite(T value)
269 {
270  return std::isfinite(value);
271 }
272 
273 inline bool isfinite(half_float::half value)
274 {
275  return half_float::isfinite(value);
276 }
277 
278 inline bool isfinite(bfloat16 value)
279 {
280  return std::isfinite(float(value));
281 }
282 } // namespace cpp11
283 } // namespace support
284 } // namespace arm_compute
285 #endif /* ARM_COMPUTE_SUPPORT_TOOLCHAINSUPPORT */
T trunc(T value)
Truncate floating-point value.
Brain floating point representation class.
Definition: Bfloat16.h:80
T copysign(T x, T y)
Composes a floating point value with the magnitude of x and the sign of y.
T nearbyint(T value)
Rounds the floating-point argument arg to an integer value in floating-point format, using the current rounding mode.
half_float::half half
16-bit floating point type
Definition: Types.h:46
decltype(strategy::transforms) typedef type
Copyright (c) 2017-2021 Arm Limited.
static bfloat16 lowest()
Lowest representative value.
Definition: Bfloat16.h:119
bool isfinite(bfloat16 value)
T fma(T x, T y, T z)
Computes (x*y) + z as if to infinite precision and rounded only once to fit the result type...
T round(T value)
Round floating-point value with half value rounding away from zero.
int snprintf(char *s, std::size_t n, const char *fmt, Ts &&... args)
Loads the data from the given location, converts them to character string equivalents and writes the ...