Home | History | Annotate | Line # | Download | only in rpc
xdr_float.c revision 1.9
      1 /*	$NetBSD: xdr_float.c,v 1.9 1995/06/05 11:48:26 pk 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.9 1995/06/05 11:48:26 pk 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 #ifdef vax
    168 /* What IEEE double precision floating point looks like on a Vax */
    169 struct	ieee_double {
    170 	unsigned int	mantissa1 : 20;
    171 	unsigned int	exp       : 11;
    172 	unsigned int	sign      : 1;
    173 	unsigned int	mantissa2 : 32;
    174 };
    175 
    176 /* Vax double precision floating point */
    177 struct  vax_double {
    178 	unsigned int	mantissa1 : 7;
    179 	unsigned int	exp       : 8;
    180 	unsigned int	sign      : 1;
    181 	unsigned int	mantissa2 : 16;
    182 	unsigned int	mantissa3 : 16;
    183 	unsigned int	mantissa4 : 16;
    184 };
    185 
    186 #define VAX_DBL_BIAS	0x81
    187 #define IEEE_DBL_BIAS	0x3ff
    188 #define MASK(nbits)	((1 << nbits) - 1)
    189 
    190 static struct dbl_limits {
    191 	struct	vax_double d;
    192 	struct	ieee_double ieee;
    193 } dbl_limits[2] = {
    194 	{{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },	/* Max Vax */
    195 	{ 0x0, 0x7ff, 0x0, 0x0 }},			/* Max IEEE */
    196 	{{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},		/* Min Vax */
    197 	{ 0x0, 0x0, 0x0, 0x0 }}				/* Min IEEE */
    198 };
    199 
    200 #endif /* vax */
    201 
    202 
    203 bool_t
    204 xdr_double(xdrs, dp)
    205 	register XDR *xdrs;
    206 	double *dp;
    207 {
    208 #ifdef IEEEFP
    209 	register int32_t *i32p;
    210 	bool_t rv;
    211 	long tmpl;
    212 #else
    213 	register long *lp;
    214 	struct	ieee_double id;
    215 	struct	vax_double vd;
    216 	register struct dbl_limits *lim;
    217 	int i;
    218 #endif
    219 
    220 	switch (xdrs->x_op) {
    221 
    222 	case XDR_ENCODE:
    223 #ifdef IEEEFP
    224 		i32p = (int32_t *)dp;
    225 #if BYTE_ORDER == BIG_ENDIAN
    226 		tmpl = *i32p++;
    227 		rv = XDR_PUTLONG(xdrs, &tmpl);
    228 		if (!rv)
    229 			return (rv);
    230 		tmpl = *i32p;
    231 		rv = XDR_PUTLONG(xdrs, &tmpl);
    232 #else
    233 		tmpl = *(i32p+1);
    234 		rv = XDR_PUTLONG(xdrs, &tmpl);
    235 		if (!rv)
    236 			return (rv);
    237 		tmpl = *i32p;
    238 		rv = XDR_PUTLONG(xdrs, &tmpl);
    239 #endif
    240 		return (rv);
    241 #else
    242 		vd = *((struct vax_double *)dp);
    243 		for (i = 0, lim = dbl_limits;
    244 			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
    245 			i++, lim++) {
    246 			if ((vd.mantissa4 == lim->d.mantissa4) &&
    247 				(vd.mantissa3 == lim->d.mantissa3) &&
    248 				(vd.mantissa2 == lim->d.mantissa2) &&
    249 				(vd.mantissa1 == lim->d.mantissa1) &&
    250 				(vd.exp == lim->d.exp)) {
    251 				id = lim->ieee;
    252 				goto shipit;
    253 			}
    254 		}
    255 		id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
    256 		id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
    257 		id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
    258 				(vd.mantissa3 << 13) |
    259 				((vd.mantissa4 >> 3) & MASK(13));
    260 	shipit:
    261 		id.sign = vd.sign;
    262 		lp = (long *)&id;
    263 		return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
    264 #endif
    265 
    266 	case XDR_DECODE:
    267 #ifdef IEEEFP
    268 		i32p = (int32_t *)dp;
    269 #if BYTE_ORDER == BIG_ENDIAN
    270 		rv = XDR_GETLONG(xdrs, &tmpl);
    271 		*i32p++ = tmpl;
    272 		if (!rv)
    273 			return (rv);
    274 		rv = XDR_GETLONG(xdrs, &tmpl);
    275 		*i32p = tmpl;
    276 #else
    277 		rv = XDR_GETLONG(xdrs, &tmpl);
    278 		*(i32p+1) = tmpl;
    279 		if (!rv)
    280 			return (rv);
    281 		rv = XDR_GETLONG(xdrs, &tmpl);
    282 		*i32p = tmpl;
    283 #endif
    284 		return (rv);
    285 #else
    286 		lp = (long *)&id;
    287 		if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(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 	return (FALSE);
    315 }
    316