Xref: utzoo comp.lang.c:29359 comp.unix.ultrix:3680 comp.sys.ibm.pc:51911 comp.sources.wanted:11984 Path: utzoo!attcan!uunet!pilchuck!dataio!bright From: bright@Data-IO.COM (Walter Bright) Newsgroups: comp.lang.c,comp.unix.ultrix,comp.sys.ibm.pc,comp.sources.wanted Subject: Re: Routine to convert between IEEE and VAX floating point ? Message-ID: <2529@dataio.Data-IO.COM> Date: 4 Jun 90 18:36:39 GMT References: <1023@rna.UUCP> <1990Jun3.084602.1662@noao.edu> Reply-To: bright@Data-IO.COM (Walter Bright) Followup-To: comp.lang.c Organization: Data I/O Corporation; Redmond, WA Lines: 47 From article <1023@rna.UUCP>, by dan@rna.UUCP (Dan Ts'o): < Does anyone have a C routine to convert from IEEE floating point < to VAX D format floating point ? Here's one I wrote to convert from VAX to PC 64-bit doubles. You could easily reverse it to go the other way. main() { double d1,d2; long *p; while (1) { scanf(" %lf",&d1); p = (long *) &d1; printf("x%08lx,x%08lx\n",p[1],p[0]); vaxdbl_to_pcdbl(&d1,&d2); p = (long *) &d2; printf("x%08lx,x%08lx\n",p[1],p[0]); } } vaxdbl_to_pcdbl(pvax,ppc) unsigned long *pvax,*ppc; { unsigned sign,exponent; unsigned long fraction; sign = (*pvax & 0x8000) != 0; exponent = (*pvax >> 7) & 0xFF; fraction = ((*pvax & 0x7F) << 16) | (*pvax >> 16); printf("sign = %d, exponent = x%x, fraction = x%lx\n", sign,exponent,fraction); if (sign == 0 && exponent == 0) { ppc[0] = 0; ppc[1] = 0; return; } /* Put sign in result */ ppc[1] = (unsigned long) sign << 31; /* Compute new exponent */ exponent += 0x3FF - 0x80 - 1; ppc[1] |= (unsigned long) exponent << 20; /* OR in fraction */ ppc[1] |= (fraction >> 3); ppc[0] = (fraction << 29) | (pvax[1] >> 3); }