From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Mark Oteiza Newsgroups: gmane.emacs.bugs Subject: bug#22689: [PATCH] Add logcount Date: Fri, 29 Sep 2017 13:41:34 -0400 Message-ID: <20170929174134.e6uxcr63dtyvd6f4@logos.localdomain> References: <87h9h9pnof.fsf@udel.edu> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1506706934 12993 195.159.176.226 (29 Sep 2017 17:42:14 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 29 Sep 2017 17:42:14 +0000 (UTC) User-Agent: NeoMutt/20170912-48-0df7d3-dirty To: 22689@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Fri Sep 29 19:42:11 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dxzIb-0002ep-7W for geb-bug-gnu-emacs@m.gmane.org; Fri, 29 Sep 2017 19:42:05 +0200 Original-Received: from localhost ([::1]:36424 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dxzIi-0002OS-Fe for geb-bug-gnu-emacs@m.gmane.org; Fri, 29 Sep 2017 13:42:12 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:33126) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dxzIb-0002OK-TN for bug-gnu-emacs@gnu.org; Fri, 29 Sep 2017 13:42:06 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dxzIY-00083C-Pg for bug-gnu-emacs@gnu.org; Fri, 29 Sep 2017 13:42:05 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:59091) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dxzIY-000826-Gt for bug-gnu-emacs@gnu.org; Fri, 29 Sep 2017 13:42:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1dxzIY-00064t-5t for bug-gnu-emacs@gnu.org; Fri, 29 Sep 2017 13:42:02 -0400 X-Loop: help-debbugs@gnu.org In-Reply-To: <87h9h9pnof.fsf@udel.edu> Resent-From: Mark Oteiza Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 29 Sep 2017 17:42:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 22689 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 22689-submit@debbugs.gnu.org id=B22689.150670690423330 (code B ref 22689); Fri, 29 Sep 2017 17:42:02 +0000 Original-Received: (at 22689) by debbugs.gnu.org; 29 Sep 2017 17:41:44 +0000 Original-Received: from localhost ([127.0.0.1]:39539 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dxzIG-00064D-2G for submit@debbugs.gnu.org; Fri, 29 Sep 2017 13:41:44 -0400 Original-Received: from mail-qt0-f193.google.com ([209.85.216.193]:52951) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dxzID-00063y-UG for 22689@debbugs.gnu.org; Fri, 29 Sep 2017 13:41:42 -0400 Original-Received: by mail-qt0-f193.google.com with SMTP id o52so468834qtc.9 for <22689@debbugs.gnu.org>; Fri, 29 Sep 2017 10:41:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=udel-edu.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=zKeBM0eJgC9mpvD32rjMsB2CsoxuwqDOXpUlaBMKEzs=; b=zbl625T9gxzalKFB6Gs9kHJCp7+j6f1odNoD/150wlq54ahAxO3cs3T61GHuo0+CUr C68Nf3jy/dAqsUe9/ETLQXHvST6leWqQz6mi613MAtOCQd3bdpn35wTDmtX12fiTjqBP DhPXh0z0pZ1kYtLviqZmCBjdC6xFOAQw01M8WMN4ECrtbf9RJuI42gz+p2Ks7JugHZvu 6Nk05QQ+JQAkmWMgAosw3bLPGGe7Q9iWXrq7nvT0mBAnIs1wwe5LDpmSHdLg4MOoJ9DR oEYJecZdxIf5kAXvIAAob0iXznWIAgiS1lLfczskGJlVFmIOQZjJ/0w1FC9ipwFgJT4h f5Yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=zKeBM0eJgC9mpvD32rjMsB2CsoxuwqDOXpUlaBMKEzs=; b=ba3PUleU6OEbQz8UPEQCOv+c/vwPR3v4z7BSqs5YhjOP/aFOrhsS5NqPS/pZ9WrWZC Dz6Qpt7Q6SxBLdEYEhreNXaBlzkX60PGFLzqXOhuc12ZYYCY/MpB5XAUY0rohK9tWUyS VJx9f5cztDmWYsaYIi+MNnyNPmrVEphtud80b90tyztEHvMRF2oK5c1Gf10xzcWM0l4r XkqJsUHs7X7YOQFHy/AqGNS6XoOMw4jn2ji3jlHJifyiTl/HDJvplktlmzqi83h70nYI /nOfhqF9/5Efsu/lNgbT/K2Ef5ZMb2oLNdJxbZoaV+bK80nsa6/vqUEmD38vsAM7Qzod ngBA== X-Gm-Message-State: AMCzsaUgYWNmvNKDFARewKMS+QzEBX3cjbOyTW6jKdvq4gVVZlniKMci N2l9Py2jk753qm5qV/I9mpcbT85aeGY= X-Google-Smtp-Source: AOwi7QANEg6Teol5FVptBTDHJE5iWtwh8UNRPJ5kyyrSWmqsdxeEwjAUSqZtFF8r2wmRmOLHZswbOg== X-Received: by 10.200.46.216 with SMTP id i24mr7556176qta.257.1506706896084; Fri, 29 Sep 2017 10:41:36 -0700 (PDT) Original-Received: from logos.localdomain (pool-173-67-36-61.bltmmd.fios.verizon.net. [173.67.36.61]) by smtp.gmail.com with ESMTPSA id z192sm2890850qka.91.2017.09.29.10.41.35 for <22689@debbugs.gnu.org> (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 29 Sep 2017 10:41:35 -0700 (PDT) Content-Disposition: inline X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:137618 Archived-At: 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);