Re: [Sbcl-commits] master: Recognize (integer-length (ldb (byte 64 0) (1- (logand n (- n))))) as ctz
"Scott L. Burson" <[email protected]>
| Newsgroups | gmane.lisp.steel-bank.devel |
|---|---|
| Message-ID | <CAF5LJ4DULSQAMTq0mb17H2ptfGFLx90CPjUe=pG3KVp5iq53fw@mail.gmail.com> |
On Wed, Dec 10, 2025 at 11:45 AM Stas Boukarev <[email protected]> wrote: > I had > trouble figuring out how to deal with 0, some code in the wild uses > things like > (1- (integer-length (logand n (- n)))) or (1- (logcount (logxor n (1- > n)))), but they return a different thing on 0. Maybe they should be > matched anyway and 0 handled with a conditional move or something. > And I decided against (logcount (ldb (byte 64 0) (lognor n (- n)))) > because popcount is potentially expensive. Speaking as someone who is actually using this operation -- doesn't the compiler know whether n can be zero? It seems to be so good at tracking the domains of integer variables. I am literally doing (loop while (/= n 0) (let ((b (least-1-bit n))) ...)) where least-1-bit expands to one of the above expressions. Surely, under these circumstances, the compiler knows that n isn't zero, so it can match all of the three or four expressions people use for this? -- Scott _______________________________________________ Sbcl-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/sbcl-devel