From 6fe5f5db496f5962e1504991bf77e2b7d23a681f Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 30 Sep 2017 13:12:33 -0700 Subject: [PATCH 1/2] Simplify logcount implementation * src/data.c (HAVE_BUILTIN_POPCOUNTLL, logcount32, logcount64): Remove. (Flogcount): Simplify by using count_one_bits. --- src/data.c | 60 +++++++----------------------------------------------------- 1 file changed, 7 insertions(+), 53 deletions(-) diff --git a/src/data.c b/src/data.c index b595e3fb1a..fd8cdd19aa 100644 --- a/src/data.c +++ b/src/data.c @@ -3069,64 +3069,18 @@ usage: (logxor &rest INTS-OR-MARKERS) */) return arith_driver (Alogxor, nargs, args); } -#if GNUC_PREREQ (4, 1, 0) -#define HAVE_BUILTIN_POPCOUNTLL -#endif - -#ifndef HAVE_BUILTIN_POPCOUNTLL -static uint32_t -logcount32 (uint32_t b) -{ - b -= (b >> 1) & 0x55555555; - b = (b & 0x33333333) + ((b >> 2) & 0x33333333); - b = (b + (b >> 4)) & 0x0f0f0f0f; - return (b * 0x01010101) >> 24; -} - -static uint64_t -logcount64 (uint64_t b) -{ - b -= (b >> 1) & 0x5555555555555555ULL; - b = (b & 0x3333333333333333ULL) + ((b >> 2) & 0x3333333333333333ULL); - b = (b + (b >> 4)) & 0x0f0f0f0f0f0f0f0fULL; - return (b * 0x0101010101010101ULL) >> 56; -} -#endif /* HAVE_BUILTIN_POPCOUNTLL */ - DEFUN ("logcount", Flogcount, Slogcount, 1, 1, 0, doc: /* Return population count of VALUE. If VALUE is negative, the count is of its two's complement representation. */) - (register Lisp_Object value) + (Lisp_Object value) { - Lisp_Object res; - EMACS_UINT v; - CHECK_NUMBER (value); - - v = XUINT (value); -#ifdef HAVE_BUILTIN_POPCOUNTLL - if (v <= UINT_MAX) - XSETINT (res, __builtin_popcount (v)); - else if (v <= ULONG_MAX) - XSETINT (res, __builtin_popcountl (v)); - else if (v <= ULONG_LONG_MAX) - XSETINT (res, __builtin_popcountll (v)); -#else /* HAVE_BUILTIN_POPCOUNTLL */ - if (v <= UINT_MAX) - XSETINT (res, logcount32 (v)); - else if (v <= ULONG_MAX || v <= ULONG_LONG_MAX) - XSETINT (res, logcount64 (v)); -#endif /* HAVE_BUILTIN_POPCOUNTLL */ - else - { - unsigned int count; - for (count = 0; v; count++) - { - v &= v - 1; - } - XSETINT (res, count); - } - return res; + EMACS_UINT v = XUINT (value); + return make_number (EMACS_UINT_WIDTH <= UINT_WIDTH + ? count_one_bits (v) + : EMACS_UINT_WIDTH <= ULONG_WIDTH + ? count_one_bits_l (v) + : count_one_bits_ll (v)); } static Lisp_Object -- 2.13.5