
Hi! Now when we have a fast popCnt# primop* it would be nice to expose it to the world through an official API. I propose we add this method to the Bits class in Data.Bits: -- | Return the number of set bits in the argument, know as the -- population count or the Hamming weight. popCount :: a -> Int I will provide a default implementation, which means that this change won't break any existing user defined instances. All the instances for the basic types (Ints, Words, etc) will use the primops. * These primops compile to a single POPCNT instruction if the user compile using -msse4.2 and a fast lookup table based implementation otherwise. Discussion period: 2 weeks Cheers, Johan

+1. I use this operation very heavily in geometric algebra.
On Wed, Aug 17, 2011 at 12:37 PM, Johan Tibell
Hi!
Now when we have a fast popCnt# primop* it would be nice to expose it to the world through an official API. I propose we add this method to the Bits class in Data.Bits:
-- | Return the number of set bits in the argument, know as the -- population count or the Hamming weight. popCount :: a -> Int
I will provide a default implementation, which means that this change won't break any existing user defined instances. All the instances for the basic types (Ints, Words, etc) will use the primops.
* These primops compile to a single POPCNT instruction if the user compile using -msse4.2 and a fast lookup table based implementation otherwise.
Discussion period: 2 weeks
Cheers, Johan
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

+1. I use this in clustering algorithms for CV.
Anthony
On Wed, Aug 17, 2011 at 12:37 PM, Johan Tibell
Hi!
Now when we have a fast popCnt# primop* it would be nice to expose it to the world through an official API. I propose we add this method to the Bits class in Data.Bits:
-- | Return the number of set bits in the argument, know as the -- population count or the Hamming weight. popCount :: a -> Int
I will provide a default implementation, which means that this change won't break any existing user defined instances. All the instances for the basic types (Ints, Words, etc) will use the primops.
* These primops compile to a single POPCNT instruction if the user compile using -msse4.2 and a fast lookup table based implementation otherwise.
Discussion period: 2 weeks
Cheers, Johan
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

+1 Excerpts from Johan Tibell's message of Wed Aug 17 12:37:25 -0400 2011:
Hi!
Now when we have a fast popCnt# primop* it would be nice to expose it to the world through an official API. I propose we add this method to the Bits class in Data.Bits:
-- | Return the number of set bits in the argument, know as the -- population count or the Hamming weight. popCount :: a -> Int
I will provide a default implementation, which means that this change won't break any existing user defined instances. All the instances for the basic types (Ints, Words, etc) will use the primops.
* These primops compile to a single POPCNT instruction if the user compile using -msse4.2 and a fast lookup table based implementation otherwise.
Discussion period: 2 weeks
Cheers, Johan

On Wed, 2011-08-17 at 18:37 +0200, Johan Tibell wrote:
Hi!
Now when we have a fast popCnt# primop* it would be nice to expose it to the world through an official API. I propose we add this method to the Bits class in Data.Bits:
-- | Return the number of set bits in the argument, know as the -- population count or the Hamming weight. popCount :: a -> Int
I will provide a default implementation, which means that this change won't break any existing user defined instances. All the instances for the basic types (Ints, Words, etc) will use the primops.
* These primops compile to a single POPCNT instruction if the user compile using -msse4.2 and a fast lookup table based implementation otherwise.
Discussion period: 2 weeks
Cheers, Johan
I posted bug for various similar instructions some time ago: http://hackage.haskell.org/trac/ghc/ticket/4102 Probably the update should be done in batch (i.e. popCount, trailing/leading zeros etc.) Regards

On Wed, Aug 17, 2011 at 7:44 PM, Maciej Marcin Piechotka
I posted bug for various similar instructions some time ago:
http://hackage.haskell.org/trac/ghc/ticket/4102
Probably the update should be done in batch (i.e. popCount, trailing/leading zeros etc.)
I'd be happy to see these as well. If someone would like to do the GHC work needed to have them translate into single machine instructions where available, have a look at the implementation of the popCnt# primops here: https://github.com/ghc/ghc/commit/2d0438f329ac153f9e59155f405d27fac0c43d65 https://github.com/ghc/packages-ghc-prim/commit/cefc19afafe5107ff98d5205c204... https://github.com/ghc/testsuite/commit/2f4d13348e2140f7fc0d4b8b995a2be0fa62... I don't have time to do so at the moment. I'd prefer to not wait with adding popCount until this is done, unless someone takes it on them to add the others before GHC 7.4. -- Johan

