I've looked into all rounding / truncating / flooring functions that I know, but all of them return multiple values, and, as an artefact, even if the second value isn't used, they create and immediately dispose of new conses. This later causes GC to kick in and can slow down the execution at random spots at seemingly random fashion.So, is there a way to avoid doing (floor x y) w/o consing extra memory? Even if there's an SBCL-specific way, that's good too.

Here's an example of what I was trying to do, but it doesn't work because the type hint of signed-byte is ignored. But this is only to illustrate that even with speed 3, the second (and obviously redundant) value from floor is created and discarded.

Hm... after some thought... how many bits is a fixnum on a 64 bit system? I think this must have been a problem then. In my test no value is larger then 2^31, but what if they can't be even this big and it creates long integers?

Thanks for the last version. That wasn't a serious test, actually there are many non-trivial ways to round up to the closest power of two fast (or, perhaps even faster then your suggestion).I realize now that floor is not the culprit, but why would log cons any memory? Is this something that is inherent to this algorithm? Do you know of any good articles on how to do logarithm by hand?

Because Common Lisp's LOG accepts all types of numerical arguments (except zero), and with negative input LOG is even capable to return complex numbers. All-in-all this is a bit more complicated than just computing a simple binary integer logarithm. I'm afraid that LOG is already "too big" for such a simple problem and tries to solve the computation in an unneccessarily complicated way. That's the drawback of a programming language handling data types automatically. Common Lisp is good, but not perfect.

"wxvxw wrote:Do you know of any good articles on how to do logarithm by hand?

On what type of numbers? Bytes, integers, fractions, floats, complexes? Every type needs a different algorithm if the result shall be fast.