Skip to content

Commit 4f658dc

Browse files
committed
Support fls().
The immediate impetus for this is that Noah Misch's patch to elide unnecessary table and index rebuilds when changing typmod for temporal types uses it; and this is extracted from that patch, with some further commentary by me. But it seems logically separate from the remainder of the patch, so I'm committing it separately; this is not the first time someone has wanted fls() in the backend and probably won't be the last. If we end up using this in more performance-critical spots it may be worthwhile to add some architecture-specific optimizations to our src/port version of fls() - e.g. any x86 platform can implement this using the assembly instruction BSRL. But performance won't matter a bit for assessing typmod changes, so I'm not worried about that right now.
1 parent f7d7dad commit 4f658dc

File tree

5 files changed

+74
-2
lines changed

5 files changed

+74
-2
lines changed

configure

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20833,7 +20833,8 @@ LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
2083320833

2083420834

2083520835

20836-
for ac_func in crypt getopt getrusage inet_aton random rint srandom strdup strerror strlcat strlcpy strtol strtoul
20836+
20837+
for ac_func in crypt fls getopt getrusage inet_aton random rint srandom strdup strerror strlcat strlcpy strtol strtoul
2083720838
do
2083820839
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
2083920840
{ $as_echo "$as_me:$LINENO: checking for $ac_func" >&5

configure.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,7 @@ fi
13351335
pgac_save_LIBS="$LIBS"
13361336
LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
13371337

1338-
AC_REPLACE_FUNCS([crypt getopt getrusage inet_aton random rint srandom strdup strerror strlcat strlcpy strtol strtoul])
1338+
AC_REPLACE_FUNCS([crypt fls getopt getrusage inet_aton random rint srandom strdup strerror strlcat strlcpy strtol strtoul])
13391339

13401340
case $host_os in
13411341

src/include/pg_config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@
158158
/* Define to 1 if you have the `fdatasync' function. */
159159
#undef HAVE_FDATASYNC
160160

161+
/* Define to 1 if you have the `fls' function. */
162+
#undef HAVE_FLS
163+
161164
/* Define to 1 if you have the `fpclass' function. */
162165
#undef HAVE_FPCLASS
163166

src/include/port.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,10 @@ extern double pg_erand48(unsigned short xseed[3]);
396396
extern long pg_lrand48(void);
397397
extern void pg_srand48(long seed);
398398

399+
#ifndef HAVE_FLS
400+
extern int fls(int mask);
401+
#endif
402+
399403
#ifndef HAVE_FSEEKO
400404
#define fseeko(a, b, c) fseek(a, b, c)
401405
#define ftello(a) ftell(a)

src/port/fls.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* fls.c
4+
* finds the last (most significant) bit that is set
5+
*
6+
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
7+
*
8+
*
9+
* IDENTIFICATION
10+
* src/port/fls.c
11+
*
12+
* This file was taken from FreeBSD to provide an implementation of fls()
13+
* for platforms that lack it. Note that the operating system's version may
14+
* be substantially more efficient thatn ours, since some platforms have an
15+
* assembly instruction that does exactly this.
16+
*
17+
* The FreeBSD copyright terms follow.
18+
*/
19+
20+
/*-
21+
* Copyright (c) 1990, 1993
22+
* The Regents of the University of California. All rights reserved.
23+
*
24+
* Redistribution and use in source and binary forms, with or without
25+
* modification, are permitted provided that the following conditions
26+
* are met:
27+
* 1. Redistributions of source code must retain the above copyright
28+
* notice, this list of conditions and the following disclaimer.
29+
* 2. Redistributions in binary form must reproduce the above copyright
30+
* notice, this list of conditions and the following disclaimer in the
31+
* documentation and/or other materials provided with the distribution.
32+
* 4. Neither the name of the University nor the names of its contributors
33+
* may be used to endorse or promote products derived from this software
34+
* without specific prior written permission.
35+
*
36+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46+
* SUCH DAMAGE.
47+
*/
48+
49+
#include "c.h"
50+
51+
/*
52+
* Find Last Set bit
53+
*/
54+
int
55+
fls(int mask)
56+
{
57+
int bit;
58+
59+
if (mask == 0)
60+
return (0);
61+
for (bit = 1; mask != 1; bit++)
62+
mask = (unsigned int)mask >> 1;
63+
return (bit);
64+
}

0 commit comments

Comments
 (0)