Xref: utzoo comp.lang.c:29483 comp.unix.ultrix:3712 comp.sys.ibm.pc:52244 comp.sources.wanted:12053 Path: utzoo!dciem!nrcaer!sce!cognos!craigs From: craigs@cognos.UUCP (Craig Statchuk) 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: <8452@cognos.UUCP> Date: 7 Jun 90 16:30:34 GMT References: <1023@rna.UUCP> <1990Jun3.084602.1662@noao.edu> Reply-To: craigs@cognos.UUCP (Craig Statchuk) Followup-To: comp.lang.c Organization: Cognos Inc., Ottawa, Canada Lines: 60 >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 ? > 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@uunet.uu.net 3755 Riverside Dr. MaBellNET: (613) 738-1440 Ottawa, Ontario K1G 3Z4 FaxNET : (613) 738-0002