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

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Postgres hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Optimize multiplications/divisions by 2 using bit shifts in hot paths
Date: 2025-09-19 02:10:58
Message-ID: CAEoWx2kWew-NMm7cRbAvy4FU0hqVCDUnBbU-P+gHou7ZS6HLRg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Hackers,

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.

For signed int:
* /2 exactly equals to >>1 when a value is non-negative, this
patch ensures only changes non-negative variables
* *2 might be different from <<1 if a value is close to MAX_INT, however,
in that case x*2 will result in a undefined result, thus existing code
should have ensured *2 should not exceed MAX_INT.

"make check" passes with this patch.

Best regards,
Chao Li (Evan)
---------------------
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachment Content-Type Size
v1-0001-Optimize-multiplications-divisions-by-2-using-bit.patch application/octet-stream 7.1 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David Rowley 2025-09-19 02:22:05 Re: Optimize multiplications/divisions by 2 using bit shifts in hot paths
Previous Message David Rowley 2025-09-19 01:43:02 Re: Having postgresql.org link to cgit instead of gitweb