1 /* $NetBSD: xdr_float.c,v 1.11 1997/03/29 21:04:08 thorpej Exp $ */ 2 3 /* 4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 5 * unrestricted use provided that this legend is included on all tape 6 * media and as a part of the software program in whole or part. Users 7 * may copy or modify Sun RPC without charge, but are not authorized 8 * to license or distribute it to anyone else except as part of a product or 9 * program developed by the user. 10 * 11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 14 * 15 * Sun RPC is provided with no support and without any obligation on the 16 * part of Sun Microsystems, Inc. to assist in its use, correction, 17 * modification or enhancement. 18 * 19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 21 * OR ANY PART THEREOF. 22 * 23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 24 * or profits or other special, indirect and consequential damages, even if 25 * Sun has been advised of the possibility of such damages. 26 * 27 * Sun Microsystems, Inc. 28 * 2550 Garcia Avenue 29 * Mountain View, California 94043 30 */ 31 32 #if defined(LIBC_SCCS) && !defined(lint) 33 /*static char *sccsid = "from: @(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";*/ 34 /*static char *sccsid = "from: @(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC";*/ 35 static char *rcsid = "$NetBSD: xdr_float.c,v 1.11 1997/03/29 21:04:08 thorpej Exp $"; 36 #endif 37 38 /* 39 * xdr_float.c, Generic XDR routines impelmentation. 40 * 41 * Copyright (C) 1984, Sun Microsystems, Inc. 42 * 43 * These are the "floating point" xdr routines used to (de)serialize 44 * most common data items. See xdr.h for more info on the interface to 45 * xdr. 46 */ 47 48 #include <stdio.h> 49 #include <sys/types.h> 50 #include <sys/param.h> 51 #include <rpc/types.h> 52 #include <rpc/xdr.h> 53 54 /* 55 * NB: Not portable. 56 * This routine works on machines with IEEE754 FP and Vaxen. 57 */ 58 59 #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \ 60 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \ 61 defined(__arm32__) || defined(__powerpc__) 62 #include <machine/endian.h> 63 #define IEEEFP 64 #endif 65 66 #ifdef vax 67 68 /* What IEEE single precision floating point looks like on a Vax */ 69 struct ieee_single { 70 unsigned int mantissa: 23; 71 unsigned int exp : 8; 72 unsigned int sign : 1; 73 }; 74 75 /* Vax single precision floating point */ 76 struct vax_single { 77 unsigned int mantissa1 : 7; 78 unsigned int exp : 8; 79 unsigned int sign : 1; 80 unsigned int mantissa2 : 16; 81 }; 82 83 #define VAX_SNG_BIAS 0x81 84 #define IEEE_SNG_BIAS 0x7f 85 86 static struct sgl_limits { 87 struct vax_single s; 88 struct ieee_single ieee; 89 } sgl_limits[2] = { 90 {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */ 91 { 0x0, 0xff, 0x0 }}, /* Max IEEE */ 92 {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */ 93 { 0x0, 0x0, 0x0 }} /* Min IEEE */ 94 }; 95 #endif /* vax */ 96 97 bool_t 98 xdr_float(xdrs, fp) 99 register XDR *xdrs; 100 register float *fp; 101 { 102 #ifdef IEEEFP 103 bool_t rv; 104 long tmpl; 105 #else 106 struct ieee_single is; 107 struct vax_single vs, *vsp; 108 struct sgl_limits *lim; 109 int i; 110 #endif 111 switch (xdrs->x_op) { 112 113 case XDR_ENCODE: 114 #ifdef IEEEFP 115 tmpl = *(int32_t *)fp; 116 return (XDR_PUTLONG(xdrs, &tmpl)); 117 #else 118 vs = *((struct vax_single *)fp); 119 for (i = 0, lim = sgl_limits; 120 i < sizeof(sgl_limits)/sizeof(struct sgl_limits); 121 i++, lim++) { 122 if ((vs.mantissa2 == lim->s.mantissa2) && 123 (vs.exp == lim->s.exp) && 124 (vs.mantissa1 == lim->s.mantissa1)) { 125 is = lim->ieee; 126 goto shipit; 127 } 128 } 129 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS; 130 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2; 131 shipit: 132 is.sign = vs.sign; 133 return (XDR_PUTLONG(xdrs, (long *)&is)); 134 #endif 135 136 case XDR_DECODE: 137 #ifdef IEEEFP 138 rv = XDR_GETLONG(xdrs, &tmpl); 139 *(int32_t *)fp = tmpl; 140 return (rv); 141 #else 142 vsp = (struct vax_single *)fp; 143 if (!XDR_GETLONG(xdrs, (long *)&is)) 144 return (FALSE); 145 for (i = 0, lim = sgl_limits; 146 i < sizeof(sgl_limits)/sizeof(struct sgl_limits); 147 i++, lim++) { 148 if ((is.exp == lim->ieee.exp) && 149 (is.mantissa == lim->ieee.mantissa)) { 150 *vsp = lim->s; 151 goto doneit; 152 } 153 } 154 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS; 155 vsp->mantissa2 = is.mantissa; 156 vsp->mantissa1 = (is.mantissa >> 16); 157 doneit: 158 vsp->sign = is.sign; 159 return (TRUE); 160 #endif 161 162 case XDR_FREE: 163 return (TRUE); 164 } 165 return (FALSE); 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