fix: Round to nearest in the fixed-point `png_muldiv` In builds without floating point arithmetic, `png_muldiv` rounded the magnitude of the quotient up whenever the remainder reached half the divisor rounded down. With an odd divisor, that also rounded up fractions just below one half, and with a divisor of 1 it moved every nonzero result one step away from zero. For example, `png_get_pixel_aspect_ratio_fixed` returned 100001 for 1 by 1 pixels per unit, and 33334 for 3 by 1. Round the magnitude up only when twice the remainder reaches the divisor. This is a cherry-pick of commit 0fdef309cc33a24081943c2ae4096950426e0216 from branch 'libpng18'.
diff --git a/png.c b/png.c index 98206c8..0ee6119 100644 --- a/png.c +++ b/png.c
@@ -2865,8 +2865,12 @@ } } - /* Handle the rounding, without wrapping the result to 0. */ - if (s00 >= (D >> 1) && result < 0xffffffffU) + /* Round to nearest, with ties away from zero, without wrapping + * the result to zero. + * The remainder s00 is less than D, so (D - s00) cannot wrap. + * The test (s00 >= D - s00) is equivalent to (2 * s00 >= D). + */ + if (s00 >= D - s00 && result < 0xffffffffU) ++result; /* Check for overflow. */