implement fxbit-count using fxpopcount

original commit: 470b088840576872fd0a7ede7bf5342f44555af0
This commit is contained in:
Matthew Flatt 2020-01-11 11:21:42 -07:00
parent 540c58bbe8
commit 196a9800f9
2 changed files with 11 additions and 20 deletions

View File

@ -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}}

View File

@ -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))])