2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
5 #include <armnn/TypesUtils.hpp>
6 #include <armnn/utility/Assert.hpp>
7 #include <armnn/utility/NumericCast.hpp>
11 /// Workaround for std:isnan() not being implemented correctly for integral types in MSVC.
12 /// https://stackoverflow.com/a/56356405
14 template <typename T, typename std::enable_if<std::is_integral<T>::value, T>::type* = nullptr>
17 // The spec defines integral types to be handled as if they were casted to doubles.
18 return std::isnan(static_cast<double>(x));
21 template <typename T, typename std::enable_if<!std::is_integral<T>::value, T>::type * = nullptr>
29 template<typename QuantizedType>
30 QuantizedType armnn::Quantize(float value, float scale, int32_t offset)
32 static_assert(IsQuantizedType<QuantizedType>(), "Not an integer type.");
33 constexpr QuantizedType max = std::numeric_limits<QuantizedType>::max();
34 constexpr QuantizedType min = std::numeric_limits<QuantizedType>::lowest();
35 ARMNN_ASSERT(scale != 0.f);
36 ARMNN_ASSERT(!std::isnan(value));
38 float clampedValue = std::min(std::max(static_cast<float>(round(value/scale) + offset), static_cast<float>(min)),
39 static_cast<float>(max));
40 auto quantizedBits = static_cast<QuantizedType>(clampedValue);
45 template <typename QuantizedType>
46 float armnn::Dequantize(QuantizedType value, float scale, int32_t offset)
48 static_assert(IsQuantizedType<QuantizedType>(), "Not an integer type.");
49 ARMNN_ASSERT(scale != 0.f);
50 ARMNN_ASSERT(!IsNan(value));
51 return (armnn::numeric_cast<float>(value - offset)) * scale;
54 /// Explicit specialization of Quantize for int8_t
56 int8_t armnn::Quantize<int8_t>(float value, float scale, int32_t offset);
58 /// Explicit specialization of Quantize for uint8_t
60 uint8_t armnn::Quantize<uint8_t>(float value, float scale, int32_t offset);
62 /// Explicit specialization of Quantize for int16_t
64 int16_t armnn::Quantize<int16_t>(float value, float scale, int32_t offset);
66 /// Explicit specialization of Quantize for int32_t
68 int32_t armnn::Quantize<int32_t>(float value, float scale, int32_t offset);
70 /// Explicit specialization of Dequantize for int8_t
72 float armnn::Dequantize<int8_t>(int8_t value, float scale, int32_t offset);
74 /// Explicit specialization of Dequantize for uint8_t
76 float armnn::Dequantize<uint8_t>(uint8_t value, float scale, int32_t offset);
78 /// Explicit specialization of Dequantize for int16_t
80 float armnn::Dequantize<int16_t>(int16_t value, float scale, int32_t offset);
82 /// Explicit specialization of Dequantize for int32_t
84 float armnn::Dequantize<int32_t>(int32_t value, float scale, int32_t offset);