blob: 36b08affb96febd41c81e59af11677622237973a [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{
43#ifdef __ANDROID__
44/** 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>
54std::string to_string(T && value)
55{
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 *
147 * @param[in] x value that contains the magnitued to be used in constructing the result.
148 * @param[in] y value that contains the sign to be used in constructin the result.
149 *
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}
157#else
158/** Convert integer and float values to string.
159 *
160 * @note This function acts as a convenience wrapper around std::to_string. The
161 * latter is missing in some Android toolchains.
162 *
163 * @param[in] value Value to be converted to string.
164 *
165 * @return String representation of @p value.
166 */
167template <typename T>
168std::string to_string(T &&value)
169{
170 return ::std::to_string(std::forward<T>(value));
171}
172
173/** Convert string values to integer.
174 *
175 * @note This function acts as a convenience wrapper around std::stoi. The
176 * latter is missing in some Android toolchains.
177 *
178 * @param[in] args Arguments forwarded to std::stoi.
179 *
180 * @return Integer representation of input string.
181 */
182template <typename... Ts>
183int stoi(Ts &&... args)
184{
185 return ::std::stoi(std::forward<Ts>(args)...);
186}
187
188/** Convert string values to unsigned long.
189 *
190 * @note This function acts as a convenience wrapper around std::stoul. The
191 * latter is missing in some Android toolchains.
192 *
193 * @param[in] args Arguments forwarded to std::stoul.
194 *
195 * @return Unsigned long representation of input string.
196 */
197template <typename... Ts>
198int stoul(Ts &&... args)
199{
200 return ::std::stoul(std::forward<Ts>(args)...);
201}
202
203/** Convert string values to float.
204 *
205 * @note This function acts as a convenience wrapper around std::stof. The
206 * latter is missing in some Android toolchains.
207 *
208 * @param[in] args Arguments forwarded to std::stof.
209 *
210 * @return Float representation of input string.
211 */
212template <typename... Ts>
213int stof(Ts &&... args)
214{
215 return ::std::stof(std::forward<Ts>(args)...);
216}
217
218/** Round floating-point value with half value rounding away from zero.
219 *
220 * @note This function implements the same behaviour as std::round except that it doesn't
221 * support Integral type. The latter is not in the namespace std in some Android toolchains.
222 *
223 * @param[in] value floating-point value to be rounded.
224 *
225 * @return Floating-point value of rounded @p value.
226 */
227template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
228inline T round(T value)
229{
230 return std::round(value);
231}
232
233/** Truncate floating-point value.
234 *
235 * @note This function implements the same behaviour as std::truncate except that it doesn't
236 * support Integral type. The latter is not in the namespace std in some Android toolchains.
237 *
238 * @param[in] value floating-point value to be truncated.
239 *
240 * @return Floating-point value of truncated @p value.
241 */
242template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
243inline T trunc(T value)
244{
245 return std::trunc(value);
246}
247
248/** Composes a floating point value with the magnitude of @p x and the sign of @p y.
249 *
250 * @note This function implements the same behaviour as std::copysign except that it doesn't
251 * support Integral type. The latter is not in the namespace std in some Android toolchains.
252 *
253 * @param[in] x value that contains the magnitued to be used in constructing the result.
254 * @param[in] y value that contains the sign to be used in constructin the result.
255 *
256 * @return Floating-point value with magnitude of @p x and sign of @p y.
257 */
258template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
259inline T copysign(T x, T y)
260{
261 return std::copysign(x, y);
262}
263#endif
264} // namespace cpp11
265
266namespace cpp14
267{
268/** make_unqiue is missing in CPP11. Reimplement it according to the standard
269 * proposal.
270 */
271template <class T>
272struct _Unique_if
273{
274 typedef std::unique_ptr<T> _Single_object;
275};
276
277template <class T>
278struct _Unique_if<T[]>
279{
280 typedef std::unique_ptr<T[]> _Unknown_bound;
281};
282
283template <class T, size_t N>
284struct _Unique_if<T[N]>
285{
286 typedef void _Known_bound;
287};
288
289template <class T, class... Args>
290typename _Unique_if<T>::_Single_object
291make_unique(Args &&... args)
292{
293 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
294}
295
296template <class T>
297typename _Unique_if<T>::_Unknown_bound
298make_unique(size_t n)
299{
300 typedef typename std::remove_extent<T>::type U;
301 return std::unique_ptr<T>(new U[n]());
302}
303
304template <class T, class... Args>
305typename _Unique_if<T>::_Known_bound
306make_unique(Args &&...) = delete;
307} // namespace cpp14
308} // namespace support
309} // namespace arm_compute
310#endif