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 *rcsid = "$OpenBSD: xdr_float.c,v 1.4 1996/08/19 08:32:03 tholo Exp $"; 32 #endif /* LIBC_SCCS and not lint */ 33 34 /* 35 * xdr_float.c, Generic XDR routines impelmentation. 36 * 37 * Copyright (C) 1984, Sun Microsystems, Inc. 38 * 39 * These are the "floating point" xdr routines used to (de)serialize 40 * most common data items. See xdr.h for more info on the interface to 41 * xdr. 42 */ 43 44 #include <stdio.h> 45 #include <sys/types.h> 46 #include <sys/param.h> 47 #include <rpc/types.h> 48 #include <rpc/xdr.h> 49 50 /* 51 * NB: Not portable. 52 * This routine works on machines with IEEE754 FP and Vaxen. 53 */ 54 55 #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \ 56 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \ 57 defined(__arm32__) 58 #include <machine/endian.h> 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 #ifdef vax 165 /* What IEEE double precision floating point looks like on a Vax */ 166 struct ieee_double { 167 unsigned int mantissa1 : 20; 168 unsigned int exp : 11; 169 unsigned int sign : 1; 170 unsigned int mantissa2 : 32; 171 }; 172 173 /* Vax double precision floating point */ 174 struct vax_double { 175 unsigned int mantissa1 : 7; 176 unsigned int exp : 8; 177 unsigned int sign : 1; 178 unsigned int mantissa2 : 16; 179 unsigned int mantissa3 : 16; 180 unsigned int mantissa4 : 16; 181 }; 182 183 #define VAX_DBL_BIAS 0x81 184 #define IEEE_DBL_BIAS 0x3ff 185 #define MASK(nbits) ((1 << nbits) - 1) 186 187 static struct dbl_limits { 188 struct vax_double d; 189 struct ieee_double ieee; 190 } dbl_limits[2] = { 191 {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */ 192 { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */ 193 {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */ 194 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */ 195 }; 196 197 #endif /* vax */ 198 199 200 bool_t 201 xdr_double(xdrs, dp) 202 register XDR *xdrs; 203 double *dp; 204 { 205 #ifdef IEEEFP 206 register int32_t *i32p; 207 bool_t rv; 208 long tmpl; 209 #else 210 register long *lp; 211 struct ieee_double id; 212 struct vax_double vd; 213 register struct dbl_limits *lim; 214 int i; 215 #endif 216 217 switch (xdrs->x_op) { 218 219 case XDR_ENCODE: 220 #ifdef IEEEFP 221 i32p = (int32_t *)dp; 222 #if BYTE_ORDER == BIG_ENDIAN 223 tmpl = *i32p++; 224 rv = XDR_PUTLONG(xdrs, &tmpl); 225 if (!rv) 226 return (rv); 227 tmpl = *i32p; 228 rv = XDR_PUTLONG(xdrs, &tmpl); 229 #else 230 tmpl = *(i32p+1); 231 rv = XDR_PUTLONG(xdrs, &tmpl); 232 if (!rv) 233 return (rv); 234 tmpl = *i32p; 235 rv = XDR_PUTLONG(xdrs, &tmpl); 236 #endif 237 return (rv); 238 #else 239 vd = *((struct vax_double *)dp); 240 for (i = 0, lim = dbl_limits; 241 i < sizeof(dbl_limits)/sizeof(struct dbl_limits); 242 i++, lim++) { 243 if ((vd.mantissa4 == lim->d.mantissa4) && 244 (vd.mantissa3 == lim->d.mantissa3) && 245 (vd.mantissa2 == lim->d.mantissa2) && 246 (vd.mantissa1 == lim->d.mantissa1) && 247 (vd.exp == lim->d.exp)) { 248 id = lim->ieee; 249 goto shipit; 250 } 251 } 252 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS; 253 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3); 254 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) | 255 (vd.mantissa3 << 13) | 256 ((vd.mantissa4 >> 3) & MASK(13)); 257 shipit: 258 id.sign = vd.sign; 259 lp = (long *)&id; 260 return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp)); 261 #endif 262 263 case XDR_DECODE: 264 #ifdef IEEEFP 265 i32p = (int32_t *)dp; 266 #if BYTE_ORDER == BIG_ENDIAN 267 rv = XDR_GETLONG(xdrs, &tmpl); 268 *i32p++ = tmpl; 269 if (!rv) 270 return (rv); 271 rv = XDR_GETLONG(xdrs, &tmpl); 272 *i32p = tmpl; 273 #else 274 rv = XDR_GETLONG(xdrs, &tmpl); 275 *(i32p+1) = tmpl; 276 if (!rv) 277 return (rv); 278 rv = XDR_GETLONG(xdrs, &tmpl); 279 *i32p = tmpl; 280 #endif 281 return (rv); 282 #else 283 lp = (long *)&id; 284 if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp)) 285 return (FALSE); 286 for (i = 0, lim = dbl_limits; 287 i < sizeof(dbl_limits)/sizeof(struct dbl_limits); 288 i++, lim++) { 289 if ((id.mantissa2 == lim->ieee.mantissa2) && 290 (id.mantissa1 == lim->ieee.mantissa1) && 291 (id.exp == lim->ieee.exp)) { 292 vd = lim->d; 293 goto doneit; 294 } 295 } 296 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS; 297 vd.mantissa1 = (id.mantissa1 >> 13); 298 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) | 299 (id.mantissa2 >> 29); 300 vd.mantissa3 = (id.mantissa2 >> 13); 301 vd.mantissa4 = (id.mantissa2 << 3); 302 doneit: 303 vd.sign = id.sign; 304 *dp = *((double *)&vd); 305 return (TRUE); 306 #endif 307 308 case XDR_FREE: 309 return (TRUE); 310 } 311 return (FALSE); 312 } 313