From f63885bc445af1329e6a5c44d94b5c5d78146b2c Mon Sep 17 00:00:00 2001 From: Michalis Spyrou Date: Wed, 16 Jan 2019 14:18:09 +0000 Subject: COMPMID-1710 Move some Util functions to core/Utils Building with exceptions=0 was failing Change-Id: I6f264dc859dedc66d12d522652134508e18d5740 Reviewed-on: https://review.mlplatform.org/526 Reviewed-by: Isabella Gottardi Tested-by: Arm Jenkins --- arm_compute/core/Utils.h | 65 ++++++++++++++++++++++++++++++++- src/core/CL/kernels/CLRangeKernel.cpp | 14 +++---- src/core/NEON/kernels/NERangeKernel.cpp | 14 +++---- utils/Utils.h | 63 -------------------------------- 4 files changed, 78 insertions(+), 78 deletions(-) diff --git a/arm_compute/core/Utils.h b/arm_compute/core/Utils.h index 696845d3ff..44974f1ecc 100644 --- a/arm_compute/core/Utils.h +++ b/arm_compute/core/Utils.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2017, 2018 ARM Limited. + * Copyright (c) 2016-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -1041,6 +1041,69 @@ inline std::string float_to_string_with_full_precision(float val) return ss.str(); } +/** Returns the number of elements required to go from start to end with the wanted step + * + * @param[in] start start value + * @param[in] end end value + * @param[in] step step value between each number in the wanted sequence + * + * @return number of elements to go from start value to end value using the wanted step + */ +inline size_t num_of_elements_in_range(const float start, const float end, const float step) +{ + ARM_COMPUTE_ERROR_ON_MSG(step == 0, "Range Step cannot be 0"); + return size_t(std::ceil((end - start) / step)); +} + +/** Returns true if the value can be represented by the given data type + * + * @param[in] val value to be checked + * @param[in] dt data type that is checked + * @param[in] quant_info quantization info if the data type is QASYMM8 + * + * @return true if the data type can hold the value. + */ +template +bool check_value_range(T val, DataType dt, QuantizationInfo quant_info = QuantizationInfo()) +{ + switch(dt) + { + case DataType::U8: + return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::QASYMM8: + { + double min = static_cast(quant_info.dequantize(0)); + double max = static_cast(quant_info.dequantize(std::numeric_limits::max())); + return ((double)val >= min && (double)val <= max); + } + case DataType::S8: + return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::U16: + return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::S16: + return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::U32: + return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::S32: + return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::U64: + return (val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::S64: + return (val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::F16: + return (val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::F32: + return (val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::F64: + return (val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + case DataType::SIZET: + return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); + default: + ARM_COMPUTE_ERROR("Data type not supported"); + return false; + } +} + #ifdef ARM_COMPUTE_DEBUG_ENABLED /** Print consecutive elements to an output stream. * diff --git a/src/core/CL/kernels/CLRangeKernel.cpp b/src/core/CL/kernels/CLRangeKernel.cpp index ae8cc0fae5..eb8822b957 100644 --- a/src/core/CL/kernels/CLRangeKernel.cpp +++ b/src/core/CL/kernels/CLRangeKernel.cpp @@ -26,7 +26,7 @@ #include "arm_compute/core/CL/CLHelpers.h" #include "arm_compute/core/CL/CLValidate.h" #include "arm_compute/core/CL/ICLTensor.h" -#include "utils/Utils.h" +#include "arm_compute/core/Utils.h" using namespace arm_compute; @@ -56,14 +56,14 @@ Status validate_arguments(const ITensorInfo &output, const float start, const fl ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start < end) && (step <= 0)), "step must be greater than 0 when start < end"); ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start > end) && (step >= 0)), "step must be less than 0 when start > end"); - ARM_COMPUTE_RETURN_ERROR_ON_MSG(!utils::check_value_range(start, output.data_type(), output.quantization_info()), "start value is outside the range of the data type"); - ARM_COMPUTE_RETURN_ERROR_ON_MSG(!utils::check_value_range(end, output.data_type(), output.quantization_info()), "end value is outside the range of the data type"); - ARM_COMPUTE_RETURN_ERROR_ON_MSG(!utils::check_value_range(step, output.data_type(), output.quantization_info()), "step value is outside the range of the data type"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(!check_value_range(start, output.data_type(), output.quantization_info()), "start value is outside the range of the data type"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(!check_value_range(end, output.data_type(), output.quantization_info()), "end value is outside the range of the data type"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(!check_value_range(step, output.data_type(), output.quantization_info()), "step value is outside the range of the data type"); ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end"); ARM_COMPUTE_RETURN_ERROR_ON_MSG(output.num_dimensions() != 1, "Output has to be a 1-D tensor"); - ARM_COMPUTE_RETURN_ERROR_ON_MSG(output.tensor_shape().total_size() < utils::num_of_elements_in_range(start, end, step), "Output tensor size is incorrect"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(output.tensor_shape().total_size() < num_of_elements_in_range(start, end, step), "Output tensor size is incorrect"); return Status{}; } @@ -72,14 +72,14 @@ std::pair validate_and_configure_window(ITensorInfo &output, con { unsigned int num_elems_processed_per_iteration = get_num_elems_processed_per_iteration(output.data_type()); // Auto initialize output if not initialized - auto_init_if_empty(output, TensorShape(utils::num_of_elements_in_range(start, end, step)), 1, output.data_type(), output.quantization_info()); + auto_init_if_empty(output, TensorShape(num_of_elements_in_range(start, end, step)), 1, output.data_type(), output.quantization_info()); // Configure kernel window Window win = calculate_max_window(output, Steps(num_elems_processed_per_iteration)); AccessWindowHorizontal output_access(&output, 0, num_elems_processed_per_iteration); bool window_changed = update_window_and_padding(win, output_access); - output_access.set_valid_region(win, ValidRegion(Coordinates(), TensorShape(utils::num_of_elements_in_range(start, end, step)))); + output_access.set_valid_region(win, ValidRegion(Coordinates(), TensorShape(num_of_elements_in_range(start, end, step)))); Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; return std::make_pair(err, win); } diff --git a/src/core/NEON/kernels/NERangeKernel.cpp b/src/core/NEON/kernels/NERangeKernel.cpp index 627ce422bd..189e77f0fc 100644 --- a/src/core/NEON/kernels/NERangeKernel.cpp +++ b/src/core/NEON/kernels/NERangeKernel.cpp @@ -32,7 +32,7 @@ #include "arm_compute/core/TensorInfo.h" #include "arm_compute/core/Validate.h" -#include "utils/Utils.h" +#include "arm_compute/core/Utils.h" namespace arm_compute { @@ -77,14 +77,14 @@ Status validate_arguments(const ITensorInfo &output, const float start, const fl ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start < end) && (step <= 0)), "step must be greater than 0 when start < end"); ARM_COMPUTE_RETURN_ERROR_ON_MSG(((start > end) && (step >= 0)), "step must be less than 0 when start > end"); - ARM_COMPUTE_RETURN_ERROR_ON_MSG(!utils::check_value_range(start, output.data_type(), output.quantization_info()), "start value is outside the range of the data type"); - ARM_COMPUTE_RETURN_ERROR_ON_MSG(!utils::check_value_range(end, output.data_type(), output.quantization_info()), "end value is outside the range of the data type"); - ARM_COMPUTE_RETURN_ERROR_ON_MSG(!utils::check_value_range(step, output.data_type(), output.quantization_info()), "step value is outside the range of the data type"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(!check_value_range(start, output.data_type(), output.quantization_info()), "start value is outside the range of the data type"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(!check_value_range(end, output.data_type(), output.quantization_info()), "end value is outside the range of the data type"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(!check_value_range(step, output.data_type(), output.quantization_info()), "step value is outside the range of the data type"); ARM_COMPUTE_RETURN_ERROR_ON_MSG((start == end), "start of the requested sequence must not be equal to the end"); ARM_COMPUTE_RETURN_ERROR_ON_MSG(output.num_dimensions() != 1, "Output has to be a 1-D tensor"); - ARM_COMPUTE_RETURN_ERROR_ON_MSG(output.tensor_shape().total_size() < utils::num_of_elements_in_range(start, end, step), "Output tensor size is incorrect"); + ARM_COMPUTE_RETURN_ERROR_ON_MSG(output.tensor_shape().total_size() < num_of_elements_in_range(start, end, step), "Output tensor size is incorrect"); return Status{}; } @@ -94,13 +94,13 @@ std::pair validate_and_configure_window(ITensorInfo &output, con const unsigned int num_elems_processed_per_iteration = 16 / output.element_size(); // Auto initialize output if not initialized - auto_init_if_empty(output, TensorShape(utils::num_of_elements_in_range(start, end, step)), 1, output.data_type(), output.quantization_info()); + auto_init_if_empty(output, TensorShape(num_of_elements_in_range(start, end, step)), 1, output.data_type(), output.quantization_info()); // Configure kernel window Window win = calculate_max_window(output, Steps(num_elems_processed_per_iteration)); AccessWindowHorizontal output_access(&output, 0, num_elems_processed_per_iteration); bool window_changed = update_window_and_padding(win, output_access); - output_access.set_valid_region(win, ValidRegion(Coordinates(), TensorShape(utils::num_of_elements_in_range(start, end, step)))); + output_access.set_valid_region(win, ValidRegion(Coordinates(), TensorShape(num_of_elements_in_range(start, end, step)))); Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; return std::make_pair(err, win); } diff --git a/utils/Utils.h b/utils/Utils.h index ad71776803..04ccc3e812 100644 --- a/utils/Utils.h +++ b/utils/Utils.h @@ -194,69 +194,6 @@ inline std::string get_typestring(DataType data_type) } } -/** Returns the number of elements required to go from start to end with the wanted step - * - * @param[in] start start value - * @param[in] end end value - * @param[in] step step value between each number in the wanted sequence - * - * @return number of elements to go from start value to end value using the wanted step - */ -inline size_t num_of_elements_in_range(const float start, const float end, const float step) -{ - ARM_COMPUTE_ERROR_ON_MSG(step == 0, "Range Step cannot be 0"); - return size_t(std::ceil((end - start) / step)); -} - -/** Returns true if the value can be represented by the given data type - * - * @param[in] val value to be checked - * @param[in] dt data type that is checked - * @param[in] quant_info quantization info if the data type is QASYMM8 - * - * @return true if the data type can hold the value. - */ -template -bool check_value_range(T val, DataType dt, QuantizationInfo quant_info = QuantizationInfo()) -{ - switch(dt) - { - case DataType::U8: - return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); - case DataType::QASYMM8: - { - double min = static_cast(quant_info.dequantize(0)); - double max = static_cast(quant_info.dequantize(std::numeric_limits::max())); - return ((double)val >= min && (double)val <= max); - } - case DataType::S8: - return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); - case DataType::U16: - return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); - case DataType::S16: - return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); - case DataType::U32: - return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); - case DataType::S32: - return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); - case DataType::U64: - return (val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); - case DataType::S64: - return (val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); - case DataType::F16: - return (val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); - case DataType::F32: - return (val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); - case DataType::F64: - return (val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); - case DataType::SIZET: - return ((static_cast(val) == val) && val >= std::numeric_limits::lowest() && val <= std::numeric_limits::max()); - default: - ARM_COMPUTE_ERROR("Data type not supported"); - return false; - } -} - /** Maps a tensor if needed * * @param[in] tensor Tensor to be mapped -- cgit v1.2.1