2 * Copyright (c) 2017 ARM Limited.
4 * SPDX-License-Identifier: MIT
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
24 #ifndef ARM_COMPUTE_TEST_TOOLCHAINSUPPORT
25 #define ARM_COMPUTE_TEST_TOOLCHAINSUPPORT
35 #include <type_traits>
43 #if(__ANDROID__ || BARE_METAL)
44 /** Convert integer and float values to string.
46 * @note This function implements the same behaviour as std::to_string. The
47 * latter is missing in some Android toolchains.
49 * @param[in] value Value to be converted to string.
51 * @return String representation of @p value.
53 template <typename T, typename std::enable_if<std::is_arithmetic<typename std::decay<T>::type>::value, int>::type = 0>
54 inline std::string to_string(T && value)
56 std::stringstream stream;
57 stream << std::forward<T>(value);
61 /** Convert string values to integer.
63 * @note This function implements the same behaviour as std::stoi. The latter
64 * is missing in some Android toolchains.
66 * @param[in] str String to be converted to int.
68 * @return Integer representation of @p str.
70 inline int stoi(const std::string &str)
72 std::stringstream stream(str);
78 /** Convert string values to unsigned long.
80 * @note This function implements the same behaviour as std::stoul. The latter
81 * is missing in some Android toolchains.
83 * @param[in] str String to be converted to unsigned long.
85 * @return Unsigned long representation of @p str.
87 inline unsigned long stoul(const std::string &str)
89 std::stringstream stream(str);
90 unsigned long value = 0;
95 /** Convert string values to float.
97 * @note This function implements the same behaviour as std::stof. The latter
98 * is missing in some Android toolchains.
100 * @param[in] str String to be converted to float.
102 * @return Float representation of @p str.
104 inline float stof(const std::string &str)
106 std::stringstream stream(str);
112 /** Round floating-point value with half value rounding away from zero.
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.
117 * @param[in] value floating-point value to be rounded.
119 * @return Floating-point value of rounded @p value.
121 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
122 inline T round(T value)
124 return ::round(value);
127 /** Truncate floating-point value.
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.
132 * @param[in] value floating-point value to be truncated.
134 * @return Floating-point value of truncated @p value.
136 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
137 inline T trunc(T value)
139 return ::trunc(value);
142 /** Composes a floating point value with the magnitude of @p x and the sign of @p y.
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.
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.
150 * @return Floating-point value with magnitude of @p x and sign of @p y.
152 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
153 inline T copysign(T x, T y)
155 return ::copysign(x, y);
157 #else /* (__ANDROID__ || BARE_METAL) */
158 /** Convert integer and float values to string.
160 * @note This function acts as a convenience wrapper around std::to_string. The
161 * latter is missing in some Android toolchains.
163 * @param[in] value Value to be converted to string.
165 * @return String representation of @p value.
167 template <typename T>
168 inline std::string to_string(T &&value)
170 return ::std::to_string(std::forward<T>(value));
173 /** Convert string values to integer.
175 * @note This function acts as a convenience wrapper around std::stoi. The
176 * latter is missing in some Android toolchains.
178 * @param[in] args Arguments forwarded to std::stoi.
180 * @return Integer representation of input string.
182 template <typename... Ts>
183 int stoi(Ts &&... args)
185 return ::std::stoi(std::forward<Ts>(args)...);
188 /** Convert string values to unsigned long.
190 * @note This function acts as a convenience wrapper around std::stoul. The
191 * latter is missing in some Android toolchains.
193 * @param[in] args Arguments forwarded to std::stoul.
195 * @return Unsigned long representation of input string.
197 template <typename... Ts>
198 int stoul(Ts &&... args)
200 return ::std::stoul(std::forward<Ts>(args)...);
203 /** Convert string values to float.
205 * @note This function acts as a convenience wrapper around std::stof. The
206 * latter is missing in some Android toolchains.
208 * @param[in] args Arguments forwarded to std::stof.
210 * @return Float representation of input string.
212 template <typename... Ts>
213 int stof(Ts &&... args)
215 return ::std::stof(std::forward<Ts>(args)...);
218 /** Round floating-point value with half value rounding away from zero.
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.
223 * @param[in] value floating-point value to be rounded.
225 * @return Floating-point value of rounded @p value.
227 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
228 inline T round(T value)
230 return std::round(value);
233 /** Truncate floating-point value.
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.
238 * @param[in] value floating-point value to be truncated.
240 * @return Floating-point value of truncated @p value.
242 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
243 inline T trunc(T value)
245 return std::trunc(value);
248 /** Composes a floating point value with the magnitude of @p x and the sign of @p y.
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.
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.
256 * @return Floating-point value with magnitude of @p x and sign of @p y.
258 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
259 inline T copysign(T x, T y)
261 return std::copysign(x, y);
263 #endif /* (__ANDROID__ || BARE_METAL) */
265 inline std::string to_string(bool value)
267 std::stringstream str;
268 str << std::boolalpha << value;
272 // std::align is missing in GCC 4.9
273 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57350
274 inline void *align(std::size_t alignment, std::size_t size, void *&ptr, std::size_t &space)
276 std::uintptr_t pn = reinterpret_cast<std::uintptr_t>(ptr);
277 std::uintptr_t aligned = (pn + alignment - 1) & -alignment;
278 std::size_t padding = aligned - pn;
279 if(space < size + padding)
286 return ptr = reinterpret_cast<void *>(aligned);
292 /** make_unqiue is missing in CPP11. Reimplement it according to the standard
298 typedef std::unique_ptr<T> _Single_object;
302 struct _Unique_if<T[]>
304 typedef std::unique_ptr<T[]> _Unknown_bound;
307 template <class T, size_t N>
308 struct _Unique_if<T[N]>
310 typedef void _Known_bound;
313 template <class T, class... Args>
314 typename _Unique_if<T>::_Single_object
315 make_unique(Args &&... args)
317 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
321 typename _Unique_if<T>::_Unknown_bound
322 make_unique(size_t n)
324 typedef typename std::remove_extent<T>::type U;
325 return std::unique_ptr<T>(new U[n]());
328 template <class T, class... Args>
329 typename _Unique_if<T>::_Known_bound
330 make_unique(Args &&...) = delete;
332 } // namespace support
333 } // namespace arm_compute
334 #endif /* ARM_COMPUTE_TEST_TOOLCHAINSUPPORT */