Main Page   Modules   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

/projects/cubeos/src_current/net/rpc/xdr_float.c

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

Generated on Thu Feb 20 15:38:46 2003 for cubeOS by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002