Routine to convert between IEEE and VAX floating point ?
Craig Statchuk
craigs at cognos.UUCP
Fri Jun 8 02:30:34 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 ?
>
I had to do this a while back. I found it easiest to convert
IEEE -> G_FLOAT ->D_FLOAT. As mentioned in another response all
you have to do swap bytes and change the exponent to go from
IEEE to G_FLOAT. The VAX RTL has the necessary routine to
change from G_FLOAT to D_FLOAT (and has another one if you wish to go
back).
Here's some code that will do the translation on the (using
bit fields yeah!):
union G_float_template {
double asDouble;
struct {
unsigned long b1:16,b2:16,b3:16,b4:16;
} asBits;
struct {
unsigned long mantissaL:20,exponent:11,sign:1,mantissaH:32;
} asFields;
};
union G_float_template value;
unsigned short s_temp;
double result;
extern double MTH$CVT_G_D();
...
/* put some value into the template -- assume it is really in IEEE format */
value.asDouble = (IEEE) 1234.5
/* exchange upper 16 bits with lower 16 bits in each word */
s_temp = value.asBits.b1;
value.asBits.b1 = value.asBits.b2;
value.asBits.b2 = s_temp;
s_temp = value.asBits.b3;
value.asBits.b3 = value.asBit.b4;
value.asBits.b4 = s_temp;
/* normalize exponent bias */
if (value.asDouble != 0.0)
net_value.asFields.exponent -= 2;
/* convert to D_FLOAT */
result = MTH$CVT_G_D(&value.asDouble);
Easy eh?
/CS
--
Craig Statchuk USENET : uunet!mitel!sce!cognos!craigs
Cognos Incorporated INTERNET : craigs%cognos.uucp at uunet.uu.net
3755 Riverside Dr. MaBellNET: (613) 738-1440
Ottawa, Ontario K1G 3Z4 FaxNET : (613) 738-0002
More information about the Comp.lang.c
mailing list