diff options
Diffstat (limited to 'support/ToolchainSupport.h')
-rw-r--r-- | support/ToolchainSupport.h | 108 |
1 files changed, 80 insertions, 28 deletions
diff --git a/support/ToolchainSupport.h b/support/ToolchainSupport.h index 8235358c39..accbb643c2 100644 --- a/support/ToolchainSupport.h +++ b/support/ToolchainSupport.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2020 ARM Limited. + * Copyright (c) 2017-2023 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -21,8 +21,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef ARM_COMPUTE_SUPPORT_TOOLCHAINSUPPORT -#define ARM_COMPUTE_SUPPORT_TOOLCHAINSUPPORT +#ifndef ACL_SUPPORT_TOOLCHAINSUPPORT_H +#define ACL_SUPPORT_TOOLCHAINSUPPORT_H + +#include "support/Bfloat16.h" +#include "support/Half.h" #include <cassert> #include <cmath> @@ -33,20 +36,21 @@ #include <string> #include <type_traits> -#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC -#include <arm_neon.h> -#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC - -#include "support/Bfloat16.h" -#include "support/Half.h" +#ifndef M_PI +#define M_PI (3.14159265358979323846) +#endif // M_PI namespace arm_compute { +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC +typedef __fp16 float16_t; +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + namespace support { namespace cpp11 { -#if(__ANDROID__ || BARE_METAL) +#if (__ANDROID__ || BARE_METAL) template <typename T> inline T nearbyint(T value) { @@ -68,6 +72,21 @@ inline T round(T value) return ::round(value); } +/** Round floating-point value with half value rounding away from zero and cast to long + * + * @note This function implements the same behaviour as std::lround except that it doesn't + * support Integral type. The latter is not in the namespace std in some Android toolchains. + * + * @param[in] value floating-point value to be rounded. + * + * @return Floating-point value of rounded @p value casted to long + */ +template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> +inline long lround(T value) +{ + return ::lround(value); +} + /** Truncate floating-point value. * * @note This function implements the same behaviour as std::truncate except that it doesn't @@ -110,11 +129,12 @@ inline T copysign(T x, T y) * * @return Result floating point value equal to (x*y) + z.c */ -template < typename T, typename = typename std::enable_if < std::is_floating_point<T>::value +template <typename T, + typename = typename std::enable_if<std::is_floating_point<T>::value #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC - || std::is_same<T, float16_t>::value + || std::is_same<T, float16_t>::value #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC - >::type > + >::type> inline T fma(T x, T y, T z) { return ::fma(x, y, z); @@ -124,15 +144,15 @@ inline T fma(T x, T y, T z) * and writes the result to a character string buffer. * * @param[in] s Pointer to a character string to write to - * @param[in] n Up to buf_size - 1 characters may be written, plus the null terminator - * @param[in] fmt Pointer to a null-terminated multibyte string specifying how to interpret the data. + * @param[in] n Up to buf_size - 1 characters may be written, plus the null ending character + * @param[in] fmt Pointer to a null-ended multibyte string specifying how to interpret the data. * @param[in] args Arguments forwarded to snprintf. * * @return Number of characters that would have been written for a sufficiently large buffer - * if successful (not including the terminating null character), or a negative value if an error occurred. + * if successful (not including the ending null character), or a negative value if an error occurred. */ template <typename... Ts> -inline int snprintf(char *s, size_t n, const char *fmt, Ts &&... args) +inline int snprintf(char *s, size_t n, const char *fmt, Ts &&...args) { return ::snprintf(s, n, fmt, std::forward<Ts>(args)...); } @@ -164,8 +184,22 @@ inline T nearbyint(T value) template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> inline T round(T value) { - //Workaround Valgrind's mismatches: when running from Valgrind the call to std::round(-4.500000) == -4.000000 instead of 5.00000 - return (value < 0.f) ? static_cast<int>(value - 0.5f) : static_cast<int>(value + 0.5f); + return std::round(value); +} + +/** Round floating-point value with half value rounding away from zero and cast to long + * + * @note This function implements the same behaviour as std::lround except that it doesn't + * support Integral type. The latter is not in the namespace std in some Android toolchains. + * + * @param[in] value floating-point value to be rounded. + * + * @return Floating-point value of rounded @p value casted to long + */ +template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type> +inline long lround(T value) +{ + return std::lround(value); } /** Truncate floating-point value. @@ -210,11 +244,12 @@ inline T copysign(T x, T y) * * @return Result floating point value equal to (x*y) + z. */ -template < typename T, typename = typename std::enable_if < std::is_floating_point<T>::value +template <typename T, + typename = typename std::enable_if<std::is_floating_point<T>::value #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC - || std::is_same<T, float16_t>::value + || std::is_same<T, float16_t>::value #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC - >::type > + >::type> inline T fma(T x, T y, T z) { return std::fma(x, y, z); @@ -224,15 +259,15 @@ inline T fma(T x, T y, T z) * and writes the result to a character string buffer. * * @param[in] s Pointer to a character string to write to - * @param[in] n Up to buf_size - 1 characters may be written, plus the null terminator - * @param[in] fmt Pointer to a null-terminated multibyte string specifying how to interpret the data. + * @param[in] n Up to buf_size - 1 characters may be written, plus the null ending character + * @param[in] fmt Pointer to a null-ended multibyte string specifying how to interpret the data. * @param[in] args Arguments forwarded to std::snprintf. * * @return Number of characters that would have been written for a sufficiently large buffer - * if successful (not including the terminating null character), or a negative value if an error occurred. + * if successful (not including the ending null character), or a negative value if an error occurred. */ template <typename... Ts> -inline int snprintf(char *s, std::size_t n, const char *fmt, Ts &&... args) +inline int snprintf(char *s, std::size_t n, const char *fmt, Ts &&...args) { return std::snprintf(s, n, fmt, std::forward<Ts>(args)...); } @@ -263,7 +298,7 @@ inline bfloat16 lowest<bfloat16>() template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type> inline bool isfinite(T value) { - return std::isfinite(value); + return std::isfinite(static_cast<double>(value)); } inline bool isfinite(half_float::half value) @@ -275,7 +310,24 @@ inline bool isfinite(bfloat16 value) { return std::isfinite(float(value)); } + +// std::signbit +template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type> +inline bool signbit(T value) +{ + return std::signbit(static_cast<double>(value)); +} + +inline bool signbit(half_float::half value) +{ + return half_float::signbit(value); +} + +inline bool signbit(bfloat16 value) +{ + return std::signbit(float(value)); +} } // namespace cpp11 } // namespace support } // namespace arm_compute -#endif /* ARM_COMPUTE_SUPPORT_TOOLCHAINSUPPORT */ +#endif // ACL_SUPPORT_TOOLCHAINSUPPORT_H |