00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include <stdio.h>
00049
00050 #include <rpc/types.h>
00051 #include <rpc/xdr.h>
00052
00053
00054
00055
00056
00057
00058 #ifdef vax
00059
00060
00061 struct ieee_single {
00062 unsigned int mantissa:23;
00063 unsigned int exp:8;
00064 unsigned int sign:1;
00065 };
00066
00067
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 },
00086 {
00087 0x0, 0xff, 0x0
00088 }
00089 },
00090 { {
00091 0x0, 0x0, 0x0, 0x0
00092 },
00093 {
00094 0x0, 0x0, 0x0
00095 }
00096 }
00097 };
00098
00099 #endif
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
00168
00169
00170 #ifdef vax
00171
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
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 },
00201 {
00202 0x0, 0x7ff, 0x0, 0x0
00203 }
00204 },
00205 { {
00206 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
00207 },
00208 {
00209 0x0, 0x0, 0x0, 0x0
00210 }
00211 }
00212 };
00213
00214 #endif
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 }