xref: /freebsd/lib/libc/xdr/xdr_float.c (revision bf51882a)
18360efbdSAlfred Perlstein /*	$NetBSD: xdr_float.c,v 1.23 2000/07/17 04:59:51 matt Exp $	*/
28360efbdSAlfred Perlstein 
3a204967aSHiroki Sato /*-
4a204967aSHiroki Sato  * Copyright (c) 2010, Oracle America, Inc.
5eae561b3SGarrett Wollman  *
6a204967aSHiroki Sato  * Redistribution and use in source and binary forms, with or without
7a204967aSHiroki Sato  * modification, are permitted provided that the following conditions are
8a204967aSHiroki Sato  * met:
9eae561b3SGarrett Wollman  *
10a204967aSHiroki Sato  *     * Redistributions of source code must retain the above copyright
11a204967aSHiroki Sato  *       notice, this list of conditions and the following disclaimer.
12a204967aSHiroki Sato  *     * Redistributions in binary form must reproduce the above
13a204967aSHiroki Sato  *       copyright notice, this list of conditions and the following
14a204967aSHiroki Sato  *       disclaimer in the documentation and/or other materials
15a204967aSHiroki Sato  *       provided with the distribution.
16a204967aSHiroki Sato  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17a204967aSHiroki Sato  *       contributors may be used to endorse or promote products derived
18a204967aSHiroki Sato  *       from this software without specific prior written permission.
19eae561b3SGarrett Wollman  *
20a204967aSHiroki Sato  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21a204967aSHiroki Sato  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22a204967aSHiroki Sato  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23a204967aSHiroki Sato  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24a204967aSHiroki Sato  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25a204967aSHiroki Sato  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26a204967aSHiroki Sato  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27a204967aSHiroki Sato  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28a204967aSHiroki Sato  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29a204967aSHiroki Sato  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30a204967aSHiroki Sato  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31a204967aSHiroki Sato  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32eae561b3SGarrett Wollman  */
33eae561b3SGarrett Wollman 
34eae561b3SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint)
35a9bdcd37SDavid E. O'Brien static char *sccsid2 = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
368360efbdSAlfred Perlstein static char *sccsid = "@(#)xdr_float.c	2.1 88/07/29 4.0 RPCSRC";
37eae561b3SGarrett Wollman #endif
38333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
39333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
40eae561b3SGarrett Wollman 
41eae561b3SGarrett Wollman /*
428360efbdSAlfred Perlstein  * xdr_float.c, Generic XDR routines implementation.
43eae561b3SGarrett Wollman  *
44eae561b3SGarrett Wollman  * These are the "floating point" xdr routines used to (de)serialize
45eae561b3SGarrett Wollman  * most common data items.  See xdr.h for more info on the interface to
46eae561b3SGarrett Wollman  * xdr.
47eae561b3SGarrett Wollman  */
48eae561b3SGarrett Wollman 
498360efbdSAlfred Perlstein #include "namespace.h"
50eae561b3SGarrett Wollman #include <sys/types.h>
51eae561b3SGarrett Wollman #include <sys/param.h>
528360efbdSAlfred Perlstein 
538360efbdSAlfred Perlstein #include <stdio.h>
548360efbdSAlfred Perlstein 
55eae561b3SGarrett Wollman #include <rpc/types.h>
56eae561b3SGarrett Wollman #include <rpc/xdr.h>
578360efbdSAlfred Perlstein #include "un-namespace.h"
58eae561b3SGarrett Wollman 
59eae561b3SGarrett Wollman /*
60eae561b3SGarrett Wollman  * NB: Not portable.
611ad08a09SPeter Wemm  * This routine works on machines with IEEE754 FP and Vaxen.
62eae561b3SGarrett Wollman  */
63eae561b3SGarrett Wollman 
641ad08a09SPeter Wemm #include <machine/endian.h>
65eae561b3SGarrett Wollman #define IEEEFP
66eae561b3SGarrett Wollman 
678360efbdSAlfred Perlstein #if defined(__vax__)
68eae561b3SGarrett Wollman 
69eae561b3SGarrett Wollman /* What IEEE single precision floating point looks like on a Vax */
70eae561b3SGarrett Wollman struct	ieee_single {
71eae561b3SGarrett Wollman 	unsigned int	mantissa: 23;
72eae561b3SGarrett Wollman 	unsigned int	exp     : 8;
73eae561b3SGarrett Wollman 	unsigned int	sign    : 1;
74eae561b3SGarrett Wollman };
75eae561b3SGarrett Wollman 
76eae561b3SGarrett Wollman /* Vax single precision floating point */
77eae561b3SGarrett Wollman struct	vax_single {
78eae561b3SGarrett Wollman 	unsigned int	mantissa1 : 7;
79eae561b3SGarrett Wollman 	unsigned int	exp       : 8;
80eae561b3SGarrett Wollman 	unsigned int	sign      : 1;
81eae561b3SGarrett Wollman 	unsigned int	mantissa2 : 16;
82eae561b3SGarrett Wollman };
83eae561b3SGarrett Wollman 
84eae561b3SGarrett Wollman #define VAX_SNG_BIAS	0x81
85eae561b3SGarrett Wollman #define IEEE_SNG_BIAS	0x7f
86eae561b3SGarrett Wollman 
87eae561b3SGarrett Wollman static struct sgl_limits {
88eae561b3SGarrett Wollman 	struct vax_single s;
89eae561b3SGarrett Wollman 	struct ieee_single ieee;
90eae561b3SGarrett Wollman } sgl_limits[2] = {
91eae561b3SGarrett Wollman 	{{ 0x7f, 0xff, 0x0, 0xffff },	/* Max Vax */
92eae561b3SGarrett Wollman 	{ 0x0, 0xff, 0x0 }},		/* Max IEEE */
93eae561b3SGarrett Wollman 	{{ 0x0, 0x0, 0x0, 0x0 },	/* Min Vax */
94eae561b3SGarrett Wollman 	{ 0x0, 0x0, 0x0 }}		/* Min IEEE */
95eae561b3SGarrett Wollman };
96eae561b3SGarrett Wollman #endif /* vax */
97eae561b3SGarrett Wollman 
98eae561b3SGarrett Wollman bool_t
99107909b8SCraig Rodrigues xdr_float(XDR *xdrs, float *fp)
100eae561b3SGarrett Wollman {
1018360efbdSAlfred Perlstein #ifndef IEEEFP
102eae561b3SGarrett Wollman 	struct ieee_single is;
103eae561b3SGarrett Wollman 	struct vax_single vs, *vsp;
104eae561b3SGarrett Wollman 	struct sgl_limits *lim;
105eae561b3SGarrett Wollman 	int i;
106eae561b3SGarrett Wollman #endif
107eae561b3SGarrett Wollman 	switch (xdrs->x_op) {
108eae561b3SGarrett Wollman 
109eae561b3SGarrett Wollman 	case XDR_ENCODE:
110eae561b3SGarrett Wollman #ifdef IEEEFP
1118360efbdSAlfred Perlstein 		return (XDR_PUTINT32(xdrs, (int32_t *)fp));
112eae561b3SGarrett Wollman #else
113eae561b3SGarrett Wollman 		vs = *((struct vax_single *)fp);
114bf51882aSPedro F. Giffuni 		for (i = 0, lim = sgl_limits; i < nitems(sgl_limits);
115eae561b3SGarrett Wollman 		    i++, lim++) {
116eae561b3SGarrett Wollman 			if ((vs.mantissa2 == lim->s.mantissa2) &&
117eae561b3SGarrett Wollman 				(vs.exp == lim->s.exp) &&
118eae561b3SGarrett Wollman 				(vs.mantissa1 == lim->s.mantissa1)) {
119eae561b3SGarrett Wollman 				is = lim->ieee;
120eae561b3SGarrett Wollman 				goto shipit;
121eae561b3SGarrett Wollman 			}
122eae561b3SGarrett Wollman 		}
123eae561b3SGarrett Wollman 		is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
124eae561b3SGarrett Wollman 		is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
125eae561b3SGarrett Wollman 	shipit:
126eae561b3SGarrett Wollman 		is.sign = vs.sign;
1278360efbdSAlfred Perlstein 		return (XDR_PUTINT32(xdrs, (int32_t *)&is));
128eae561b3SGarrett Wollman #endif
129eae561b3SGarrett Wollman 
130eae561b3SGarrett Wollman 	case XDR_DECODE:
131eae561b3SGarrett Wollman #ifdef IEEEFP
1328360efbdSAlfred Perlstein 		return (XDR_GETINT32(xdrs, (int32_t *)fp));
133eae561b3SGarrett Wollman #else
134eae561b3SGarrett Wollman 		vsp = (struct vax_single *)fp;
1358360efbdSAlfred Perlstein 		if (!XDR_GETINT32(xdrs, (int32_t *)&is))
136eae561b3SGarrett Wollman 			return (FALSE);
137bf51882aSPedro F. Giffuni 		for (i = 0, lim = sgl_limits; i < nitems(sgl_limits);
138eae561b3SGarrett Wollman 		    i++, lim++) {
139eae561b3SGarrett Wollman 			if ((is.exp == lim->ieee.exp) &&
140eae561b3SGarrett Wollman 				(is.mantissa == lim->ieee.mantissa)) {
141eae561b3SGarrett Wollman 				*vsp = lim->s;
142eae561b3SGarrett Wollman 				goto doneit;
143eae561b3SGarrett Wollman 			}
144eae561b3SGarrett Wollman 		}
145eae561b3SGarrett Wollman 		vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
146eae561b3SGarrett Wollman 		vsp->mantissa2 = is.mantissa;
147eae561b3SGarrett Wollman 		vsp->mantissa1 = (is.mantissa >> 16);
148eae561b3SGarrett Wollman 	doneit:
149eae561b3SGarrett Wollman 		vsp->sign = is.sign;
150eae561b3SGarrett Wollman 		return (TRUE);
151eae561b3SGarrett Wollman #endif
152eae561b3SGarrett Wollman 
153eae561b3SGarrett Wollman 	case XDR_FREE:
154eae561b3SGarrett Wollman 		return (TRUE);
155eae561b3SGarrett Wollman 	}
1568360efbdSAlfred Perlstein 	/* NOTREACHED */
157eae561b3SGarrett Wollman 	return (FALSE);
158eae561b3SGarrett Wollman }
159eae561b3SGarrett Wollman 
1608360efbdSAlfred Perlstein #if defined(__vax__)
161eae561b3SGarrett Wollman /* What IEEE double precision floating point looks like on a Vax */
162eae561b3SGarrett Wollman struct	ieee_double {
163eae561b3SGarrett Wollman 	unsigned int	mantissa1 : 20;
164eae561b3SGarrett Wollman 	unsigned int	exp       : 11;
165eae561b3SGarrett Wollman 	unsigned int	sign      : 1;
166eae561b3SGarrett Wollman 	unsigned int	mantissa2 : 32;
167eae561b3SGarrett Wollman };
168eae561b3SGarrett Wollman 
169eae561b3SGarrett Wollman /* Vax double precision floating point */
170eae561b3SGarrett Wollman struct  vax_double {
171eae561b3SGarrett Wollman 	unsigned int	mantissa1 : 7;
172eae561b3SGarrett Wollman 	unsigned int	exp       : 8;
173eae561b3SGarrett Wollman 	unsigned int	sign      : 1;
174eae561b3SGarrett Wollman 	unsigned int	mantissa2 : 16;
175eae561b3SGarrett Wollman 	unsigned int	mantissa3 : 16;
176eae561b3SGarrett Wollman 	unsigned int	mantissa4 : 16;
177eae561b3SGarrett Wollman };
178eae561b3SGarrett Wollman 
179eae561b3SGarrett Wollman #define VAX_DBL_BIAS	0x81
180eae561b3SGarrett Wollman #define IEEE_DBL_BIAS	0x3ff
181eae561b3SGarrett Wollman #define MASK(nbits)	((1 << nbits) - 1)
182eae561b3SGarrett Wollman 
183eae561b3SGarrett Wollman static struct dbl_limits {
184eae561b3SGarrett Wollman 	struct	vax_double d;
185eae561b3SGarrett Wollman 	struct	ieee_double ieee;
186eae561b3SGarrett Wollman } dbl_limits[2] = {
187eae561b3SGarrett Wollman 	{{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },	/* Max Vax */
188eae561b3SGarrett Wollman 	{ 0x0, 0x7ff, 0x0, 0x0 }},			/* Max IEEE */
189eae561b3SGarrett Wollman 	{{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},		/* Min Vax */
190eae561b3SGarrett Wollman 	{ 0x0, 0x0, 0x0, 0x0 }}				/* Min IEEE */
191eae561b3SGarrett Wollman };
192eae561b3SGarrett Wollman 
193eae561b3SGarrett Wollman #endif /* vax */
194eae561b3SGarrett Wollman 
195eae561b3SGarrett Wollman 
196eae561b3SGarrett Wollman bool_t
197d660d38dSCraig Rodrigues xdr_double(XDR *xdrs, double *dp)
198eae561b3SGarrett Wollman {
1991ad08a09SPeter Wemm #ifdef IEEEFP
2008360efbdSAlfred Perlstein 	int32_t *i32p;
2011ad08a09SPeter Wemm 	bool_t rv;
2021ad08a09SPeter Wemm #else
2038360efbdSAlfred Perlstein 	int32_t *lp;
204eae561b3SGarrett Wollman 	struct	ieee_double id;
205eae561b3SGarrett Wollman 	struct	vax_double vd;
2068360efbdSAlfred Perlstein 	struct dbl_limits *lim;
207eae561b3SGarrett Wollman 	int i;
208eae561b3SGarrett Wollman #endif
209eae561b3SGarrett Wollman 
210eae561b3SGarrett Wollman 	switch (xdrs->x_op) {
211eae561b3SGarrett Wollman 
212eae561b3SGarrett Wollman 	case XDR_ENCODE:
213eae561b3SGarrett Wollman #ifdef IEEEFP
2148360efbdSAlfred Perlstein 		i32p = (int32_t *)(void *)dp;
215eae561b3SGarrett Wollman #if BYTE_ORDER == BIG_ENDIAN
2168360efbdSAlfred Perlstein 		rv = XDR_PUTINT32(xdrs, i32p);
2171ad08a09SPeter Wemm 		if (!rv)
2181ad08a09SPeter Wemm 			return (rv);
2198360efbdSAlfred Perlstein 		rv = XDR_PUTINT32(xdrs, i32p+1);
220eae561b3SGarrett Wollman #else
2218360efbdSAlfred Perlstein 		rv = XDR_PUTINT32(xdrs, i32p+1);
2221ad08a09SPeter Wemm 		if (!rv)
2231ad08a09SPeter Wemm 			return (rv);
2248360efbdSAlfred Perlstein 		rv = XDR_PUTINT32(xdrs, i32p);
225eae561b3SGarrett Wollman #endif
2261ad08a09SPeter Wemm 		return (rv);
227eae561b3SGarrett Wollman #else
228eae561b3SGarrett Wollman 		vd = *((struct vax_double *)dp);
229bf51882aSPedro F. Giffuni 		for (i = 0, lim = dbl_limits; i < nitems(dbl_limits);
230eae561b3SGarrett Wollman 		    i++, lim++) {
231eae561b3SGarrett Wollman 			if ((vd.mantissa4 == lim->d.mantissa4) &&
232eae561b3SGarrett Wollman 				(vd.mantissa3 == lim->d.mantissa3) &&
233eae561b3SGarrett Wollman 				(vd.mantissa2 == lim->d.mantissa2) &&
234eae561b3SGarrett Wollman 				(vd.mantissa1 == lim->d.mantissa1) &&
235eae561b3SGarrett Wollman 				(vd.exp == lim->d.exp)) {
236eae561b3SGarrett Wollman 				id = lim->ieee;
237eae561b3SGarrett Wollman 				goto shipit;
238eae561b3SGarrett Wollman 			}
239eae561b3SGarrett Wollman 		}
240eae561b3SGarrett Wollman 		id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
241eae561b3SGarrett Wollman 		id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
242eae561b3SGarrett Wollman 		id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
243eae561b3SGarrett Wollman 				(vd.mantissa3 << 13) |
244eae561b3SGarrett Wollman 				((vd.mantissa4 >> 3) & MASK(13));
245eae561b3SGarrett Wollman 	shipit:
246eae561b3SGarrett Wollman 		id.sign = vd.sign;
2478360efbdSAlfred Perlstein 		lp = (int32_t *)&id;
2488360efbdSAlfred Perlstein 		return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
249eae561b3SGarrett Wollman #endif
250eae561b3SGarrett Wollman 
251eae561b3SGarrett Wollman 	case XDR_DECODE:
252eae561b3SGarrett Wollman #ifdef IEEEFP
2538360efbdSAlfred Perlstein 		i32p = (int32_t *)(void *)dp;
254eae561b3SGarrett Wollman #if BYTE_ORDER == BIG_ENDIAN
2558360efbdSAlfred Perlstein 		rv = XDR_GETINT32(xdrs, i32p);
2561ad08a09SPeter Wemm 		if (!rv)
2571ad08a09SPeter Wemm 			return (rv);
2588360efbdSAlfred Perlstein 		rv = XDR_GETINT32(xdrs, i32p+1);
259eae561b3SGarrett Wollman #else
2608360efbdSAlfred Perlstein 		rv = XDR_GETINT32(xdrs, i32p+1);
2611ad08a09SPeter Wemm 		if (!rv)
2621ad08a09SPeter Wemm 			return (rv);
2638360efbdSAlfred Perlstein 		rv = XDR_GETINT32(xdrs, i32p);
264eae561b3SGarrett Wollman #endif
2651ad08a09SPeter Wemm 		return (rv);
266eae561b3SGarrett Wollman #else
2678360efbdSAlfred Perlstein 		lp = (int32_t *)&id;
2688360efbdSAlfred Perlstein 		if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
269eae561b3SGarrett Wollman 			return (FALSE);
270bf51882aSPedro F. Giffuni 		for (i = 0, lim = dbl_limits; i < nitems(dbl_limits);
271eae561b3SGarrett Wollman 		    i++, lim++) {
272eae561b3SGarrett Wollman 			if ((id.mantissa2 == lim->ieee.mantissa2) &&
273eae561b3SGarrett Wollman 				(id.mantissa1 == lim->ieee.mantissa1) &&
274eae561b3SGarrett Wollman 				(id.exp == lim->ieee.exp)) {
275eae561b3SGarrett Wollman 				vd = lim->d;
276eae561b3SGarrett Wollman 				goto doneit;
277eae561b3SGarrett Wollman 			}
278eae561b3SGarrett Wollman 		}
279eae561b3SGarrett Wollman 		vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
280eae561b3SGarrett Wollman 		vd.mantissa1 = (id.mantissa1 >> 13);
281eae561b3SGarrett Wollman 		vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
282eae561b3SGarrett Wollman 				(id.mantissa2 >> 29);
283eae561b3SGarrett Wollman 		vd.mantissa3 = (id.mantissa2 >> 13);
284eae561b3SGarrett Wollman 		vd.mantissa4 = (id.mantissa2 << 3);
285eae561b3SGarrett Wollman 	doneit:
286eae561b3SGarrett Wollman 		vd.sign = id.sign;
287eae561b3SGarrett Wollman 		*dp = *((double *)&vd);
288eae561b3SGarrett Wollman 		return (TRUE);
289eae561b3SGarrett Wollman #endif
290eae561b3SGarrett Wollman 
291eae561b3SGarrett Wollman 	case XDR_FREE:
292eae561b3SGarrett Wollman 		return (TRUE);
293eae561b3SGarrett Wollman 	}
2948360efbdSAlfred Perlstein 	/* NOTREACHED */
295eae561b3SGarrett Wollman 	return (FALSE);
296eae561b3SGarrett Wollman }
297