| From: | Joseph Yu <kiddo831007(at)gmail(dot)com> |
|---|---|
| To: | pgsql-hackers(at)postgresql(dot)org |
| Subject: | use __builtin_clz to compute most significant bit set |
| Date: | 2023-02-25 19:13:53 |
| Message-ID: | CAA0WR3AgGcboOWfg-u_fBP+bqxEYLuqDaOuvufyrFE-vG4xFYA@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
hi community
This is the first time for me to submit a patch to Postgres community.
instead of using for loop to find the most significant bit set. we could
use __builtin_clz function to first find the number of leading zeros for
the mask and then we can find the index by 32 - __builtin_clz(mask).
diff --git a/src/port/fls.c b/src/port/fls.c
index 19b4221826..4f4c412732 100644
--- a/src/port/fls.c
+++ b/src/port/fls.c
@@ -54,11 +54,7 @@
int
fls(int mask)
{
- int bit;
-
if (mask == 0)
return (0);
- for (bit = 1; mask != 1; bit++)
- mask = (unsigned int) mask >> 1;
- return (bit);
+ return (sizeof(int) << 3) - __builtin_clz(mask);
}
Best Regards,
Joseph
| Attachment | Content-Type | Size |
|---|---|---|
| fls.patch | text/x-patch | 352 bytes |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Nathan Bossart | 2023-02-25 19:28:25 | Re: stopgap fix for signal handling during restore_command |
| Previous Message | Andres Freund | 2023-02-25 19:07:42 | Re: stopgap fix for signal handling during restore_command |