From:	CRDGW2::CRDGW2::MRGATE::"SMTP::CRVAX.SRI.COM::RELAY-INFO-VAX" 16-JAN-1990 08:24
To:	MRGATE::"ARISIA::EVERHART"
Subj:	Re: WANTED: VAX <-> IEEE floating point conversion routines

Received: From UCBVAX.BERKELEY.EDU by CRVAX.SRI.COM with TCP; Tue, 16 JAN 90 04:36:17 PDT
Received: by ucbvax.Berkeley.EDU (5.61/1.41)
	id AA18152; Tue, 16 Jan 90 04:21:25 -0800
Received: from USENET by ucbvax.Berkeley.EDU with netnews
	for info-vax@kl.sri.com (info-vax@kl.sri.com)
	(contact usenet@ucbvax.Berkeley.EDU if you have questions)
Date: 16 Jan 90 02:00:24 GMT
From: cs.utexas.edu!samsung!munnari.oz.au!uniwa!vax6!crobw@tut.cis.ohio-state.edu  (Rob Wright, VMS Systems Group, Curtin University)
Organization: Curtin University of Technology
Subject: Re: WANTED: VAX <-> IEEE floating point conversion routines
Message-Id: <1688@acad.cut.oz>
References: <18479@ipl.jpl.nasa.gov>
Sender: info-vax-request@kl.sri.com
To: info-vax@kl.sri.com

In article <18479@ipl.jpl.nasa.gov>, tcg060@ipl.jpl.nasa.gov (Tom Greer) writes:
> Does anyone have VAX to IEEE and IEEE to VAX floating point conversion
> routines.  I hate reinventing the wheel.  Thanks in advance.
> Tom.
> -----------------------------------------------------------------------------
> Tom Greer                             |  tcg060@ipl.jpl.nasa.gov
> Image Processing Laboratory           |  mipl3::tcg060  (SPAN)
> Jet Propulsion Laboratory             |
> -----------------------------------------------------------------------------

I have included three responses to earlier questions on this topic. Each
response starts with the a line containing "RESPONSE n >>>"

RESPONSE 1 >>>
X-VMS-News: vax3 comp.os.vms:1034
From: woods@ncar.ucar.edu (Greg Woods)
Subject:Re: difference between vax floating point representation and IEEE
Date: 21 Jul 88 22:08:44 GMT
Message-ID:<462@ncar.ucar.edu>

In article <2256@hubcap.UUCP> ghosh@hubcap.UUCP (Amitava Ghosh) writes:
>does anyone know what the difference is between the IEEE floating
>point representation and the way the VAX stores numbers. If so
>how does one convert from one format to the other.  

  Here's some C subroutines to convert back and forth between VAX and
IEEE format. I use them to convert binary files between VAXes and Suns
and it works fairly well. They all take a pointer to the data and
how many data elements to convert as arguments.

--Greg
-------------------------------------------------------------------------
union  ie3float { char c[4]; int  exp; float f; };
void vf2ie3f(u,n) register union ie3float *u; register int n; {
/* VAX float to IEEE float */
register char tmp;
register int i;
for (i=0; i < n; u++,i++) {
   if (! u->exp) continue;
   tmp=u->c[0]; u->c[0]=u->c[1]-1; u->c[1]=tmp;
   tmp=u->c[2]; u->c[2]=u->c[3];   u->c[3]=tmp;
}
}

void ie3f2vf(u,n) register union ie3float *u; register int n; {
/* IEEE float to VAX float */
register char tmp;
register int i;
for (i=0; i < n; u++,i++) {
   if (! u->exp) continue;
   tmp=u->c[0]+1; u->c[0]=u->c[1]; u->c[1]=tmp;
   tmp=u->c[2]; u->c[2]=u->c[3];   u->c[3]=tmp;
}
}

void i4conv(u,n) union ie3float *u; register int n; {
/* 4-byte integers; self-inverting operation so only one routine needed
   to go both ways */
register char tmp; register int i;
for (i=0; i < n; u++,i++) {
   tmp=u->c[0]; u->c[0]=u->c[3]; u->c[3]=tmp;
   tmp=u->c[1]; u->c[1]=u->c[2]; u->c[2]=tmp;
}
}

void i2conv(u,n) register union ie3float *u; int n; {
   swab(u,2*n); /* included for the hell of it */
}

RESPONSE 2 >>>
X-VMS-News: vax3 comp.os.vms:1041
From: pack@acdpyr.ucar.edu (Dan Packman)
Subject:Re: difference between vax floating point representation and IEEE
Date: 22 Jul 88 22:37:55 GMT
Message-ID:<471@ncar.ucar.edu>

