not long ago in this newsgroup, it was suggested that an
implementation of gzip in common lisp would be useful. so, as a
wannabe lisp hacker, i decided to give that a go. however, in
the process of writing code to compute CRCs, i ran across the
need to do bit shifting. the ash function was all i could
find, but since it preserves the sign, i needed to write a
function to extract the part i wanted:
(defun lsr (i n)
"logically shift i n bits to the right"
(let* ((len (integer-length i))
(b (byte (- len n) 0))
(x (ash i (- n))))
(ldb b x)))
now, the performance of this function most probably will not be
a tremendous issue but i can't help wondering if there isn't some
more "efficient" way to do this.
···@foil.apk.net (Matt Emerson) writes:
> not long ago in this newsgroup, it was suggested that an
> implementation of gzip in common lisp would be useful. so, as a
> wannabe lisp hacker, i decided to give that a go.
[...]
As a side issue (irrelevant to the question): have you considered
using the existing code in libz? Building a Lisp interface to the
appropriate C primitives should not be very hard, and should provide a
fast and reliable streamed `gzip' implementation. Portability may be
a problem, though.
--
Hrvoje Niksic <·······@srce.hr> | Student at FER Zagreb, Croatia
--------------------------------+--------------------------------
* Q: What is an experienced Emacs user?
* A: A person who wishes that the terminal had pedals.
Hrvoje Niksic <·······@srce.hr> writes:
> ···@foil.apk.net (Matt Emerson) writes:
>
> > not long ago in this newsgroup, it was suggested that an
> > implementation of gzip in common lisp would be useful. so, as a
> > wannabe lisp hacker, i decided to give that a go.
> [...]
>
> As a side issue (irrelevant to the question): have you considered
> using the existing code in libz? Building a Lisp interface to the
> appropriate C primitives should not be very hard, and should provide a
> fast and reliable streamed `gzip' implementation. Portability may be
> a problem, though.
>
> --
> Hrvoje Niksic <·······@srce.hr> | Student at FER Zagreb, Croatia
> --------------------------------+--------------------------------
> * Q: What is an experienced Emacs user?
> * A: A person who wishes that the terminal had pedals.
I think that this ( using code from libz ) just is against the spirit of
the issue. The whole meaning of it, as I see it, is doing the whole thing
in Lisp, from the ground up.
Andreas
Andreas Eder <···@laphroig.mch.sni.de> writes:
> > As a side issue (irrelevant to the question): have you considered
> > using the existing code in libz? Building a Lisp interface to the
> > appropriate C primitives should not be very hard, and should provide a
> > fast and reliable streamed `gzip' implementation. Portability may be
> > a problem, though.
[...]
> I think that this ( using code from libz ) just is against the spirit of
> the issue. The whole meaning of it, as I see it, is doing the whole thing
> in Lisp, from the ground up.
I was not talking about spirit -- it was just an idea to reuse the
existing code. I still think it's a good idea, when Lisp
implementations allow it?
I know this doesn't answer the original question, which is why I
called it a "side issue."
--
Hrvoje Niksic <·······@srce.hr> | Student at FER Zagreb, Croatia
--------------------------------+--------------------------------
Oh lord won't you buy me a color TV...
Andreas Eder <···@laphroig.mch.sni.de> writes:
> > As a side issue (irrelevant to the question): have you considered
> > using the existing code in libz? Building a Lisp interface to the
> > appropriate C primitives should not be very hard, and should provide a
> > fast and reliable streamed `gzip' implementation. Portability may be
> > a problem, though.
[...]
> I think that this ( using code from libz ) just is against the spirit of
> the issue. The whole meaning of it, as I see it, is doing the whole thing
> in Lisp, from the ground up.
I was not talking about spirit -- it was just an idea to reuse the
existing code. I still think it's a good idea, for Lisp
implementations that allow it.
I know this doesn't answer the original question, which is why I
called it a "side issue."
--
Hrvoje Niksic <·······@srce.hr> | Student at FER Zagreb, Croatia
--------------------------------+--------------------------------
Oh lord won't you buy me a color TV...
In article <··················@foil.apk.net>, ···@foil.apk.net (Matt Emerson) writes:
>> not long ago in this newsgroup, it was suggested that an
>> implementation of gzip in common lisp would be useful. so, as a
>> wannabe lisp hacker, i decided to give that a go.
Super. Will Hartung (······@netcom.com) mentioned he's working on this also.
You might want to coordinate.
>> however, in
>> the process of writing code to compute CRCs, i ran across the
>> need to do bit shifting. the ash function was all i could
>> find, but since it preserves the sign, i needed to write a
>> function to extract the part i wanted:
>>
>> (defun lsr (i n)
>> "logically shift i n bits to the right"
>> (let* ((len (integer-length i))
>> (b (byte (- len n) 0))
>> (x (ash i (- n))))
>> (ldb b x)))
>>
>> now, the performance of this function most probably will not be
>> a tremendous issue but i can't help wondering if there isn't some
>> more "efficient" way to do this.
>>
(ash i (- n)) is sufficient for a positive number.
In a CRC application I would expect all the numbers to be positive.
If not, test it first
(defun lsr (i n) (if (>= i 0) (ash i (- n)) (error "lsr got negative number"))
BTW, using the return value of #'byte is generally to be avoided
when efficiency is important, for CL programs.
It will cons since it must be able to
represent bytes for infinite integers, and thus needs two fixnums
for the width and start. The LispM implementation coded
the value into a fixnum, which is "buggy" on huge massive integers,
but makes the function actually useful for efficient use.
Any efficient compiler special cases its typical use
and breaks out the two argument, so it doesn't cons
(ldb (byte 8 8) x) ==> (low-level-ldb 8 8 x)
It can't do this if you use a byte-spec as a first-class object.
-kelly murray ···@franz.com
In article <··················@foil.apk.net>,
Matt Emerson <···@foil.apk.net> wrote:
>(defun lsr (i n)
> "logically shift i n bits to the right"
> (let* ((len (integer-length i))
> (b (byte (- len n) 0))
> (x (ash i (- n))))
> (ldb b x)))
Why do you use LDB *and* ASH, instead of simply specifying the position in
the byte specifier? E.g.
(defun lsr (i n)
(ldb (byte (- (integer-length i) n)
n)
i))
I wonder why Common Lisp has no logical shift operator? Didn't MacLisp
have LSH?
--
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.