From d8734b55d89f05901ba9a75349761a9c955d9243 Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Fri, 22 Dec 2017 15:27:52 +0000 Subject: COMPMID-793 : Add graph intermediate representation Change-Id: Ic1685de4e19e0ac79669ef2da64e1dc96c7ea0bf Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/115248 Tested-by: Jenkins Reviewed-by: Anthony Barbier --- arm_compute/core/Error.h | 14 +- arm_compute/core/FixedPoint.inl | 2 +- arm_compute/core/Helpers.h | 1 - arm_compute/core/ITensorInfo.h | 2 +- arm_compute/core/TensorShape.h | 2 +- arm_compute/core/Types.h | 2 +- arm_compute/core/utils/logging/Types.h | 3 +- arm_compute/core/utils/misc/CRTP.h | 54 +++++++ arm_compute/core/utils/misc/Cast.h | 76 +++++++++ arm_compute/core/utils/misc/Iterable.h | 92 +++++++++++ arm_compute/core/utils/misc/ShapeCalculator.h | 18 ++- arm_compute/core/utils/misc/Utility.h | 169 ++++++++++++++++++++ arm_compute/core/utils/misc/utility.h | 170 --------------------- arm_compute/core/utils/strong_type/StrongType.h | 77 ++++++++++ .../core/utils/strong_type/StrongTypeAttributes.h | 64 ++++++++ 15 files changed, 566 insertions(+), 180 deletions(-) create mode 100644 arm_compute/core/utils/misc/CRTP.h create mode 100644 arm_compute/core/utils/misc/Cast.h create mode 100644 arm_compute/core/utils/misc/Iterable.h create mode 100644 arm_compute/core/utils/misc/Utility.h delete mode 100644 arm_compute/core/utils/misc/utility.h create mode 100644 arm_compute/core/utils/strong_type/StrongType.h create mode 100644 arm_compute/core/utils/strong_type/StrongTypeAttributes.h (limited to 'arm_compute/core') diff --git a/arm_compute/core/Error.h b/arm_compute/core/Error.h index 56c7ccdd93..f178936ede 100644 --- a/arm_compute/core/Error.h +++ b/arm_compute/core/Error.h @@ -29,6 +29,16 @@ namespace arm_compute { +/** Ignores unused arguments + * + * @tparam T Argument types + */ +template +inline void ignore_unused(T &&...) +{ +} + +/** Available error codes */ enum class ErrorCode { OK, /**< No error */ @@ -142,9 +152,9 @@ Status create_error(ErrorCode error_code, const char *function, const char *file * This is useful if for example a variable is only used * in debug builds and generates a warning in release builds. * - * @param[in] var Variable which is unused. + * @param[in] ... Variables which are unused. */ -#define ARM_COMPUTE_UNUSED(var) (void)(var) +#define ARM_COMPUTE_UNUSED(...) ignore_unused(__VA_ARGS__) // NOLINT /** Creates an error with a given message * diff --git a/arm_compute/core/FixedPoint.inl b/arm_compute/core/FixedPoint.inl index 9c7e35ab16..eb3516e8db 100644 --- a/arm_compute/core/FixedPoint.inl +++ b/arm_compute/core/FixedPoint.inl @@ -22,7 +22,7 @@ * SOFTWARE. */ #include "arm_compute/core/Error.h" -#include "arm_compute/core/utils/misc/utility.h" +#include "arm_compute/core/utils/misc/Utility.h" #include #include diff --git a/arm_compute/core/Helpers.h b/arm_compute/core/Helpers.h index 24ba521d60..1554f63904 100644 --- a/arm_compute/core/Helpers.h +++ b/arm_compute/core/Helpers.h @@ -33,7 +33,6 @@ #include "arm_compute/core/TensorShape.h" #include "arm_compute/core/Types.h" #include "arm_compute/core/Window.h" -#include "arm_compute/core/utils/misc/utility.h" #include #include diff --git a/arm_compute/core/ITensorInfo.h b/arm_compute/core/ITensorInfo.h index 167fb41bb3..ce0cf53fdf 100644 --- a/arm_compute/core/ITensorInfo.h +++ b/arm_compute/core/ITensorInfo.h @@ -30,7 +30,7 @@ #include "arm_compute/core/Types.h" #include "arm_compute/core/Utils.h" #include "arm_compute/core/utils/misc/ICloneable.h" -#include "arm_compute/core/utils/misc/utility.h" +#include "arm_compute/core/utils/misc/Utility.h" #include diff --git a/arm_compute/core/TensorShape.h b/arm_compute/core/TensorShape.h index dc836c98da..d5532e8a6a 100644 --- a/arm_compute/core/TensorShape.h +++ b/arm_compute/core/TensorShape.h @@ -26,7 +26,7 @@ #include "arm_compute/core/Dimensions.h" #include "arm_compute/core/Error.h" -#include "arm_compute/core/utils/misc/utility.h" +#include "arm_compute/core/utils/misc/Utility.h" #include #include diff --git a/arm_compute/core/Types.h b/arm_compute/core/Types.h index 143ee025f5..ae88e607c3 100644 --- a/arm_compute/core/Types.h +++ b/arm_compute/core/Types.h @@ -1076,5 +1076,5 @@ enum class ConvolutionMethod DIRECT, /**< Direct convolution */ WINOGRAD /**< Convolution using Winograd */ }; -} +} // namespace arm_compute #endif /* __ARM_COMPUTE_TYPES_H__ */ diff --git a/arm_compute/core/utils/logging/Types.h b/arm_compute/core/utils/logging/Types.h index 171270d4ef..0b40e3d7a1 100644 --- a/arm_compute/core/utils/logging/Types.h +++ b/arm_compute/core/utils/logging/Types.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2017 ARM Limited. + * Copyright (c) 2016-2018 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -36,6 +36,7 @@ enum class LogLevel : unsigned int VERBOSE, /**< All logging messages */ INFO, /**< Information log level */ WARN, /**< Warning log level */ + ERROR, /**< Error log level */ OFF /**< No logging */ }; diff --git a/arm_compute/core/utils/misc/CRTP.h b/arm_compute/core/utils/misc/CRTP.h new file mode 100644 index 0000000000..9947312f95 --- /dev/null +++ b/arm_compute/core/utils/misc/CRTP.h @@ -0,0 +1,54 @@ +/* + * 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_MISC_CRTP_H__ +#define __ARM_COMPUTE_MISC_CRTP_H__ + +namespace arm_compute +{ +namespace misc +{ +/** Curiously recurring template pattern Interface */ +template class Type> +struct CRTP +{ +public: + using ExactType = T; + +protected: + const T &impl() const + { + return static_cast(*this); + } + T &impl() + { + return static_cast(*this); + } + +private: + CRTP() = default; + friend Type; +}; +} // namespace misc +} // namespace arm_compute +#endif /* __ARM_COMPUTE_MISC_CRTP_H__ */ diff --git a/arm_compute/core/utils/misc/Cast.h b/arm_compute/core/utils/misc/Cast.h new file mode 100644 index 0000000000..f6c91dd2de --- /dev/null +++ b/arm_compute/core/utils/misc/Cast.h @@ -0,0 +1,76 @@ +/* + * 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_MISC_CAST_H__ +#define __ARM_COMPUTE_MISC_CAST_H__ + +#include "arm_compute/core/Error.h" + +namespace arm_compute +{ +namespace utils +{ +namespace cast +{ +/** Polymorphic cast between two types + * + * @warning Will throw an exception if cast cannot take place + * + * @tparam Target Target to cast type + * @tparam Source Source from cast type + * + * @param[in] v Value to cast + * + * @return The casted type + */ +template +inline Target polymorphic_cast(Source *v) +{ + if(dynamic_cast(v) == nullptr) + { + throw std::bad_cast(); + } + return static_cast(v); +} + +/** Polymorphic down cast between two types + * + * @warning Will assert if cannot take place + * + * @tparam Target Target to cast type + * @tparam Source Source from cast type + * + * @param[in] v Value to cast + * + * @return The casted type + */ +template +inline Target polymorphic_downcast(Source *v) +{ + ARM_COMPUTE_ERROR_ON(dynamic_cast(v) != static_cast(v)); + return static_cast(v); +} +} // namespace cast +} // namespace utils +} // namespace arm_compute +#endif /* __ARM_COMPUTE_MISC_CAST_H__ */ diff --git a/arm_compute/core/utils/misc/Iterable.h b/arm_compute/core/utils/misc/Iterable.h new file mode 100644 index 0000000000..96a650af35 --- /dev/null +++ b/arm_compute/core/utils/misc/Iterable.h @@ -0,0 +1,92 @@ +/* + * 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_MISC_ITERABLE_H__ +#define __ARM_COMPUTE_MISC_ITERABLE_H__ + +#include + +namespace arm_compute +{ +namespace utils +{ +namespace iterable +{ +/** Reverse range iterable class + * + * @tparam T Type to create a reverse range on + */ +template +class reverse_iterable +{ +public: + /** Default constructor + * + * @param[in] it Value to reverse iterate on + */ + explicit reverse_iterable(T &it) + : _it(it) + { + } + + typename T::reverse_iterator begin() + { + return _it.rbegin(); + } + + typename T::reverse_iterator end() + { + return _it.rend(); + } + + typename T::const_reverse_iterator cbegin() + { + return _it.rbegin(); + } + + typename T::const_reverse_iterator cend() + { + return _it.rend(); + } + +private: + T &_it; +}; + +/** Creates a reverse iterable for a given type + * + * @tparam T Type to create a reverse iterable on + * + * @param[in] val Iterable input + * + * @return Reverse iterable container + */ +template +reverse_iterable reverse_iterate(T &val) +{ + return reverse_iterable(val); +} +} // namespace iterable +} // namespace utils +} // namespace arm_compute +#endif /* __ARM_COMPUTE_MISC_ITERABLE_H__ */ diff --git a/arm_compute/core/utils/misc/ShapeCalculator.h b/arm_compute/core/utils/misc/ShapeCalculator.h index 2919625511..354f60d016 100644 --- a/arm_compute/core/utils/misc/ShapeCalculator.h +++ b/arm_compute/core/utils/misc/ShapeCalculator.h @@ -174,7 +174,6 @@ inline TensorShape compute_interleave_custom_shape(const TensorShape &input, con return output_shape; } - inline TensorShape compute_fully_connected_reshaped_weights_shape(const ITensorInfo *input, bool transpose_weights, bool is_batched_fc_layer, const int interleave) { TensorShape output_shape{ input->tensor_shape() }; @@ -194,7 +193,6 @@ inline TensorShape compute_fully_connected_reshaped_weights_shape(const ITensorI return output_shape; } - inline TensorShape compute_winograd_input_transform_shape(const ITensorInfo &input, const PadStrideInfo &conv_info, const Size2D &kernel_size) { // Compute height @@ -212,6 +210,22 @@ inline TensorShape compute_winograd_input_transform_shape(const ITensorInfo &inp return output_shape; } +inline TensorShape compute_deep_convolution_shape(const ITensorInfo &input, const ITensorInfo &weights, PadStrideInfo conv_info) +{ + const TensorShape input_shape{ input.tensor_shape() }; + const TensorShape weights_shape{ weights.tensor_shape() }; + + unsigned int output_width = 0; + unsigned int output_height = 0; + std::tie(output_width, output_height) = scaled_dimensions(input_shape.x(), input_shape.y(), weights_shape.x(), weights_shape.y(), conv_info); + + TensorShape output_shape{ input_shape }; + output_shape.set(0, output_width); + output_shape.set(1, output_height); + output_shape.set(2, weights_shape[3]); + + return output_shape; +} } // namespace shape_calculator } // namespace misc } // namespace arm_compute diff --git a/arm_compute/core/utils/misc/Utility.h b/arm_compute/core/utils/misc/Utility.h new file mode 100644 index 0000000000..639f2e155d --- /dev/null +++ b/arm_compute/core/utils/misc/Utility.h @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2017-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_MISC_UTILITY_H__ +#define __ARM_COMPUTE_MISC_UTILITY_H__ + +#include +#include +#include +#include +#include + +namespace arm_compute +{ +namespace utility +{ +/** @cond */ +template +struct index_sequence +{ +}; + +template +struct index_sequence_generator : index_sequence_generator < N - 1, N - 1, S... > +{ +}; + +template +struct index_sequence_generator<0u, S...> : index_sequence +{ + using type = index_sequence; +}; + +template +using index_sequence_t = typename index_sequence_generator::type; +/** @endcond */ + +namespace detail +{ +template ::value_type, sizeof...(S)>> +T make_array(Iterator first, index_sequence) +{ + return T{ { first[S]... } }; +} +} // namespace detail + +template +std::array::value_type, N> make_array(Iterator first, Iterator last) +{ + return detail::make_array(first, index_sequence_t {}); +} + +/** Performs clamping among a lower and upper value. + * + * @param[in] n Value to clamp. + * @param[in] lower Lower threshold. + * @param[in] upper Upper threshold. + * + * @return Clamped value. + */ +template +inline T clamp(const T &n, const T &lower, const T &upper) +{ + return std::max(lower, std::min(n, upper)); +} + +/** Base case of for_each. Does nothing. */ +template +inline void for_each(F &&) +{ +} + +/** Call the function for each of the arguments + * + * @param[in] func Function to be called + * @param[in] arg Argument passed to the function + * @param[in] args Remaining arguments + */ +template +inline void for_each(F &&func, T &&arg, Ts &&... args) +{ + func(std::forward(arg)); + for_each(std::forward(func), std::forward(args)...); +} + +/** Base case of foldl. + * + * @return value. + */ +template +inline T &&foldl(F &&, T &&value) +{ + return std::forward(value); +} + +/** Fold left. + * + * @param[in] func Function to be called + * @param[in] initial Initial value + * @param[in] value Argument passed to the function + * @param[in] values Remaining arguments + */ +template +inline auto foldl(F &&func, T &&initial, U &&value, Us &&... values) -> decltype(func(std::forward(initial), std::forward(value))) +{ + return foldl(std::forward(func), func(std::forward(initial), std::forward(value)), std::forward(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 +T saturate_cast(U val) +{ + const auto low = static_cast(std::numeric_limits::lowest()); + const auto high = static_cast(std::numeric_limits::max()); + return static_cast(clamp(val, low, high)); +} + +/** Perform an index sort of a given vector. + * + * @param[in] v Vector to sort + * + * @return Sorted index vector. + */ +template +std::vector sort_indices(const std::vector &v) +{ + std::vector idx(v.size()); + std::iota(idx.begin(), idx.end(), 0); + + std::sort(idx.begin(), idx.end(), + [&v](size_t i1, size_t i2) + { + return v[i1] < v[i2]; + }); + + return idx; +} +} // namespace utility +} // namespace arm_compute +#endif /* __ARM_COMPUTE_MISC_UTILITY_H__ */ diff --git a/arm_compute/core/utils/misc/utility.h b/arm_compute/core/utils/misc/utility.h deleted file mode 100644 index 8ba92310da..0000000000 --- a/arm_compute/core/utils/misc/utility.h +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright (c) 2017-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_MISC_UTILITY_H__ -#define __ARM_COMPUTE_MISC_UTILITY_H__ - -#include -#include -#include -#include -#include - -namespace arm_compute -{ -namespace utility -{ -/** @cond */ -template -struct index_sequence -{ -}; - -template -struct index_sequence_generator : index_sequence_generator < N - 1, N - 1, S... > -{ -}; - -template -struct index_sequence_generator<0u, S...> : index_sequence -{ - using type = index_sequence; -}; - -template -using index_sequence_t = typename index_sequence_generator::type; -/** @endcond */ - -namespace detail -{ -template ::value_type, sizeof...(S)>> -T make_array(Iterator first, index_sequence) -{ - return T{ { first[S]... } }; -} -} // namespace detail - -template -std::array::value_type, N> make_array(Iterator first, Iterator last) -{ - return detail::make_array(first, index_sequence_t {}); -} - -/** Performs clamping among a lower and upper value. - * - * @param[in] n Value to clamp. - * @param[in] lower Lower threshold. - * @param[in] upper Upper threshold. - * - * @return Clamped value. - */ -template -inline T clamp(const T &n, const T &lower, const T &upper) -{ - return std::max(lower, std::min(n, upper)); -} - -/** Base case of for_each. Does nothing. */ -template -inline void for_each(F &&) -{ -} - -/** Call the function for each of the arguments - * - * @param[in] func Function to be called - * @param[in] arg Argument passed to the function - * @param[in] args Remaining arguments - */ -template -inline void for_each(F &&func, T &&arg, Ts &&... args) -{ - func(std::forward(arg)); - for_each(std::forward(func), std::forward(args)...); -} - -/** Base case of foldl. - * - * @return value. - */ -template -inline T &&foldl(F &&, T &&value) -{ - return std::forward(value); -} - -/** Fold left. - * - * @param[in] func Function to be called - * @param[in] initial Initial value - * @param[in] value Argument passed to the function - * @param[in] values Remaining arguments - */ -template -inline auto foldl(F &&func, T &&initial, U &&value, Us &&... values) -> decltype(func(std::forward(initial), std::forward(value))) -{ - return foldl(std::forward(func), func(std::forward(initial), std::forward(value)), std::forward(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 -T saturate_cast(U val) -{ - const auto low = static_cast(std::numeric_limits::lowest()); - const auto high = static_cast(std::numeric_limits::max()); - return static_cast(clamp(val, low, high)); -} - -/** Perform an index sort of a given vector. - * - * @param[in] v Vector to sort - * - * @return Sorted index vector. - */ -template -std::vector sort_indices(const std::vector &v) -{ - std::vector idx(v.size()); - std::iota(idx.begin(), idx.end(), 0); - - std::sort(idx.begin(), idx.end(), - [&v](size_t i1, size_t i2) - { - return v[i1] < v[i2]; - }); - - return idx; -} - -} // namespace utility -} // namespace arm_compute -#endif /* __ARM_COMPUTE_MISC_UTILITY_H__ */ diff --git a/arm_compute/core/utils/strong_type/StrongType.h b/arm_compute/core/utils/strong_type/StrongType.h new file mode 100644 index 0000000000..5a38edbbd6 --- /dev/null +++ b/arm_compute/core/utils/strong_type/StrongType.h @@ -0,0 +1,77 @@ +/* + * 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_STRONG_TYPE_STRONG_TYPE_H__ +#define __ARM_COMPUTE_STRONG_TYPE_STRONG_TYPE_H__ + +#include + +namespace arm_compute +{ +namespace strong_type +{ +/** Strong type + * + * @tparam T Exact type of the Strong Type + * @tparam Tag Tag used to distinguish between types with same T + * @tparam Attributes Attributes of the Strong Type + */ +template class... Attributes> +class StrongType : public Attributes>... +{ +public: + /** Exact underlying type **/ + using ExactType = T; + +public: + /** Default Constructor + * + * @param[in] val Initialization value + */ + StrongType(T val) + : _val(val) + { + } + /** Accessor of the value of the exact type + * + * @return Exact type value + */ + T &get() + { + return _val; + } + /** Accessor of the value of the exact type + * + * @return Exact type value + */ + const T &get() const + { + return _val; + } + +private: + T _val = {}; +}; +} // namespace strong_type +} // namespace arm_compute +#endif /* __ARM_COMPUTE_STRONG_TYPE_STRONG_TYPE_H__ */ diff --git a/arm_compute/core/utils/strong_type/StrongTypeAttributes.h b/arm_compute/core/utils/strong_type/StrongTypeAttributes.h new file mode 100644 index 0000000000..b5ed48f5ce --- /dev/null +++ b/arm_compute/core/utils/strong_type/StrongTypeAttributes.h @@ -0,0 +1,64 @@ +/* + * 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_STRONG_TYPE_STRONG_TYPE_ATTRIBUTES_H__ +#define __ARM_COMPUTE_STRONG_TYPE_STRONG_TYPE_ATTRIBUTES_H__ + +#include "arm_compute/core/utils/misc/CRTP.h" + +namespace arm_compute +{ +namespace strong_type +{ +/** Comparable attribute */ +template +struct Comparable : misc::CRTP +{ + bool operator==(T const &other) const + { + return this->impl().get() == other.get(); + } + bool operator!=(T const &other) const + { + return !(*this == other); + } + bool operator>(T const &other) const + { + return this->impl().get() > other.get(); + } + bool operator<(T const &other) const + { + return this->impl().get() < other.get(); + } + bool operator>=(T const &other) const + { + return !(*this < other); + } + bool operator<=(T const &other) const + { + return !(*this > other); + } +}; +} // namespace strong_type +} // namespace arm_compute +#endif /* __ARM_COMPUTE_STRONG_TYPE_STRONG_TYPE_ATTRIBUTES_H__ */ -- cgit v1.2.1