In article <2256@hubcap.UUCP> ghosh@hubcap.UUCP (Amitava Ghosh) writes:
>does anyone know what the difference is between the IEEE floating
>point representation and the way the VAX stores numbers. If so
>how does one convert from one format to the other...
The following program converts from vax f format to IEEE (UNIX point of view)
--------cut here-------------
      subroutine vax2lcl(n,iarray)
      dimension iarray(n)
      character*1 conv(4),ccc
      integer iconv
      equivalence (iconv,conv)
c Convert from vax floating point f format to pyramid IEEE single
      do 10 i=1,n
        iconv=iarray(i)
        if (iconv.ne.0) then
         ccc=conv(1)
         iii=ichar(conv(2))-1
         if (iii.lt.0)then
           write(0,100)iconv
 100       format(' vax2lcl: overflow int in =',i10)
           call exit(1)
         endif
         conv(1)=char(iii)
         conv(2)=ccc
         ccc=conv(3)
         conv(3)=conv(4)
         conv(4)=ccc
         iarray(i)=iconv
        endif
  10  continue
      return
      end
--------cut here-------------
The following converts an array of IEEE single floats to vax floats
It is written to work on a VAX/VMS machine
--------cut here-------------
      subroutine pyr2lcl(n,iarray)
      dimension iarray(n)
      byte conv(4),ccc
      integer iconv
      equivalence (iconv,conv)
c Convert from pyramid IEEE single to vax floating point f format
      do 10 i=1,n
        iconv=iarray(i)
        if (iconv.ne.0) then
         ccc=conv(2)
         iii=conv(1)+1
         if (iii.gt.255)then
           write(0,100)iconv
 100       format(' pyr2lcl: overflow int in =',i10)
           call exit(1)
         endif
         conv(2)=iii
         conv(1)=ccc
         ccc=conv(3)
         conv(3)=conv(4)
         conv(4)=ccc
         iarray(i)=iconv
        endif
  10  continue
      return
      end

RESPONSE 3 >>>
X-VMS-News: vax3 comp.os.vms:5427
From: olson@modular.UUCP (Jon Olson)
Subject:Re: VMS->IEEE fp conversion
Date: 22 Feb 89 00:10:00 GMT
Message-ID:<648@modular.UUCP>

I wrote the following MACRO routines a while ago to handle
conversion between IEEE and VAX F-floating point format.
I normally don't write MACRO routines because software portability
is important but, in this case, portability isn't really useful
to anything other than a VAX.  MACRO has circular shift instructions
which aren't available in C or FORTRAN.  Anyway, here is the macro
to do the floating point conversions.  Note that each routine takes
a F-floating point number by reference so they are easily called
from either FORTRAN or C.
;
;	Synopsis: Convert IEEE float to VAX F-float format
;
;	void ntohf( pf )
;	float *pf;
;
   .ENTRY    NTOHF,0
    MOVL     B^4(AP),R1			; R2 = pf
    MOVB     B^2(R1),R0			; shuffle bytes around from BIG-ENDIAN
    ASHL     #8,R0,R0			; to LITTLE-ENDIAN format
    MOVB     B^3(R1),R0
    ASHL     #8,R0,R0
    MOVB     (R1),R0
    ASHL     #8,R0,R0
    MOVB     B^1(R1),R0
    ROTL     #-7,R0,R0			; shift exponent to low-byte
    TSTB     R0				; dont change exponent if zero
    BEQL     NTOHF0
    ADDB2    #2,R0			; difference of 2 between IEEE and vax
NTOHF0:
    ROTL     #7,R0,R0			; shift exponent back proper place
    MOVL     R0,(R1)
    RET
;
;	Synopsis: Convert VAX F-float to IEEE format
;
;	void htonf( pf )
;	float *pf;
;
   .ENTRY    HTONF,0
    MOVL     B^4(AP),R1			; R2 = pf
    MOVL     (R1),R0
    ROTL     #-7,R0,R0			; shift exponent to low-byte
    TSTB     R0				; dont change exponent if zero
    BEQL     NTOHF0
    SUBB2    #2,R0			; difference of 2 between IEEE and vax
HTONF0:
    ROTL     #7,R0,R0			; shift back to proper format
    MOVB     R0,B^1(R1)
    ASHL     #-8,R0,R0
    MOVB     R0,(R1)
    ASHL     #-8,R0,R0
    MOVB     R0,B^3(R1)
    ASHL     #-8,R0,R0
    MOVB     R0,B^2(R1)
    RET

-- 
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
Rob Wright               |  psi%050529452300030::CROBW
Curtin University        |  CROBW%acad.curtin.edu.au@uunet.uu.net
Perth, Western Australia |  CROBW%acad.curtin.edu.au%munnari.oz@cunyvm.bitnet
Voice:+61 9 351 7385	 |  munnari!acad.curtin.edu.au!CROBW@uk.ac.ukc 
FAX:     09-351-2673     |  {backbones}!munnari!acad.curtin.edu.au!CROBW
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_

