Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p6kmq-0004G5-NQ for pgsql-general@arkaria.postgresql.org; Sun, 18 Dec 2022 03:56:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p6kmo-00059d-I2 for pgsql-general@arkaria.postgresql.org; Sun, 18 Dec 2022 03:56:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p6kmn-00059T-W6 for pgsql-general@lists.postgresql.org; Sun, 18 Dec 2022 03:56:26 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1p6kmh-0006sT-H6 for pgsql-general@lists.postgresql.org; Sun, 18 Dec 2022 03:56:25 +0000 Received: by mail-pl1-x631.google.com with SMTP id 17so6061154pll.0 for ; Sat, 17 Dec 2022 19:56:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=pD0Us9g436+UDEDVVaf+WKyXz7MPMKuO6naPkbsLEUY=; b=ek6NFhGnmHRH2RRoHHmJBSTje8WqFEuDs7/8PRJlukARgvhr3KwFrzjFUadhEfjnbR qXsHTUhQmxBvYzw12OhaXtRYmGzYhqCxY/ZXlfht+3YiIzlvZCbsKX4NUa8PrJ32kCJf nR8s2BzXIi6Qpyoj8jzn8NiSsmCaUvhCwNwsHM1EBDASXW22bgI6Kg5AuLwDDkNxuon7 bRiBM2HCAvJA4vaWZJp6AGs4v3WDJwzYZl5kZuTnVT4Fq7aYr/dPI4ztLNmtAdpF5j2z LaV7jWNs3AhYbWCWoFmhwgRhr4Iy2Y7QJTg3G9fr18kKxWHXqdoqLbZ7QC8GkxzYByLB dC/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=pD0Us9g436+UDEDVVaf+WKyXz7MPMKuO6naPkbsLEUY=; b=Bmq8iYzyzrYovvdpQDNsQ1jHPHNsQkr5l5TQAoV/L3uZg6993NBfvQGHOHFckYoznR dFRuyp7orPc6QIMNMyqG+TOdM3Wle/XCpBwyqa9LznPR7oQHnejPjjmw+BrcSRDlDv4V r/oxXRA7zd/Iiz97AKefHNiimAldRx7hoqnr1yJ1IEMnv1ulWjjy0IRQNZC56ALPx2V1 IkkfOdaA/sFizJKy9hFu0dbIaIMBx69zIMUljNuO5Q7ZDSz9xxAt9VAgU51UJxYa9/+j maZISRd71lQEbcTn11eRUWKUC4W/Ryws9Id0I4+wqDFomQ4r8WNRC5uV7f51bCE0lR+t IEoA== X-Gm-Message-State: AFqh2kocnnRK0Lhs+MKm8QGfl4DnpzeDSe/xs+S+nLn4S0CP1eequJtL V3ev5MqhMeZ+S9w1QGFeb2A= X-Google-Smtp-Source: AMrXdXuilQTdUyDmkEw451WDC5hNH5UICD3afsJwE69MX9zCev6UDlOwNxvFqrLYFLS6TkbgGgO2qQ== X-Received: by 2002:a17:902:6ac5:b0:189:f1de:2d4e with SMTP id i5-20020a1709026ac500b00189f1de2d4emr4804503plt.45.1671335777969; Sat, 17 Dec 2022 19:56:17 -0800 (PST) Received: from ?IPV6:2601:681:5a00:f790:6b0:2592:a028:5aa4? ([2601:681:5a00:f790:6b0:2592:a028:5aa4]) by smtp.gmail.com with ESMTPSA id s11-20020a170902ea0b00b00178143a728esm4266875plg.275.2022.12.17.19.56.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 17 Dec 2022 19:56:17 -0800 (PST) Content-Type: multipart/alternative; boundary="------------o00oUzQFcQpO0mTOOA7OT0lh" Message-ID: <806b2307-31f8-5c1b-3503-de7d20ad282c@gmail.com> Date: Sat, 17 Dec 2022 20:56:16 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: integer square root function proposed Content-Language: en-CA To: "Martin L. Buchanan" Cc: pgsql-general@lists.postgresql.org References: From: Rob Sargent In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------o00oUzQFcQpO0mTOOA7OT0lh Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 12/17/22 20:40, Martin L. Buchanan wrote: > Dear Rob and all readers: > > Generating prime numbers is one example where you use integer square > root in the inner loop, going from integer to integer. > > Calculating an integer square root from an integer input may have a > more efficient algorithm than doing so in floating-point, with the > caveat that an underlying processor architecture may provide > floating-point square root instructions but not integer square root > instructions. In that particular case an implementation could use the > floating-point instructions internally. > > Some but not all programming languages provide isqrt > directly, math.isqrt in Python or isqrt in Common Lisp for example. > > It would be a useful and convenient function and would not, I believe, > impair the other features of PostgreSQL in any way. > > That said, as a PG novice (2+ years now), I completely defer to the > greater wisdom of those much more involved in PostgreSQL. So something > for you all to think about. > Seasons greetings to you and welcome to the list.  Here we generally bottom post. I have zero say in whether or not isqrt might get added to postgres but in the meantime you might craft your own function (sql or plpgsql) which does the necessary casting.  Or are you specifically after a faster function?  And would a plpythonu function using their isqrt be fast enough.  (Note the "u" in plpythonu means "untrusted") --------------o00oUzQFcQpO0mTOOA7OT0lh Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 12/17/22 20:40, Martin L. Buchanan wrote:
Dear Rob and all readers:

Generating prime numbers is one example where you use integer square root in the inner loop, going from integer to integer.

Calculating an integer square root from an integer input may have a more efficient algorithm than doing so in floating-point, with the caveat that an underlying processor architecture may provide floating-point square root instructions but not integer square root instructions. In that particular case an implementation could use the floating-point instructions internally.

Some but not all programming languages provide isqrt directly, math.isqrt in Python or isqrt in Common Lisp for example.

It would be a useful and convenient function and would not, I believe, impair the other features of PostgreSQL in any way.

That said, as a PG novice (2+ years now), I completely defer to the greater wisdom of those much more involved in PostgreSQL. So something for you all to think about.


Seasons greetings to you and welcome to the list.  Here we generally bottom post.

I have zero say in whether or not isqrt might get added to postgres but in the meantime you might craft your own function (sql or plpgsql) which does the necessary casting.  Or are you specifically after a faster function?  And would a plpythonu function using their isqrt be fast enough.  (Note the "u" in plpythonu means "untrusted")



--------------o00oUzQFcQpO0mTOOA7OT0lh--