From fb879821119c93109198a576e7b93e80b3dc0616 Mon Sep 17 00:00:00 2001 From: TatWai Chong Date: Thu, 31 Aug 2023 16:58:27 -0700 Subject: Add integer divide with floor for coordinate calculation Align with the change in the spec. Define idiv_floor to give equivalent behavior to the floating-point floor function for image coordinate calculation. Change-Id: Ice06d354d58b1bb0dfedab55c9cc9eac1598b50c Signed-off-by: TatWai Chong --- reference_model/src/graph_node.cc | 13 +++++++++++++ reference_model/src/graph_node.h | 1 + reference_model/src/ops/image.cc | 9 +++------ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/reference_model/src/graph_node.cc b/reference_model/src/graph_node.cc index 1781e40..791ed45 100644 --- a/reference_model/src/graph_node.cc +++ b/reference_model/src/graph_node.cc @@ -229,3 +229,16 @@ int GraphNode::idiv_check(int input1, int input2, int& result) result = input1 / input2; return 0; } + +// perform an integer division with rounding towards minus infinity +int GraphNode::idiv_floor(int input1, int input2) +{ + ERROR_IF(input2 == 0, "idiv_floor: input2 must not be zero"); + int result = input1 / input2; + + if (result * input2 > input1) + { + result--; + } + return result; +} diff --git a/reference_model/src/graph_node.h b/reference_model/src/graph_node.h index 900f4b8..440ca04 100644 --- a/reference_model/src/graph_node.h +++ b/reference_model/src/graph_node.h @@ -256,6 +256,7 @@ public: // Helper functions. int idiv_check(int input1, int input2, int& result); + int idiv_floor(int input1, int input2); protected: // Print out a node validation error diff --git a/reference_model/src/ops/image.cc b/reference_model/src/ops/image.cc index cf2b60a..b1b762f 100644 --- a/reference_model/src/ops/image.cc +++ b/reference_model/src/ops/image.cc @@ -159,16 +159,15 @@ int OpResize::eval() int32_t y = oy * scale_y_d + offset_y; int32_t x = ox * scale_x_d + offset_x; - int32_t iy; - int32_t ix; + int16_t iy = idiv_floor(y, scale_y_n); + int16_t ix = idiv_floor(x, scale_x_n); + resize_t dy; resize_t dx; if (std::is_same::value) { const double fy_double = static_cast(y) / static_cast(scale_y_n); const double fx_double = static_cast(x) / static_cast(scale_x_n); - iy = floor(fy_double); - ix = floor(fx_double); dy = (resize_t)(fy_double - iy); dx = (resize_t)(fx_double - ix); @@ -177,8 +176,6 @@ int OpResize::eval() { const float fy = static_cast(y) / static_cast(scale_y_n); const float fx = static_cast(x) / static_cast(scale_x_n); - iy = floor(fy); - ix = floor(fx); if (std::is_floating_point::value || (typeid(resize_t) == typeid(Eigen::bfloat16)) || (typeid(resize_t) == typeid(half_float::half))) -- cgit v1.2.1