blob: 020a4a112b351b52ea22dade7f049dcc2d2c1529 [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
198/** Loads the data from the given location, converts them to character string equivalents
199 * and writes the result to a character string buffer.
200 *
201 * @param[in] s Pointer to a character string to write to
202 * @param[in] n Up to buf_size - 1 characters may be written, plus the null terminator
203 * @param[in] fmt Pointer to a null-terminated multibyte string specifying how to interpret the data.
204 * @param[in] args Arguments forwarded to snprintf.
205 *
206 * @return Number of characters that would have been written for a sufficiently large buffer
207 * if successful (not including the terminating null character), or a negative value if an error occurred.
208 */
209template <typename... Ts>
210inline int snprintf(char *s, size_t n, const char *fmt, Ts &&... args)
211{
212 return ::snprintf(s, n, fmt, std::forward<Ts>(args)...);
213}
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100214#else /* (__ANDROID__ || BARE_METAL) */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100215/** Convert integer and float values to string.
216 *
217 * @note This function acts as a convenience wrapper around std::to_string. The
218 * latter is missing in some Android toolchains.
219 *
220 * @param[in] value Value to be converted to string.
221 *
222 * @return String representation of @p value.
223 */
224template <typename T>
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100225inline std::string to_string(T &&value)
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100226{
227 return ::std::to_string(std::forward<T>(value));
228}
229
Michalis Spyroue6bcb5b2019-06-07 11:47:16 +0100230/** Rounds the floating-point argument arg to an integer value in floating-point format, using the current rounding mode.
231 *
232 * @note This function acts as a convenience wrapper around std::nearbyint. The
233 * latter is missing in some Android toolchains.
234 *
235 * @param[in] value Value to be rounded.
236 *
237 * @return The rounded value.
238 */
239template <typename T>
240inline T nearbyint(T value)
241{
242 return std::nearbyint(value);
243}
244
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100245/** Convert string values to float.
246 *
247 * @note This function acts as a convenience wrapper around std::stof. The
248 * latter is missing in some Android toolchains.
249 *
250 * @param[in] args Arguments forwarded to std::stof.
251 *
252 * @return Float representation of input string.
253 */
254template <typename... Ts>
255int stof(Ts &&... args)
256{
257 return ::std::stof(std::forward<Ts>(args)...);
258}
259
260/** Round floating-point value with half value rounding away from zero.
261 *
262 * @note This function implements the same behaviour as std::round except that it doesn't
263 * support Integral type. The latter is not in the namespace std in some Android toolchains.
264 *
265 * @param[in] value floating-point value to be rounded.
266 *
267 * @return Floating-point value of rounded @p value.
268 */
269template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
270inline T round(T value)
271{
Pablo Tello826c7692018-01-16 11:41:12 +0000272 //Workaround Valgrind's mismatches: when running from Valgrind the call to std::round(-4.500000) == -4.000000 instead of 5.00000
273 return (value < 0.f) ? static_cast<int>(value - 0.5f) : static_cast<int>(value + 0.5f);
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100274}
275
276/** Truncate floating-point value.
277 *
278 * @note This function implements the same behaviour as std::truncate except that it doesn't
279 * support Integral type. The latter is not in the namespace std in some Android toolchains.
280 *
281 * @param[in] value floating-point value to be truncated.
282 *
283 * @return Floating-point value of truncated @p value.
284 */
285template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
286inline T trunc(T value)
287{
288 return std::trunc(value);
289}
290
291/** Composes a floating point value with the magnitude of @p x and the sign of @p y.
292 *
293 * @note This function implements the same behaviour as std::copysign except that it doesn't
294 * support Integral type. The latter is not in the namespace std in some Android toolchains.
295 *
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100296 * @param[in] x value that contains the magnitude to be used in constructing the result.
297 * @param[in] y value that contains the sign to be used in construct in the result.
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100298 *
299 * @return Floating-point value with magnitude of @p x and sign of @p y.
300 */
301template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
302inline T copysign(T x, T y)
303{
304 return std::copysign(x, y);
305}
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100306
307/** Loads the data from the given location, converts them to character string equivalents
308 * and writes the result to a character string buffer.
309 *
310 * @param[in] s Pointer to a character string to write to
311 * @param[in] n Up to buf_size - 1 characters may be written, plus the null terminator
312 * @param[in] fmt Pointer to a null-terminated multibyte string specifying how to interpret the data.
313 * @param[in] args Arguments forwarded to std::snprintf.
314 *
315 * @return Number of characters that would have been written for a sufficiently large buffer
316 * if successful (not including the terminating null character), or a negative value if an error occurred.
317 */
318template <typename... Ts>
319inline int snprintf(char *s, std::size_t n, const char *fmt, Ts &&... args)
320{
321 return std::snprintf(s, n, fmt, std::forward<Ts>(args)...);
322}
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100323#endif /* (__ANDROID__ || BARE_METAL) */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100324
325inline std::string to_string(bool value)
326{
327 std::stringstream str;
328 str << std::boolalpha << value;
329 return str.str();
330}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100331
332// std::align is missing in GCC 4.9
333// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57350
334inline void *align(std::size_t alignment, std::size_t size, void *&ptr, std::size_t &space)
335{
336 std::uintptr_t pn = reinterpret_cast<std::uintptr_t>(ptr);
337 std::uintptr_t aligned = (pn + alignment - 1) & -alignment;
338 std::size_t padding = aligned - pn;
339 if(space < size + padding)
340 {
341 return nullptr;
342 }
343
344 space -= padding;
345
346 return ptr = reinterpret_cast<void *>(aligned);
347}
Anthony Barbier3a6163e2018-08-10 17:36:36 +0100348// std::numeric_limits<T>::lowest
349template <typename T>
350inline T lowest()
351{
352 return std::numeric_limits<T>::lowest();
353}
354
355#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
356template <>
357inline __fp16 lowest<__fp16>()
358{
359 return std::numeric_limits<half_float::half>::lowest();
360}
361#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100362
363// std::isfinite
364template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
365inline bool isfinite(T value)
366{
367 return std::isfinite(value);
368}
369
370inline bool isfinite(half_float::half value)
371{
372 return half_float::isfinite(value);
373}
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100374} // namespace cpp11
375
376namespace cpp14
377{
Georgios Pinitas84b51ad2017-11-07 13:24:57 +0000378/** make_unique is missing in CPP11. Re-implement it according to the standard proposal. */
Alex Gildayc357c472018-03-21 13:54:09 +0000379
380/**<Template for single object */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100381template <class T>
382struct _Unique_if
383{
Alex Gildayc357c472018-03-21 13:54:09 +0000384 typedef std::unique_ptr<T> _Single_object; /**< Single object type */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100385};
386
Alex Gildayc357c472018-03-21 13:54:09 +0000387/** Template for array */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100388template <class T>
389struct _Unique_if<T[]>
390{
Alex Gildayc357c472018-03-21 13:54:09 +0000391 typedef std::unique_ptr<T[]> _Unknown_bound; /**< Array type */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100392};
393
Alex Gildayc357c472018-03-21 13:54:09 +0000394/** Template for array with known bounds (to throw an error).
395 *
396 * @note this is intended to never be hit.
397 */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100398template <class T, size_t N>
399struct _Unique_if<T[N]>
400{
Alex Gildayc357c472018-03-21 13:54:09 +0000401 typedef void _Known_bound; /**< Should never be used */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100402};
403
Alex Gildayc357c472018-03-21 13:54:09 +0000404/** Construct a single object and return a unique pointer to it.
405 *
406 * @param[in] args Constructor arguments.
407 *
408 * @return a unique pointer to the new object.
409 */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100410template <class T, class... Args>
411typename _Unique_if<T>::_Single_object
412make_unique(Args &&... args)
413{
414 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
415}
416
Alex Gildayc357c472018-03-21 13:54:09 +0000417/** Construct an array of objects and return a unique pointer to it.
418 *
419 * @param[in] n Array size
420 *
421 * @return a unique pointer to the new array.
422 */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100423template <class T>
424typename _Unique_if<T>::_Unknown_bound
425make_unique(size_t n)
426{
427 typedef typename std::remove_extent<T>::type U;
428 return std::unique_ptr<T>(new U[n]());
429}
430
Alex Gildayc357c472018-03-21 13:54:09 +0000431/** It is invalid to attempt to make_unique an array with known bounds. */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100432template <class T, class... Args>
433typename _Unique_if<T>::_Known_bound
434make_unique(Args &&...) = delete;
435} // namespace cpp14
436} // namespace support
437} // namespace arm_compute
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100438#endif /* ARM_COMPUTE_TEST_TOOLCHAINSUPPORT */