aboutsummaryrefslogtreecommitdiff
path: root/arm_compute/core/utils
diff options
context:
space:
mode:
authorGeorgios Pinitas <georgios.pinitas@arm.com>2018-11-19 11:56:51 +0000
committerGeorgios Pinitas <georgios.pinitas@arm.com>2018-11-22 12:41:32 +0000
commit303f0dbebf631b3db00d9d64e71018abbbe9d4fe (patch)
tree631e70c9a8141f1262752829a64b3e33c7f1ee93 /arm_compute/core/utils
parent9d3a831d4131f8a8b37f127f11d36848d33e8496 (diff)
downloadComputeLibrary-303f0dbebf631b3db00d9d64e71018abbbe9d4fe.tar.gz
COMPMID-1718: Extend DepthConvert to support Cast
Change-Id: I6ee2c0b670727fc808fa636c53ddfaec3a0036c9
Diffstat (limited to 'arm_compute/core/utils')
-rw-r--r--arm_compute/core/utils/misc/Requires.h51
-rw-r--r--arm_compute/core/utils/misc/Rounding.h205
-rw-r--r--arm_compute/core/utils/misc/SaturateCast.h218
-rw-r--r--arm_compute/core/utils/misc/Traits.h47
-rw-r--r--arm_compute/core/utils/misc/Utility.h16
5 files changed, 521 insertions, 16 deletions
diff --git a/arm_compute/core/utils/misc/Requires.h b/arm_compute/core/utils/misc/Requires.h
new file mode 100644
index 0000000000..2852300bb1
--- /dev/null
+++ b/arm_compute/core/utils/misc/Requires.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef __ARM_COMPUTE_UTILS_REQUIRES_H__
+#define __ARM_COMPUTE_UTILS_REQUIRES_H__
+
+namespace arm_compute
+{
+namespace utils
+{
+namespace requires
+{
+// *INDENT-OFF*
+// clang-format off
+namespace detail
+{
+enum class enabler
+{
+};
+} // namespace arm_compute
+
+/** Requirements as template */
+#define REQUIRES_T(...) template <bool Cond = (__VA_ARGS__), typename std::enable_if<Cond, int>::type = 0>
+/** Requirements as template argument */
+#define REQUIRES_TA(...) typename = typename std::enable_if<(__VA_ARGS__), arm_compute::utils::requires::detail::enabler>::type
+// clang-format on
+// *INDENT-ON*
+} // namespace requires
+} // namespace utils
+} // namespace arm_compute
+#endif /*__ARM_COMPUTE_UTILS_REQUIRES_H__ */
diff --git a/arm_compute/core/utils/misc/Rounding.h b/arm_compute/core/utils/misc/Rounding.h
new file mode 100644
index 0000000000..3f86cc8db3
--- /dev/null
+++ b/arm_compute/core/utils/misc/Rounding.h
@@ -0,0 +1,205 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef __ARM_COMPUTE_UTILS_ROUNDING_H__
+#define __ARM_COMPUTE_UTILS_ROUNDING_H__
+
+#include "arm_compute/core/Error.h"
+#include "arm_compute/core/utils/misc/Requires.h"
+#include "arm_compute/core/utils/misc/Traits.h"
+#include "support/ToolchainSupport.h"
+
+#include <cmath>
+
+namespace arm_compute
+{
+namespace utils
+{
+namespace rounding
+{
+/** Rounding mode */
+enum class RoundingMode
+{
+ TO_ZERO, /**< Round towards zero */
+ AWAY_FROM_ZERO, /**< Round away from zero */
+ HALF_TO_ZERO, /**< Round half towards from zero */
+ HALF_AWAY_FROM_ZERO, /**< Round half away from zero */
+ HALF_UP, /**< Round half towards positive infinity */
+ HALF_DOWN, /**< Round half towards negative infinity */
+ HALF_EVEN /**< Round half towards nearest even */
+};
+
+/** Round floating-point value with round to zero
+ *
+ * @tparam T Parameter type. Should be of floating point type.
+ *
+ * @param[in] value floating-point value to be rounded.
+ *
+ * @return Floating-point value of rounded @p value.
+ */
+template <typename T, REQUIRES_TA(traits::is_floating_point<T>::value)>
+inline T round_to_zero(T value)
+{
+ T res = std::floor(std::fabs(value));
+ return (value < 0.f) ? -res : res;
+}
+
+/** Round floating-point value with round away from zero
+ *
+ * @tparam T Parameter type. Should be of floating point type.
+ *
+ * @param[in] value floating-point value to be rounded.
+ *
+ * @return Floating-point value of rounded @p value.
+ */
+template <typename T, REQUIRES_TA(traits::is_floating_point<T>::value)>
+inline T round_away_from_zero(T value)
+{
+ T res = std::ceil(std::fabs(value));
+ return (value < 0.f) ? -res : res;
+}
+
+/** Round floating-point value with half value rounding towards zero.
+ *
+ * @tparam T Parameter type. Should be of floating point type.
+ *
+ * @param[in] value floating-point value to be rounded.
+ *
+ * @return Floating-point value of rounded @p value.
+ */
+template <typename T, REQUIRES_TA(traits::is_floating_point<T>::value)>
+inline T round_half_to_zero(T value)
+{
+ T res = T(std::ceil(std::fabs(value) - 0.5f));
+ return (value < 0.f) ? -res : res;
+}
+
+/** Round floating-point value with half value rounding away from zero.
+ *
+ * @tparam T Parameter type. Should be of floating point type.
+ *
+ * @param[in] value floating-point value to be rounded.
+ *
+ * @return Floating-point value of rounded @p value.
+ */
+template <typename T, REQUIRES_TA(traits::is_floating_point<T>::value)>
+inline T round_half_away_from_zero(T value)
+{
+ T res = T(std::floor(std::fabs(value) + 0.5f));
+ return (value < 0.f) ? -res : res;
+}
+
+/** Round floating-point value with half value rounding to positive infinity.
+ *
+ * @tparam T Parameter type. Should be of floating point type.
+ *
+ * @param[in] value floating-point value to be rounded.
+ *
+ * @return Floating-point value of rounded @p value.
+ */
+template <typename T, REQUIRES_TA(traits::is_floating_point<T>::value)>
+inline T round_half_up(T value)
+{
+ return std::floor(value + 0.5f);
+}
+
+/** Round floating-point value with half value rounding to negative infinity.
+ *
+ * @tparam T Parameter type. Should be of floating point type.
+ *
+ * @param[in] value floating-point value to be rounded.
+ *
+ * @return Floating-point value of rounded @p value.
+ */
+template <typename T, REQUIRES_TA(traits::is_floating_point<T>::value)>
+inline T round_half_down(T value)
+{
+ return std::ceil(value - 0.5f);
+}
+
+/** Round floating-point value with half value rounding to nearest even.
+ *
+ * @tparam T Parameter type. Should be of floating point type.
+ *
+ * @param[in] value floating-point value to be rounded.
+ * @param[in] epsilon precision.
+ *
+ * @return Floating-point value of rounded @p value.
+ */
+template <typename T, REQUIRES_TA(traits::is_floating_point<T>::value)>
+inline T round_half_even(T value, T epsilon = std::numeric_limits<T>::epsilon())
+{
+ T positive_value = std::abs(value);
+ T ipart = 0;
+ std::modf(positive_value, &ipart);
+ // If 'value' is exactly halfway between two integers
+ if(std::abs(positive_value - (ipart + 0.5f)) < epsilon)
+ {
+ // If 'ipart' is even then return 'ipart'
+ if(std::fmod(ipart, 2.f) < epsilon)
+ {
+ return support::cpp11::copysign(ipart, value);
+ }
+ // Else return the nearest even integer
+ return support::cpp11::copysign(std::ceil(ipart + 0.5f), value);
+ }
+ // Otherwise use the usual round to closest
+ return support::cpp11::copysign(support::cpp11::round(positive_value), value);
+}
+
+/** Round floating-point value given a rounding mode
+ *
+ * @tparam T Parameter type. Should be of floating point type.
+ *
+ * @param[in] value floating-point value to be rounded.
+ * @param[in] rounding_mode Rounding mode to use.
+ *
+ * @return Floating-point value of rounded @p value.
+ */
+template <typename T, REQUIRES_TA(traits::is_floating_point<T>::value)>
+inline T round(T value, RoundingMode rounding_mode)
+{
+ switch(rounding_mode)
+ {
+ case RoundingMode::TO_ZERO:
+ return round_to_zero(value);
+ case RoundingMode::AWAY_FROM_ZERO:
+ return round_away_from_zero(value);
+ case RoundingMode::HALF_TO_ZERO:
+ return round_half_to_zero(value);
+ case RoundingMode::HALF_AWAY_FROM_ZERO:
+ return round_half_away_from_zero(value);
+ case RoundingMode::HALF_UP:
+ return round_half_up(value);
+ case RoundingMode::HALF_DOWN:
+ return round_half_down(value);
+ case RoundingMode::HALF_EVEN:
+ return round_half_even(value);
+ default:
+ ARM_COMPUTE_ERROR("Unsupported rounding mode!");
+ }
+}
+} // namespace rounding
+} // namespace utils
+} // namespace arm_compute
+#endif /*__ARM_COMPUTE_UTILS_ROUNDING_H__ */
diff --git a/arm_compute/core/utils/misc/SaturateCast.h b/arm_compute/core/utils/misc/SaturateCast.h
new file mode 100644
index 0000000000..b9e4787d9a
--- /dev/null
+++ b/arm_compute/core/utils/misc/SaturateCast.h
@@ -0,0 +1,218 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef __ARM_COMPUTE_UTILS_CAST_SATURATE_CAST_H__
+#define __ARM_COMPUTE_UTILS_CAST_SATURATE_CAST_H__
+
+#include "arm_compute/core/utils/misc/Rounding.h"
+#include "arm_compute/core/utils/misc/Traits.h"
+#include "arm_compute/core/utils/misc/Utility.h"
+
+namespace arm_compute
+{
+namespace utils
+{
+namespace cast
+{
+// *INDENT-OFF*
+// clang-format off
+// same type
+template<typename T,
+ typename U,
+ typename std::enable_if<std::is_same<T, U>::value, int >::type = 0 >
+T saturate_cast(U v)
+{
+ return v;
+}
+
+// signed -> signed widening/same_width
+template<typename T,
+ typename U,
+ typename std::enable_if<std::is_integral<T>::value &&
+ std::is_integral<U>::value &&
+ std::is_signed<U>() &&
+ std::is_signed<T>() &&
+ !std::is_same<T, U>::value &&
+ sizeof(T) >= sizeof(U),
+ int >::type = 0 >
+inline T saturate_cast(U v)
+{
+ return static_cast<T>(v);
+}
+// signed -> signed narrowing
+template<typename T,
+ typename U,
+ typename std::enable_if<std::is_integral<T>::value &&
+ std::is_integral<U>::value &&
+ std::is_signed<U>() &&
+ std::is_signed<T>() &&
+ !std::is_same<T, U>::value &&
+ sizeof(T) < sizeof(U),
+ int >::type = 0 >
+inline T saturate_cast(U v)
+{
+ return static_cast<T>(utility::clamp<U>(v, std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max()));
+}
+
+// unsigned -> signed widening
+template<typename T,
+ typename U,
+ typename std::enable_if<std::is_integral<T>::value &&
+ std::is_integral<U>::value &&
+ std::is_unsigned<U>() &&
+ std::is_signed<T>() &&
+ !std::is_same<T, U>::value &&
+ (sizeof(T) > sizeof(U)),
+ int >::type = 0 >
+inline T saturate_cast(U v)
+{
+ return static_cast<T>(v);
+}
+// unsigned -> signed narrowing
+template<typename T,
+ typename U,
+ typename std::enable_if<std::is_integral<T>::value &&
+ std::is_integral<U>::value &&
+ std::is_unsigned<U>() &&
+ std::is_signed<T>() &&
+ !std::is_same<T, U>::value &&
+ sizeof(T) < sizeof(U),
+ int >::type = 0 >
+inline T saturate_cast(U v)
+{
+ return static_cast<T>(std::min<U>(v, std::numeric_limits<T>::max()));
+}
+// unsigned -> signed same_width
+template<typename T,
+ typename U,
+ typename std::enable_if<std::is_integral<T>::value &&
+ std::is_integral<U>::value &&
+ std::is_unsigned<U>() &&
+ std::is_signed<T>() &&
+ !std::is_same<T, U>::value &&
+ sizeof(T) == sizeof(U),
+ int >::type = 0 >
+inline T saturate_cast(U v)
+{
+ return static_cast<T>(std::min<U>(v, std::numeric_limits<T>::max()));
+}
+
+// signed -> unsigned widening/same width
+template<typename T,
+ typename U,
+ typename std::enable_if<std::is_integral<T>::value &&
+ std::is_integral<U>::value &&
+ std::is_signed<U>() &&
+ std::is_unsigned<T>() &&
+ !std::is_same<T, U>::value &&
+ sizeof(T) >= sizeof(U),
+ int >::type = 0 >
+inline T saturate_cast(U v)
+{
+ return static_cast<T>(std::max<U>(0, v));
+}
+
+// signed -> unsigned narrowing
+template<typename T,
+ typename U,
+ typename std::enable_if<std::is_integral<T>::value &&
+ std::is_integral<U>::value &&
+ std::is_signed<U>() &&
+ std::is_unsigned<T>() &&
+ !std::is_same<T, U>::value &&
+ sizeof(T) < sizeof(U),
+ int >::type = 0 >
+inline T saturate_cast(U v)
+{
+ return static_cast<T>(utility::clamp<U>(v, 0, std::numeric_limits<T>::max()));
+}
+
+// unsigned -> unsigned widening/same width
+template<typename T,
+ typename U,
+ typename std::enable_if<std::is_integral<T>::value &&
+ std::is_integral<U>::value &&
+ std::is_unsigned<T>() &&
+ std::is_unsigned<U>() &&
+ !std::is_same<T, U>::value &&
+ sizeof(T) >= sizeof(U),
+ int >::type = 0 >
+inline T saturate_cast(U v)
+{
+ return static_cast<T>(v);
+}
+
+// unsigned -> unsigned narrowing
+template<typename T,
+ typename U,
+ typename std::enable_if<std::is_integral<T>::value &&
+ std::is_integral<U>::value &&
+ std::is_unsigned<T>() &&
+ std::is_unsigned<U>() &&
+ !std::is_same<T, U>::value &&
+ sizeof(T) < sizeof(U),
+ int >::type = 0 >
+inline T saturate_cast(U v)
+{
+ return static_cast<T>(utility::clamp<U>(v, std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max()));
+}
+
+// float -> int
+template<typename T,
+ typename U,
+ typename std::enable_if<std::is_integral<T>::value &&
+ traits::is_floating_point<U>::value,
+ int >::type = 0 >
+inline T saturate_cast(U v)
+{
+ int32_t vi = utils::rounding::round_half_away_from_zero(v);
+ return saturate_cast<T>(vi);
+}
+
+// int -> float
+template<typename T,
+ typename U,
+ typename std::enable_if<traits::is_floating_point<T>::value &&
+ std::is_integral<U>::value,
+ int >::type = 0 >
+inline T saturate_cast(U v)
+{
+ return static_cast<T>(v);
+}
+
+// float -> float
+template<typename T,
+ typename U,
+ typename std::enable_if<traits::is_floating_point<T>::value &&
+ traits::is_floating_point<U>::value,
+ int >::type = 0 >
+inline T saturate_cast(U v)
+{
+ return static_cast<T>(v);
+}
+// clang-format on
+// *INDENT-ON*
+} // namespace cast
+} // namespace utils
+} // namespace arm_compute
+#endif /* __ARM_COMPUTE_UTILS_CAST_SATURATE_CAST_H__ */
diff --git a/arm_compute/core/utils/misc/Traits.h b/arm_compute/core/utils/misc/Traits.h
new file mode 100644
index 0000000000..9d86dd1b3c
--- /dev/null
+++ b/arm_compute/core/utils/misc/Traits.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef __ARM_COMPUTE_UTILS_TRAITS_TRAITS_H__
+#define __ARM_COMPUTE_UTILS_TRAITS_TRAITS_H__
+
+#include <type_traits>
+
+namespace arm_compute
+{
+namespace utils
+{
+namespace traits
+{
+template <typename T>
+struct is_floating_point : public std::is_floating_point<T>
+{
+};
+
+template <>
+struct is_floating_point<half> : public std::true_type
+{
+};
+} // namespace traits
+} // namespace utils
+} // namespace arm_compute
+#endif /* __ARM_COMPUTE_UTILS_TRAITS_TRAITS_H__ */
diff --git a/arm_compute/core/utils/misc/Utility.h b/arm_compute/core/utils/misc/Utility.h
index 0a9f180b4c..37c8b66e06 100644
--- a/arm_compute/core/utils/misc/Utility.h
+++ b/arm_compute/core/utils/misc/Utility.h
@@ -128,22 +128,6 @@ inline auto foldl(F &&func, T &&initial, U &&value, Us &&... values) -> decltype
return foldl(std::forward<F>(func), func(std::forward<T>(initial), std::forward<U>(value)), std::forward<Us>(values)...);
}
-/** Type cast with saturation.
- *
- * @param[in] val Value of type U to cast.
- *
- * @return Original value clamped to numeric limits of T and converted to type T.
- *
- * @warning Numeric limits of T must be representable without loss in type U.
- */
-template <typename T, typename U>
-T saturate_cast(U val)
-{
- const auto low = static_cast<U>(std::numeric_limits<T>::lowest());
- const auto high = static_cast<U>(std::numeric_limits<T>::max());
- return static_cast<T>(clamp(val, low, high));
-}
-
/** Perform an index sort of a given vector.
*
* @param[in] v Vector to sort