If someone wanted to spruce up my patch here
http://hackage.haskell.org/trac/ghc/ticket/3489 to include the GMP popcount
call, so that we can make the Integer instance of Bits work efficiently with
the new call, that might also help this proposal. But yeah, even if not, +1.
On Wed, Aug 17, 2011 at 4:33 PM, Johan Tibell
On Wed, Aug 17, 2011 at 7:44 PM, Maciej Marcin Piechotka
wrote: I posted bug for various similar instructions some time ago:
http://hackage.haskell.org/trac/ghc/ticket/4102
Probably the update should be done in batch (i.e. popCount, trailing/leading zeros etc.)
I'd be happy to see these as well. If someone would like to do the GHC work needed to have them translate into single machine instructions where available, have a look at the implementation of the popCnt# primops here:
https://github.com/ghc/ghc/commit/2d0438f329ac153f9e59155f405d27fac0c43d65
https://github.com/ghc/packages-ghc-prim/commit/cefc19afafe5107ff98d5205c204...
https://github.com/ghc/testsuite/commit/2f4d13348e2140f7fc0d4b8b995a2be0fa62...
I don't have time to do so at the moment.
I'd prefer to not wait with adding popCount until this is done, unless someone takes it on them to add the others before GHC 7.4.
-- Johan
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

First, +1 for a popCount primitive On Wednesday 17 August 2011, 22:33:29, Johan Tibell wrote:
On Wed, Aug 17, 2011 at 7:44 PM, Maciej Marcin Piechotka
wrote: I posted bug for various similar instructions some time ago:
http://hackage.haskell.org/trac/ghc/ticket/4102
Probably the update should be done in batch (i.e. popCount, trailing/leading zeros etc.)
Very desirable to have these too.
I'd be happy to see these as well. If someone would like to do the GHC work needed to have them translate into single machine instructions where available, have a look at the implementation of the popCnt# primops here:
I intend to see whether I can learn from those how to do it, but
https://github.com/ghc/ghc/commit/2d0438f329ac153f9e59155f405d27fac0c43 d65 https://github.com/ghc/packages-ghc-prim/commit/cefc19afafe5107ff98d520 5c204b190da1d497b https://github.com/ghc/testsuite/commit/2f4d13348e2140f7fc0d4b8b995a2be 0fa6291f0
I don't have time to do so at the moment.
nor have I, no time for hacking before September :(
I'd prefer to not wait with adding popCount until this is done, unless someone takes it on them to add the others before GHC 7.4.
No, don't wait. While it would be nice to have the others equally soon, it's better to have one goody than none. Cheers, Daniel

+1
I'd prefer to not wait with adding popCount until this is done, unless
someone takes it on them to add the others before GHC 7.4.
No, don't wait. While it would be nice to have the others equally soon, it's better to have one goody than none.
How about making the API change in one go -- i.e. add the default implementations for all of them, which is easy. Then people can come along and do the GHC work necessary to make the others efficient later. In fact, changing the API could be a "forcing function" to make the others happen ;-). -Ryan

On Thu, Aug 18, 2011 at 3:13 PM, Ryan Newton
+1
I'd prefer to not wait with adding popCount until this is done, unless someone takes it on them to add the others before GHC 7.4.
No, don't wait. While it would be nice to have the others equally soon, it's better to have one goody than none.
How about making the API change in one go -- i.e. add the default implementations for all of them, which is easy. Then people can come along and do the GHC work necessary to make the others efficient later. In fact, changing the API could be a "forcing function" to make the others happen ;-).
If someone wants to do the work I'm happy to review the patches.

Hi all, Since there were no objections I'm pushing this slightly before the 2 week deadline, as I'm moving countries soon and won't have much time then. If someone feels strongly that this is the wrong thing to do, please shout and I'll revert it. Cheers, Johan
participants (8)
-
Anthony Cowley
-
Daniel Fischer
-
Daniel Peebles
-
Edward Kmett
-
Edward Z. Yang
-
Johan Tibell
-
Maciej Marcin Piechotka
-
Ryan Newton