From c4b20d0a3a7a53a0480a4c21c68b2c1794512629 Mon Sep 17 00:00:00 2001 From: John Bowler Date: Mon, 27 Jan 2025 10:51:01 -0800 Subject: [PATCH] test: add configuration tests and fix bugs The two new configuation tests, fixed.dfa and float-fixed.dfa verify that the 'standard' configuration of libpng works without floating point arithmetic. Signed-off-by: John Bowler --- contrib/conftest/fixed.dfa | 15 +++++++++++++++ contrib/conftest/float-fixed.dfa | 14 ++++++++++++++ png.c | 24 ++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 contrib/conftest/fixed.dfa create mode 100644 contrib/conftest/float-fixed.dfa diff --git a/contrib/conftest/fixed.dfa b/contrib/conftest/fixed.dfa new file mode 100644 index 000000000..cb45f0136 --- /dev/null +++ b/contrib/conftest/fixed.dfa @@ -0,0 +1,15 @@ +# fixed.dfa +# Build time configuration of libpng +# +# Author: John Bowler +# Copyright: (c) John Bowler, 2025 +# Usage rights: +# To the extent possible under law, the author has waived all copyright and +# related or neighboring rights to this work. This work is published from: +# United States. +# +# Test the standard libpng configuration without floating point (the internal +# fixed point implementations are used instead). +# +option FLOATING_ARITHMETIC off +option FLOATING_POINT off diff --git a/contrib/conftest/float-fixed.dfa b/contrib/conftest/float-fixed.dfa new file mode 100644 index 000000000..c13da3198 --- /dev/null +++ b/contrib/conftest/float-fixed.dfa @@ -0,0 +1,14 @@ +# fixed-float.dfa +# Build time configuration of libpng +# +# Author: John Bowler +# Copyright: (c) John Bowler, 2025 +# Usage rights: +# To the extent possible under law, the author has waived all copyright and +# related or neighboring rights to this work. This work is published from: +# United States. +# +# Test the standard libpng configuration with the fixed point internal +# implementation in place of the default floating point +# +option FLOATING_ARITHMETIC off diff --git a/png.c b/png.c index 660a0b920..0ce05310d 100644 --- a/png.c +++ b/png.c @@ -2931,6 +2931,30 @@ png_gamma_significant(png_fixed_point gamma_val) gamma_val > PNG_FP_1 + PNG_GAMMA_THRESHOLD_FIXED; } +#ifndef PNG_FLOATING_ARITHMETIC_SUPPORTED +/* A local convenience routine. */ +static png_fixed_point +png_product2(png_fixed_point a, png_fixed_point b) +{ + /* The required result is a * b; the following preserves accuracy. */ +#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED /* Should now be unused */ + double r = a * 1E-5; + r *= b; + r = floor(r+.5); + + if (r <= 2147483647. && r >= -2147483648.) + return (png_fixed_point)r; +#else + png_fixed_point res; + + if (png_muldiv(&res, a, b, 100000) != 0) + return res; +#endif + + return 0; /* overflow */ +} +#endif /* FLOATING_ARITHMETIC */ + png_fixed_point png_reciprocal2(png_fixed_point a, png_fixed_point b) {