Re: [HACKERS] [PATCH v2] Add bit operations util header
* Jeremy Kerr: +#if defined(__GNUC__) \ + (defined(__ppc__) || defined(__powerpc__) || \ + defined(__ppc64__) || defined (__powerpc64__)) If you require GCC anyway, you can use __builtin_clz instead. (It's been available since GCC 4.1 at least.) -- Florian Weimerfwei...@bfk.de BFK edv-consulting GmbH http://www.bfk.de/ Kriegsstraße 100 tel: +49-721-96201-1 D-76133 Karlsruhe fax: +49-721-96201-99 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers
Re: [HACKERS] [PATCH v2] Add bit operations util header
Florian, +#if defined(__GNUC__) \ + (defined(__ppc__) || defined(__powerpc__) || \ +defined(__ppc64__) || defined (__powerpc64__)) If you require GCC anyway, you can use __builtin_clz instead. (It's been available since GCC 4.1 at least.) Because now we have to test the compiler *and* the version as well? But I do agree that using the builtins makes for much better code; I'm looking at a future change that does this. Cheers, Jeremy -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers
Re: [HACKERS] [PATCH v2] Add bit operations util header
* Jeremy Kerr: Florian, +#if defined(__GNUC__) \ + (defined(__ppc__) || defined(__powerpc__) || \ + defined(__ppc64__) || defined (__powerpc64__)) If you require GCC anyway, you can use __builtin_clz instead. (It's been available since GCC 4.1 at least.) Because now we have to test the compiler *and* the version as well? This builtin is not architecture-specific, so you'd save the architecture check. -- Florian Weimerfwei...@bfk.de BFK edv-consulting GmbH http://www.bfk.de/ Kriegsstraße 100 tel: +49-721-96201-1 D-76133 Karlsruhe fax: +49-721-96201-99 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers
Re: [HACKERS] [PATCH v2] Add bit operations util header
Florian Weimer fwei...@bfk.de writes: * Jeremy Kerr: Because now we have to test the compiler *and* the version as well? This builtin is not architecture-specific, so you'd save the architecture check. The appropriate way to handle it would be a configure probe to see if the function is available, thus avoiding any wired-in knowledge about compiler or compiler version *or* architecture. The other thing I didn't like about the patch was the assumption that it's okay to have a static inline function in a header. You can get away with that in gcc but *not* in other compilers. Look at the existing coding patterns for, eg, list_head; then go thou and do likewise. Or, since there's currently no need for the code outside aset.c, forget about putting it in a header and just plop it into aset.c. regards, tom lane -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers
Re: [HACKERS] [PATCH v2] Add bit operations util header
Hi Tom, The other thing I didn't like about the patch was the assumption that it's okay to have a static inline function in a header. You can get away with that in gcc but *not* in other compilers. Gee, you user-space guys have it tough! :D Point taken, will rework. Look at the existing coding patterns for, eg, list_head; then go thou and do likewise. Or, since there's currently no need for the code outside aset.c, forget about putting it in a header and just plop it into aset.c. OK, I'll add a configure check and conditionally use the builtin if it's available. I have some other patches that could be improved by using other builtins, so it would be a good opportunity to figure out a nice pattern for doing this. Cheers, Jeremy -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers
[HACKERS] [PATCH v2] Add bit operations util header
Add a utility header for simple bit operatios - bitops.h. At present, just contains the fls() (find last set bit) function. Signed-off-by: Jeremy Kerr j...@ozlabs.org --- v2: only use inline asm with gcc --- src/include/utils/bitops.h | 53 + 1 file changed, 53 insertions(+) diff --git a/src/include/utils/bitops.h b/src/include/utils/bitops.h new file mode 100644 index 000..4f2bbc9 --- /dev/null +++ b/src/include/utils/bitops.h @@ -0,0 +1,53 @@ +/*- + * + * bitops.h + * Simple bit operations. + * + * Portions Copyright (c) 2009, PostgreSQL Global Development Group + * + * $PostgreSQL$ + * + *- + */ +#ifndef BITOPS_H +#define BITOPS_H + +#if defined(__GNUC__) \ + (defined(__ppc__) || defined(__powerpc__) || \ +defined(__ppc64__) || defined (__powerpc64__)) + +static inline int +fls(unsigned int x) +{ + int lz; + asm(cntlz %0,%1 : =r (lz) : r (x)); + return 32 - lz; +} + +#else /* !(gcc powerpc) */ + +/* Architecture-independent implementations */ + +/* + * fls: find last set bit. + * + * Returns the 1-based index of the most-significant bit in x. The MSB + * is bit number 32, the LSB is bit number 1. If x is zero, returns zero. + */ +static inline int +fls(unsigned int x) +{ + int ls = 0; + + while (x != 0) + { + ls++; + x = 1; + } + + return ls; +} + +#endif + +#endif /* BITOPS_H */ -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers