Routine to convert between IEEE and VAX floating point ?
Walter Bright
bright at Data-IO.COM
Tue Jun 5 04:36:39 AEST 1990
>From article <1023 at rna.UUCP>, by dan at 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);
}
More information about the Comp.lang.c
mailing list