Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

xdr_floa.c

Go to the documentation of this file.
00001 /*********************************************************************
00002  * RPC for the Windows NT Operating System
00003  * 1993 by Martin F. Gergeleit
00004  * Users may use, copy or modify Sun RPC for the Windows NT Operating
00005  * System according to the Sun copyright below.
00006  *
00007  * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO
00008  * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE
00009  * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
00010  *********************************************************************/
00011 
00012 /* @(#)xdr_float.c      2.1 88/07/29 4.0 RPCSRC */
00013 /*
00014  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00015  * unrestricted use provided that this legend is included on all tape
00016  * media and as a part of the software program in whole or part.  Users
00017  * may copy or modify Sun RPC without charge, but are not authorized
00018  * to license or distribute it to anyone else except as part of a product or
00019  * program developed by the user.
00020  *
00021  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00022  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00023  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00024  *
00025  * Sun RPC is provided with no support and without any obligation on the
00026  * part of Sun Microsystems, Inc. to assist in its use, correction,
00027  * modification or enhancement.
00028  *
00029  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00030  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00031  * OR ANY PART THEREOF.
00032  *
00033  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00034  * or profits or other special, indirect and consequential damages, even if
00035  * Sun has been advised of the possibility of such damages.
00036  *
00037  * Sun Microsystems, Inc.
00038  * 2550 Garcia Avenue
00039  * Mountain View, California  94043
00040  */
00041 #if !defined(lint) && defined(SCCSIDS)
00042 static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
00043 #endif
00044 
00045 /*
00046  * xdr_float.c, Generic XDR routines impelmentation.
00047  *
00048  * Copyright (C) 1984, Sun Microsystems, Inc.
00049  *
00050  * These are the "floating point" xdr routines used to (de)serialize
00051  * most common data items.  See xdr.h for more info on the interface to
00052  * xdr.
00053  */
00054 
00055 #if !defined(UNDER_CE) && !defined(WINDOWS_CE)
00056 #include <stdio.h>
00057 #endif
00058 
00059 #include "xdr.h"
00060 
00061 /*
00062  * NB: Not portable.
00063  * This routine works on Suns (Sky / 68000's) and Vaxen.
00064  */
00065 
00066 #ifdef vax
00067 
00068 /* What IEEE single precision floating point looks like on a Vax */
00069 struct  ieee_single {
00070         unsigned int    mantissa: 23;
00071         unsigned int    exp     : 8;
00072         unsigned int    sign    : 1;
00073 };
00074 
00075 /* Vax single precision floating point */
00076 struct  vax_single {
00077         unsigned int    mantissa1 : 7;
00078         unsigned int    exp       : 8;
00079         unsigned int    sign      : 1;
00080         unsigned int    mantissa2 : 16;
00081 };
00082 
00083 #define VAX_SNG_BIAS    0x81
00084 #define IEEE_SNG_BIAS   0x7f
00085 
00086 static struct sgl_limits {
00087         struct vax_single s;
00088         struct ieee_single ieee;
00089 } sgl_limits[2] = {
00090         {{ 0x7f, 0xff, 0x0, 0xffff },   /* Max Vax */
00091         { 0x0, 0xff, 0x0 }},            /* Max IEEE */
00092         {{ 0x0, 0x0, 0x0, 0x0 },        /* Min Vax */
00093         { 0x0, 0x0, 0x0 }}              /* Min IEEE */
00094 };
00095 #endif /* vax */
00096 
00097 bool_t
00098 xdr_float(xdrs, fp)
00099         register XDR *xdrs;
00100         register float *fp;
00101 {
00102 
00103 #if !defined(mc68000) && !defined(sparc) && !defined(mips) && !defined(mmax) && !defined(_X86_)
00104         struct ieee_single is;
00105         struct vax_single vs, *vsp;
00106         struct sgl_limits *lim;
00107         int i;
00108 #endif
00109         switch (xdrs->x_op) {
00110 
00111         case XDR_ENCODE:
00112 #if defined(mc68000) || defined(sparc) || defined(mips) || defined(mmax) || defined(_X86_)
00113                 return (XDR_PUTLONG(xdrs, (long *)fp));
00114 #else
00115                 vs = *((struct vax_single *)fp);
00116                 for (i = 0, lim = sgl_limits;
00117                         i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
00118                         i++, lim++) {
00119                         if ((vs.mantissa2 == lim->s.mantissa2) &&
00120                                 (vs.exp == lim->s.exp) &&
00121                                 (vs.mantissa1 == lim->s.mantissa1)) {
00122                                 is = lim->ieee;
00123                                 goto shipit;
00124                         }
00125                 }
00126                 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
00127                 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
00128         shipit:
00129                 is.sign = vs.sign;
00130                 return (XDR_PUTLONG(xdrs, (long *)&is));
00131 #endif
00132 
00133         case XDR_DECODE:
00134 #if defined(mc68000) || defined(sparc) || defined(mips) || defined(mmax) || defined(_X86_)
00135                 return (XDR_GETLONG(xdrs, (long *)fp));
00136 #else
00137                 vsp = (struct vax_single *)fp;
00138                 if (!XDR_GETLONG(xdrs, (long *)&is))
00139                         return (FALSE);
00140                 for (i = 0, lim = sgl_limits;
00141                         i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
00142                         i++, lim++) {
00143                         if ((is.exp == lim->ieee.exp) &&
00144                                 (is.mantissa == lim->ieee.mantissa)) {
00145                                 *vsp = lim->s;
00146                                 goto doneit;
00147                         }
00148                 }
00149                 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
00150                 vsp->mantissa2 = is.mantissa;
00151                 vsp->mantissa1 = (is.mantissa >> 16);
00152         doneit:
00153                 vsp->sign = is.sign;
00154                 return (TRUE);
00155 #endif
00156 
00157         case XDR_FREE:
00158                 return (TRUE);
00159         }
00160         return (FALSE);
00161 }
00162 
00163 /*
00164  * This routine works on Suns (Sky / 68000's) and Vaxen.
00165  */
00166 
00167 #ifdef vax
00168 /* What IEEE double precision floating point looks like on a Vax */
00169 struct  ieee_double {
00170         unsigned int    mantissa1 : 20;
00171         unsigned int    exp       : 11;
00172         unsigned int    sign      : 1;
00173         unsigned int    mantissa2 : 32;
00174 };
00175 
00176 /* Vax double precision floating point */
00177 struct  vax_double {
00178         unsigned int    mantissa1 : 7;
00179         unsigned int    exp       : 8;
00180         unsigned int    sign      : 1;
00181         unsigned int    mantissa2 : 16;
00182         unsigned int    mantissa3 : 16;
00183         unsigned int    mantissa4 : 16;
00184 };
00185 
00186 #define VAX_DBL_BIAS    0x81
00187 #define IEEE_DBL_BIAS   0x3ff
00188 #define MASK(nbits)     ((1 << nbits) - 1)
00189 
00190 static struct dbl_limits {
00191         struct  vax_double d;
00192         struct  ieee_double ieee;
00193 } dbl_limits[2] = {
00194         {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },   /* Max Vax */
00195         { 0x0, 0x7ff, 0x0, 0x0 }},                      /* Max IEEE */
00196         {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},               /* Min Vax */
00197         { 0x0, 0x0, 0x0, 0x0 }}                         /* Min IEEE */
00198 };
00199 
00200 #endif /* vax */
00201 
00202 
00203 bool_t
00204 xdr_double(xdrs, dp)
00205         register XDR *xdrs;
00206         double *dp;
00207 {
00208         register long *lp;
00209 #if !defined(mc68000) && !defined(sparc) && !defined(mips) && !defined(mmax) && !defined(_X86_)
00210         struct  ieee_double id;
00211         struct  vax_double vd;
00212         register struct dbl_limits *lim;
00213         int i;
00214 #endif
00215 
00216         switch (xdrs->x_op) {
00217 
00218         case XDR_ENCODE:
00219 #if defined(mc68000) || defined(sparc) || defined(mips) || defined(mmax) || defined(_X86_)
00220                 lp = (long *)dp;
00221 #else
00222                 vd = *((struct vax_double *)dp);
00223                 for (i = 0, lim = dbl_limits;
00224                         i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
00225                         i++, lim++) {
00226                         if ((vd.mantissa4 == lim->d.mantissa4) &&
00227                                 (vd.mantissa3 == lim->d.mantissa3) &&
00228                                 (vd.mantissa2 == lim->d.mantissa2) &&
00229                                 (vd.mantissa1 == lim->d.mantissa1) &&
00230                                 (vd.exp == lim->d.exp)) {
00231                                 id = lim->ieee;
00232                                 goto shipit;
00233                         }
00234                 }
00235                 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
00236                 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
00237                 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
00238                                 (vd.mantissa3 << 13) |
00239                                 ((vd.mantissa4 >> 3) & MASK(13));
00240         shipit:
00241                 id.sign = vd.sign;
00242                 lp = (long *)&id;
00243 #endif
00244 #if defined(_X86_)
00245                 return (XDR_PUTLONG(xdrs, lp+1) && XDR_PUTLONG(xdrs, lp));
00246 #else
00247                 return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
00248 #endif
00249         case XDR_DECODE:
00250 #if defined(mc68000) || defined(sparc) || defined(mips) || defined(mmax) || defined(_X86_)
00251                 lp = (long *)dp;
00252 #if defined(_X86_)
00253                 return (XDR_GETLONG(xdrs, lp+1) && XDR_GETLONG(xdrs, lp));
00254 #else
00255                 return (XDR_GETLONG(xdrs, lp++) && XDR_GETLONG(xdrs, lp));
00256 #endif
00257 #else
00258                 lp = (long *)&id;
00259                 if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
00260                         return (FALSE);
00261                 for (i = 0, lim = dbl_limits;
00262                         i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
00263                         i++, lim++) {
00264                         if ((id.mantissa2 == lim->ieee.mantissa2) &&
00265                                 (id.mantissa1 == lim->ieee.mantissa1) &&
00266                                 (id.exp == lim->ieee.exp)) {
00267                                 vd = lim->d;
00268                                 goto doneit;
00269                         }
00270                 }
00271                 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
00272                 vd.mantissa1 = (id.mantissa1 >> 13);
00273                 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
00274                                 (id.mantissa2 >> 29);
00275                 vd.mantissa3 = (id.mantissa2 >> 13);
00276                 vd.mantissa4 = (id.mantissa2 << 3);
00277         doneit:
00278                 vd.sign = id.sign;
00279                 *dp = *((double *)&vd);
00280                 return (TRUE);
00281 #endif
00282 
00283         case XDR_FREE:
00284                 return (TRUE);
00285         }
00286         return (FALSE);
00287 }

Generated on Sun Dec 2 15:56:54 2001 for rcslib by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001