From: Ewan Dennis
Subject: Simple binary I/O question
Date: 
Message-ID: <36F7ACCD.B3ABE6B8@innerworkings.co.uk>
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.

From: Christopher J. Vogt
Subject: Re: Simple binary I/O question
Date: 
Message-ID: <36F7FC53.A4C9949C@computer.org>
Ewan Dennis wrote:
> 
> 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.


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))
From: Erik Naggum
Subject: Re: Simple binary I/O question
Date: 
Message-ID: <3131225340246095@naggum.no>
* Ewan Dennis <····@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
From: Ewan Dennis
Subject: Re: Simple binary I/O question
Date: 
Message-ID: <36F8BEC4.1D62037B@innerworkings.co.uk>
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
From: Erik Naggum
Subject: Re: Simple binary I/O question
Date: 
Message-ID: <3131271135046431@naggum.no>
* Ewan Dennis <····@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
From: Raymond Toy
Subject: Re: Simple binary I/O question
Date: 
Message-ID: <4noglihjsf.fsf@rtp.ericsson.se>
>>>>> "Erik" == Erik Naggum <····@naggum.no> writes:

    Erik> * Ewan Dennis <····@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