blob: 03bbff9abadc61507701aecc94d3cf49d67bab13 [file] [log] [blame]
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +01001/*
Michalis Spyroue6bcb5b2019-06-07 11:47:16 +01002 * Copyright (c) 2017-2019 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 */
24#ifndef ARM_COMPUTE_TEST_TOOLCHAINSUPPORT
25#define ARM_COMPUTE_TEST_TOOLCHAINSUPPORT
26
27#include <algorithm>
Pablo Tello65f99822018-05-24 11:40:15 +010028#include <cassert>
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010029#include <cmath>
30#include <cstddef>
31#include <limits>
32#include <memory>
33#include <numeric>
34#include <sstream>
35#include <string>
36#include <type_traits>
37
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +010038#include "support/Half.h"
39
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010040namespace arm_compute
41{
42namespace support
43{
44namespace cpp11
45{
Pablo Tello65f99822018-05-24 11:40:15 +010046enum class NumericBase
47{
48 BASE_10,
49 BASE_16
50};
51
52/** Convert string values to integer.
53 *
54 * @note This function implements the same behaviour as std::stoi. The latter
55 * is missing in some Android toolchains.
56 *
57 * @param[in] str String to be converted to int.
58 * @param[in] pos If idx is not a null pointer, the function sets the value of pos to the position of the first character in str after the number.
59 * @param[in] base Numeric base used to interpret the string.
60 *
61 * @return Integer representation of @p str.
62 */
63inline int stoi(const std::string &str, std::size_t *pos = 0, NumericBase base = NumericBase::BASE_10)
64{
65 assert(base == NumericBase::BASE_10 || base == NumericBase::BASE_16);
66 unsigned int x;
67 std::stringstream ss;
68 if(base == NumericBase::BASE_16)
69 {
70 ss << std::hex;
71 }
72 ss << str;
73 ss >> x;
74 return x;
75}
76
77/** Convert string values to unsigned long.
78 *
79 * @note This function implements the same behaviour as std::stoul. The latter
80 * is missing in some Android toolchains.
81 *
82 * @param[in] str String to be converted to unsigned long.
83 * @param[in] pos If idx is not a null pointer, the function sets the value of pos to the position of the first character in str after the number.
84 * @param[in] base Numeric base used to interpret the string.
85 *
86 * @return Unsigned long representation of @p str.
87 */
88inline unsigned long stoul(const std::string &str, std::size_t *pos = 0, NumericBase base = NumericBase::BASE_10)
89{
90 assert(base == NumericBase::BASE_10 || base == NumericBase::BASE_16);
91 std::stringstream stream;
92 unsigned long value = 0;
93 if(base == NumericBase::BASE_16)
94 {
95 stream << std::hex;
96 }
97 stream << str;
98 stream >> value;
99 return value;
100}
101
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100102#if(__ANDROID__ || BARE_METAL)
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100103/** Convert integer and float values to string.
104 *
105 * @note This function implements the same behaviour as std::to_string. The
106 * latter is missing in some Android toolchains.
107 *
108 * @param[in] value Value to be converted to string.
109 *
110 * @return String representation of @p value.
111 */
112template <typename T, typename std::enable_if<std::is_arithmetic<typename std::decay<T>::type>::value, int>::type = 0>
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100113inline std::string to_string(T && value)
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100114{
115 std::stringstream stream;
116 stream << std::forward<T>(value);
117 return stream.str();
118}
119
Michalis Spyroue6bcb5b2019-06-07 11:47:16 +0100120/** Rounds the floating-point argument arg to an integer value in floating-point format, using the current rounding mode.
121 *
122 * @note This function acts as a convenience wrapper around std::nearbyint. The
123 * latter is missing in some Android toolchains.
124 *
125 * @param[in] value Value to be rounded.
126 *
127 * @return The rounded value.
128 */
129template <typename T>
130inline T nearbyint(T value)
131{
132 return ::nearbyint(value);
133}
134
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100135/** Convert string values to float.
136 *
137 * @note This function implements the same behaviour as std::stof. The latter
138 * is missing in some Android toolchains.
139 *
140 * @param[in] str String to be converted to float.
141 *
142 * @return Float representation of @p str.
143 */
144inline float stof(const std::string &str)
145{
146 std::stringstream stream(str);
147 float value = 0.f;
148 stream >> value;
149 return value;
150}
151
152/** Round floating-point value with half value rounding away from zero.
153 *
154 * @note This function implements the same behaviour as std::round except that it doesn't
155 * support Integral type. The latter is not in the namespace std in some Android toolchains.
156 *
157 * @param[in] value floating-point value to be rounded.
158 *
159 * @return Floating-point value of rounded @p value.
160 */
161template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
162inline T round(T value)
163{
164 return ::round(value);
165}
166
167/** Truncate floating-point value.
168 *
169 * @note This function implements the same behaviour as std::truncate except that it doesn't
170 * support Integral type. The latter is not in the namespace std in some Android toolchains.
171 *
172 * @param[in] value floating-point value to be truncated.
173 *
174 * @return Floating-point value of truncated @p value.
175 */
176template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
177inline T trunc(T value)
178{
179 return ::trunc(value);
180}
181
182/** Composes a floating point value with the magnitude of @p x and the sign of @p y.
183 *
184 * @note This function implements the same behaviour as std::copysign except that it doesn't
185 * support Integral type. The latter is not in the namespace std in some Android toolchains.
186 *
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100187 * @param[in] x value that contains the magnitude to be used in constructing the result.
188 * @param[in] y value that contains the sign to be used in construct in the result.
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100189 *
190 * @return Floating-point value with magnitude of @p x and sign of @p y.
191 */
192template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
193inline T copysign(T x, T y)
194{
195 return ::copysign(x, y);
196}
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100197
Georgios Pinitas1c29ffc2019-08-01 15:03:00 +0100198/** Computes (x*y) + z as if to infinite precision and rounded only once to fit the result type.
199 *
200 * @note This function implements the same behaviour as std::fma except that it doesn't
201 * support Integral type. The latter is not in the namespace std in some Android toolchains.
202 *
203 * @param[in] x floating-point value
204 * @param[in] y floating-point value
205 * @param[in] z floating-point value
206 *
207 * @return Result floating point value equal to (x*y) + z.c
208 */
209template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
210inline T fma(T x, T y, T z)
211{
212 return ::fma(x, y, z);
213}
214
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100215/** Loads the data from the given location, converts them to character string equivalents
216 * and writes the result to a character string buffer.
217 *
218 * @param[in] s Pointer to a character string to write to
219 * @param[in] n Up to buf_size - 1 characters may be written, plus the null terminator
220 * @param[in] fmt Pointer to a null-terminated multibyte string specifying how to interpret the data.
221 * @param[in] args Arguments forwarded to snprintf.
222 *
223 * @return Number of characters that would have been written for a sufficiently large buffer
224 * if successful (not including the terminating null character), or a negative value if an error occurred.
225 */
226template <typename... Ts>
227inline int snprintf(char *s, size_t n, const char *fmt, Ts &&... args)
228{
229 return ::snprintf(s, n, fmt, std::forward<Ts>(args)...);
230}
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100231#else /* (__ANDROID__ || BARE_METAL) */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100232/** Convert integer and float values to string.
233 *
234 * @note This function acts as a convenience wrapper around std::to_string. The
235 * latter is missing in some Android toolchains.
236 *
237 * @param[in] value Value to be converted to string.
238 *
239 * @return String representation of @p value.
240 */
241template <typename T>
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100242inline std::string to_string(T &&value)
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100243{
244 return ::std::to_string(std::forward<T>(value));
245}
246
Michalis Spyroue6bcb5b2019-06-07 11:47:16 +0100247/** Rounds the floating-point argument arg to an integer value in floating-point format, using the current rounding mode.
248 *
249 * @note This function acts as a convenience wrapper around std::nearbyint. The
250 * latter is missing in some Android toolchains.
251 *
252 * @param[in] value Value to be rounded.
253 *
254 * @return The rounded value.
255 */
256template <typename T>
257inline T nearbyint(T value)
258{
259 return std::nearbyint(value);
260}
261
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100262/** Convert string values to float.
263 *
264 * @note This function acts as a convenience wrapper around std::stof. The
265 * latter is missing in some Android toolchains.
266 *
267 * @param[in] args Arguments forwarded to std::stof.
268 *
269 * @return Float representation of input string.
270 */
271template <typename... Ts>
272int stof(Ts &&... args)
273{
274 return ::std::stof(std::forward<Ts>(args)...);
275}
276
277/** Round floating-point value with half value rounding away from zero.
278 *
279 * @note This function implements the same behaviour as std::round except that it doesn't
280 * support Integral type. The latter is not in the namespace std in some Android toolchains.
281 *
282 * @param[in] value floating-point value to be rounded.
283 *
284 * @return Floating-point value of rounded @p value.
285 */
286template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
287inline T round(T value)
288{
Pablo Tello826c7692018-01-16 11:41:12 +0000289 //Workaround Valgrind's mismatches: when running from Valgrind the call to std::round(-4.500000) == -4.000000 instead of 5.00000
290 return (value < 0.f) ? static_cast<int>(value - 0.5f) : static_cast<int>(value + 0.5f);
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100291}
292
293/** Truncate floating-point value.
294 *
295 * @note This function implements the same behaviour as std::truncate except that it doesn't
296 * support Integral type. The latter is not in the namespace std in some Android toolchains.
297 *
298 * @param[in] value floating-point value to be truncated.
299 *
300 * @return Floating-point value of truncated @p value.
301 */
302template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
303inline T trunc(T value)
304{
305 return std::trunc(value);
306}
307
308/** Composes a floating point value with the magnitude of @p x and the sign of @p y.
309 *
310 * @note This function implements the same behaviour as std::copysign except that it doesn't
311 * support Integral type. The latter is not in the namespace std in some Android toolchains.
312 *
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100313 * @param[in] x value that contains the magnitude to be used in constructing the result.
314 * @param[in] y value that contains the sign to be used in construct in the result.
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100315 *
316 * @return Floating-point value with magnitude of @p x and sign of @p y.
317 */
318template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
319inline T copysign(T x, T y)
320{
321 return std::copysign(x, y);
322}
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100323
Georgios Pinitas1c29ffc2019-08-01 15:03:00 +0100324/** Computes (x*y) + z as if to infinite precision and rounded only once to fit the result type.
325 *
326 * @note This function implements the same behaviour as std::fma except that it doesn't
327 * support Integral type. The latter is not in the namespace std in some Android toolchains.
328 *
329 * @param[in] x floating-point value
330 * @param[in] y floating-point value
331 * @param[in] z floating-point value
332 *
333 * @return Result floating point value equal to (x*y) + z.
334 */
335template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
336inline T fma(T x, T y, T z)
337{
338 return std::fma(x, y, z);
339}
340
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100341/** Loads the data from the given location, converts them to character string equivalents
342 * and writes the result to a character string buffer.
343 *
344 * @param[in] s Pointer to a character string to write to
345 * @param[in] n Up to buf_size - 1 characters may be written, plus the null terminator
346 * @param[in] fmt Pointer to a null-terminated multibyte string specifying how to interpret the data.
347 * @param[in] args Arguments forwarded to std::snprintf.
348 *
349 * @return Number of characters that would have been written for a sufficiently large buffer
350 * if successful (not including the terminating null character), or a negative value if an error occurred.
351 */
352template <typename... Ts>
353inline int snprintf(char *s, std::size_t n, const char *fmt, Ts &&... args)
354{
355 return std::snprintf(s, n, fmt, std::forward<Ts>(args)...);
356}
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100357#endif /* (__ANDROID__ || BARE_METAL) */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100358
359inline std::string to_string(bool value)
360{
361 std::stringstream str;
362 str << std::boolalpha << value;
363 return str.str();
364}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100365
366// std::align is missing in GCC 4.9
367// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57350
368inline void *align(std::size_t alignment, std::size_t size, void *&ptr, std::size_t &space)
369{
370 std::uintptr_t pn = reinterpret_cast<std::uintptr_t>(ptr);
371 std::uintptr_t aligned = (pn + alignment - 1) & -alignment;
372 std::size_t padding = aligned - pn;
373 if(space < size + padding)
374 {
375 return nullptr;
376 }
377
378 space -= padding;
379
380 return ptr = reinterpret_cast<void *>(aligned);
381}
Anthony Barbier3a6163e2018-08-10 17:36:36 +0100382// std::numeric_limits<T>::lowest
383template <typename T>
384inline T lowest()
385{
386 return std::numeric_limits<T>::lowest();
387}
388
389#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
390template <>
391inline __fp16 lowest<__fp16>()
392{
393 return std::numeric_limits<half_float::half>::lowest();
394}
395#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100396
397// std::isfinite
398template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
399inline bool isfinite(T value)
400{
401 return std::isfinite(value);
402}
403
404inline bool isfinite(half_float::half value)
405{
406 return half_float::isfinite(value);
407}
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100408} // namespace cpp11
409
410namespace cpp14
411{
Georgios Pinitas84b51ad2017-11-07 13:24:57 +0000412/** make_unique is missing in CPP11. Re-implement it according to the standard proposal. */
Alex Gildayc357c472018-03-21 13:54:09 +0000413
414/**<Template for single object */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100415template <class T>
416struct _Unique_if
417{
Alex Gildayc357c472018-03-21 13:54:09 +0000418 typedef std::unique_ptr<T> _Single_object; /**< Single object type */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100419};
420
Alex Gildayc357c472018-03-21 13:54:09 +0000421/** Template for array */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100422template <class T>
423struct _Unique_if<T[]>
424{
Alex Gildayc357c472018-03-21 13:54:09 +0000425 typedef std::unique_ptr<T[]> _Unknown_bound; /**< Array type */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100426};
427
Alex Gildayc357c472018-03-21 13:54:09 +0000428/** Template for array with known bounds (to throw an error).
429 *
430 * @note this is intended to never be hit.
431 */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100432template <class T, size_t N>
433struct _Unique_if<T[N]>
434{
Alex Gildayc357c472018-03-21 13:54:09 +0000435 typedef void _Known_bound; /**< Should never be used */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100436};
437
Alex Gildayc357c472018-03-21 13:54:09 +0000438/** Construct a single object and return a unique pointer to it.
439 *
440 * @param[in] args Constructor arguments.
441 *
442 * @return a unique pointer to the new object.
443 */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100444template <class T, class... Args>
445typename _Unique_if<T>::_Single_object
446make_unique(Args &&... args)
447{
448 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
449}
450
Alex Gildayc357c472018-03-21 13:54:09 +0000451/** Construct an array of objects and return a unique pointer to it.
452 *
453 * @param[in] n Array size
454 *
455 * @return a unique pointer to the new array.
456 */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100457template <class T>
458typename _Unique_if<T>::_Unknown_bound
459make_unique(size_t n)
460{
461 typedef typename std::remove_extent<T>::type U;
462 return std::unique_ptr<T>(new U[n]());
463}
464
Alex Gildayc357c472018-03-21 13:54:09 +0000465/** It is invalid to attempt to make_unique an array with known bounds. */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100466template <class T, class... Args>
467typename _Unique_if<T>::_Known_bound
468make_unique(Args &&...) = delete;
469} // namespace cpp14
470} // namespace support
471} // namespace arm_compute
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100472#endif /* ARM_COMPUTE_TEST_TOOLCHAINSUPPORT */