Move requantize function to OperationsUtils.h
The function is useful for SELECT op implementation and possibly for
some other ops supporting inputs with different quantization parameters.
Test: NeuralNetworksTest_static
Change-Id: I1d835451b9728acf813009861abd5a04a07bb73f
Merged-In: I1d835451b9728acf813009861abd5a04a07bb73f
(cherry picked from commit 7c158207c5120dd75854465c6c975d39b40a1ec1)
diff --git a/common/OperationsUtils.cpp b/common/OperationsUtils.cpp
index 68dd817..7cd3176 100644
--- a/common/OperationsUtils.cpp
+++ b/common/OperationsUtils.cpp
@@ -269,6 +269,11 @@
return true;
}
+uint8_t requantize(uint8_t value, const Shape& oldShape, const Shape& newShape) {
+ double doubleValue = (value - oldShape.offset) * oldShape.scale;
+ return static_cast<uint8_t>(doubleValue / newShape.scale + newShape.offset);
+}
+
bool addMulPrepare(const Shape& in1, const Shape& in2, Shape* out) {
NN_OPS_CHECK(getNumberOfDimensions(in1) <= 4 && getNumberOfDimensions(in2) <= 4);
NN_OPS_CHECK(in1.type == in2.type);
diff --git a/common/include/OperationsUtils.h b/common/include/OperationsUtils.h
index fadd03c..7c92f00 100644
--- a/common/include/OperationsUtils.h
+++ b/common/include/OperationsUtils.h
@@ -326,6 +326,9 @@
// shape. Returns true on success and false on error.
bool calculateBroadcastedShape(const Shape& in1, const Shape& in2, Shape* out);
+// Dequantizes a value and quantizes it back using new scale and offset.
+uint8_t requantize(uint8_t value, const Shape& oldShape, const Shape& newShape);
+
// Preparation functions for the corresponding ops
bool addMulPrepare(const Shape& in1, const Shape& in2, Shape* out1);
diff --git a/common/operations/MaximumMinimum.cpp b/common/operations/MaximumMinimum.cpp
index a44d2c1..d56e311 100644
--- a/common/operations/MaximumMinimum.cpp
+++ b/common/operations/MaximumMinimum.cpp
@@ -18,6 +18,7 @@
#include "MaximumMinimum.h"
#include "IndexedShapeWrapper.h"
+#include "OperationsUtils.h"
#include "Tracing.h"
namespace android {
@@ -52,11 +53,6 @@
return true;
}
-uint8_t requantize(uint8_t value, const Shape& oldShape, const Shape& newShape) {
- double doubleValue = (value - oldShape.offset) * oldShape.scale;
- return static_cast<uint8_t>(doubleValue / newShape.scale + newShape.offset);
-}
-
bool evalQuant8(const uint8_t* aData, const Shape& aShape, const uint8_t* bData,
const Shape& bShape, bool isMinimum, uint8_t* outputData,
const Shape& outputShape) {