GNU bug report logs -
#22689
25.1.50; implement logcount
Previous Next
Reported by: Mark Oteiza <mvoteiza <at> udel.edu>
Date: Mon, 15 Feb 2016 23:20:01 UTC
Severity: wishlist
Found in version 25.1.50
Done: Mark Oteiza <mvoteiza <at> udel.edu>
Bug is archived. No further changes may be made.
Full log
View this message in rfc822 format
Hi,
I made an attempt implementing this:
diff --git a/src/data.c b/src/data.c
index e070be6c20..1332173dcd 100644
--- a/src/data.c
+++ b/src/data.c
@@ -3069,6 +3069,57 @@ usage: (logxor &rest INTS-OR-MARKERS) */)
return arith_driver (Alogxor, nargs, args);
}
+#if defined (__POPCNT__) && defined (__GNUC__) && (__GNUC__> 4 || (__GNUC__== 4 && __GNUC_MINOR__> 1))
+#define HAVE_BUILTIN_POPCOUNTLL
+#endif
+
+static uint32_t
+bitcount32 (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
+bitcount64 (uint64_t b)
+{
+ b -= (b >> 1) & 0x5555555555555555;
+ b = (b & 0x3333333333333333) + ((b >> 2) & 0x3333333333333333);
+ b = (b + (b >> 4)) & 0x0f0f0f0f0f0f0f0f;
+ return (b * 0x0101010101010101) >> 56;
+}
+
+DEFUN ("logcount", Flogcount, Slogcount, 1, 1, 0,
+ doc: /* Return population count of VALUE. */)
+ (register Lisp_Object value)
+{
+ Lisp_Object res;
+
+ CHECK_NUMBER (value);
+
+#ifdef HAVE_BUILTIN_POPCOUNTLL
+ XSETINT (res, __builtin_popcount (XUINT (value)));
+#else /* HAVE_BUILTIN_POPCOUNTLL */
+ if (XINT (value) <= UINT_MAX)
+ XSETINT (res, bitcount32 (XUINT (value)));
+ else if (XINT (value) <= ULONG_MAX)
+ XSETINT (res, bitcount64 (XUINT (value)));
+ else
+ {
+ int count;
+ EMACS_UINT v = XUINT (value);
+ for (count = 0; v; count++)
+ {
+ v &= v - 1;
+ }
+ XSETINT (res, v);
+ }
+#endif /* HAVE_BUILTIN_POPCOUNTLL */
+ return res;
+}
+
static Lisp_Object
ash_lsh_impl (Lisp_Object value, Lisp_Object count, bool lsh)
{
@@ -3856,6 +3907,7 @@ syms_of_data (void)
defsubr (&Slogand);
defsubr (&Slogior);
defsubr (&Slogxor);
+ defsubr (&Slogcount);
defsubr (&Slsh);
defsubr (&Sash);
defsubr (&Sadd1);
This bug report was last modified 7 years and 291 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.