1 /* $NetBSD: xdr_float.c,v 1.18 1998/11/15 17:32:47 christos 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 #include <sys/cdefs.h> 33 #if defined(LIBC_SCCS) && !defined(lint) 34 #if 0 35 static char *sccsid = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro"; 36 static char *sccsid = "@(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC"; 37 #else 38 __RCSID("$NetBSD: xdr_float.c,v 1.18 1998/11/15 17:32:47 christos Exp $"); 39 #endif 40 #endif 41 42 /* 43 * xdr_float.c, Generic XDR routines impelmentation. 44 * 45 * Copyright (C) 1984, Sun Microsystems, Inc. 46 * 47 * These are the "floating point" xdr routines used to (de)serialize 48 * most common data items. See xdr.h for more info on the interface to 49 * xdr. 50 */ 51 52 #include "namespace.h" 53 54 #include <sys/types.h> 55 #include <sys/param.h> 56 57 #include <stdio.h> 58 59 #include <rpc/types.h> 60 #include <rpc/xdr.h> 61 62 #ifdef __weak_alias 63 __weak_alias(xdr_double,_xdr_double); 64 __weak_alias(xdr_float,_xdr_float); 65 #endif 66 67 /* 68 * NB: Not portable. 69 * This routine works on machines with IEEE754 FP and Vaxen. 70 */ 71 72 #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \ 73 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \ 74 defined(__arm32__) || defined(__powerpc__) 75 #include <machine/endian.h> 76 #define IEEEFP 77 #endif 78 79 #if defined(__vax__) 80 81 /* What IEEE single precision floating point looks like on a Vax */ 82 struct ieee_single { 83 unsigned int mantissa: 23; 84 unsigned int exp : 8; 85 unsigned int sign : 1; 86 }; 87 88 /* Vax single precision floating point */ 89 struct vax_single { 90 unsigned int mantissa1 : 7; 91 unsigned int exp : 8; 92 unsigned int sign : 1; 93 unsigned int mantissa2 : 16; 94 }; 95 96 #define VAX_SNG_BIAS 0x81 97 #define IEEE_SNG_BIAS 0x7f 98 99 static struct sgl_limits { 100 struct vax_single s; 101 struct ieee_single ieee; 102 } sgl_limits[2] = { 103 {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */ 104 { 0x0, 0xff, 0x0 }}, /* Max IEEE */ 105 {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */ 106 { 0x0, 0x0, 0x0 }} /* Min IEEE */ 107 }; 108 #endif /* vax */ 109 110 bool_t 111 xdr_float(xdrs, fp) 112 XDR *xdrs; 113 float *fp; 114 { 115 #ifdef IEEEFP 116 bool_t rv; 117 long tmpl; 118 #else 119 struct ieee_single is; 120 struct vax_single vs, *vsp; 121 struct sgl_limits *lim; 122 int i; 123 #endif 124 switch (xdrs->x_op) { 125 126 case XDR_ENCODE: 127 #ifdef IEEEFP 128 tmpl = *(int32_t *)(void *)fp; 129 return (XDR_PUTLONG(xdrs, &tmpl)); 130 #else 131 vs = *((struct vax_single *)fp); 132 for (i = 0, lim = sgl_limits; 133 i < sizeof(sgl_limits)/sizeof(struct sgl_limits); 134 i++, lim++) { 135 if ((vs.mantissa2 == lim->s.mantissa2) && 136 (vs.exp == lim->s.exp) && 137 (vs.mantissa1 == lim->s.mantissa1)) { 138 is = lim->ieee; 139 goto shipit; 140 } 141 } 142 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS; 143 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2; 144 shipit: 145 is.sign = vs.sign; 146 return (XDR_PUTLONG(xdrs, (long *)&is)); 147 #endif 148 149 case XDR_DECODE: 150 #ifdef IEEEFP 151 rv = XDR_GETLONG(xdrs, &tmpl); 152 *(int32_t *)(void *)fp = (int32_t)tmpl; 153 return (rv); 154 #else 155 vsp = (struct vax_single *)fp; 156 if (!XDR_GETLONG(xdrs, (long *)&is)) 157 return (FALSE); 158 for (i = 0, lim = sgl_limits; 159 i < sizeof(sgl_limits)/sizeof(struct sgl_limits); 160 i++, lim++) { 161 if ((is.exp == lim->ieee.exp) && 162 (is.mantissa == lim->ieee.mantissa)) { 163 *vsp = lim->s; 164 goto doneit; 165 } 166 } 167 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS; 168 vsp->mantissa2 = is.mantissa; 169 vsp->mantissa1 = (is.mantissa >> 16); 170 doneit: 171 vsp->sign = is.sign; 172 return (TRUE); 173 #endif 174 175 case XDR_FREE: 176 return (TRUE); 177 } 178 /* NOTREACHED */ 179 return (FALSE); 180 } 181 182 #if defined(__vax__) 183 /* What IEEE double precision floating point looks like on a Vax */ 184 struct ieee_double { 185 unsigned int mantissa1 : 20; 186 unsigned int exp : 11; 187 unsigned int sign : 1; 188 unsigned int mantissa2 : 32; 189 }; 190 191 /* Vax double precision floating point */ 192 struct vax_double { 193 unsigned int mantissa1 : 7; 194 unsigned int exp : 8; 195 unsigned int sign : 1; 196 unsigned int mantissa2 : 16; 197 unsigned int mantissa3 : 16; 198 unsigned int mantissa4 : 16; 199 }; 200 201 #define VAX_DBL_BIAS 0x81 202 #define IEEE_DBL_BIAS 0x3ff 203 #define MASK(nbits) ((1 << nbits) - 1) 204 205 static struct dbl_limits { 206 struct vax_double d; 207 struct ieee_double ieee; 208 } dbl_limits[2] = { 209 {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */ 210 { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */ 211 {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */ 212 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */ 213 }; 214 215 #endif /* vax */ 216 217 218 bool_t 219 xdr_double(xdrs, dp) 220 XDR *xdrs; 221 double *dp; 222 { 223 #ifdef IEEEFP 224 int32_t *i32p; 225 bool_t rv; 226 long tmpl; 227 #else 228 long *lp; 229 struct ieee_double id; 230 struct vax_double vd; 231 struct dbl_limits *lim; 232 int i; 233 #endif 234 235 switch (xdrs->x_op) { 236 237 case XDR_ENCODE: 238 #ifdef IEEEFP 239 i32p = (int32_t *)(void *)dp; 240 #if BYTE_ORDER == BIG_ENDIAN 241 tmpl = *i32p++; 242 rv = XDR_PUTLONG(xdrs, &tmpl); 243 if (!rv) 244 return (rv); 245 tmpl = *i32p; 246 rv = XDR_PUTLONG(xdrs, &tmpl); 247 #else 248 tmpl = *(i32p+1); 249 rv = XDR_PUTLONG(xdrs, &tmpl); 250 if (!rv) 251 return (rv); 252 tmpl = *i32p; 253 rv = XDR_PUTLONG(xdrs, &tmpl); 254 #endif 255 return (rv); 256 #else 257 vd = *((struct vax_double *)dp); 258 for (i = 0, lim = dbl_limits; 259 i < sizeof(dbl_limits)/sizeof(struct dbl_limits); 260 i++, lim++) { 261 if ((vd.mantissa4 == lim->d.mantissa4) && 262 (vd.mantissa3 == lim->d.mantissa3) && 263 (vd.mantissa2 == lim->d.mantissa2) && 264 (vd.mantissa1 == lim->d.mantissa1) && 265 (vd.exp == lim->d.exp)) { 266 id = lim->ieee; 267 goto shipit; 268 } 269 } 270 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS; 271 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3); 272 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) | 273 (vd.mantissa3 << 13) | 274 ((vd.mantissa4 >> 3) & MASK(13)); 275 shipit: 276 id.sign = vd.sign; 277 lp = (long *)&id; 278 return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp)); 279 #endif 280 281 case XDR_DECODE: 282 #ifdef IEEEFP 283 i32p = (int32_t *)(void *)dp; 284 #if BYTE_ORDER == BIG_ENDIAN 285 rv = XDR_GETLONG(xdrs, &tmpl); 286 *i32p++ = tmpl; 287 if (!rv) 288 return (rv); 289 rv = XDR_GETLONG(xdrs, &tmpl); 290 *i32p = tmpl; 291 #else 292 rv = XDR_GETLONG(xdrs, &tmpl); 293 *(i32p+1) = (int32_t)tmpl; 294 if (!rv) 295 return (rv); 296 rv = XDR_GETLONG(xdrs, &tmpl); 297 *i32p = (int32_t)tmpl; 298 #endif 299 return (rv); 300 #else 301 lp = (long *)&id; 302 if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp)) 303 return (FALSE); 304 for (i = 0, lim = dbl_limits; 305 i < sizeof(dbl_limits)/sizeof(struct dbl_limits); 306 i++, lim++) { 307 if ((id.mantissa2 == lim->ieee.mantissa2) && 308 (id.mantissa1 == lim->ieee.mantissa1) && 309 (id.exp == lim->ieee.exp)) { 310 vd = lim->d; 311 goto doneit; 312 } 313 } 314 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS; 315 vd.mantissa1 = (id.mantissa1 >> 13); 316 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) | 317 (id.mantissa2 >> 29); 318 vd.mantissa3 = (id.mantissa2 >> 13); 319 vd.mantissa4 = (id.mantissa2 << 3); 320 doneit: 321 vd.sign = id.sign; 322 *dp = *((double *)&vd); 323 return (TRUE); 324 #endif 325 326 case XDR_FREE: 327 return (TRUE); 328 } 329 /* NOTREACHED */ 330 return (FALSE); 331 } 332