blob: 7d02e67ec6b117d2d6c51770753dda8d366121ac [file] [log] [blame]
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +01001/*
Pablo Tello826c7692018-01-16 11:41:12 +00002 * Copyright (c) 2017-2018 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
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100120/** Convert string values to float.
121 *
122 * @note This function implements the same behaviour as std::stof. The latter
123 * is missing in some Android toolchains.
124 *
125 * @param[in] str String to be converted to float.
126 *
127 * @return Float representation of @p str.
128 */
129inline float stof(const std::string &str)
130{
131 std::stringstream stream(str);
132 float value = 0.f;
133 stream >> value;
134 return value;
135}
136
137/** Round floating-point value with half value rounding away from zero.
138 *
139 * @note This function implements the same behaviour as std::round except that it doesn't
140 * support Integral type. The latter is not in the namespace std in some Android toolchains.
141 *
142 * @param[in] value floating-point value to be rounded.
143 *
144 * @return Floating-point value of rounded @p value.
145 */
146template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
147inline T round(T value)
148{
149 return ::round(value);
150}
151
152/** Truncate floating-point value.
153 *
154 * @note This function implements the same behaviour as std::truncate 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 truncated.
158 *
159 * @return Floating-point value of truncated @p value.
160 */
161template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
162inline T trunc(T value)
163{
164 return ::trunc(value);
165}
166
167/** Composes a floating point value with the magnitude of @p x and the sign of @p y.
168 *
169 * @note This function implements the same behaviour as std::copysign except that it doesn't
170 * support Integral type. The latter is not in the namespace std in some Android toolchains.
171 *
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100172 * @param[in] x value that contains the magnitude to be used in constructing the result.
173 * @param[in] y value that contains the sign to be used in construct in the result.
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100174 *
175 * @return Floating-point value with magnitude of @p x and sign of @p y.
176 */
177template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
178inline T copysign(T x, T y)
179{
180 return ::copysign(x, y);
181}
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100182
183/** Loads the data from the given location, converts them to character string equivalents
184 * and writes the result to a character string buffer.
185 *
186 * @param[in] s Pointer to a character string to write to
187 * @param[in] n Up to buf_size - 1 characters may be written, plus the null terminator
188 * @param[in] fmt Pointer to a null-terminated multibyte string specifying how to interpret the data.
189 * @param[in] args Arguments forwarded to snprintf.
190 *
191 * @return Number of characters that would have been written for a sufficiently large buffer
192 * if successful (not including the terminating null character), or a negative value if an error occurred.
193 */
194template <typename... Ts>
195inline int snprintf(char *s, size_t n, const char *fmt, Ts &&... args)
196{
197 return ::snprintf(s, n, fmt, std::forward<Ts>(args)...);
198}
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100199#else /* (__ANDROID__ || BARE_METAL) */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100200/** Convert integer and float values to string.
201 *
202 * @note This function acts as a convenience wrapper around std::to_string. The
203 * latter is missing in some Android toolchains.
204 *
205 * @param[in] value Value to be converted to string.
206 *
207 * @return String representation of @p value.
208 */
209template <typename T>
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100210inline std::string to_string(T &&value)
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100211{
212 return ::std::to_string(std::forward<T>(value));
213}
214
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100215/** Convert string values to float.
216 *
217 * @note This function acts as a convenience wrapper around std::stof. The
218 * latter is missing in some Android toolchains.
219 *
220 * @param[in] args Arguments forwarded to std::stof.
221 *
222 * @return Float representation of input string.
223 */
224template <typename... Ts>
225int stof(Ts &&... args)
226{
227 return ::std::stof(std::forward<Ts>(args)...);
228}
229
230/** Round floating-point value with half value rounding away from zero.
231 *
232 * @note This function implements the same behaviour as std::round except that it doesn't
233 * support Integral type. The latter is not in the namespace std in some Android toolchains.
234 *
235 * @param[in] value floating-point value to be rounded.
236 *
237 * @return Floating-point value of rounded @p value.
238 */
239template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
240inline T round(T value)
241{
Pablo Tello826c7692018-01-16 11:41:12 +0000242 //Workaround Valgrind's mismatches: when running from Valgrind the call to std::round(-4.500000) == -4.000000 instead of 5.00000
243 return (value < 0.f) ? static_cast<int>(value - 0.5f) : static_cast<int>(value + 0.5f);
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100244}
245
246/** Truncate floating-point value.
247 *
248 * @note This function implements the same behaviour as std::truncate except that it doesn't
249 * support Integral type. The latter is not in the namespace std in some Android toolchains.
250 *
251 * @param[in] value floating-point value to be truncated.
252 *
253 * @return Floating-point value of truncated @p value.
254 */
255template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
256inline T trunc(T value)
257{
258 return std::trunc(value);
259}
260
261/** Composes a floating point value with the magnitude of @p x and the sign of @p y.
262 *
263 * @note This function implements the same behaviour as std::copysign except that it doesn't
264 * support Integral type. The latter is not in the namespace std in some Android toolchains.
265 *
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100266 * @param[in] x value that contains the magnitude to be used in constructing the result.
267 * @param[in] y value that contains the sign to be used in construct in the result.
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100268 *
269 * @return Floating-point value with magnitude of @p x and sign of @p y.
270 */
271template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
272inline T copysign(T x, T y)
273{
274 return std::copysign(x, y);
275}
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100276
277/** Loads the data from the given location, converts them to character string equivalents
278 * and writes the result to a character string buffer.
279 *
280 * @param[in] s Pointer to a character string to write to
281 * @param[in] n Up to buf_size - 1 characters may be written, plus the null terminator
282 * @param[in] fmt Pointer to a null-terminated multibyte string specifying how to interpret the data.
283 * @param[in] args Arguments forwarded to std::snprintf.
284 *
285 * @return Number of characters that would have been written for a sufficiently large buffer
286 * if successful (not including the terminating null character), or a negative value if an error occurred.
287 */
288template <typename... Ts>
289inline int snprintf(char *s, std::size_t n, const char *fmt, Ts &&... args)
290{
291 return std::snprintf(s, n, fmt, std::forward<Ts>(args)...);
292}
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100293#endif /* (__ANDROID__ || BARE_METAL) */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100294
295inline std::string to_string(bool value)
296{
297 std::stringstream str;
298 str << std::boolalpha << value;
299 return str.str();
300}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100301
302// std::align is missing in GCC 4.9
303// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57350
304inline void *align(std::size_t alignment, std::size_t size, void *&ptr, std::size_t &space)
305{
306 std::uintptr_t pn = reinterpret_cast<std::uintptr_t>(ptr);
307 std::uintptr_t aligned = (pn + alignment - 1) & -alignment;
308 std::size_t padding = aligned - pn;
309 if(space < size + padding)
310 {
311 return nullptr;
312 }
313
314 space -= padding;
315
316 return ptr = reinterpret_cast<void *>(aligned);
317}
Anthony Barbier3a6163e2018-08-10 17:36:36 +0100318// std::numeric_limits<T>::lowest
319template <typename T>
320inline T lowest()
321{
322 return std::numeric_limits<T>::lowest();
323}
324
325#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
326template <>
327inline __fp16 lowest<__fp16>()
328{
329 return std::numeric_limits<half_float::half>::lowest();
330}
331#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100332
333// std::isfinite
334template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
335inline bool isfinite(T value)
336{
337 return std::isfinite(value);
338}
339
340inline bool isfinite(half_float::half value)
341{
342 return half_float::isfinite(value);
343}
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100344} // namespace cpp11
345
346namespace cpp14
347{
Georgios Pinitas84b51ad2017-11-07 13:24:57 +0000348/** make_unique is missing in CPP11. Re-implement it according to the standard proposal. */
Alex Gildayc357c472018-03-21 13:54:09 +0000349
350/**<Template for single object */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100351template <class T>
352struct _Unique_if
353{
Alex Gildayc357c472018-03-21 13:54:09 +0000354 typedef std::unique_ptr<T> _Single_object; /**< Single object type */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100355};
356
Alex Gildayc357c472018-03-21 13:54:09 +0000357/** Template for array */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100358template <class T>
359struct _Unique_if<T[]>
360{
Alex Gildayc357c472018-03-21 13:54:09 +0000361 typedef std::unique_ptr<T[]> _Unknown_bound; /**< Array type */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100362};
363
Alex Gildayc357c472018-03-21 13:54:09 +0000364/** Template for array with known bounds (to throw an error).
365 *
366 * @note this is intended to never be hit.
367 */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100368template <class T, size_t N>
369struct _Unique_if<T[N]>
370{
Alex Gildayc357c472018-03-21 13:54:09 +0000371 typedef void _Known_bound; /**< Should never be used */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100372};
373
Alex Gildayc357c472018-03-21 13:54:09 +0000374/** Construct a single object and return a unique pointer to it.
375 *
376 * @param[in] args Constructor arguments.
377 *
378 * @return a unique pointer to the new object.
379 */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100380template <class T, class... Args>
381typename _Unique_if<T>::_Single_object
382make_unique(Args &&... args)
383{
384 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
385}
386
Alex Gildayc357c472018-03-21 13:54:09 +0000387/** Construct an array of objects and return a unique pointer to it.
388 *
389 * @param[in] n Array size
390 *
391 * @return a unique pointer to the new array.
392 */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100393template <class T>
394typename _Unique_if<T>::_Unknown_bound
395make_unique(size_t n)
396{
397 typedef typename std::remove_extent<T>::type U;
398 return std::unique_ptr<T>(new U[n]());
399}
400
Alex Gildayc357c472018-03-21 13:54:09 +0000401/** It is invalid to attempt to make_unique an array with known bounds. */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100402template <class T, class... Args>
403typename _Unique_if<T>::_Known_bound
404make_unique(Args &&...) = delete;
405} // namespace cpp14
406} // namespace support
407} // namespace arm_compute
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100408#endif /* ARM_COMPUTE_TEST_TOOLCHAINSUPPORT */