Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple binary I/O question

67 views
Skip to first unread message

Ewan Dennis

unread,
Mar 23, 1999, 3:00:00 AM3/23/99
to
I'm trying to barf binary floating point
values out of a LISP environment and into
a file.

The file streaming stuff seems to be geared
towards bytes and text. This would be fine if
it let you just throw the bits of an IEEE (hah)
standard single-float onto a stream.

I'm obviously missing something simple. Could
someone please put me out of my misery?

I'm using version 4.3.2 of Allegro Common Lisp, BTW.

Ewan Dennis
Innerworkings Ltd.

Christopher J. Vogt

unread,
Mar 23, 1999, 3:00:00 AM3/23/99
to


Here are some hints, I hope this helps.

(with-open-file (stream "d:/temp/float-bin" :direction :output :if-exists :supersede :element-type 'unsigned-byte)
(write-binary-float (float-binary 1.23) stream))

(defun float-to-binary (float)
(multiple-value-bind (mantissa exponent sign) (integer-decode-float float)
(let ((value (dpb (ldb (byte 8 0) (+ 150 exponent)) (byte 8 23) (ldb (byte 23 0) mantissa))))
(if (minusp sign)
(dpb 1 (byte 31 1) value)
value))))

;;;
;;; big endian or little endian?
;;;
(defun write-binary-float (float stream)
(write-byte (ldb (byte 8 24) float) stream)
(write-byte (ldb (byte 8 16) float) stream)
(write-byte (ldb (byte 8 8) float) stream)
(write-byte (ldb (byte 8 0) float) stream))

Erik Naggum

unread,
Mar 24, 1999, 3:00:00 AM3/24/99
to
* Ewan Dennis <ew...@innerworkings.co.uk>

| I'm trying to barf binary floating point values out of a LISP environment
| and into a file.

that is actually not a meaningful request.

| The file streaming stuff seems to be geared towards bytes and text. This
| would be fine if it let you just throw the bits of an IEEE (hah) standard
| single-float onto a stream.

why do you need this? do you need to talk to a different program? do
you think (or know) that reading and writing floating-point numbers is
a real bottle-neck in your application?

| I'm obviously missing something simple. Could someone please put me out
| of my misery?

could be you simply need FASL-WRITE and FASL-READ in Allegro CL 4.3.2.
these functions preserve the notion of type that exists in memory, and
which does _not_ exist in a file to which you have just dumped a bunch of
bytes, just like raw bytes in memory doesn't have crucial type knowledge
in C, but it has in Common Lisp.

as I told someone else: chuck the C mind-set and re-evaluate the problem.

#:Erik

Ewan Dennis

unread,
Mar 24, 1999, 3:00:00 AM3/24/99
to
Erik Naggum wrote:

<snipetty>

> why do you need this? do you need to talk to a different program? do
> you think (or know) that reading and writing floating-point numbers is
> a real bottle-neck in your application?

I'm talking to the rest of the world, I'm afraid. My LISP code is not
running
on its own.

> as I told someone else: chuck the C mind-set and re-evaluate the problem.

Nice idea, and I like what little I know about the functional paradigm
but
I'm working on a big project in which the LISP part is only a component.

Cheers for the kick up the proverbial. I understand the argument.

Defense over.

Ewan Dennis

Erik Naggum

unread,
Mar 24, 1999, 3:00:00 AM3/24/99
to
* Ewan Dennis <ew...@innerworkings.co.uk>

| I'm talking to the rest of the world, I'm afraid. My LISP code is not
| running on its own.

then there are two options: (1) realize that you will never have portable
CL code if it tries to write out bytes from memory, anyway, and then just
do it, or (2) use a foreign function to do it for you and call it with
the floating point values. option (2) is probably a lot easier if you
don't like to experiment with system internals.

#:Erik

Raymond Toy

unread,
Mar 24, 1999, 3:00:00 AM3/24/99
to
>>>>> "Erik" == Erik Naggum <er...@naggum.no> writes:

Erik> * Ewan Dennis <ew...@innerworkings.co.uk>
Erik> | I'm talking to the rest of the world, I'm afraid. My LISP code is not
Erik> | running on its own.

Erik> then there are two options: (1) realize that you will never have portable
Erik> CL code if it tries to write out bytes from memory, anyway, and then just
Erik> do it, or (2) use a foreign function to do it for you and call it with
Erik> the floating point values. option (2) is probably a lot easier if you
Erik> don't like to experiment with system internals.

Why is (1) true? Someone already sent portable code to get at the
bits of a float and pack them in a way that could be sent out to the
outside world.

Ray


0 new messages