diff --git a/csug/numeric.stex b/csug/numeric.stex index 00c45d44c8..e7ba98d1eb 100644 --- a/csug/numeric.stex +++ b/csug/numeric.stex @@ -1378,9 +1378,9 @@ bits in a fixnum, i.e., %---------------------------------------------------------------------------- \entryheader -\formdef{fxpopcount}{\categoryprocedure}{(fxpopcount \var{fixnum} \dots)} -\formdef{fxpopcount32}{\categoryprocedure}{(fxpopcount32 \var{fixnum} \dots)} -\formdef{fxpopcount16}{\categoryprocedure}{(fxpopcount16 \var{fixnum} \dots)} +\formdef{fxpopcount}{\categoryprocedure}{(fxpopcount \var{fixnum})} +\formdef{fxpopcount32}{\categoryprocedure}{(fxpopcount32 \var{fixnum})} +\formdef{fxpopcount16}{\categoryprocedure}{(fxpopcount16 \var{fixnum})} \returns number of bits set in \var{fixnum} \listlibraries \endentryheader @@ -1391,6 +1391,11 @@ bits in a fixnum, i.e., \var{fixnum} is treated as a two's complement integer, regardless of the underlying representation. +See also \scheme{fxbit-count}, which produces the same result as +\scheme{fxpopcount} for the domain of \scheme{fxpopcount}. Because +\scheme{fxbit-count} also handles negative arguments, however, it does +not map as simply to certain processor instructions. + \section{Random Number Generation\label{SECTNUMERICRANDOM}} diff --git a/s/5_3.ss b/s/5_3.ss index ae5746f768..f2f88f7eba 100644 --- a/s/5_3.ss +++ b/s/5_3.ss @@ -2489,23 +2489,9 @@ (make-count-table))) (define $fxbit-count (lambda (n) - (if (fx= n 0) - 0 - (constant-case ptr-bits - [(64) - (fx+ (bytevector-u8-ref count-table (fxlogand n #xff)) - (bytevector-u8-ref count-table (fxlogand (fxsrl n 8) #xff)) - (bytevector-u8-ref count-table (fxlogand (fxsrl n 16) #xff)) - (bytevector-u8-ref count-table (fxlogand (fxsrl n 24) #xff)) - (bytevector-u8-ref count-table (fxlogand (fxsrl n 32) #xff)) - (bytevector-u8-ref count-table (fxlogand (fxsrl n 40) #xff)) - (bytevector-u8-ref count-table (fxlogand (fxsrl n 48) #xff)) - (bytevector-u8-ref count-table (fxlogand (fxsrl n 56) #xff)))] - [(32) - (fx+ (bytevector-u8-ref count-table (fxlogand n #xff)) - (bytevector-u8-ref count-table (fxlogand (fxsrl n 8) #xff)) - (bytevector-u8-ref count-table (fxlogand (fxsrl n 16) #xff)) - (bytevector-u8-ref count-table (fxlogand (fxsrl n 24) #xff)))])))) + (if (fx< n 0) + (fxnot (fxpopcount (fxnot n))) + (fxpopcount n)))) (define $big-bit-count (lambda (n) (let ([end (fx+ (fx* ($bignum-length n) (constant bigit-bytes)) (constant bignum-data-disp))])