Re: Optimize mul_var() for var1ndigits >= 8

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: Joel Jacobson <joel(at)compiler(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Optimize mul_var() for var1ndigits >= 8
Date: 2024-08-12 22:56:38
Message-ID: CAEZATCVe1yLxzqGyeiMuVekAZzAUW+_56VHMTduB9_xJVb1=Hg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, 12 Aug 2024 at 16:17, Joel Jacobson <joel(at)compiler(dot)org> wrote:
>
> On Mon, Aug 12, 2024, at 17:14, Joel Jacobson wrote:
> > The case found with the smallest rscale adjustment was this one:
> > -[ RECORD 1 ]------+--------------------------------
> > var1 | 0.0000000000009873307197037692
> > var2 | 0.426697279270850
> > rscale_adjustment | -15
> > expected | 0.0000000000004212913318381285
> > numeric_mul_rscale | 0.0000000000004212913318381284
> > diff | -0.0000000000000000000000000001
>

Hmm, interesting example. There will of course always be cases where
the result isn't exact, but HEAD does produce the expected result in
this case, and the intention is to always produce a result at least as
accurate as HEAD, so it isn't working as expected.

Looking more closely, the problem is that to fully compute the
required guard digits, it is necessary to compute at least one extra
output base-NBASE digit, because the product of base-NBASE^2 digits
contributes to the next base-NBASE digit up. So instead of

maxdigitpairs = (maxdigits + 1) / 2;

we should do

maxdigitpairs = maxdigits / 2 + 1;

Additionally, since maxdigits is based on res_weight, we should
actually do the res_weight adjustments for odd-length inputs before
computing maxdigits. (Otherwise we're actually computing more digits
than strictly necessary for odd-length inputs, so this is a minor
optimisation.)

Updated patch attached, which fixes the above example and all the
other differences produced by your test. I think, with a little
thought, it ought to be possible to produce examples that round
incorrectly in a more systematic (less brute-force) way. It should
then be possible to construct examples where the patch differs from
HEAD, but hopefully only by being more accurate, not less.

Regards,
Dean

Attachment Content-Type Size
v5-0001-Extend-mul_var_short-to-5-and-6-digit-inputs.patch text/x-patch 11.1 KB
v5-0002-Optimise-numeric-multiplication-using-base-NBASE-.patch text/x-patch 13.9 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2024-08-12 23:15:22 Re: libpq: Fix lots of discrepancies in PQtrace
Previous Message Alvaro Herrera 2024-08-12 22:54:37 Re: Missing reflection in nls.mk from recent basebackup changes