Re: Use pg_neg_s*_overflow() for open-coded negation overflow checks

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: Ewan Young <kdbase(dot)hack(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, David Rowley <dgrowleyml(at)gmail(dot)com>, Dagfinn Ilmari Mannsåker <ilmari(at)ilmari(dot)org>, Daniel Gustafsson <daniel(at)yesql(dot)se>, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Subject: Re: Use pg_neg_s*_overflow() for open-coded negation overflow checks
Date: 2026-09-01 23:16:14
Message-ID: apdcvq-ok1CKRcUH@paquier.xyz
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Sep 01, 2026 at 02:26:38PM +0800, Ewan Young wrote:
> I see this went in as 8e483af5515 in the meantime -- thanks a lot for
> the commit and the credit!
>
> FWIW, the two um/div sites left out, int2um() and int2div(), turn out
> to be mixed rather than clean: clang 15 compiles the int16
> __builtin_sub_overflow() one instruction worse on the hot path, while
> gcc 12 does one better (neg + jo).

Please note that I'd be OK to look at this stuff again if we have
other similar opportunities. I am not really convinced by your
v2-0002. The other opportunities in v2-0001 could be reconsidered if
rewritten in a different way, but it's also tricky to evaluate due to
the with/without builtin and compiler requirements.

I would accept something that proves to be a net benefit in rather new
versions of gcc and clang, as well as a net benefit with/without the
builtin. 8e483af5515 was exactly that, with less or the same amount of
instructions (the cash change was super nice) for all setups I have
tested (Linux and macos).
--
Michael

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2026-09-01 23:17:38 Re: Code-comment typos.
Previous Message Tobias Bussmann 2026-09-01 23:10:16 Re: Credits For v19