Home | History | Annotate | Line # | Download | only in rpc
xdr_float.c revision 1.18.6.1
      1  1.18.6.1  wrstuden /*	$NetBSD: xdr_float.c,v 1.18.6.1 1999/12/27 18:29:44 wrstuden Exp $	*/
      2       1.6       cgd 
      3       1.1       cgd /*
      4       1.1       cgd  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5       1.1       cgd  * unrestricted use provided that this legend is included on all tape
      6       1.1       cgd  * media and as a part of the software program in whole or part.  Users
      7       1.1       cgd  * may copy or modify Sun RPC without charge, but are not authorized
      8       1.1       cgd  * to license or distribute it to anyone else except as part of a product or
      9       1.1       cgd  * program developed by the user.
     10       1.1       cgd  *
     11       1.1       cgd  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12       1.1       cgd  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13       1.1       cgd  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14       1.1       cgd  *
     15       1.1       cgd  * Sun RPC is provided with no support and without any obligation on the
     16       1.1       cgd  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17       1.1       cgd  * modification or enhancement.
     18       1.1       cgd  *
     19       1.1       cgd  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20       1.1       cgd  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21       1.1       cgd  * OR ANY PART THEREOF.
     22       1.1       cgd  *
     23       1.1       cgd  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24       1.1       cgd  * or profits or other special, indirect and consequential damages, even if
     25       1.1       cgd  * Sun has been advised of the possibility of such damages.
     26       1.1       cgd  *
     27       1.1       cgd  * Sun Microsystems, Inc.
     28       1.1       cgd  * 2550 Garcia Avenue
     29       1.1       cgd  * Mountain View, California  94043
     30       1.1       cgd  */
     31       1.1       cgd 
     32      1.12  christos #include <sys/cdefs.h>
     33       1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     34      1.12  christos #if 0
     35      1.12  christos static char *sccsid = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
     36      1.12  christos static char *sccsid = "@(#)xdr_float.c	2.1 88/07/29 4.0 RPCSRC";
     37      1.12  christos #else
     38  1.18.6.1  wrstuden __RCSID("$NetBSD: xdr_float.c,v 1.18.6.1 1999/12/27 18:29:44 wrstuden Exp $");
     39      1.12  christos #endif
     40       1.1       cgd #endif
     41       1.1       cgd 
     42       1.1       cgd /*
     43       1.1       cgd  * xdr_float.c, Generic XDR routines impelmentation.
     44       1.1       cgd  *
     45       1.1       cgd  * Copyright (C) 1984, Sun Microsystems, Inc.
     46       1.1       cgd  *
     47       1.1       cgd  * These are the "floating point" xdr routines used to (de)serialize
     48       1.1       cgd  * most common data items.  See xdr.h for more info on the interface to
     49       1.1       cgd  * xdr.
     50       1.1       cgd  */
     51       1.1       cgd 
     52      1.13       jtc #include "namespace.h"
     53      1.16     lukem 
     54       1.1       cgd #include <sys/types.h>
     55       1.1       cgd #include <sys/param.h>
     56      1.16     lukem 
     57      1.16     lukem #include <stdio.h>
     58      1.16     lukem 
     59       1.1       cgd #include <rpc/types.h>
     60       1.1       cgd #include <rpc/xdr.h>
     61      1.13       jtc 
     62      1.13       jtc #ifdef __weak_alias
     63      1.13       jtc __weak_alias(xdr_double,_xdr_double);
     64      1.13       jtc __weak_alias(xdr_float,_xdr_float);
     65      1.13       jtc #endif
     66       1.1       cgd 
     67       1.1       cgd /*
     68       1.1       cgd  * NB: Not portable.
     69       1.7       jtc  * This routine works on machines with IEEE754 FP and Vaxen.
     70       1.1       cgd  */
     71       1.1       cgd 
     72       1.5       cgd #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
     73      1.10      mark     defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
     74  1.18.6.1  wrstuden     defined(__arm32__) || defined(__powerpc__) || defined(__sh3__)
     75       1.7       jtc #include <machine/endian.h>
     76       1.1       cgd #define IEEEFP
     77       1.1       cgd #endif
     78       1.1       cgd 
     79      1.17      matt #if defined(__vax__)
     80       1.1       cgd 
     81       1.1       cgd /* What IEEE single precision floating point looks like on a Vax */
     82       1.1       cgd struct	ieee_single {
     83       1.1       cgd 	unsigned int	mantissa: 23;
     84       1.1       cgd 	unsigned int	exp     : 8;
     85       1.1       cgd 	unsigned int	sign    : 1;
     86       1.1       cgd };
     87       1.1       cgd 
     88       1.1       cgd /* Vax single precision floating point */
     89       1.1       cgd struct	vax_single {
     90       1.1       cgd 	unsigned int	mantissa1 : 7;
     91       1.1       cgd 	unsigned int	exp       : 8;
     92       1.1       cgd 	unsigned int	sign      : 1;
     93       1.1       cgd 	unsigned int	mantissa2 : 16;
     94       1.1       cgd };
     95       1.1       cgd 
     96       1.1       cgd #define VAX_SNG_BIAS	0x81
     97       1.1       cgd #define IEEE_SNG_BIAS	0x7f
     98       1.1       cgd 
     99       1.1       cgd static struct sgl_limits {
    100       1.1       cgd 	struct vax_single s;
    101       1.1       cgd 	struct ieee_single ieee;
    102       1.1       cgd } sgl_limits[2] = {
    103       1.1       cgd 	{{ 0x7f, 0xff, 0x0, 0xffff },	/* Max Vax */
    104       1.1       cgd 	{ 0x0, 0xff, 0x0 }},		/* Max IEEE */
    105       1.1       cgd 	{{ 0x0, 0x0, 0x0, 0x0 },	/* Min Vax */
    106       1.1       cgd 	{ 0x0, 0x0, 0x0 }}		/* Min IEEE */
    107       1.1       cgd };
    108       1.1       cgd #endif /* vax */
    109       1.1       cgd 
    110       1.1       cgd bool_t
    111       1.1       cgd xdr_float(xdrs, fp)
    112      1.16     lukem 	XDR *xdrs;
    113      1.16     lukem 	float *fp;
    114       1.1       cgd {
    115       1.4       cgd #ifdef IEEEFP
    116       1.4       cgd 	bool_t rv;
    117      1.15     lukem 	long tmpl;
    118       1.4       cgd #else
    119       1.1       cgd 	struct ieee_single is;
    120       1.1       cgd 	struct vax_single vs, *vsp;
    121       1.1       cgd 	struct sgl_limits *lim;
    122       1.1       cgd 	int i;
    123       1.1       cgd #endif
    124       1.1       cgd 	switch (xdrs->x_op) {
    125       1.1       cgd 
    126       1.1       cgd 	case XDR_ENCODE:
    127       1.1       cgd #ifdef IEEEFP
    128      1.18  christos 		tmpl = *(int32_t *)(void *)fp;
    129       1.4       cgd 		return (XDR_PUTLONG(xdrs, &tmpl));
    130       1.1       cgd #else
    131       1.1       cgd 		vs = *((struct vax_single *)fp);
    132       1.1       cgd 		for (i = 0, lim = sgl_limits;
    133       1.1       cgd 			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
    134       1.1       cgd 			i++, lim++) {
    135       1.1       cgd 			if ((vs.mantissa2 == lim->s.mantissa2) &&
    136       1.1       cgd 				(vs.exp == lim->s.exp) &&
    137       1.1       cgd 				(vs.mantissa1 == lim->s.mantissa1)) {
    138       1.1       cgd 				is = lim->ieee;
    139       1.1       cgd 				goto shipit;
    140       1.1       cgd 			}
    141       1.1       cgd 		}
    142       1.1       cgd 		is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
    143       1.1       cgd 		is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
    144       1.1       cgd 	shipit:
    145       1.1       cgd 		is.sign = vs.sign;
    146      1.15     lukem 		return (XDR_PUTLONG(xdrs, (long *)&is));
    147       1.1       cgd #endif
    148       1.1       cgd 
    149       1.1       cgd 	case XDR_DECODE:
    150       1.1       cgd #ifdef IEEEFP
    151       1.4       cgd 		rv = XDR_GETLONG(xdrs, &tmpl);
    152      1.18  christos 		*(int32_t *)(void *)fp = (int32_t)tmpl;
    153       1.4       cgd 		return (rv);
    154       1.1       cgd #else
    155       1.1       cgd 		vsp = (struct vax_single *)fp;
    156      1.15     lukem 		if (!XDR_GETLONG(xdrs, (long *)&is))
    157       1.1       cgd 			return (FALSE);
    158       1.1       cgd 		for (i = 0, lim = sgl_limits;
    159       1.1       cgd 			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
    160       1.1       cgd 			i++, lim++) {
    161       1.1       cgd 			if ((is.exp == lim->ieee.exp) &&
    162       1.1       cgd 				(is.mantissa == lim->ieee.mantissa)) {
    163       1.1       cgd 				*vsp = lim->s;
    164       1.1       cgd 				goto doneit;
    165       1.1       cgd 			}
    166       1.1       cgd 		}
    167       1.1       cgd 		vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
    168       1.1       cgd 		vsp->mantissa2 = is.mantissa;
    169       1.1       cgd 		vsp->mantissa1 = (is.mantissa >> 16);
    170       1.1       cgd 	doneit:
    171       1.1       cgd 		vsp->sign = is.sign;
    172       1.1       cgd 		return (TRUE);
    173       1.1       cgd #endif
    174       1.1       cgd 
    175       1.1       cgd 	case XDR_FREE:
    176       1.1       cgd 		return (TRUE);
    177       1.1       cgd 	}
    178      1.18  christos 	/* NOTREACHED */
    179       1.1       cgd 	return (FALSE);
    180       1.1       cgd }
    181       1.1       cgd 
    182      1.17      matt #if defined(__vax__)
    183       1.1       cgd /* What IEEE double precision floating point looks like on a Vax */
    184       1.1       cgd struct	ieee_double {
    185       1.1       cgd 	unsigned int	mantissa1 : 20;
    186       1.1       cgd 	unsigned int	exp       : 11;
    187       1.1       cgd 	unsigned int	sign      : 1;
    188       1.1       cgd 	unsigned int	mantissa2 : 32;
    189       1.1       cgd };
    190       1.1       cgd 
    191       1.1       cgd /* Vax double precision floating point */
    192       1.1       cgd struct  vax_double {
    193       1.1       cgd 	unsigned int	mantissa1 : 7;
    194       1.1       cgd 	unsigned int	exp       : 8;
    195       1.1       cgd 	unsigned int	sign      : 1;
    196       1.1       cgd 	unsigned int	mantissa2 : 16;
    197       1.1       cgd 	unsigned int	mantissa3 : 16;
    198       1.1       cgd 	unsigned int	mantissa4 : 16;
    199       1.1       cgd };
    200       1.1       cgd 
    201       1.1       cgd #define VAX_DBL_BIAS	0x81
    202       1.1       cgd #define IEEE_DBL_BIAS	0x3ff
    203       1.1       cgd #define MASK(nbits)	((1 << nbits) - 1)
    204       1.1       cgd 
    205       1.1       cgd static struct dbl_limits {
    206       1.1       cgd 	struct	vax_double d;
    207       1.1       cgd 	struct	ieee_double ieee;
    208       1.1       cgd } dbl_limits[2] = {
    209       1.1       cgd 	{{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },	/* Max Vax */
    210       1.1       cgd 	{ 0x0, 0x7ff, 0x0, 0x0 }},			/* Max IEEE */
    211       1.1       cgd 	{{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},		/* Min Vax */
    212       1.1       cgd 	{ 0x0, 0x0, 0x0, 0x0 }}				/* Min IEEE */
    213       1.1       cgd };
    214       1.1       cgd 
    215       1.1       cgd #endif /* vax */
    216       1.1       cgd 
    217       1.1       cgd 
    218       1.1       cgd bool_t
    219       1.1       cgd xdr_double(xdrs, dp)
    220      1.16     lukem 	XDR *xdrs;
    221       1.1       cgd 	double *dp;
    222       1.1       cgd {
    223       1.4       cgd #ifdef IEEEFP
    224      1.16     lukem 	int32_t *i32p;
    225       1.4       cgd 	bool_t rv;
    226      1.15     lukem 	long tmpl;
    227       1.4       cgd #else
    228      1.16     lukem 	long *lp;
    229       1.1       cgd 	struct	ieee_double id;
    230       1.1       cgd 	struct	vax_double vd;
    231      1.16     lukem 	struct dbl_limits *lim;
    232       1.1       cgd 	int i;
    233       1.1       cgd #endif
    234       1.1       cgd 
    235       1.1       cgd 	switch (xdrs->x_op) {
    236       1.1       cgd 
    237       1.1       cgd 	case XDR_ENCODE:
    238       1.1       cgd #ifdef IEEEFP
    239      1.18  christos 		i32p = (int32_t *)(void *)dp;
    240       1.1       cgd #if BYTE_ORDER == BIG_ENDIAN
    241       1.4       cgd 		tmpl = *i32p++;
    242       1.4       cgd 		rv = XDR_PUTLONG(xdrs, &tmpl);
    243       1.4       cgd 		if (!rv)
    244       1.4       cgd 			return (rv);
    245       1.4       cgd 		tmpl = *i32p;
    246       1.4       cgd 		rv = XDR_PUTLONG(xdrs, &tmpl);
    247       1.1       cgd #else
    248       1.9        pk 		tmpl = *(i32p+1);
    249       1.4       cgd 		rv = XDR_PUTLONG(xdrs, &tmpl);
    250       1.4       cgd 		if (!rv)
    251       1.4       cgd 			return (rv);
    252       1.4       cgd 		tmpl = *i32p;
    253       1.4       cgd 		rv = XDR_PUTLONG(xdrs, &tmpl);
    254       1.1       cgd #endif
    255       1.4       cgd 		return (rv);
    256       1.1       cgd #else
    257       1.1       cgd 		vd = *((struct vax_double *)dp);
    258       1.1       cgd 		for (i = 0, lim = dbl_limits;
    259       1.1       cgd 			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
    260       1.1       cgd 			i++, lim++) {
    261       1.1       cgd 			if ((vd.mantissa4 == lim->d.mantissa4) &&
    262       1.1       cgd 				(vd.mantissa3 == lim->d.mantissa3) &&
    263       1.1       cgd 				(vd.mantissa2 == lim->d.mantissa2) &&
    264       1.1       cgd 				(vd.mantissa1 == lim->d.mantissa1) &&
    265       1.1       cgd 				(vd.exp == lim->d.exp)) {
    266       1.1       cgd 				id = lim->ieee;
    267       1.1       cgd 				goto shipit;
    268       1.1       cgd 			}
    269       1.1       cgd 		}
    270       1.1       cgd 		id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
    271       1.1       cgd 		id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
    272       1.1       cgd 		id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
    273       1.1       cgd 				(vd.mantissa3 << 13) |
    274       1.1       cgd 				((vd.mantissa4 >> 3) & MASK(13));
    275       1.1       cgd 	shipit:
    276       1.1       cgd 		id.sign = vd.sign;
    277      1.15     lukem 		lp = (long *)&id;
    278       1.1       cgd 		return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
    279       1.1       cgd #endif
    280       1.1       cgd 
    281       1.1       cgd 	case XDR_DECODE:
    282       1.1       cgd #ifdef IEEEFP
    283      1.18  christos 		i32p = (int32_t *)(void *)dp;
    284       1.1       cgd #if BYTE_ORDER == BIG_ENDIAN
    285       1.4       cgd 		rv = XDR_GETLONG(xdrs, &tmpl);
    286       1.4       cgd 		*i32p++ = tmpl;
    287       1.4       cgd 		if (!rv)
    288       1.4       cgd 			return (rv);
    289       1.4       cgd 		rv = XDR_GETLONG(xdrs, &tmpl);
    290       1.4       cgd 		*i32p = tmpl;
    291       1.1       cgd #else
    292       1.4       cgd 		rv = XDR_GETLONG(xdrs, &tmpl);
    293      1.18  christos 		*(i32p+1) = (int32_t)tmpl;
    294       1.4       cgd 		if (!rv)
    295       1.4       cgd 			return (rv);
    296       1.4       cgd 		rv = XDR_GETLONG(xdrs, &tmpl);
    297      1.18  christos 		*i32p = (int32_t)tmpl;
    298       1.1       cgd #endif
    299       1.4       cgd 		return (rv);
    300       1.1       cgd #else
    301       1.1       cgd 		lp = (long *)&id;
    302       1.1       cgd 		if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
    303       1.1       cgd 			return (FALSE);
    304       1.1       cgd 		for (i = 0, lim = dbl_limits;
    305       1.1       cgd 			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
    306       1.1       cgd 			i++, lim++) {
    307       1.1       cgd 			if ((id.mantissa2 == lim->ieee.mantissa2) &&
    308       1.1       cgd 				(id.mantissa1 == lim->ieee.mantissa1) &&
    309       1.1       cgd 				(id.exp == lim->ieee.exp)) {
    310       1.1       cgd 				vd = lim->d;
    311       1.1       cgd 				goto doneit;
    312       1.1       cgd 			}
    313       1.1       cgd 		}
    314       1.1       cgd 		vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
    315       1.1       cgd 		vd.mantissa1 = (id.mantissa1 >> 13);
    316       1.1       cgd 		vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
    317       1.1       cgd 				(id.mantissa2 >> 29);
    318       1.1       cgd 		vd.mantissa3 = (id.mantissa2 >> 13);
    319       1.1       cgd 		vd.mantissa4 = (id.mantissa2 << 3);
    320       1.1       cgd 	doneit:
    321       1.1       cgd 		vd.sign = id.sign;
    322       1.1       cgd 		*dp = *((double *)&vd);
    323       1.1       cgd 		return (TRUE);
    324       1.1       cgd #endif
    325       1.1       cgd 
    326       1.1       cgd 	case XDR_FREE:
    327       1.1       cgd 		return (TRUE);
    328       1.1       cgd 	}
    329      1.18  christos 	/* NOTREACHED */
    330       1.1       cgd 	return (FALSE);
    331       1.1       cgd }
    332