From 5e96be7707a571b136dc64256af399dbbb0fdfe0 Mon Sep 17 00:00:00 2001 From: Vidhya Sudhan Loganathan Date: Tue, 18 Dec 2018 14:17:00 +0000 Subject: COMPMID-1722 : CL: Implement Range Change-Id: I88da6eb5289c303b1dc91606c1560ce629746058 Reviewed-on: https://review.mlplatform.org/381 Reviewed-by: Gian Marco Iodice Tested-by: Arm Jenkins Reviewed-by: Georgios Pinitas --- utils/Utils.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'utils') diff --git a/utils/Utils.h b/utils/Utils.h index 8cac857178..031a3726a1 100644 --- a/utils/Utils.h +++ b/utils/Utils.h @@ -194,6 +194,55 @@ inline std::string get_typestring(DataType data_type) } } +/** 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