xref: /freebsd/lib/libc/xdr/xdr_float.c (revision 107909b8)
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 #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
651ad08a09SPeter Wemm     defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
66e7d939bdSMarcel Moolenaar     defined(__arm__) || defined(__ppc__) || \
67d422e6f9SAndrew Turner     defined(__arm26__) || defined(__sparc64__) || defined(__amd64__) || \
68d422e6f9SAndrew Turner     defined(__aarch64__)
691ad08a09SPeter Wemm #include <machine/endian.h>
70eae561b3SGarrett Wollman #define IEEEFP
71eae561b3SGarrett Wollman #endif
72eae561b3SGarrett Wollman 
738360efbdSAlfred Perlstein #if defined(__vax__)
74eae561b3SGarrett Wollman 
75eae561b3SGarrett Wollman /* What IEEE single precision floating point looks like on a Vax */
76eae561b3SGarrett Wollman struct	ieee_single {
77eae561b3SGarrett Wollman 	unsigned int	mantissa: 23;
78eae561b3SGarrett Wollman 	unsigned int	exp     : 8;
79eae561b3SGarrett Wollman 	unsigned int	sign    : 1;
80eae561b3SGarrett Wollman };
81eae561b3SGarrett Wollman 
82eae561b3SGarrett Wollman /* Vax single precision floating point */
83eae561b3SGarrett Wollman struct	vax_single {
84eae561b3SGarrett Wollman 	unsigned int	mantissa1 : 7;
85eae561b3SGarrett Wollman 	unsigned int	exp       : 8;
86eae561b3SGarrett Wollman 	unsigned int	sign      : 1;
87eae561b3SGarrett Wollman 	unsigned int	mantissa2 : 16;
88eae561b3SGarrett Wollman };
89eae561b3SGarrett Wollman 
90eae561b3SGarrett Wollman #define VAX_SNG_BIAS	0x81
91eae561b3SGarrett Wollman #define IEEE_SNG_BIAS	0x7f
92eae561b3SGarrett Wollman 
93eae561b3SGarrett Wollman static struct sgl_limits {
94eae561b3SGarrett Wollman 	struct vax_single s;
95eae561b3SGarrett Wollman 	struct ieee_single ieee;
96eae561b3SGarrett Wollman } sgl_limits[2] = {
97eae561b3SGarrett Wollman 	{{ 0x7f, 0xff, 0x0, 0xffff },	/* Max Vax */
98eae561b3SGarrett Wollman 	{ 0x0, 0xff, 0x0 }},		/* Max IEEE */
99eae561b3SGarrett Wollman 	{{ 0x0, 0x0, 0x0, 0x0 },	/* Min Vax */
100eae561b3SGarrett Wollman 	{ 0x0, 0x0, 0x0 }}		/* Min IEEE */
101eae561b3SGarrett Wollman };
102eae561b3SGarrett Wollman #endif /* vax */
103eae561b3SGarrett Wollman 
104eae561b3SGarrett Wollman bool_t
105107909b8SCraig Rodrigues xdr_float(XDR *xdrs, float *fp)
106eae561b3SGarrett Wollman {
1078360efbdSAlfred Perlstein #ifndef IEEEFP
108eae561b3SGarrett Wollman 	struct ieee_single is;
109eae561b3SGarrett Wollman 	struct vax_single vs, *vsp;
110eae561b3SGarrett Wollman 	struct sgl_limits *lim;
111eae561b3SGarrett Wollman 	int i;
112eae561b3SGarrett Wollman #endif
113eae561b3SGarrett Wollman 	switch (xdrs->x_op) {
114eae561b3SGarrett Wollman 
115eae561b3SGarrett Wollman 	case XDR_ENCODE:
116eae561b3SGarrett Wollman #ifdef IEEEFP
1178360efbdSAlfred Perlstein 		return (XDR_PUTINT32(xdrs, (int32_t *)fp));
118eae561b3SGarrett Wollman #else
119eae561b3SGarrett Wollman 		vs = *((struct vax_single *)fp);
120eae561b3SGarrett Wollman 		for (i = 0, lim = sgl_limits;
121eae561b3SGarrett Wollman 			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
122eae561b3SGarrett Wollman 			i++, lim++) {
123eae561b3SGarrett Wollman 			if ((vs.mantissa2 == lim->s.mantissa2) &&
124eae561b3SGarrett Wollman 				(vs.exp == lim->s.exp) &&
125eae561b3SGarrett Wollman 				(vs.mantissa1 == lim->s.mantissa1)) {
126eae561b3SGarrett Wollman 				is = lim->ieee;
127eae561b3SGarrett Wollman 				goto shipit;
128eae561b3SGarrett Wollman 			}
129eae561b3SGarrett Wollman 		}
130eae561b3SGarrett Wollman 		is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
131eae561b3SGarrett Wollman 		is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
132eae561b3SGarrett Wollman 	shipit:
133eae561b3SGarrett Wollman 		is.sign = vs.sign;
1348360efbdSAlfred Perlstein 		return (XDR_PUTINT32(xdrs, (int32_t *)&is));
135eae561b3SGarrett Wollman #endif
136eae561b3SGarrett Wollman 
137eae561b3SGarrett Wollman 	case XDR_DECODE:
138eae561b3SGarrett Wollman #ifdef IEEEFP
1398360efbdSAlfred Perlstein 		return (XDR_GETINT32(xdrs, (int32_t *)fp));
140eae561b3SGarrett Wollman #else
141eae561b3SGarrett Wollman 		vsp = (struct vax_single *)fp;
1428360efbdSAlfred Perlstein 		if (!XDR_GETINT32(xdrs, (int32_t *)&is))
143eae561b3SGarrett Wollman 			return (FALSE);
144eae561b3SGarrett Wollman 		for (i = 0, lim = sgl_limits;
145eae561b3SGarrett Wollman 			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
146eae561b3SGarrett Wollman 			i++, lim++) {
147eae561b3SGarrett Wollman 			if ((is.exp == lim->ieee.exp) &&
148eae561b3SGarrett Wollman 				(is.mantissa == lim->ieee.mantissa)) {
149eae561b3SGarrett Wollman 				*vsp = lim->s;
150eae561b3SGarrett Wollman 				goto doneit;
151eae561b3SGarrett Wollman 			}
152eae561b3SGarrett Wollman 		}
153eae561b3SGarrett Wollman 		vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
154eae561b3SGarrett Wollman 		vsp->mantissa2 = is.mantissa;
155eae561b3SGarrett Wollman 		vsp->mantissa1 = (is.mantissa >> 16);
156eae561b3SGarrett Wollman 	doneit:
157eae561b3SGarrett Wollman 		vsp->sign = is.sign;
158eae561b3SGarrett Wollman 		return (TRUE);
159eae561b3SGarrett Wollman #endif
160eae561b3SGarrett Wollman 
161eae561b3SGarrett Wollman 	case XDR_FREE:
162eae561b3SGarrett Wollman 		return (TRUE);
163eae561b3SGarrett Wollman 	}
1648360efbdSAlfred Perlstein 	/* NOTREACHED */
165eae561b3SGarrett Wollman 	return (FALSE);
166eae561b3SGarrett Wollman }
167eae561b3SGarrett Wollman 
1688360efbdSAlfred Perlstein #if defined(__vax__)
169eae561b3SGarrett Wollman /* What IEEE double precision floating point looks like on a Vax */
170eae561b3SGarrett Wollman struct	ieee_double {
171eae561b3SGarrett Wollman 	unsigned int	mantissa1 : 20;
172eae561b3SGarrett Wollman 	unsigned int	exp       : 11;
173eae561b3SGarrett Wollman 	unsigned int	sign      : 1;
174eae561b3SGarrett Wollman 	unsigned int	mantissa2 : 32;
175eae561b3SGarrett Wollman };
176eae561b3SGarrett Wollman 
177eae561b3SGarrett Wollman /* Vax double precision floating point */
178eae561b3SGarrett Wollman struct  vax_double {
179eae561b3SGarrett Wollman 	unsigned int	mantissa1 : 7;
180eae561b3SGarrett Wollman 	unsigned int	exp       : 8;
181eae561b3SGarrett Wollman 	unsigned int	sign      : 1;
182eae561b3SGarrett Wollman 	unsigned int	mantissa2 : 16;
183eae561b3SGarrett Wollman 	unsigned int	mantissa3 : 16;
184eae561b3SGarrett Wollman 	unsigned int	mantissa4 : 16;
185eae561b3SGarrett Wollman };
186eae561b3SGarrett Wollman 
187eae561b3SGarrett Wollman #define VAX_DBL_BIAS	0x81
188eae561b3SGarrett Wollman #define IEEE_DBL_BIAS	0x3ff
189eae561b3SGarrett Wollman #define MASK(nbits)	((1 << nbits) - 1)
190eae561b3SGarrett Wollman 
191eae561b3SGarrett Wollman static struct dbl_limits {
192eae561b3SGarrett Wollman 	struct	vax_double d;
193eae561b3SGarrett Wollman 	struct	ieee_double ieee;
194eae561b3SGarrett Wollman } dbl_limits[2] = {
195eae561b3SGarrett Wollman 	{{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },	/* Max Vax */
196eae561b3SGarrett Wollman 	{ 0x0, 0x7ff, 0x0, 0x0 }},			/* Max IEEE */
197eae561b3SGarrett Wollman 	{{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},		/* Min Vax */
198eae561b3SGarrett Wollman 	{ 0x0, 0x0, 0x0, 0x0 }}				/* Min IEEE */
199eae561b3SGarrett Wollman };
200eae561b3SGarrett Wollman 
201eae561b3SGarrett Wollman #endif /* vax */
202eae561b3SGarrett Wollman 
203eae561b3SGarrett Wollman 
204eae561b3SGarrett Wollman bool_t
205d660d38dSCraig Rodrigues xdr_double(XDR *xdrs, double *dp)
206eae561b3SGarrett Wollman {
2071ad08a09SPeter Wemm #ifdef IEEEFP
2088360efbdSAlfred Perlstein 	int32_t *i32p;
2091ad08a09SPeter Wemm 	bool_t rv;
2101ad08a09SPeter Wemm #else
2118360efbdSAlfred Perlstein 	int32_t *lp;
212eae561b3SGarrett Wollman 	struct	ieee_double id;
213eae561b3SGarrett Wollman 	struct	vax_double vd;
2148360efbdSAlfred Perlstein 	struct dbl_limits *lim;
215eae561b3SGarrett Wollman 	int i;
216eae561b3SGarrett Wollman #endif
217eae561b3SGarrett Wollman 
218eae561b3SGarrett Wollman 	switch (xdrs->x_op) {
219eae561b3SGarrett Wollman 
220eae561b3SGarrett Wollman 	case XDR_ENCODE:
221eae561b3SGarrett Wollman #ifdef IEEEFP
2228360efbdSAlfred Perlstein 		i32p = (int32_t *)(void *)dp;
223eae561b3SGarrett Wollman #if BYTE_ORDER == BIG_ENDIAN
2248360efbdSAlfred Perlstein 		rv = XDR_PUTINT32(xdrs, i32p);
2251ad08a09SPeter Wemm 		if (!rv)
2261ad08a09SPeter Wemm 			return (rv);
2278360efbdSAlfred Perlstein 		rv = XDR_PUTINT32(xdrs, i32p+1);
228eae561b3SGarrett Wollman #else
2298360efbdSAlfred Perlstein 		rv = XDR_PUTINT32(xdrs, i32p+1);
2301ad08a09SPeter Wemm 		if (!rv)
2311ad08a09SPeter Wemm 			return (rv);
2328360efbdSAlfred Perlstein 		rv = XDR_PUTINT32(xdrs, i32p);
233eae561b3SGarrett Wollman #endif
2341ad08a09SPeter Wemm 		return (rv);
235eae561b3SGarrett Wollman #else
236eae561b3SGarrett Wollman 		vd = *((struct vax_double *)dp);
237eae561b3SGarrett Wollman 		for (i = 0, lim = dbl_limits;
238eae561b3SGarrett Wollman 			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
239eae561b3SGarrett Wollman 			i++, lim++) {
240eae561b3SGarrett Wollman 			if ((vd.mantissa4 == lim->d.mantissa4) &&
241eae561b3SGarrett Wollman 				(vd.mantissa3 == lim->d.mantissa3) &&
242eae561b3SGarrett Wollman 				(vd.mantissa2 == lim->d.mantissa2) &&
243eae561b3SGarrett Wollman 				(vd.mantissa1 == lim->d.mantissa1) &&
244eae561b3SGarrett Wollman 				(vd.exp == lim->d.exp)) {
245eae561b3SGarrett Wollman 				id = lim->ieee;
246eae561b3SGarrett Wollman 				goto shipit;
247eae561b3SGarrett Wollman 			}
248eae561b3SGarrett Wollman 		}
249eae561b3SGarrett Wollman 		id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
250eae561b3SGarrett Wollman 		id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
251eae561b3SGarrett Wollman 		id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
252eae561b3SGarrett Wollman 				(vd.mantissa3 << 13) |
253eae561b3SGarrett Wollman 				((vd.mantissa4 >> 3) & MASK(13));
254eae561b3SGarrett Wollman 	shipit:
255eae561b3SGarrett Wollman 		id.sign = vd.sign;
2568360efbdSAlfred Perlstein 		lp = (int32_t *)&id;
2578360efbdSAlfred Perlstein 		return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
258eae561b3SGarrett Wollman #endif
259eae561b3SGarrett Wollman 
260eae561b3SGarrett Wollman 	case XDR_DECODE:
261eae561b3SGarrett Wollman #ifdef IEEEFP
2628360efbdSAlfred Perlstein 		i32p = (int32_t *)(void *)dp;
263eae561b3SGarrett Wollman #if BYTE_ORDER == BIG_ENDIAN
2648360efbdSAlfred Perlstein 		rv = XDR_GETINT32(xdrs, i32p);
2651ad08a09SPeter Wemm 		if (!rv)
2661ad08a09SPeter Wemm 			return (rv);
2678360efbdSAlfred Perlstein 		rv = XDR_GETINT32(xdrs, i32p+1);
268eae561b3SGarrett Wollman #else
2698360efbdSAlfred Perlstein 		rv = XDR_GETINT32(xdrs, i32p+1);
2701ad08a09SPeter Wemm 		if (!rv)
2711ad08a09SPeter Wemm 			return (rv);
2728360efbdSAlfred Perlstein 		rv = XDR_GETINT32(xdrs, i32p);
273eae561b3SGarrett Wollman #endif
2741ad08a09SPeter Wemm 		return (rv);
275eae561b3SGarrett Wollman #else
2768360efbdSAlfred Perlstein 		lp = (int32_t *)&id;
2778360efbdSAlfred Perlstein 		if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
278eae561b3SGarrett Wollman 			return (FALSE);
279eae561b3SGarrett Wollman 		for (i = 0, lim = dbl_limits;
280eae561b3SGarrett Wollman 			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
281eae561b3SGarrett Wollman 			i++, lim++) {
282eae561b3SGarrett Wollman 			if ((id.mantissa2 == lim->ieee.mantissa2) &&
283eae561b3SGarrett Wollman 				(id.mantissa1 == lim->ieee.mantissa1) &&
284eae561b3SGarrett Wollman 				(id.exp == lim->ieee.exp)) {
285eae561b3SGarrett Wollman 				vd = lim->d;
286eae561b3SGarrett Wollman 				goto doneit;
287eae561b3SGarrett Wollman 			}
288eae561b3SGarrett Wollman 		}
289eae561b3SGarrett Wollman 		vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
290eae561b3SGarrett Wollman 		vd.mantissa1 = (id.mantissa1 >> 13);
291eae561b3SGarrett Wollman 		vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
292eae561b3SGarrett Wollman 				(id.mantissa2 >> 29);
293eae561b3SGarrett Wollman 		vd.mantissa3 = (id.mantissa2 >> 13);
294eae561b3SGarrett Wollman 		vd.mantissa4 = (id.mantissa2 << 3);
295eae561b3SGarrett Wollman 	doneit:
296eae561b3SGarrett Wollman 		vd.sign = id.sign;
297eae561b3SGarrett Wollman 		*dp = *((double *)&vd);
298eae561b3SGarrett Wollman 		return (TRUE);
299eae561b3SGarrett Wollman #endif
300eae561b3SGarrett Wollman 
301eae561b3SGarrett Wollman 	case XDR_FREE:
302eae561b3SGarrett Wollman 		return (TRUE);
303eae561b3SGarrett Wollman 	}
3048360efbdSAlfred Perlstein 	/* NOTREACHED */
305eae561b3SGarrett Wollman 	return (FALSE);
306eae561b3SGarrett Wollman }
307