Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 7d9a78e | 2023-05-31 13:18:33 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2023 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 | */ |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_SUPPORT_TOOLCHAINSUPPORT |
| 25 | #define ARM_COMPUTE_SUPPORT_TOOLCHAINSUPPORT |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 26 | |
Pablo Tello | 65f9982 | 2018-05-24 11:40:15 +0100 | [diff] [blame] | 27 | #include <cassert> |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 28 | #include <cmath> |
| 29 | #include <cstddef> |
| 30 | #include <limits> |
| 31 | #include <memory> |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 32 | #include <sstream> |
| 33 | #include <string> |
| 34 | #include <type_traits> |
| 35 | |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 36 | #include "support/Bfloat16.h" |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 37 | #include "support/Half.h" |
| 38 | |
SiCongLi | 410e21e | 2020-12-11 15:07:53 +0000 | [diff] [blame] | 39 | #ifndef M_PI |
| 40 | #define M_PI (3.14159265358979323846) |
| 41 | #endif // M_PI |
| 42 | |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 43 | namespace arm_compute |
| 44 | { |
Matthew Bentham | 7d9a78e | 2023-05-31 13:18:33 +0000 | [diff] [blame] | 45 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 46 | typedef __fp16 float16_t; |
| 47 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 48 | |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 49 | namespace support |
| 50 | { |
| 51 | namespace cpp11 |
| 52 | { |
Michalis Spyrou | 07781ac | 2017-08-31 15:11:41 +0100 | [diff] [blame] | 53 | #if(__ANDROID__ || BARE_METAL) |
Michalis Spyrou | e6bcb5b | 2019-06-07 11:47:16 +0100 | [diff] [blame] | 54 | template <typename T> |
| 55 | inline T nearbyint(T value) |
| 56 | { |
Michalis Spyrou | 748a7c8 | 2019-10-07 13:00:44 +0100 | [diff] [blame] | 57 | return static_cast<T>(::nearbyint(value)); |
Michalis Spyrou | e6bcb5b | 2019-06-07 11:47:16 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 60 | /** Round floating-point value with half value rounding away from zero. |
| 61 | * |
| 62 | * @note This function implements the same behaviour as std::round except that it doesn't |
| 63 | * support Integral type. The latter is not in the namespace std in some Android toolchains. |
| 64 | * |
| 65 | * @param[in] value floating-point value to be rounded. |
| 66 | * |
| 67 | * @return Floating-point value of rounded @p value. |
| 68 | */ |
| 69 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 70 | inline T round(T value) |
| 71 | { |
| 72 | return ::round(value); |
| 73 | } |
| 74 | |
Giorgio Arena | 433ea49 | 2021-05-26 15:32:50 +0100 | [diff] [blame] | 75 | /** Round floating-point value with half value rounding away from zero and cast to long |
| 76 | * |
| 77 | * @note This function implements the same behaviour as std::lround except that it doesn't |
| 78 | * support Integral type. The latter is not in the namespace std in some Android toolchains. |
| 79 | * |
| 80 | * @param[in] value floating-point value to be rounded. |
| 81 | * |
| 82 | * @return Floating-point value of rounded @p value casted to long |
| 83 | */ |
| 84 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 85 | inline long lround(T value) |
| 86 | { |
| 87 | return ::lround(value); |
| 88 | } |
| 89 | |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 90 | /** Truncate floating-point value. |
| 91 | * |
| 92 | * @note This function implements the same behaviour as std::truncate except that it doesn't |
| 93 | * support Integral type. The latter is not in the namespace std in some Android toolchains. |
| 94 | * |
| 95 | * @param[in] value floating-point value to be truncated. |
| 96 | * |
| 97 | * @return Floating-point value of truncated @p value. |
| 98 | */ |
| 99 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 100 | inline T trunc(T value) |
| 101 | { |
| 102 | return ::trunc(value); |
| 103 | } |
| 104 | |
| 105 | /** Composes a floating point value with the magnitude of @p x and the sign of @p y. |
| 106 | * |
| 107 | * @note This function implements the same behaviour as std::copysign except that it doesn't |
| 108 | * support Integral type. The latter is not in the namespace std in some Android toolchains. |
| 109 | * |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 110 | * @param[in] x value that contains the magnitude to be used in constructing the result. |
| 111 | * @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] | 112 | * |
| 113 | * @return Floating-point value with magnitude of @p x and sign of @p y. |
| 114 | */ |
| 115 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 116 | inline T copysign(T x, T y) |
| 117 | { |
| 118 | return ::copysign(x, y); |
| 119 | } |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 120 | |
Georgios Pinitas | 1c29ffc | 2019-08-01 15:03:00 +0100 | [diff] [blame] | 121 | /** Computes (x*y) + z as if to infinite precision and rounded only once to fit the result type. |
| 122 | * |
| 123 | * @note This function implements the same behaviour as std::fma except that it doesn't |
| 124 | * support Integral type. The latter is not in the namespace std in some Android toolchains. |
| 125 | * |
| 126 | * @param[in] x floating-point value |
| 127 | * @param[in] y floating-point value |
| 128 | * @param[in] z floating-point value |
| 129 | * |
| 130 | * @return Result floating point value equal to (x*y) + z.c |
| 131 | */ |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 132 | template < typename T, typename = typename std::enable_if < std::is_floating_point<T>::value |
| 133 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 134 | || std::is_same<T, float16_t>::value |
| 135 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 136 | >::type > |
Georgios Pinitas | 1c29ffc | 2019-08-01 15:03:00 +0100 | [diff] [blame] | 137 | inline T fma(T x, T y, T z) |
| 138 | { |
| 139 | return ::fma(x, y, z); |
| 140 | } |
| 141 | |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 142 | /** Loads the data from the given location, converts them to character string equivalents |
| 143 | * and writes the result to a character string buffer. |
| 144 | * |
| 145 | * @param[in] s Pointer to a character string to write to |
ramelg01 | b2eba7f | 2021-12-23 08:32:08 +0000 | [diff] [blame] | 146 | * @param[in] n Up to buf_size - 1 characters may be written, plus the null ending character |
| 147 | * @param[in] fmt Pointer to a null-ended multibyte string specifying how to interpret the data. |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 148 | * @param[in] args Arguments forwarded to snprintf. |
| 149 | * |
| 150 | * @return Number of characters that would have been written for a sufficiently large buffer |
ramelg01 | b2eba7f | 2021-12-23 08:32:08 +0000 | [diff] [blame] | 151 | * if successful (not including the ending null character), or a negative value if an error occurred. |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 152 | */ |
| 153 | template <typename... Ts> |
| 154 | inline int snprintf(char *s, size_t n, const char *fmt, Ts &&... args) |
| 155 | { |
| 156 | return ::snprintf(s, n, fmt, std::forward<Ts>(args)...); |
| 157 | } |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 158 | #else /* (__ANDROID__ || BARE_METAL) */ |
Michalis Spyrou | e6bcb5b | 2019-06-07 11:47:16 +0100 | [diff] [blame] | 159 | /** Rounds the floating-point argument arg to an integer value in floating-point format, using the current rounding mode. |
| 160 | * |
| 161 | * @note This function acts as a convenience wrapper around std::nearbyint. The |
| 162 | * latter is missing in some Android toolchains. |
| 163 | * |
| 164 | * @param[in] value Value to be rounded. |
| 165 | * |
| 166 | * @return The rounded value. |
| 167 | */ |
| 168 | template <typename T> |
| 169 | inline T nearbyint(T value) |
| 170 | { |
Michalis Spyrou | 748a7c8 | 2019-10-07 13:00:44 +0100 | [diff] [blame] | 171 | return static_cast<T>(std::nearbyint(value)); |
Michalis Spyrou | e6bcb5b | 2019-06-07 11:47:16 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 174 | /** Round floating-point value with half value rounding away from zero. |
| 175 | * |
| 176 | * @note This function implements the same behaviour as std::round except that it doesn't |
| 177 | * support Integral type. The latter is not in the namespace std in some Android toolchains. |
| 178 | * |
| 179 | * @param[in] value floating-point value to be rounded. |
| 180 | * |
| 181 | * @return Floating-point value of rounded @p value. |
| 182 | */ |
| 183 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 184 | inline T round(T value) |
| 185 | { |
Pablo Tello | 826c769 | 2018-01-16 11:41:12 +0000 | [diff] [blame] | 186 | //Workaround Valgrind's mismatches: when running from Valgrind the call to std::round(-4.500000) == -4.000000 instead of 5.00000 |
| 187 | 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] | 188 | } |
| 189 | |
Giorgio Arena | 433ea49 | 2021-05-26 15:32:50 +0100 | [diff] [blame] | 190 | /** Round floating-point value with half value rounding away from zero and cast to long |
| 191 | * |
| 192 | * @note This function implements the same behaviour as std::lround except that it doesn't |
| 193 | * support Integral type. The latter is not in the namespace std in some Android toolchains. |
| 194 | * |
| 195 | * @param[in] value floating-point value to be rounded. |
| 196 | * |
| 197 | * @return Floating-point value of rounded @p value casted to long |
| 198 | */ |
| 199 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 200 | inline long lround(T value) |
| 201 | { |
| 202 | return std::lround(value); |
| 203 | } |
| 204 | |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 205 | /** Truncate floating-point value. |
| 206 | * |
| 207 | * @note This function implements the same behaviour as std::truncate except that it doesn't |
| 208 | * support Integral type. The latter is not in the namespace std in some Android toolchains. |
| 209 | * |
| 210 | * @param[in] value floating-point value to be truncated. |
| 211 | * |
| 212 | * @return Floating-point value of truncated @p value. |
| 213 | */ |
| 214 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 215 | inline T trunc(T value) |
| 216 | { |
| 217 | return std::trunc(value); |
| 218 | } |
| 219 | |
| 220 | /** Composes a floating point value with the magnitude of @p x and the sign of @p y. |
| 221 | * |
| 222 | * @note This function implements the same behaviour as std::copysign except that it doesn't |
| 223 | * support Integral type. The latter is not in the namespace std in some Android toolchains. |
| 224 | * |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 225 | * @param[in] x value that contains the magnitude to be used in constructing the result. |
| 226 | * @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] | 227 | * |
| 228 | * @return Floating-point value with magnitude of @p x and sign of @p y. |
| 229 | */ |
| 230 | template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> |
| 231 | inline T copysign(T x, T y) |
| 232 | { |
| 233 | return std::copysign(x, y); |
| 234 | } |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 235 | |
Georgios Pinitas | 1c29ffc | 2019-08-01 15:03:00 +0100 | [diff] [blame] | 236 | /** Computes (x*y) + z as if to infinite precision and rounded only once to fit the result type. |
| 237 | * |
| 238 | * @note This function implements the same behaviour as std::fma except that it doesn't |
| 239 | * support Integral type. The latter is not in the namespace std in some Android toolchains. |
| 240 | * |
| 241 | * @param[in] x floating-point value |
| 242 | * @param[in] y floating-point value |
| 243 | * @param[in] z floating-point value |
| 244 | * |
| 245 | * @return Result floating point value equal to (x*y) + z. |
| 246 | */ |
Giorgio Arena | d93e263 | 2019-10-15 11:09:33 +0100 | [diff] [blame] | 247 | template < typename T, typename = typename std::enable_if < std::is_floating_point<T>::value |
| 248 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 249 | || std::is_same<T, float16_t>::value |
| 250 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 251 | >::type > |
Georgios Pinitas | 1c29ffc | 2019-08-01 15:03:00 +0100 | [diff] [blame] | 252 | inline T fma(T x, T y, T z) |
| 253 | { |
| 254 | return std::fma(x, y, z); |
| 255 | } |
| 256 | |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 257 | /** Loads the data from the given location, converts them to character string equivalents |
| 258 | * and writes the result to a character string buffer. |
| 259 | * |
| 260 | * @param[in] s Pointer to a character string to write to |
ramelg01 | b2eba7f | 2021-12-23 08:32:08 +0000 | [diff] [blame] | 261 | * @param[in] n Up to buf_size - 1 characters may be written, plus the null ending character |
| 262 | * @param[in] fmt Pointer to a null-ended multibyte string specifying how to interpret the data. |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 263 | * @param[in] args Arguments forwarded to std::snprintf. |
| 264 | * |
| 265 | * @return Number of characters that would have been written for a sufficiently large buffer |
ramelg01 | b2eba7f | 2021-12-23 08:32:08 +0000 | [diff] [blame] | 266 | * if successful (not including the ending null character), or a negative value if an error occurred. |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 267 | */ |
| 268 | template <typename... Ts> |
| 269 | inline int snprintf(char *s, std::size_t n, const char *fmt, Ts &&... args) |
| 270 | { |
| 271 | return std::snprintf(s, n, fmt, std::forward<Ts>(args)...); |
| 272 | } |
Michalis Spyrou | 07781ac | 2017-08-31 15:11:41 +0100 | [diff] [blame] | 273 | #endif /* (__ANDROID__ || BARE_METAL) */ |
Moritz Pflanzer | 572ade7 | 2017-07-21 17:36:33 +0100 | [diff] [blame] | 274 | |
Anthony Barbier | 3a6163e | 2018-08-10 17:36:36 +0100 | [diff] [blame] | 275 | // std::numeric_limits<T>::lowest |
| 276 | template <typename T> |
| 277 | inline T lowest() |
| 278 | { |
| 279 | return std::numeric_limits<T>::lowest(); |
| 280 | } |
| 281 | |
| 282 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 283 | template <> |
| 284 | inline __fp16 lowest<__fp16>() |
| 285 | { |
| 286 | return std::numeric_limits<half_float::half>::lowest(); |
| 287 | } |
| 288 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 289 | |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 290 | template <> |
| 291 | inline bfloat16 lowest<bfloat16>() |
| 292 | { |
| 293 | return bfloat16::lowest(); |
| 294 | } |
| 295 | |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 296 | // std::isfinite |
| 297 | template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type> |
| 298 | inline bool isfinite(T value) |
| 299 | { |
Pablo Tello | 29cab36 | 2022-03-10 17:05:34 +0000 | [diff] [blame] | 300 | return std::isfinite(static_cast<double>(value)); |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | inline bool isfinite(half_float::half value) |
| 304 | { |
| 305 | return half_float::isfinite(value); |
| 306 | } |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 307 | |
| 308 | inline bool isfinite(bfloat16 value) |
| 309 | { |
| 310 | return std::isfinite(float(value)); |
| 311 | } |
SiCongLi | b99e54e | 2022-01-05 12:18:03 +0000 | [diff] [blame] | 312 | |
| 313 | // std::signbit |
| 314 | template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type> |
| 315 | inline bool signbit(T value) |
| 316 | { |
Pablo Tello | 29cab36 | 2022-03-10 17:05:34 +0000 | [diff] [blame] | 317 | return std::signbit(static_cast<double>(value)); |
SiCongLi | b99e54e | 2022-01-05 12:18:03 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | inline bool signbit(half_float::half value) |
| 321 | { |
| 322 | return half_float::signbit(value); |
| 323 | } |
| 324 | |
| 325 | inline bool signbit(bfloat16 value) |
| 326 | { |
| 327 | return std::signbit(float(value)); |
| 328 | } |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 329 | } // namespace cpp11 |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 330 | } // namespace support |
| 331 | } // namespace arm_compute |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 332 | #endif /* ARM_COMPUTE_SUPPORT_TOOLCHAINSUPPORT */ |