Re: Some improvements to numeric sqrt() and ln()

From: Tels <nospam-pg-abuse(at)bloodgate(dot)com>
To: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Some improvements to numeric sqrt() and ln()
Date: 2020-03-03 00:17:02
Message-ID: fbeb0c1236f61c121b4dbc0f21fa49c4@bloodgate.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Dear Dean,

On 2020-03-01 20:47, Dean Rasheed wrote:
> On Fri, 28 Feb 2020 at 08:15, Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
> wrote:
>>
>> It's possible that there are further gains to be had in the sqrt()
>> algorithm on platforms that support 128-bit integers, but I haven't
>> had a chance to investigate that yet.
>>
>
> Rebased patch attached, now using 128-bit integers for part of
> sqrt_var() on platforms that support them. This turned out to be well
> worth it (1.5 to 2 times faster than the previous version if the
> result has less than 30 or 40 digits).

Thank you for these patches, these sound like really nice improvements.
One thing can to my mind while reading the patch:

+ * If r < 0 Then
+ * Let r = r + 2*s - 1
+ * Let s = s - 1

+ /* s is too large by 1; let r = r + 2*s - 1 and s = s - 1 */
+ r_int64 += 2 * s_int64 - 1;
+ s_int64--;

This can be reformulated as:

+ * If r < 0 Then
+ * Let r = r + s
+ * Let s = s - 1
+ * Let r = r + s

+ /* s is too large by 1; let r = r + 2*s - 1 and s = s - 1 */
+ r_int64 += s_int64;
+ s_int64--;
+ r_int64 += s_int64;

which would remove one mul/shift and the temp. variable. Mind you, I
have
not benchmarked this, so it might make little difference, but maybe it
is
worth trying it.

Best regards,

Tels

Attachment Content-Type Size
numeric-sqrt-v2.patch text/x-patch 21.3 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Kyotaro Horiguchi 2020-03-03 00:27:20 Re: pg_stat_progress_basebackup - progress reporting for pg_basebackup, in the server side
Previous Message Peter Geoghegan 2020-03-03 00:03:52 Re: Improve search for missing parent downlinks in amcheck