Home | History | Annotate | Line # | Download | only in rpc
xdr_float.c revision 1.27
      1 /*	$NetBSD: xdr_float.c,v 1.27 2001/02/21 18:09:25 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.27 2001/02/21 18:09:25 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) || \
    234     (defined(__arm__) && !defined(__VFP_FP__))
    235 		rv = XDR_PUTINT32(xdrs, i32p);
    236 		if (!rv)
    237 			return (rv);
    238 		rv = XDR_PUTINT32(xdrs, i32p+1);
    239 #else
    240 		rv = XDR_PUTINT32(xdrs, i32p+1);
    241 		if (!rv)
    242 			return (rv);
    243 		rv = XDR_PUTINT32(xdrs, i32p);
    244 #endif
    245 		return (rv);
    246 #else
    247 		vd = *((struct vax_double *)dp);
    248 		for (i = 0, lim = dbl_limits;
    249 			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
    250 			i++, lim++) {
    251 			if ((vd.mantissa4 == lim->d.mantissa4) &&
    252 				(vd.mantissa3 == lim->d.mantissa3) &&
    253 				(vd.mantissa2 == lim->d.mantissa2) &&
    254 				(vd.mantissa1 == lim->d.mantissa1) &&
    255 				(vd.exp == lim->d.exp)) {
    256 				id = lim->ieee;
    257 				goto shipit;
    258 			}
    259 		}
    260 		id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
    261 		id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
    262 		id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
    263 				(vd.mantissa3 << 13) |
    264 				((vd.mantissa4 >> 3) & MASK(13));
    265 	shipit:
    266 		id.sign = vd.sign;
    267 		lp = (int32_t *)&id;
    268 		return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
    269 #endif
    270 
    271 	case XDR_DECODE:
    272 #ifdef IEEEFP
    273 		i32p = (int32_t *)(void *)dp;
    274 #if BYTE_ORDER == BIG_ENDIAN || \
    275     (defined(__arm__) && !defined(__VFP_FP__))
    276 		rv = XDR_GETINT32(xdrs, i32p);
    277 		if (!rv)
    278 			return (rv);
    279 		rv = XDR_GETINT32(xdrs, i32p+1);
    280 #else
    281 		rv = XDR_GETINT32(xdrs, i32p+1);
    282 		if (!rv)
    283 			return (rv);
    284 		rv = XDR_GETINT32(xdrs, i32p);
    285 #endif
    286 		return (rv);
    287 #else
    288 		lp = (int32_t *)&id;
    289 		if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
    290 			return (FALSE);
    291 		for (i = 0, lim = dbl_limits;
    292 			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
    293 			i++, lim++) {
    294 			if ((id.mantissa2 == lim->ieee.mantissa2) &&
    295 				(id.mantissa1 == lim->ieee.mantissa1) &&
    296 				(id.exp == lim->ieee.exp)) {
    297 				vd = lim->d;
    298 				goto doneit;
    299 			}
    300 		}
    301 		vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
    302 		vd.mantissa1 = (id.mantissa1 >> 13);
    303 		vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
    304 				(id.mantissa2 >> 29);
    305 		vd.mantissa3 = (id.mantissa2 >> 13);
    306 		vd.mantissa4 = (id.mantissa2 << 3);
    307 	doneit:
    308 		vd.sign = id.sign;
    309 		*dp = *((double *)&vd);
    310 		return (TRUE);
    311 #endif
    312 
    313 	case XDR_FREE:
    314 		return (TRUE);
    315 	}
    316 	/* NOTREACHED */
    317 	return (FALSE);
    318 }
    319