diff options
| author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2025-04-26 16:23:59 -0700 |
|---|---|---|
| committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2025-04-26 16:40:29 -0700 |
| commit | 68ba622791713cf82017c3c49162b94abaa3d139 (patch) | |
| tree | e103a2f6d60abcf2794def3566fe4d66cc3163cb | |
| parent | e9394ac675e4395f81ac1f19d0736dbe3478d6a1 (diff) | |
Use __builtin_popcount if available to replace bit_count()
If the compiler knows of a better method for counting the number of
bits set in a word for the target CPU, let it use that, instead of the
classic algorithm optimized for PDP-6.
Based on xorg/lib/libxext@490a25e6f8a4d2482af4364c700b68ad11a4d10b
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Part-of: <https://gitlab.freedesktop.org/xorg/test/rendercheck/-/merge_requests/4>
| -rw-r--r-- | main.c | 12 | ||||
| -rw-r--r-- | rendercheck.h | 27 |
2 files changed, 24 insertions, 15 deletions
@@ -42,18 +42,6 @@ int pixmap_move_iter = 1; int win_width = 40; int win_height = 200; -int -bit_count(int i) -{ - int count; - - count = (i >> 1) & 033333333333; - count = i - count - ((count >> 1) & 033333333333); - count = (((count + (count >> 3)) & 030707070707) % 077); - /* HAKMEM 169 */ - return count; -} - /* This is not complete, but decent enough for now.*/ void describe_format(char **desc, const char *prefix, XRenderPictFormat *format) diff --git a/rendercheck.h b/rendercheck.h index 30ced40..d8c5e41 100644 --- a/rendercheck.h +++ b/rendercheck.h @@ -49,6 +49,30 @@ static inline void errx(int eval, const char *fmt, ...) #define max(a, b) (a > b ? a : b) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#ifndef __has_builtin +# define __has_builtin(x) 0 /* Compatibility with older compilers */ +#endif + +#if __has_builtin(__builtin_popcount) +# define bit_count __builtin_popcount +#else +/* + * Count the number of bits set to 1 in a 32-bit word. + * Algorithm from MIT AI Lab Memo 239: "HAKMEM", ITEM 169. + * https://dspace.mit.edu/handle/1721.1/6086 + */ +static inline int +bit_count(int i) +{ + int count; + + count = (i >> 1) & 033333333333; + count = i - count - ((count >> 1) & 033333333333); + count = (((count + (count >> 3)) & 030707070707) % 077); + return count; +} +#endif + typedef struct _color4d { double r, g, b, a; @@ -165,9 +189,6 @@ extern int num_colors; void describe_format(char **desc, const char *prefix, XRenderPictFormat *format); -int -bit_count(int i); - void print_tests(FILE *file, int tests); |
