unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Daniel Colascione <dancol@dancol.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: dmantipov@yandex.ru, emacs-devel@gnu.org
Subject: Re: Set operations on bool-vectors
Date: Sat, 21 Sep 2013 00:43:10 -0700	[thread overview]
Message-ID: <523D4E0E.9050500@dancol.org> (raw)
In-Reply-To: <83txhek5ku.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 2458 bytes --]

On 9/21/13 12:16 AM, Eli Zaretskii wrote:
>> Date: Fri, 20 Sep 2013 19:49:00 -0700
>> From: Daniel Colascione <dancol@dancol.org>
>> Cc: Emacs development discussions <emacs-devel@gnu.org>
>>
>>>> +static inline
>>>> +EMACS_INT
>>>> +popcount_size_t(size_t val)
>>>> +{
>>>> +  EMACS_INT count;
>>>> +
>>>> +#if defined __GNUC__ && BITS_PER_SIZE_T == 64
>>>> +  count = __builtin_popcountll (val);
>>>> +#elif defined __GNUC__ && BITS_PER_SIZE_T == 32
>>>> +  count = __builtin_popcount (val);
>>>> +#elif defined __MSC_VER && BITS_PER_SIZE_T == 64
>>>> +# pragma intrinsic __popcnt64
>>>> +  count = __popcnt64 (val);
>>>> +#elif defined __MSC_VER && BITS_PER_SIZE_T == 32
>>>> +# pragma intrinsic __popcnt
>>>> +  count = __popcnt (val);
>>>> +#else
>>>> +  {
>>>> +    EMACS_INT j;
>>>> +    count = 0;
>>>> +    for (j = 0; j < BITS_PER_SIZE_T; ++j)
>>>> +      count += !!((((size_t) 1) << j) & val);
>>>> +  }
>>>> +#endif
>>>
>>> Why loop? See http://en.wikipedia.org/wiki/Hamming_weight.
>>
>> I didn't want to put a lot of effort into a code path we'll probably
>> never use.  Recall that if we're using icc or gcc or Visual C++ or
>> Clang, we'll be using a compiler intrinsic, which will probably compile
>> down to a single machine instruction.
>>
>> By the way: can someone test that the Visual C++ alternate actually
>> works? I don't have access to a Windows machine at the moment.
> 
> I don't see why it won't work, per documentation on this page:
> 
>   http://msdn.microsoft.com/en-us/library/bb385231%28v=vs.90%29.aspx
> 
> However, I think you will need to make usage of these intrinsics
> compiler version dependent.  GCC supports them starting from 3.4,
> whereas MSVC seems to support them since Studio 2008, i.e. _MSC_VER =
> 1500 or higher.

Fair enough.

> It is also not clear to me what will the MSVC intrinsic do if the
> binary ever runs on a CPU that doesn't support SSE4, the MSDN
> documentation seems to say that the results are unpredictable,
> i.e. that there's no fallback, like GCC has in libgcc.  So perhaps we
> should also guard that with a Windows version (assuming that old
> machines will only ever run Windows 9x).

Good point. SSE4 is much too recent to require. Making the cpuid check
shouldn't be too hard. I have no way to actually test the fallback,
though. (It's easy to test the fallback code, but not that easy to test
falling back to it.)


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2013-09-21  7:43 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-20 22:59 Set operations on bool-vectors Daniel Colascione
2013-09-21  1:57 ` Stefan Monnier
2013-09-21  2:44   ` Daniel Colascione
2013-09-21 15:51     ` Stefan Monnier
2013-09-21  2:26 ` Dmitry Antipov
2013-09-21  2:49   ` Daniel Colascione
2013-09-21  7:16     ` Eli Zaretskii
2013-09-21  7:43       ` Daniel Colascione [this message]
2013-09-21  7:36 ` Andreas Schwab
2013-09-21  7:38   ` Daniel Colascione
2013-09-21  8:35     ` Andreas Schwab
2013-09-21  8:49       ` Daniel Colascione

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=523D4E0E.9050500@dancol.org \
    --to=dancol@dancol.org \
    --cc=dmantipov@yandex.ru \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).