Enhancement Request: Adding population count for Integers to the standard image

Jecel Assumpcao Jr jecel at merlintec.com
Fri Dec 10 12:43:56 UTC 2004


Matthew S. Hamrick  wrote on Fri, 10 Dec 2004 10:55:29 -0500
> There are historical reasons for calling this method popCount. The most 
> significant being most of the people that will use it work within a 400 
> yard radius of Ft. Meade, Maryland, Menwith Hill, England, and Moffett 
> Field in Sunnyvale, California. They all call this operation 
> "population count".

Heh, this reminded me of something Seymour Cray said as quoted by Butler
Lampson as quoted by Gordon Bell (and now being quoted by me, and you
can quote me on that ;-)

http://research.microsoft.com/users/gbell/craytalk/sld051.htm

"... he hated the population count unit..."

Though SmallIntegers are not the interesting case and the speed up is
not great, my guess is that Cray would have implemented it as something
like:

SmallInteger>>popCount
    | sum |
    "please insert test and exception for negative numbers"
    sum := (self bitAnd: 16r55555555) +
        ((self bitShift: -1) bitAnd: 16r55555555).
    sum := (sum bitAnd: 16r33333333) + 
        ((sum bitShift: -2) bitAnd: 16r33333333).
    sum := (sum bitAnd: 16r0F0F0F0F) +   "16r07070707 would also work"
        ((sum bitShift: -4) bitAnd: 16r0F0F0F0F).
    sum := (sum bitAnd: 16r00FF00FF) +   "16r000F000F would also work"
        ((sum bitShift: -8) bitAnd: 16r00FF00FF).
    ^ (sum bitAnd: 16r0000FFFF) +   "16r0000001F would also work"
        ((sum bitShift: -16) bitAnd: 16r0000FFFF).

These are all 31 bit operations, in case that is not obvious (though the
code for 32 bits would be exactly the same...). While I have not looked,
I imagine something similar could be done with LargeIntegers. Perhaps
with BitBLT in the spirit of Dan Ingalls' old Life demo.

-- Jecel



More information about the Squeak-dev mailing list