1 /* 2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 3 * unrestricted use provided that this legend is included on all tape 4 * media and as a part of the software program in whole or part. Users 5 * may copy or modify Sun RPC without charge, but are not authorized 6 * to license or distribute it to anyone else except as part of a product or 7 * program developed by the user. 8 * 9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 12 * 13 * Sun RPC is provided with no support and without any obligation on the 14 * part of Sun Microsystems, Inc. to assist in its use, correction, 15 * modification or enhancement. 16 * 17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 19 * OR ANY PART THEREOF. 20 * 21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 22 * or profits or other special, indirect and consequential damages, even if 23 * Sun has been advised of the possibility of such damages. 24 * 25 * Sun Microsystems, Inc. 26 * 2550 Garcia Avenue 27 * Mountain View, California 94043 28 */ 29 30 #if defined(LIBC_SCCS) && !defined(lint) 31 /*static char *sccsid = "from: @(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";*/ 32 /*static char *sccsid = "from: @(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC";*/ 33 static char *rcsid = "$Id: xdr_float.c,v 1.4 1994/12/20 16:12:10 cgd Exp $"; 34 #endif 35 36 /* 37 * xdr_float.c, Generic XDR routines impelmentation. 38 * 39 * Copyright (C) 1984, Sun Microsystems, Inc. 40 * 41 * These are the "floating point" xdr routines used to (de)serialize 42 * most common data items. See xdr.h for more info on the interface to 43 * xdr. 44 */ 45 46 #include <stdio.h> 47 #include <sys/types.h> 48 #include <sys/param.h> 49 #include <rpc/types.h> 50 #include <rpc/xdr.h> 51 52 /* 53 * NB: Not portable. 54 * This routine works on Suns (Sky / 68000's), i386's, MIPS, NS32k and Vaxen, 55 * Sparcs, and Alphas 56 */ 57 58 #if defined(mc68000)||defined(sparc)||defined(i386)||defined(mips)||defined(ns32k)||defined(alpha) 59 #define IEEEFP 60 #endif 61 62 #ifdef vax 63 64 /* What IEEE single precision floating point looks like on a Vax */ 65 struct ieee_single { 66 unsigned int mantissa: 23; 67 unsigned int exp : 8; 68 unsigned int sign : 1; 69 }; 70 71 /* Vax single precision floating point */ 72 struct vax_single { 73 unsigned int mantissa1 : 7; 74 unsigned int exp : 8; 75 unsigned int sign : 1; 76 unsigned int mantissa2 : 16; 77 }; 78 79 #define VAX_SNG_BIAS 0x81 80 #define IEEE_SNG_BIAS 0x7f 81 82 static struct sgl_limits { 83 struct vax_single s; 84 struct ieee_single ieee; 85 } sgl_limits[2] = { 86 {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */ 87 { 0x0, 0xff, 0x0 }}, /* Max IEEE */ 88 {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */ 89 { 0x0, 0x0, 0x0 }} /* Min IEEE */ 90 }; 91 #endif /* vax */ 92 93 bool_t 94 xdr_float(xdrs, fp) 95 register XDR *xdrs; 96 register float *fp; 97 { 98 #ifdef IEEEFP 99 bool_t rv; 100 long tmpl; 101 #else 102 struct ieee_single is; 103 struct vax_single vs, *vsp; 104 struct sgl_limits *lim; 105 int i; 106 #endif 107 switch (xdrs->x_op) { 108 109 case XDR_ENCODE: 110 #ifdef IEEEFP 111 tmpl = *(int32_t *)fp; 112 return (XDR_PUTLONG(xdrs, &tmpl)); 113 #else 114 vs = *((struct vax_single *)fp); 115 for (i = 0, lim = sgl_limits; 116 i < sizeof(sgl_limits)/sizeof(struct sgl_limits); 117 i++, lim++) { 118 if ((vs.mantissa2 == lim->s.mantissa2) && 119 (vs.exp == lim->s.exp) && 120 (vs.mantissa1 == lim->s.mantissa1)) { 121 is = lim->ieee; 122 goto shipit; 123 } 124 } 125 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS; 126 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2; 127 shipit: 128 is.sign = vs.sign; 129 return (XDR_PUTLONG(xdrs, (long *)&is)); 130 #endif 131 132 case XDR_DECODE: 133 #ifdef IEEEFP 134 rv = XDR_GETLONG(xdrs, &tmpl); 135 *(int32_t *)fp = tmpl; 136 return (rv); 137 #else 138 vsp = (struct vax_single *)fp; 139 if (!XDR_GETLONG(xdrs, (long *)&is)) 140 return (FALSE); 141 for (i = 0, lim = sgl_limits; 142 i < sizeof(sgl_limits)/sizeof(struct sgl_limits); 143 i++, lim++) { 144 if ((is.exp == lim->ieee.exp) && 145 (is.mantissa == lim->ieee.mantissa)) { 146 *vsp = lim->s; 147 goto doneit; 148 } 149 } 150 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS; 151 vsp->mantissa2 = is.mantissa; 152 vsp->mantissa1 = (is.mantissa >> 16); 153 doneit: 154 vsp->sign = is.sign; 155 return (TRUE); 156 #endif 157 158 case XDR_FREE: 159 return (TRUE); 160 } 161 return (FALSE); 162 } 163 164 /* 165 * This routine works on Suns (Sky / 68000's), i386's, MIPS and Vaxen. 166 */ 167 168 #ifdef vax 169 /* What IEEE double precision floating point looks like on a Vax */ 170 struct ieee_double { 171 unsigned int mantissa1 : 20; 172 unsigned int exp : 11; 173 unsigned int sign : 1; 174 unsigned int mantissa2 : 32; 175 }; 176 177 /* Vax double precision floating point */ 178 struct vax_double { 179 unsigned int mantissa1 : 7; 180 unsigned int exp : 8; 181 unsigned int sign : 1; 182 unsigned int mantissa2 : 16; 183 unsigned int mantissa3 : 16; 184 unsigned int mantissa4 : 16; 185 }; 186 187 #define VAX_DBL_BIAS 0x81 188 #define IEEE_DBL_BIAS 0x3ff 189 #define MASK(nbits) ((1 << nbits) - 1) 190 191 static struct dbl_limits { 192 struct vax_double d; 193 struct ieee_double ieee; 194 } dbl_limits[2] = { 195 {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */ 196 { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */ 197 {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */ 198 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */ 199 }; 200 201 #endif /* vax */ 202 203 204 bool_t 205 xdr_double(xdrs, dp) 206 register XDR *xdrs; 207 double *dp; 208 { 209 #ifdef IEEEFP 210 register int32_t *i32p; 211 bool_t rv; 212 long tmpl; 213 #else 214 register long *lp; 215 struct ieee_double id; 216 struct vax_double vd; 217 register struct dbl_limits *lim; 218 int i; 219 #endif 220 221 switch (xdrs->x_op) { 222 223 case XDR_ENCODE: 224 #ifdef IEEEFP 225 i32p = (int32_t *)dp; 226 #if BYTE_ORDER == BIG_ENDIAN 227 tmpl = *i32p++; 228 rv = XDR_PUTLONG(xdrs, &tmpl); 229 if (!rv) 230 return (rv); 231 tmpl = *i32p; 232 rv = XDR_PUTLONG(xdrs, &tmpl); 233 #else 234 tmpl = *i32p+1; 235 rv = XDR_PUTLONG(xdrs, &tmpl); 236 if (!rv) 237 return (rv); 238 tmpl = *i32p; 239 rv = XDR_PUTLONG(xdrs, &tmpl); 240 #endif 241 return (rv); 242 #else 243 vd = *((struct vax_double *)dp); 244 for (i = 0, lim = dbl_limits; 245 i < sizeof(dbl_limits)/sizeof(struct dbl_limits); 246 i++, lim++) { 247 if ((vd.mantissa4 == lim->d.mantissa4) && 248 (vd.mantissa3 == lim->d.mantissa3) && 249 (vd.mantissa2 == lim->d.mantissa2) && 250 (vd.mantissa1 == lim->d.mantissa1) && 251 (vd.exp == lim->d.exp)) { 252 id = lim->ieee; 253 goto shipit; 254 } 255 } 256 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS; 257 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3); 258 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) | 259 (vd.mantissa3 << 13) | 260 ((vd.mantissa4 >> 3) & MASK(13)); 261 shipit: 262 id.sign = vd.sign; 263 lp = (long *)&id; 264 return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp)); 265 #endif 266 267 case XDR_DECODE: 268 #ifdef IEEEFP 269 i32p = (int32_t *)dp; 270 #if BYTE_ORDER == BIG_ENDIAN 271 rv = XDR_GETLONG(xdrs, &tmpl); 272 *i32p++ = tmpl; 273 if (!rv) 274 return (rv); 275 rv = XDR_GETLONG(xdrs, &tmpl); 276 *i32p = tmpl; 277 #else 278 rv = XDR_GETLONG(xdrs, &tmpl); 279 *(i32p+1) = tmpl; 280 if (!rv) 281 return (rv); 282 rv = XDR_GETLONG(xdrs, &tmpl); 283 *i32p = tmpl; 284 #endif 285 return (rv); 286 #else 287 lp = (long *)&id; 288 if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp)) 289 return (FALSE); 290 for (i = 0, lim = dbl_limits; 291 i < sizeof(dbl_limits)/sizeof(struct dbl_limits); 292 i++, lim++) { 293 if ((id.mantissa2 == lim->ieee.mantissa2) && 294 (id.mantissa1 == lim->ieee.mantissa1) && 295 (id.exp == lim->ieee.exp)) { 296 vd = lim->d; 297 goto doneit; 298 } 299 } 300 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS; 301 vd.mantissa1 = (id.mantissa1 >> 13); 302 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) | 303 (id.mantissa2 >> 29); 304 vd.mantissa3 = (id.mantissa2 >> 13); 305 vd.mantissa4 = (id.mantissa2 << 3); 306 doneit: 307 vd.sign = id.sign; 308 *dp = *((double *)&vd); 309 return (TRUE); 310 #endif 311 312 case XDR_FREE: 313 return (TRUE); 314 } 315 return (FALSE); 316 } 317