Home | History | Annotate | Line # | Download | only in rpc
xdr_float.c revision 1.7
      1 /*	$NetBSD: xdr_float.c,v 1.7 1995/03/21 20:54:05 jtc 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.7 1995/03/21 20:54:05 jtc 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 #include <machine/endian.h>
     62 #define IEEEFP
     63 #endif
     64 
     65 #ifdef vax
     66 
     67 /* What IEEE single precision floating point looks like on a Vax */
     68 struct	ieee_single {
     69 	unsigned int	mantissa: 23;
     70 	unsigned int	exp     : 8;
     71 	unsigned int	sign    : 1;
     72 };
     73 
     74 /* Vax single precision floating point */
     75 struct	vax_single {
     76 	unsigned int	mantissa1 : 7;
     77 	unsigned int	exp       : 8;
     78 	unsigned int	sign      : 1;
     79 	unsigned int	mantissa2 : 16;
     80 };
     81 
     82 #define VAX_SNG_BIAS	0x81
     83 #define IEEE_SNG_BIAS	0x7f
     84 
     85 static struct sgl_limits {
     86 	struct vax_single s;
     87 	struct ieee_single ieee;
     88 } sgl_limits[2] = {
     89 	{{ 0x7f, 0xff, 0x0, 0xffff },	/* Max Vax */
     90 	{ 0x0, 0xff, 0x0 }},		/* Max IEEE */
     91 	{{ 0x0, 0x0, 0x0, 0x0 },	/* Min Vax */
     92 	{ 0x0, 0x0, 0x0 }}		/* Min IEEE */
     93 };
     94 #endif /* vax */
     95 
     96 bool_t
     97 xdr_float(xdrs, fp)
     98 	register XDR *xdrs;
     99 	register float *fp;
    100 {
    101 #ifdef IEEEFP
    102 	bool_t rv;
    103 	long tmpl;
    104 #else
    105 	struct ieee_single is;
    106 	struct vax_single vs, *vsp;
    107 	struct sgl_limits *lim;
    108 	int i;
    109 #endif
    110 	switch (xdrs->x_op) {
    111 
    112 	case XDR_ENCODE:
    113 #ifdef IEEEFP
    114 		tmpl = *(int32_t *)fp;
    115 		return (XDR_PUTLONG(xdrs, &tmpl));
    116 #else
    117 		vs = *((struct vax_single *)fp);
    118 		for (i = 0, lim = sgl_limits;
    119 			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
    120 			i++, lim++) {
    121 			if ((vs.mantissa2 == lim->s.mantissa2) &&
    122 				(vs.exp == lim->s.exp) &&
    123 				(vs.mantissa1 == lim->s.mantissa1)) {
    124 				is = lim->ieee;
    125 				goto shipit;
    126 			}
    127 		}
    128 		is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
    129 		is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
    130 	shipit:
    131 		is.sign = vs.sign;
    132 		return (XDR_PUTLONG(xdrs, (long *)&is));
    133 #endif
    134 
    135 	case XDR_DECODE:
    136 #ifdef IEEEFP
    137 		rv = XDR_GETLONG(xdrs, &tmpl);
    138 		*(int32_t *)fp = tmpl;
    139 		return (rv);
    140 #else
    141 		vsp = (struct vax_single *)fp;
    142 		if (!XDR_GETLONG(xdrs, (long *)&is))
    143 			return (FALSE);
    144 		for (i = 0, lim = sgl_limits;
    145 			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
    146 			i++, lim++) {
    147 			if ((is.exp == lim->ieee.exp) &&
    148 				(is.mantissa == lim->ieee.mantissa)) {
    149 				*vsp = lim->s;
    150 				goto doneit;
    151 			}
    152 		}
    153 		vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
    154 		vsp->mantissa2 = is.mantissa;
    155 		vsp->mantissa1 = (is.mantissa >> 16);
    156 	doneit:
    157 		vsp->sign = is.sign;
    158 		return (TRUE);
    159 #endif
    160 
    161 	case XDR_FREE:
    162 		return (TRUE);
    163 	}
    164 	return (FALSE);
    165 }
    166 
    167 /*
    168  * This routine works on Suns (Sky / 68000's), i386's, MIPS and Vaxen.
    169  */
    170 
    171 #ifdef vax
    172 /* What IEEE double precision floating point looks like on a Vax */
    173 struct	ieee_double {
    174 	unsigned int	mantissa1 : 20;
    175 	unsigned int	exp       : 11;
    176 	unsigned int	sign      : 1;
    177 	unsigned int	mantissa2 : 32;
    178 };
    179 
    180 /* Vax double precision floating point */
    181 struct  vax_double {
    182 	unsigned int	mantissa1 : 7;
    183 	unsigned int	exp       : 8;
    184 	unsigned int	sign      : 1;
    185 	unsigned int	mantissa2 : 16;
    186 	unsigned int	mantissa3 : 16;
    187 	unsigned int	mantissa4 : 16;
    188 };
    189 
    190 #define VAX_DBL_BIAS	0x81
    191 #define IEEE_DBL_BIAS	0x3ff
    192 #define MASK(nbits)	((1 << nbits) - 1)
    193 
    194 static struct dbl_limits {
    195 	struct	vax_double d;
    196 	struct	ieee_double ieee;
    197 } dbl_limits[2] = {
    198 	{{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },	/* Max Vax */
    199 	{ 0x0, 0x7ff, 0x0, 0x0 }},			/* Max IEEE */
    200 	{{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},		/* Min Vax */
    201 	{ 0x0, 0x0, 0x0, 0x0 }}				/* Min IEEE */
    202 };
    203 
    204 #endif /* vax */
    205 
    206 
    207 bool_t
    208 xdr_double(xdrs, dp)
    209 	register XDR *xdrs;
    210 	double *dp;
    211 {
    212 #ifdef IEEEFP
    213 	register int32_t *i32p;
    214 	bool_t rv;
    215 	long tmpl;
    216 #else
    217 	register long *lp;
    218 	struct	ieee_double id;
    219 	struct	vax_double vd;
    220 	register struct dbl_limits *lim;
    221 	int i;
    222 #endif
    223 
    224 	switch (xdrs->x_op) {
    225 
    226 	case XDR_ENCODE:
    227 #ifdef IEEEFP
    228 		i32p = (int32_t *)dp;
    229 #if BYTE_ORDER == BIG_ENDIAN
    230 		tmpl = *i32p++;
    231 		rv = XDR_PUTLONG(xdrs, &tmpl);
    232 		if (!rv)
    233 			return (rv);
    234 		tmpl = *i32p;
    235 		rv = XDR_PUTLONG(xdrs, &tmpl);
    236 #else
    237 		tmpl = *i32p+1;
    238 		rv = XDR_PUTLONG(xdrs, &tmpl);
    239 		if (!rv)
    240 			return (rv);
    241 		tmpl = *i32p;
    242 		rv = XDR_PUTLONG(xdrs, &tmpl);
    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 = (long *)&id;
    267 		return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
    268 #endif
    269 
    270 	case XDR_DECODE:
    271 #ifdef IEEEFP
    272 		i32p = (int32_t *)dp;
    273 #if BYTE_ORDER == BIG_ENDIAN
    274 		rv = XDR_GETLONG(xdrs, &tmpl);
    275 		*i32p++ = tmpl;
    276 		if (!rv)
    277 			return (rv);
    278 		rv = XDR_GETLONG(xdrs, &tmpl);
    279 		*i32p = tmpl;
    280 #else
    281 		rv = XDR_GETLONG(xdrs, &tmpl);
    282 		*(i32p+1) = tmpl;
    283 		if (!rv)
    284 			return (rv);
    285 		rv = XDR_GETLONG(xdrs, &tmpl);
    286 		*i32p = tmpl;
    287 #endif
    288 		return (rv);
    289 #else
    290 		lp = (long *)&id;
    291 		if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
    292 			return (FALSE);
    293 		for (i = 0, lim = dbl_limits;
    294 			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
    295 			i++, lim++) {
    296 			if ((id.mantissa2 == lim->ieee.mantissa2) &&
    297 				(id.mantissa1 == lim->ieee.mantissa1) &&
    298 				(id.exp == lim->ieee.exp)) {
    299 				vd = lim->d;
    300 				goto doneit;
    301 			}
    302 		}
    303 		vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
    304 		vd.mantissa1 = (id.mantissa1 >> 13);
    305 		vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
    306 				(id.mantissa2 >> 29);
    307 		vd.mantissa3 = (id.mantissa2 >> 13);
    308 		vd.mantissa4 = (id.mantissa2 << 3);
    309 	doneit:
    310 		vd.sign = id.sign;
    311 		*dp = *((double *)&vd);
    312 		return (TRUE);
    313 #endif
    314 
    315 	case XDR_FREE:
    316 		return (TRUE);
    317 	}
    318 	return (FALSE);
    319 }
    320