Raymond Toy writes:
> There appears to be a typo in the current haipart. Would the following
> work better:
>
> (defun haipart (x n)
> (let ((x (abs x)))
> (cond ((< n 0)
> (logand x (1- (ash 1 (- n)))))
> (t
> (ash x (min (- n (integer-length x))
> 0))))))
>
> The bug is (< n 0). It was (< x 0).
Yes, this works better!
I didn't look very close, because the other definition seemed clearer
to me.
> What does haipart mean anyway? What is it really supposed to do?
Well, haipart gives you the high part of an integer.
(haipart integer count) returns the count high bits of integer if
count is > 0 or the low count bits if count < 0.
That is why i think the other definition of haipart in clmacs.lisp is
clearer.
Especially for negative count (haipart int count) is just
(ldb (byte (- count) 0) int).
In fact in numth.lisp haipart is always used with a count of -2 or -4
so it could easily be replaced by (ldb (byte 2 0) int) (resp. with
4).
This should be faster and more lucid as well.
I did only a little bit of performance testing on cmucl 18e and that
showed that indeed the above version of haipart is roughly 30% faster.
But just doing the ldb thing without the overhead of function calling
an all that gives a factor of about 10.
So, maybe we should use your corrected version, but use the ldb thing
in the few places in numth.lisp where it is used.
> Andreas> PS: I think I will go over all the numbertheoretic functions and
> Andreas> correct them, because most are not working properly (see the comment
> Andreas> on primep in the mailing list).
>
> That would be excellent.
I'm working on primep. But I'm not content with pseudoprimality, so
implementing a fast primep test will still take some time.
Andreas
--
Wherever I lay my .emacs, there´s my $HOME.