Home | History | Annotate | Line # | Download | only in rpc
xdr_float.c revision 1.40
      1 /*	$NetBSD: xdr_float.c,v 1.40 2014/08/24 17:07:00 matt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010, Oracle America, Inc.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions are
      8  * met:
      9  *
     10  *     * Redistributions of source code must retain the above copyright
     11  *       notice, this list of conditions and the following disclaimer.
     12  *     * Redistributions in binary form must reproduce the above
     13  *       copyright notice, this list of conditions and the following
     14  *       disclaimer in the documentation and/or other materials
     15  *       provided with the distribution.
     16  *     * Neither the name of the "Oracle America, Inc." nor the names of its
     17  *       contributors may be used to endorse or promote products derived
     18  *       from this software without specific prior written permission.
     19  *
     20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     25  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #if defined(LIBC_SCCS) && !defined(lint)
     36 #if 0
     37 static char *sccsid = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
     38 static char *sccsid = "@(#)xdr_float.c	2.1 88/07/29 4.0 RPCSRC";
     39 #else
     40 __RCSID("$NetBSD: xdr_float.c,v 1.40 2014/08/24 17:07:00 matt Exp $");
     41 #endif
     42 #endif
     43 
     44 /*
     45  * xdr_float.c, Generic XDR routines implementation.
     46  *
     47  * Copyright (C) 1984, Sun Microsystems, Inc.
     48  *
     49  * These are the "floating point" xdr routines used to (de)serialize
     50  * most common data items.  See xdr.h for more info on the interface to
     51  * xdr.
     52  */
     53 
     54 #include "namespace.h"
     55 
     56 #include <sys/types.h>
     57 #include <sys/param.h>
     58 
     59 #include <stdio.h>
     60 
     61 #include <rpc/types.h>
     62 #include <rpc/xdr.h>
     63 
     64 #ifdef __weak_alias
     65 __weak_alias(xdr_double,_xdr_double)
     66 __weak_alias(xdr_float,_xdr_float)
     67 #endif
     68 
     69 /*
     70  * NB: Not portable.
     71  * This routine works on machines with IEEE754 FP and Vaxen.
     72  */
     73 
     74 #if !defined(__vax__)
     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(XDR *xdrs, float *fp)
    112 {
    113 #ifndef IEEEFP
    114 	struct ieee_single is;
    115 	struct vax_single vs, *vsp;
    116 	struct sgl_limits *lim;
    117 	size_t i;
    118 #endif
    119 	switch (xdrs->x_op) {
    120 
    121 	case XDR_ENCODE:
    122 #ifdef IEEEFP
    123 		return (XDR_PUTINT32(xdrs, (int32_t *)(void *)fp));
    124 #else
    125 		vs = *((struct vax_single *)(void *)fp);
    126 		for (i = 0, lim = sgl_limits;
    127 			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
    128 			i++, lim++) {
    129 			if ((vs.mantissa2 == lim->s.mantissa2) &&
    130 				(vs.exp == lim->s.exp) &&
    131 				(vs.mantissa1 == lim->s.mantissa1)) {
    132 				is = lim->ieee;
    133 				goto shipit;
    134 			}
    135 		}
    136 		is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
    137 		is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
    138 	shipit:
    139 		is.sign = vs.sign;
    140 		return (XDR_PUTINT32(xdrs, (int32_t *)(void *)&is));
    141 #endif
    142 
    143 	case XDR_DECODE:
    144 #ifdef IEEEFP
    145 		return (XDR_GETINT32(xdrs, (int32_t *)(void *)fp));
    146 #else
    147 		vsp = (struct vax_single *)(void *)fp;
    148 		if (!XDR_GETINT32(xdrs, (int32_t *)(void *)&is))
    149 			return (FALSE);
    150 		for (i = 0, lim = sgl_limits;
    151 			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
    152 			i++, lim++) {
    153 			if ((is.exp == lim->ieee.exp) &&
    154 				(is.mantissa == lim->ieee.mantissa)) {
    155 				*vsp = lim->s;
    156 				goto doneit;
    157 			}
    158 		}
    159 		vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
    160 		vsp->mantissa2 = is.mantissa;
    161 		vsp->mantissa1 = ((unsigned int)is.mantissa >> 16);
    162 	doneit:
    163 		vsp->sign = is.sign;
    164 		return (TRUE);
    165 #endif
    166 
    167 	case XDR_FREE:
    168 		return (TRUE);
    169 	}
    170 	/* NOTREACHED */
    171 	return (FALSE);
    172 }
    173 
    174 #if defined(__vax__)
    175 /* What IEEE double precision floating point looks like on a Vax */
    176 struct	ieee_double {
    177 	unsigned int	mantissa1 : 20;
    178 	unsigned int	exp       : 11;
    179 	unsigned int	sign      : 1;
    180 	unsigned int	mantissa2 : 32;
    181 };
    182 
    183 /* Vax double precision floating point */
    184 struct  vax_double {
    185 	unsigned int	mantissa1 : 7;
    186 	unsigned int	exp       : 8;
    187 	unsigned int	sign      : 1;
    188 	unsigned int	mantissa2 : 16;
    189 	unsigned int	mantissa3 : 16;
    190 	unsigned int	mantissa4 : 16;
    191 };
    192 
    193 #define VAX_DBL_BIAS	0x81
    194 #define IEEE_DBL_BIAS	0x3ff
    195 #define MASK(nbits)	((1 << nbits) - 1)
    196 
    197 static struct dbl_limits {
    198 	struct	vax_double d;
    199 	struct	ieee_double ieee;
    200 } dbl_limits[2] = {
    201 	{{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },	/* Max Vax */
    202 	{ 0x0, 0x7ff, 0x0, 0x0 }},			/* Max IEEE */
    203 	{{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},		/* Min Vax */
    204 	{ 0x0, 0x0, 0x0, 0x0 }}				/* Min IEEE */
    205 };
    206 
    207 #endif /* vax */
    208 
    209 
    210 bool_t
    211 xdr_double(XDR *xdrs, double *dp)
    212 {
    213 #ifdef IEEEFP
    214 	int32_t *i32p;
    215 	bool_t rv;
    216 #else
    217 	int32_t *lp;
    218 	struct	ieee_double id;
    219 	struct	vax_double vd;
    220 	struct dbl_limits *lim;
    221 	size_t i;
    222 #endif
    223 
    224 	switch (xdrs->x_op) {
    225 
    226 	case XDR_ENCODE:
    227 #ifdef IEEEFP
    228 		i32p = (int32_t *)(void *)dp;
    229 #if (BYTE_ORDER == BIG_ENDIAN) || \
    230     (defined(__arm__) && !defined(__VFP_FP__))
    231 		rv = XDR_PUTINT32(xdrs, i32p);
    232 		if (!rv)
    233 			return (rv);
    234 		rv = XDR_PUTINT32(xdrs, i32p+1);
    235 #else
    236 		rv = XDR_PUTINT32(xdrs, i32p+1);
    237 		if (!rv)
    238 			return (rv);
    239 		rv = XDR_PUTINT32(xdrs, i32p);
    240 #endif
    241 		return (rv);
    242 #else
    243 		vd = *((struct vax_double *)(void *)dp);
    244 		for (i = 0, lim = dbl_limits;
    245 			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
    246 			i++, lim++) {
    247 			if ((vd.mantissa4 == lim->d.mantissa4) &&
    248 				(vd.mantissa3 == lim->d.mantissa3) &&
    249 				(vd.mantissa2 == lim->d.mantissa2) &&
    250 				(vd.mantissa1 == lim->d.mantissa1) &&
    251 				(vd.exp == lim->d.exp)) {
    252 				id = lim->ieee;
    253 				goto shipit;
    254 			}
    255 		}
    256 		id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
    257 		id.mantissa1 = (vd.mantissa1 << 13) |
    258 			    ((unsigned int)vd.mantissa2 >> 3);
    259 		id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
    260 				(vd.mantissa3 << 13) |
    261 				(((unsigned int)vd.mantissa4 >> 3) & MASK(13));
    262 	shipit:
    263 		id.sign = vd.sign;
    264 		lp = (int32_t *)(void *)&id;
    265 		return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
    266 #endif
    267 
    268 	case XDR_DECODE:
    269 #ifdef IEEEFP
    270 		i32p = (int32_t *)(void *)dp;
    271 #if BYTE_ORDER == BIG_ENDIAN || \
    272     (defined(__arm__) && !defined(__VFP_FP__))
    273 		rv = XDR_GETINT32(xdrs, i32p);
    274 		if (!rv)
    275 			return (rv);
    276 		rv = XDR_GETINT32(xdrs, i32p+1);
    277 #else
    278 		rv = XDR_GETINT32(xdrs, i32p+1);
    279 		if (!rv)
    280 			return (rv);
    281 		rv = XDR_GETINT32(xdrs, i32p);
    282 #endif
    283 		return (rv);
    284 #else
    285 		lp = (int32_t *)(void *)&id;
    286 		if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
    287 			return (FALSE);
    288 		for (i = 0, lim = dbl_limits;
    289 			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
    290 			i++, lim++) {
    291 			if ((id.mantissa2 == lim->ieee.mantissa2) &&
    292 				(id.mantissa1 == lim->ieee.mantissa1) &&
    293 				(id.exp == lim->ieee.exp)) {
    294 				vd = lim->d;
    295 				goto doneit;
    296 			}
    297 		}
    298 		vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
    299 		vd.mantissa1 = ((unsigned int)id.mantissa1 >> 13);
    300 		vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
    301 				((unsigned int)id.mantissa2 >> 29);
    302 		vd.mantissa3 = ((unsigned int)id.mantissa2 >> 13);
    303 		vd.mantissa4 = (id.mantissa2 << 3);
    304 	doneit:
    305 		vd.sign = id.sign;
    306 		*dp = *((double *)(void *)&vd);
    307 		return (TRUE);
    308 #endif
    309 
    310 	case XDR_FREE:
    311 		return (TRUE);
    312 	}
    313 	/* NOTREACHED */
    314 	return (FALSE);
    315 }
    316