Re: Optimize multiplications/divisions by 2 using bit shifts in hot paths

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Cc: Postgres hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Optimize multiplications/divisions by 2 using bit shifts in hot paths
Date: 2025-09-19 02:22:05
Message-ID: CAApHDvq7U5T5vyW9rvKj-xgxV-Sx3XDr30sMH0ydFbMDDxcwcQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, 19 Sept 2025 at 14:11, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> wrote:
> This patch uses left/right shift operators to optimize *2 and /2 operations in some functions that are in critical paths.
>
> For unsigned int, *2 and /2 exactly equal to <<1 and >>1.

The compiler is most likely going to do this anyway. Try it out at
https://godbolt.org/z/Y538Yd4Ka

What maybe is worth looking at is verifying which of these variables
is signed when it really should be unsigned. Dividing by 2 and >> 1
aren't the same for negative numbers.

David

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Fujii Masao 2025-09-19 02:43:28 Re: Suggestion to add --continue-client-on-abort option to pgbench
Previous Message Chao Li 2025-09-19 02:10:58 Optimize multiplications/divisions by 2 using bit shifts in hot paths