From fe2ac6d7b9cbc7de4befa128a1ac514712030e74 Mon Sep 17 00:00:00 2001 From: Eric Kunze Date: Thu, 31 Aug 2023 16:14:05 -0700 Subject: Add integer divide with floor for coordinate calculation Define idiv_floor to give equivalent behavior to the floating-point floor function for image coordinate calculation. Change-Id: Id6268794b1e3ce5cc1114bda74dd06b892457a8e Signed-off-by: Eric Kunze --- chapters/image.adoc | 4 ++-- chapters/pseudocode.adoc | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/chapters/image.adoc b/chapters/image.adoc index da839f8..59e5ddf 100644 --- a/chapters/image.adoc +++ b/chapters/image.adoc @@ -85,8 +85,8 @@ for_each(0 <= n < N, 0 <= oy < OH, 0 <= ox < OW; 0 <= c < C) { int32_t y = oy * scale_y_d + offset_y; int32_t x = ox * scale_x_d + offset_x; - int16_t iy = floor(y / scale_y_n); - int16_t ix = floor(x / scale_x_n); + int16_t iy = idiv_floor(y, scale_y_n); + int16_t ix = idiv_floor(x, scale_x_n); int16_t ry = y - iy * scale_y_n; // (y % scale_y_n) int16_t rx = x - ix * scale_x_n; // (x % scale_x_n) diff --git a/chapters/pseudocode.adoc b/chapters/pseudocode.adoc index 55c35d4..d674c9c 100644 --- a/chapters/pseudocode.adoc +++ b/chapters/pseudocode.adoc @@ -477,6 +477,16 @@ int32_t idiv_check(int32_t input1, int32_t input2) { return input1 / input2; // exact quotient without rounding } +// perform an integer division with rounding towards minus infinity + +int32_t idiv_floor(int32_t input1, int32_t input2) { + int32_t rval = input1 / input2; + if (rval * input2 > input1) { + rval--; + } + return rval; +} + int32_t length(in_t input) return number of elements in input list -- cgit v1.2.1