Re: [Sbcl-commits] master: Recognize (integer-length (ldb (byte 64 0) (1- (logand n (- n))))) as ctz
Stas Boukarev <[email protected]>
| Newsgroups | gmane.lisp.steel-bank.devel |
|---|---|
| Message-ID | <CAF63=10oM3kniEu+GvRLg+=x9Lqe2NJqkVioJ0i5G1fK+FYUiw@mail.gmail.com> |
Without modular arithmetic things turn out pretty badly if it's not recognized. So it's not a good idea to rely on 0 being excluded. On Fri, Jan 9, 2026 at 11:35 PM Scott L. Burson <[email protected]> wrote: > > 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