Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Lisp vs. I/O

Path: g2news1.google.com!news4.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local1.nntp.dca.giganews.com!nntp.speakeasy.net!news.speakeasy.net.POSTED!not-for-mail
NNTP-Posting-Date: Wed, 08 Dec 2004 10:29:14 -0600
Sender: duane@gemini
Newsgroups: comp.lang.lisp
Subject: Re: Lisp vs. I/O
References: <56a5817f.0412071401.936511a@posting.google.com> <pcopt1laerb.fsf@shuttle.math.ntnu.no> <m3is7dr9c4.fsf@javamonkey.com> <pcollc9adua.fsf@shuttle.math.ntnu.no> <87653dzltm.fsf@thalassa.informatimago.com> <87sm6h36vh.fsf@thelonious.dyndns.org> <87brd4ykf4.fsf@thalassa.informatimago.com>
From: Duane Rettig <du...@franz.com>
Date: 08 Dec 2004 08:29:13 -0800
Message-ID: <4is7cpxd2.fsf@franz.com>
User-Agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.1 (Cuyahoga Valley)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Lines: 98
NNTP-Posting-Host: 64.81.254.38
X-Trace: sv3-zpeMIFTunk26NSmbESrDXSeyNIQnDmeTDmTzEL47zz/lLiqT9QwuF2YNNgvsnl75GjakfBIxEVEkBKp!MW586z3XjoHAAh3XG2qF5bCd0JZfumgMdxP3aJZMp72QMPLEOLM2YM0V9ggpNV684tasdVQkJjGX!kQ==
X-Complaints-To: abuse@speakeasy.net
X-DMCA-Complaints-To: ab...@speakeasy.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.20

Pascal Bourguignon <s...@mouse-potato.com> writes:

> Russell McManus <russell_mcma...@yahoo.com> writes:
> 
> > Pascal Bourguignon <s...@mouse-potato.com> writes:
> > 
> > > Always "brevity", "velocity"...  But what about correctness and
> > > interchangeability (portability)?
> > 
> > OK, I'll bite.  Is there some way to recreate the value written out by
> > this program in portable common lisp?  The best I've come up with in
> > the past is to use the FFI of the implementation.
> > 
> > Here is the C program I am wondering about:
> > 
> > #include <fcntl.h>
> > int main(int argc,char*argv[]) {
> >   int fd=open("/var/tmp/foo", O_WRONLY|O_TRUNC|O_CREAT);
> >   double v = 1234.567;
> >   write(fd,(char*)&v,sizeof(v));
> > }
> 
> (loop with s = (format nil "~64,'0B~%"(float-64-to-ieee-754 1234.567d0)) 
>       for i from 56 downto 0 by 8 
>       do (princ (subseq s i (+ 8 i))) (princ " "))

 [ ... ]

> You can write the binary file with:
> 
>     (with-open-file (out "value.ieee-754-double" :direction :output
>                          :if-does-not-exist :create
>                          :if-exists :supersede
>                          :element-type '(unsigned-byte 64))
>         (write-byte (float-64-to-ieee-754 1234.567d0) out))
> 
> 
> Note that the file is written with the host byte sex, that is it is
> wrong.  But that bug was *specified* by the C programmer...

I don't know that this is the case.  The C programmer tends not to care
about endianness if working on one machine; it is networking that
forces a programmer to consider byte-swapping.  This is true in any
language.  However....

> Now, let's return the challenge and ask the C programmer to write the
> same IEEE-754 double in network byte order, so the file be portable
> accross platforms.  Something like:

 [ ... example, 13 LOC ... ]

> Of course, the difficuly lies in float-64-to-ieee-754.  Have the same
> C source  work on any bytesex platform, AND on platforms that don't
> use IEEE-754 as native floating-point format.

 [ ... largish example of 27 LOC ... ]

This is too large.  network ordering should be an integral part of
a good i/o system, and although write-vector/read-vector are not
standard, they could easily be so, and will do the whole job for
you.

If you are familiar with an old television show called "Name That Tune",
you know that the famous phrase is "I can name that tune in X notes".
Well in the spirit of "name that tune", I can write that code in 7 LOC!

On one machine, with some system-identifying calls, over an NFS LAN:

CL-USER(1): (software-type t)
"FreeBSD"
CL-USER(2): (machine-type)
"x86"
CL-USER(3): (with-open-file (out "~/ieee-754-double" :direction :output
                                 :if-does-not-exist :create
                                :if-exists :supersede)
              (write-vector 1234.567d0 out :endian-swap :network-order))
12
CL-USER(4): 

and on another machine on the same LAN:

CL-USER(1): (software-type t)
"Solaris"
CL-USER(2): (machine-type)
"SPARC"
CL-USER(3): (defparameter *foo* (make-array 1 :element-type 'double-float))
*FOO*
CL-USER(4): (with-open-file (in "~/ieee-754-double")
              (read-vector *foo* in :endian-swap :network-order))
8
CL-USER(5): *foo*
#(1234.567d0)
CL-USER(6): 

-- 
Duane Rettig    du...@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182