Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 1 | /* |
Pablo Tello | 826c769 | 2018-01-16 11:41:12 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 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> |
Pablo Tello | 65f9982 | 2018-05-24 11:40:15 +0100 | [diff] [blame] | 28 | #include <cassert> |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 29 | #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 Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 38 | #include "support/Half.h" |
| 39 | |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace support |
| 43 | { |
| 44 | namespace cpp11 |
| 45 | { |
Pablo Tello | 65f9982 | 2018-05-24 11:40:15 +0100 | [diff] [blame] | 46 | enum 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 | */ |
| 63 | inline 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 | */ |
| 88 | inline 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 Spyrou | 07781ac | 2017-08-31 15:11:41 +0100 | [diff] [blame] | 102 | #if(__ANDROID__ || BARE_METAL) |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 103 | /** 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 | */ |
| 112 | template <typename T, typename std::enable_if<std::is_arithmetic<typename std::decay<T>::type>::value, int>::type = 0> |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 113 | inline std::string to_string(T && value) |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 114 | { |
| 115 | std::stringstream stream; |
| 116 | stream << std::forward<T>(value); |
| 117 | return stream.str(); |
| 118 | } |
| 119 | |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 120 | /** 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 | */ |
| 129 | inline 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 | */ |
| 146 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 147 | inline 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 | */ |
| 161 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 162 | inline 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 Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 172 | * @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 Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 174 | * |
| 175 | * @return Floating-point value with magnitude of @p x and sign of @p y. |
| 176 | */ |
| 177 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 178 | inline T copysign(T x, T y) |
| 179 | { |
| 180 | return ::copysign(x, y); |
| 181 | } |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 182 | |
| 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 | */ |
| 194 | template <typename... Ts> |
| 195 | inline 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 Spyrou | 07781ac | 2017-08-31 15:11:41 +0100 | [diff] [blame] | 199 | #else /* (__ANDROID__ || BARE_METAL) */ |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 200 | /** 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 | */ |
| 209 | template <typename T> |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 210 | inline std::string to_string(T &&value) |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 211 | { |
| 212 | return ::std::to_string(std::forward<T>(value)); |
| 213 | } |
| 214 | |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 215 | /** 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 | */ |
| 224 | template <typename... Ts> |
| 225 | int 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 | */ |
| 239 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 240 | inline T round(T value) |
| 241 | { |
Pablo Tello | 826c769 | 2018-01-16 11:41:12 +0000 | [diff] [blame] | 242 | //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 Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 244 | } |
| 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 | */ |
| 255 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 256 | inline 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 Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 266 | * @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 Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 268 | * |
| 269 | * @return Floating-point value with magnitude of @p x and sign of @p y. |
| 270 | */ |
| 271 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 272 | inline T copysign(T x, T y) |
| 273 | { |
| 274 | return std::copysign(x, y); |
| 275 | } |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 276 | |
| 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 | */ |
| 288 | template <typename... Ts> |
| 289 | inline 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 Spyrou | 07781ac | 2017-08-31 15:11:41 +0100 | [diff] [blame] | 293 | #endif /* (__ANDROID__ || BARE_METAL) */ |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 294 | |
| 295 | inline std::string to_string(bool value) |
| 296 | { |
| 297 | std::stringstream str; |
| 298 | str << std::boolalpha << value; |
| 299 | return str.str(); |
| 300 | } |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 301 | |
| 302 | // std::align is missing in GCC 4.9 |
| 303 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57350 |
| 304 | inline 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 Barbier | 3a6163e | 2018-08-10 17:36:36 +0100 | [diff] [blame] | 318 | // std::numeric_limits<T>::lowest |
| 319 | template <typename T> |
| 320 | inline T lowest() |
| 321 | { |
| 322 | return std::numeric_limits<T>::lowest(); |
| 323 | } |
| 324 | |
| 325 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 326 | template <> |
| 327 | inline __fp16 lowest<__fp16>() |
| 328 | { |
| 329 | return std::numeric_limits<half_float::half>::lowest(); |
| 330 | } |
| 331 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 332 | |
| 333 | // std::isfinite |
| 334 | template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type> |
| 335 | inline bool isfinite(T value) |
| 336 | { |
| 337 | return std::isfinite(value); |
| 338 | } |
| 339 | |
| 340 | inline bool isfinite(half_float::half value) |
| 341 | { |
| 342 | return half_float::isfinite(value); |
| 343 | } |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 344 | } // namespace cpp11 |
| 345 | |
| 346 | namespace cpp14 |
| 347 | { |
Georgios Pinitas | 84b51ad | 2017-11-07 13:24:57 +0000 | [diff] [blame] | 348 | /** make_unique is missing in CPP11. Re-implement it according to the standard proposal. */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 349 | |
| 350 | /**<Template for single object */ |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 351 | template <class T> |
| 352 | struct _Unique_if |
| 353 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 354 | typedef std::unique_ptr<T> _Single_object; /**< Single object type */ |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 355 | }; |
| 356 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 357 | /** Template for array */ |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 358 | template <class T> |
| 359 | struct _Unique_if<T[]> |
| 360 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 361 | typedef std::unique_ptr<T[]> _Unknown_bound; /**< Array type */ |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 362 | }; |
| 363 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 364 | /** Template for array with known bounds (to throw an error). |
| 365 | * |
| 366 | * @note this is intended to never be hit. |
| 367 | */ |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 368 | template <class T, size_t N> |
| 369 | struct _Unique_if<T[N]> |
| 370 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 371 | typedef void _Known_bound; /**< Should never be used */ |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 372 | }; |
| 373 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 374 | /** 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 Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 380 | template <class T, class... Args> |
| 381 | typename _Unique_if<T>::_Single_object |
| 382 | make_unique(Args &&... args) |
| 383 | { |
| 384 | return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
| 385 | } |
| 386 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 387 | /** 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 Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 393 | template <class T> |
| 394 | typename _Unique_if<T>::_Unknown_bound |
| 395 | make_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 Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 401 | /** It is invalid to attempt to make_unique an array with known bounds. */ |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 402 | template <class T, class... Args> |
| 403 | typename _Unique_if<T>::_Known_bound |
| 404 | make_unique(Args &&...) = delete; |
| 405 | } // namespace cpp14 |
| 406 | } // namespace support |
| 407 | } // namespace arm_compute |
Moritz Pflanzer | fc95ed2 | 2017-07-05 11:07:07 +0100 | [diff] [blame] | 408 | #endif /* ARM_COMPUTE_TEST_TOOLCHAINSUPPORT */ |