blob: ab2a9fe80fb7b9f7c9af856a0d2774b1fa6f1f93 [file] [log] [blame]
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +01001/*
2 * Copyright (c) 2017 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_TEST_TOOLCHAINSUPPORT
25#define ARM_COMPUTE_TEST_TOOLCHAINSUPPORT
26
27#include <algorithm>
28#include <cmath>
29#include <cstddef>
30#include <limits>
31#include <memory>
32#include <numeric>
33#include <sstream>
34#include <string>
35#include <type_traits>
36
37namespace arm_compute
38{
39namespace support
40{
41namespace cpp11
42{
Michalis Spyrou07781ac2017-08-31 15:11:41 +010043#if(__ANDROID__ || BARE_METAL)
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010044/** Convert integer and float values to string.
45 *
46 * @note This function implements the same behaviour as std::to_string. The
47 * latter is missing in some Android toolchains.
48 *
49 * @param[in] value Value to be converted to string.
50 *
51 * @return String representation of @p value.
52 */
53template <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 +010054inline std::string to_string(T && value)
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +010055{
56 std::stringstream stream;
57 stream << std::forward<T>(value);
58 return stream.str();
59}
60
61/** Convert string values to integer.
62 *
63 * @note This function implements the same behaviour as std::stoi. The latter
64 * is missing in some Android toolchains.
65 *
66 * @param[in] str String to be converted to int.
67 *
68 * @return Integer representation of @p str.
69 */
70inline int stoi(const std::string &str)
71{
72 std::stringstream stream(str);
73 int value = 0;
74 stream >> value;
75 return value;
76}
77
78/** Convert string values to unsigned long.
79 *
80 * @note This function implements the same behaviour as std::stoul. The latter
81 * is missing in some Android toolchains.
82 *
83 * @param[in] str String to be converted to unsigned long.
84 *
85 * @return Unsigned long representation of @p str.
86 */
87inline unsigned long stoul(const std::string &str)
88{
89 std::stringstream stream(str);
90 unsigned long value = 0;
91 stream >> value;
92 return value;
93}
94
95/** Convert string values to float.
96 *
97 * @note This function implements the same behaviour as std::stof. The latter
98 * is missing in some Android toolchains.
99 *
100 * @param[in] str String to be converted to float.
101 *
102 * @return Float representation of @p str.
103 */
104inline float stof(const std::string &str)
105{
106 std::stringstream stream(str);
107 float value = 0.f;
108 stream >> value;
109 return value;
110}
111
112/** Round floating-point value with half value rounding away from zero.
113 *
114 * @note This function implements the same behaviour as std::round except that it doesn't
115 * support Integral type. The latter is not in the namespace std in some Android toolchains.
116 *
117 * @param[in] value floating-point value to be rounded.
118 *
119 * @return Floating-point value of rounded @p value.
120 */
121template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
122inline T round(T value)
123{
124 return ::round(value);
125}
126
127/** Truncate floating-point value.
128 *
129 * @note This function implements the same behaviour as std::truncate except that it doesn't
130 * support Integral type. The latter is not in the namespace std in some Android toolchains.
131 *
132 * @param[in] value floating-point value to be truncated.
133 *
134 * @return Floating-point value of truncated @p value.
135 */
136template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
137inline T trunc(T value)
138{
139 return ::trunc(value);
140}
141
142/** Composes a floating point value with the magnitude of @p x and the sign of @p y.
143 *
144 * @note This function implements the same behaviour as std::copysign except that it doesn't
145 * support Integral type. The latter is not in the namespace std in some Android toolchains.
146 *
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100147 * @param[in] x value that contains the magnitude to be used in constructing the result.
148 * @param[in] y value that contains the sign to be used in construct in the result.
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100149 *
150 * @return Floating-point value with magnitude of @p x and sign of @p y.
151 */
152template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
153inline T copysign(T x, T y)
154{
155 return ::copysign(x, y);
156}
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100157
158/** Loads the data from the given location, converts them to character string equivalents
159 * and writes the result to a character string buffer.
160 *
161 * @param[in] s Pointer to a character string to write to
162 * @param[in] n Up to buf_size - 1 characters may be written, plus the null terminator
163 * @param[in] fmt Pointer to a null-terminated multibyte string specifying how to interpret the data.
164 * @param[in] args Arguments forwarded to snprintf.
165 *
166 * @return Number of characters that would have been written for a sufficiently large buffer
167 * if successful (not including the terminating null character), or a negative value if an error occurred.
168 */
169template <typename... Ts>
170inline int snprintf(char *s, size_t n, const char *fmt, Ts &&... args)
171{
172 return ::snprintf(s, n, fmt, std::forward<Ts>(args)...);
173}
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100174#else /* (__ANDROID__ || BARE_METAL) */
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100175/** Convert integer and float values to string.
176 *
177 * @note This function acts as a convenience wrapper around std::to_string. The
178 * latter is missing in some Android toolchains.
179 *
180 * @param[in] value Value to be converted to string.
181 *
182 * @return String representation of @p value.
183 */
184template <typename T>
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100185inline std::string to_string(T &&value)
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100186{
187 return ::std::to_string(std::forward<T>(value));
188}
189
190/** Convert string values to integer.
191 *
192 * @note This function acts as a convenience wrapper around std::stoi. The
193 * latter is missing in some Android toolchains.
194 *
195 * @param[in] args Arguments forwarded to std::stoi.
196 *
197 * @return Integer representation of input string.
198 */
199template <typename... Ts>
200int stoi(Ts &&... args)
201{
202 return ::std::stoi(std::forward<Ts>(args)...);
203}
204
205/** Convert string values to unsigned long.
206 *
207 * @note This function acts as a convenience wrapper around std::stoul. The
208 * latter is missing in some Android toolchains.
209 *
210 * @param[in] args Arguments forwarded to std::stoul.
211 *
212 * @return Unsigned long representation of input string.
213 */
214template <typename... Ts>
215int stoul(Ts &&... args)
216{
217 return ::std::stoul(std::forward<Ts>(args)...);
218}
219
220/** Convert string values to float.
221 *
222 * @note This function acts as a convenience wrapper around std::stof. The
223 * latter is missing in some Android toolchains.
224 *
225 * @param[in] args Arguments forwarded to std::stof.
226 *
227 * @return Float representation of input string.
228 */
229template <typename... Ts>
230int stof(Ts &&... args)
231{
232 return ::std::stof(std::forward<Ts>(args)...);
233}
234
235/** Round floating-point value with half value rounding away from zero.
236 *
237 * @note This function implements the same behaviour as std::round except that it doesn't
238 * support Integral type. The latter is not in the namespace std in some Android toolchains.
239 *
240 * @param[in] value floating-point value to be rounded.
241 *
242 * @return Floating-point value of rounded @p value.
243 */
244template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
245inline T round(T value)
246{
247 return std::round(value);
248}
249
250/** Truncate floating-point value.
251 *
252 * @note This function implements the same behaviour as std::truncate except that it doesn't
253 * support Integral type. The latter is not in the namespace std in some Android toolchains.
254 *
255 * @param[in] value floating-point value to be truncated.
256 *
257 * @return Floating-point value of truncated @p value.
258 */
259template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
260inline T trunc(T value)
261{
262 return std::trunc(value);
263}
264
265/** Composes a floating point value with the magnitude of @p x and the sign of @p y.
266 *
267 * @note This function implements the same behaviour as std::copysign except that it doesn't
268 * support Integral type. The latter is not in the namespace std in some Android toolchains.
269 *
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100270 * @param[in] x value that contains the magnitude to be used in constructing the result.
271 * @param[in] y value that contains the sign to be used in construct in the result.
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100272 *
273 * @return Floating-point value with magnitude of @p x and sign of @p y.
274 */
275template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
276inline T copysign(T x, T y)
277{
278 return std::copysign(x, y);
279}
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100280
281/** Loads the data from the given location, converts them to character string equivalents
282 * and writes the result to a character string buffer.
283 *
284 * @param[in] s Pointer to a character string to write to
285 * @param[in] n Up to buf_size - 1 characters may be written, plus the null terminator
286 * @param[in] fmt Pointer to a null-terminated multibyte string specifying how to interpret the data.
287 * @param[in] args Arguments forwarded to std::snprintf.
288 *
289 * @return Number of characters that would have been written for a sufficiently large buffer
290 * if successful (not including the terminating null character), or a negative value if an error occurred.
291 */
292template <typename... Ts>
293inline int snprintf(char *s, std::size_t n, const char *fmt, Ts &&... args)
294{
295 return std::snprintf(s, n, fmt, std::forward<Ts>(args)...);
296}
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100297#endif /* (__ANDROID__ || BARE_METAL) */
Moritz Pflanzer572ade72017-07-21 17:36:33 +0100298
299inline std::string to_string(bool value)
300{
301 std::stringstream str;
302 str << std::boolalpha << value;
303 return str.str();
304}
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100305
306// std::align is missing in GCC 4.9
307// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57350
308inline void *align(std::size_t alignment, std::size_t size, void *&ptr, std::size_t &space)
309{
310 std::uintptr_t pn = reinterpret_cast<std::uintptr_t>(ptr);
311 std::uintptr_t aligned = (pn + alignment - 1) & -alignment;
312 std::size_t padding = aligned - pn;
313 if(space < size + padding)
314 {
315 return nullptr;
316 }
317
318 space -= padding;
319
320 return ptr = reinterpret_cast<void *>(aligned);
321}
Moritz Pflanzerd0ae8b82017-06-29 14:51:57 +0100322} // namespace cpp11
323
324namespace cpp14
325{
326/** make_unqiue is missing in CPP11. Reimplement it according to the standard
327 * proposal.
328 */
329template <class T>
330struct _Unique_if
331{
332 typedef std::unique_ptr<T> _Single_object;
333};
334
335template <class T>
336struct _Unique_if<T[]>
337{
338 typedef std::unique_ptr<T[]> _Unknown_bound;
339};
340
341template <class T, size_t N>
342struct _Unique_if<T[N]>
343{
344 typedef void _Known_bound;
345};
346
347template <class T, class... Args>
348typename _Unique_if<T>::_Single_object
349make_unique(Args &&... args)
350{
351 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
352}
353
354template <class T>
355typename _Unique_if<T>::_Unknown_bound
356make_unique(size_t n)
357{
358 typedef typename std::remove_extent<T>::type U;
359 return std::unique_ptr<T>(new U[n]());
360}
361
362template <class T, class... Args>
363typename _Unique_if<T>::_Known_bound
364make_unique(Args &&...) = delete;
365} // namespace cpp14
366} // namespace support
367} // namespace arm_compute
Moritz Pflanzerfc95ed22017-07-05 11:07:07 +0100368#endif /* ARM_COMPUTE_TEST_TOOLCHAINSUPPORT */