Thanks to visit codestin.com
Credit goes to github.com

Skip to content

convi: ensure double sum precision for floats #4418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2025

Conversation

lovell
Copy link
Member

@lovell lovell commented Mar 12, 2025

The CONV_FLOAT inner loop macro is used for both float and double images so the type of sum should probably match.

I've not had a chance to performance test this change but it has the possibility of making integer convolution of float images a tiny bit faster.

@jcupitt
Copy link
Member

jcupitt commented Mar 12, 2025

Hmm, this is likely to be slower, isn't it? Though I've not tried a benchmark ahem. C always uses double for all intermediate values, so:

    float a, b = ... something;
    float c = a * b;

Compiles to something like:

    float c = double_to_float(double_multiply(float_to_double(a), float_to_double(b)));

If you just use double everywhere you'll save a lot of unnecessary conversions.

float can useful for specifying storage (eg. float a[10] is nice and small), but you should generally use double everywhere else.

@lovell lovell force-pushed the convi-float-precision branch from f3ccc68 to 251f134 Compare March 12, 2025 11:19
@jcupitt
Copy link
Member

jcupitt commented Mar 12, 2025

Actually, I say that, but now I can't find a reference to float -> double promotion in the standard. I'll keep looking.

@lovell
Copy link
Member Author

lovell commented Mar 12, 2025

Yes, this might be a C99 thing. I think the problem here is that t is integer and p could be a float, which isn't automatically promoted. I've updated the PR to explicitly cast the multiplication up to double precision instead.

@jcupitt
Copy link
Member

jcupitt commented Mar 12, 2025

I tried with compiler explorer and this:

float fred(float a, float b, float c) { return a * b + c; }

Generates single-precision float mul and add instructions on all compilers I tested, so I'm wrong heh. Perhaps I was thinking of the promotion rules around untyped arguments.

@lovell lovell changed the title convi: ensure sum precision matches band format for floats convi: ensure double sum precision for floats Mar 12, 2025
@jcupitt jcupitt merged commit fd1ec8a into libvips:master Mar 17, 2025
6 checks passed
@lovell lovell deleted the convi-float-precision branch March 17, 2025 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants