1 /* $NetBSD: xdr_float.c,v 1.26 2001/02/18 22:13:41 bjh21 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.26 2001/02/18 22:13:41 bjh21 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(__arm__) || defined(__powerpc__) || defined(__sh3__) 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 #ifndef IEEEFP 116 struct ieee_single is; 117 struct vax_single vs, *vsp; 118 struct sgl_limits *lim; 119 int i; 120 #endif 121 switch (xdrs->x_op) { 122 123 case XDR_ENCODE: 124 #ifdef IEEEFP 125 return (XDR_PUTINT32(xdrs, (int32_t *)(void *)fp)); 126 #else 127 vs = *((struct vax_single *)fp); 128 for (i = 0, lim = sgl_limits; 129 i < sizeof(sgl_limits)/sizeof(struct sgl_limits); 130 i++, lim++) { 131 if ((vs.mantissa2 == lim->s.mantissa2) && 132 (vs.exp == lim->s.exp) && 133 (vs.mantissa1 == lim->s.mantissa1)) { 134 is = lim->ieee; 135 goto shipit; 136 } 137 } 138 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS; 139 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2; 140 shipit: 141 is.sign = vs.sign; 142 return (XDR_PUTINT32(xdrs, (int32_t *)&is)); 143 #endif 144 145 case XDR_DECODE: 146 #ifdef IEEEFP 147 return (XDR_GETINT32(xdrs, (int32_t *)(void *)fp)); 148 #else 149 vsp = (struct vax_single *)fp; 150 if (!XDR_GETINT32(xdrs, (int32_t *)&is)) 151 return (FALSE); 152 for (i = 0, lim = sgl_limits; 153 i < sizeof(sgl_limits)/sizeof(struct sgl_limits); 154 i++, lim++) { 155 if ((is.exp == lim->ieee.exp) && 156 (is.mantissa == lim->ieee.mantissa)) { 157 *vsp = lim->s; 158 goto doneit; 159 } 160 } 161 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS; 162 vsp->mantissa2 = is.mantissa; 163 vsp->mantissa1 = (is.mantissa >> 16); 164 doneit: 165 vsp->sign = is.sign; 166 return (TRUE); 167 #endif 168 169 case XDR_FREE: 170 return (TRUE); 171 } 172 /* NOTREACHED */ 173 return (FALSE); 174 } 175 176 #if defined(__vax__) 177 /* What IEEE double precision floating point looks like on a Vax */ 178 struct ieee_double { 179 unsigned int mantissa1 : 20; 180 unsigned int exp : 11; 181 unsigned int sign : 1; 182 unsigned int mantissa2 : 32; 183 }; 184 185 /* Vax double precision floating point */ 186 struct vax_double { 187 unsigned int mantissa1 : 7; 188 unsigned int exp : 8; 189 unsigned int sign : 1; 190 unsigned int mantissa2 : 16; 191 unsigned int mantissa3 : 16; 192 unsigned int mantissa4 : 16; 193 }; 194 195 #define VAX_DBL_BIAS 0x81 196 #define IEEE_DBL_BIAS 0x3ff 197 #define MASK(nbits) ((1 << nbits) - 1) 198 199 static struct dbl_limits { 200 struct vax_double d; 201 struct ieee_double ieee; 202 } dbl_limits[2] = { 203 {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */ 204 { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */ 205 {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */ 206 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */ 207 }; 208 209 #endif /* vax */ 210 211 212 bool_t 213 xdr_double(xdrs, dp) 214 XDR *xdrs; 215 double *dp; 216 { 217 #ifdef IEEEFP 218 int32_t *i32p; 219 bool_t rv; 220 #else 221 int32_t *lp; 222 struct ieee_double id; 223 struct vax_double vd; 224 struct dbl_limits *lim; 225 int i; 226 #endif 227 228 switch (xdrs->x_op) { 229 230 case XDR_ENCODE: 231 #ifdef IEEEFP 232 i32p = (int32_t *)(void *)dp; 233 #if (BYTE_ORDER == BIG_ENDIAN) || defined(__arm__) 234 rv = XDR_PUTINT32(xdrs, i32p); 235 if (!rv) 236 return (rv); 237 rv = XDR_PUTINT32(xdrs, i32p+1); 238 #else 239 rv = XDR_PUTINT32(xdrs, i32p+1); 240 if (!rv) 241 return (rv); 242 rv = XDR_PUTINT32(xdrs, i32p); 243 #endif 244 return (rv); 245 #else 246 vd = *((struct vax_double *)dp); 247 for (i = 0, lim = dbl_limits; 248 i < sizeof(dbl_limits)/sizeof(struct dbl_limits); 249 i++, lim++) { 250 if ((vd.mantissa4 == lim->d.mantissa4) && 251 (vd.mantissa3 == lim->d.mantissa3) && 252 (vd.mantissa2 == lim->d.mantissa2) && 253 (vd.mantissa1 == lim->d.mantissa1) && 254 (vd.exp == lim->d.exp)) { 255 id = lim->ieee; 256 goto shipit; 257 } 258 } 259 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS; 260 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3); 261 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) | 262 (vd.mantissa3 << 13) | 263 ((vd.mantissa4 >> 3) & MASK(13)); 264 shipit: 265 id.sign = vd.sign; 266 lp = (int32_t *)&id; 267 return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp)); 268 #endif 269 270 case XDR_DECODE: 271 #ifdef IEEEFP 272 i32p = (int32_t *)(void *)dp; 273 #if BYTE_ORDER == BIG_ENDIAN || defined(__arm__) 274 rv = XDR_GETINT32(xdrs, i32p); 275 if (!rv) 276 return (rv); 277 rv = XDR_GETINT32(xdrs, i32p+1); 278 #else 279 rv = XDR_GETINT32(xdrs, i32p+1); 280 if (!rv) 281 return (rv); 282 rv = XDR_GETINT32(xdrs, i32p); 283 #endif 284 return (rv); 285 #else 286 lp = (int32_t *)&id; 287 if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp)) 288 return (FALSE); 289 for (i = 0, lim = dbl_limits; 290 i < sizeof(dbl_limits)/sizeof(struct dbl_limits); 291 i++, lim++) { 292 if ((id.mantissa2 == lim->ieee.mantissa2) && 293 (id.mantissa1 == lim->ieee.mantissa1) && 294 (id.exp == lim->ieee.exp)) { 295 vd = lim->d; 296 goto doneit; 297 } 298 } 299 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS; 300 vd.mantissa1 = (id.mantissa1 >> 13); 301 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) | 302 (id.mantissa2 >> 29); 303 vd.mantissa3 = (id.mantissa2 >> 13); 304 vd.mantissa4 = (id.mantissa2 << 3); 305 doneit: 306 vd.sign = id.sign; 307 *dp = *((double *)&vd); 308 return (TRUE); 309 #endif 310 311 case XDR_FREE: 312 return (TRUE); 313 } 314 /* NOTREACHED */ 315 return (FALSE); 316 } 317