xref: /freebsd/lib/libc/xdr/xdr_float.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*	$NetBSD: xdr_float.c,v 1.23 2000/07/17 04:59:51 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 #if defined(LIBC_SCCS) && !defined(lint)
35 static char *sccsid2 = "@(#)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 #endif
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 /*
42  * xdr_float.c, Generic XDR routines implementation.
43  *
44  * These are the "floating point" xdr routines used to (de)serialize
45  * most common data items.  See xdr.h for more info on the interface to
46  * xdr.
47  */
48 
49 #include "namespace.h"
50 #include <sys/types.h>
51 #include <sys/param.h>
52 
53 #include <stdio.h>
54 
55 #include <rpc/types.h>
56 #include <rpc/xdr.h>
57 #include "un-namespace.h"
58 
59 /*
60  * NB: Not portable.
61  * This routine works on machines with IEEE754 FP and Vaxen.
62  */
63 
64 #include <machine/endian.h>
65 #define IEEEFP
66 
67 #if defined(__vax__)
68 
69 /* What IEEE single precision floating point looks like on a Vax */
70 struct	ieee_single {
71 	unsigned int	mantissa: 23;
72 	unsigned int	exp     : 8;
73 	unsigned int	sign    : 1;
74 };
75 
76 /* Vax single precision floating point */
77 struct	vax_single {
78 	unsigned int	mantissa1 : 7;
79 	unsigned int	exp       : 8;
80 	unsigned int	sign      : 1;
81 	unsigned int	mantissa2 : 16;
82 };
83 
84 #define VAX_SNG_BIAS	0x81
85 #define IEEE_SNG_BIAS	0x7f
86 
87 static struct sgl_limits {
88 	struct vax_single s;
89 	struct ieee_single ieee;
90 } sgl_limits[2] = {
91 	{{ 0x7f, 0xff, 0x0, 0xffff },	/* Max Vax */
92 	{ 0x0, 0xff, 0x0 }},		/* Max IEEE */
93 	{{ 0x0, 0x0, 0x0, 0x0 },	/* Min Vax */
94 	{ 0x0, 0x0, 0x0 }}		/* Min IEEE */
95 };
96 #endif /* vax */
97 
98 bool_t
99 xdr_float(XDR *xdrs, float *fp)
100 {
101 #ifndef IEEEFP
102 	struct ieee_single is;
103 	struct vax_single vs, *vsp;
104 	struct sgl_limits *lim;
105 	int i;
106 #endif
107 	switch (xdrs->x_op) {
108 
109 	case XDR_ENCODE:
110 #ifdef IEEEFP
111 		return (XDR_PUTINT32(xdrs, (int32_t *)fp));
112 #else
113 		vs = *((struct vax_single *)fp);
114 		for (i = 0, lim = sgl_limits;
115 			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
116 			i++, lim++) {
117 			if ((vs.mantissa2 == lim->s.mantissa2) &&
118 				(vs.exp == lim->s.exp) &&
119 				(vs.mantissa1 == lim->s.mantissa1)) {
120 				is = lim->ieee;
121 				goto shipit;
122 			}
123 		}
124 		is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
125 		is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
126 	shipit:
127 		is.sign = vs.sign;
128 		return (XDR_PUTINT32(xdrs, (int32_t *)&is));
129 #endif
130 
131 	case XDR_DECODE:
132 #ifdef IEEEFP
133 		return (XDR_GETINT32(xdrs, (int32_t *)fp));
134 #else
135 		vsp = (struct vax_single *)fp;
136 		if (!XDR_GETINT32(xdrs, (int32_t *)&is))
137 			return (FALSE);
138 		for (i = 0, lim = sgl_limits;
139 			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
140 			i++, lim++) {
141 			if ((is.exp == lim->ieee.exp) &&
142 				(is.mantissa == lim->ieee.mantissa)) {
143 				*vsp = lim->s;
144 				goto doneit;
145 			}
146 		}
147 		vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
148 		vsp->mantissa2 = is.mantissa;
149 		vsp->mantissa1 = (is.mantissa >> 16);
150 	doneit:
151 		vsp->sign = is.sign;
152 		return (TRUE);
153 #endif
154 
155 	case XDR_FREE:
156 		return (TRUE);
157 	}
158 	/* NOTREACHED */
159 	return (FALSE);
160 }
161 
162 #if defined(__vax__)
163 /* What IEEE double precision floating point looks like on a Vax */
164 struct	ieee_double {
165 	unsigned int	mantissa1 : 20;
166 	unsigned int	exp       : 11;
167 	unsigned int	sign      : 1;
168 	unsigned int	mantissa2 : 32;
169 };
170 
171 /* Vax double precision floating point */
172 struct  vax_double {
173 	unsigned int	mantissa1 : 7;
174 	unsigned int	exp       : 8;
175 	unsigned int	sign      : 1;
176 	unsigned int	mantissa2 : 16;
177 	unsigned int	mantissa3 : 16;
178 	unsigned int	mantissa4 : 16;
179 };
180 
181 #define VAX_DBL_BIAS	0x81
182 #define IEEE_DBL_BIAS	0x3ff
183 #define MASK(nbits)	((1 << nbits) - 1)
184 
185 static struct dbl_limits {
186 	struct	vax_double d;
187 	struct	ieee_double ieee;
188 } dbl_limits[2] = {
189 	{{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },	/* Max Vax */
190 	{ 0x0, 0x7ff, 0x0, 0x0 }},			/* Max IEEE */
191 	{{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},		/* Min Vax */
192 	{ 0x0, 0x0, 0x0, 0x0 }}				/* Min IEEE */
193 };
194 
195 #endif /* vax */
196 
197 
198 bool_t
199 xdr_double(XDR *xdrs, double *dp)
200 {
201 #ifdef IEEEFP
202 	int32_t *i32p;
203 	bool_t rv;
204 #else
205 	int32_t *lp;
206 	struct	ieee_double id;
207 	struct	vax_double vd;
208 	struct dbl_limits *lim;
209 	int i;
210 #endif
211 
212 	switch (xdrs->x_op) {
213 
214 	case XDR_ENCODE:
215 #ifdef IEEEFP
216 		i32p = (int32_t *)(void *)dp;
217 #if BYTE_ORDER == BIG_ENDIAN
218 		rv = XDR_PUTINT32(xdrs, i32p);
219 		if (!rv)
220 			return (rv);
221 		rv = XDR_PUTINT32(xdrs, i32p+1);
222 #else
223 		rv = XDR_PUTINT32(xdrs, i32p+1);
224 		if (!rv)
225 			return (rv);
226 		rv = XDR_PUTINT32(xdrs, i32p);
227 #endif
228 		return (rv);
229 #else
230 		vd = *((struct vax_double *)dp);
231 		for (i = 0, lim = dbl_limits;
232 			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
233 			i++, lim++) {
234 			if ((vd.mantissa4 == lim->d.mantissa4) &&
235 				(vd.mantissa3 == lim->d.mantissa3) &&
236 				(vd.mantissa2 == lim->d.mantissa2) &&
237 				(vd.mantissa1 == lim->d.mantissa1) &&
238 				(vd.exp == lim->d.exp)) {
239 				id = lim->ieee;
240 				goto shipit;
241 			}
242 		}
243 		id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
244 		id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
245 		id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
246 				(vd.mantissa3 << 13) |
247 				((vd.mantissa4 >> 3) & MASK(13));
248 	shipit:
249 		id.sign = vd.sign;
250 		lp = (int32_t *)&id;
251 		return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
252 #endif
253 
254 	case XDR_DECODE:
255 #ifdef IEEEFP
256 		i32p = (int32_t *)(void *)dp;
257 #if BYTE_ORDER == BIG_ENDIAN
258 		rv = XDR_GETINT32(xdrs, i32p);
259 		if (!rv)
260 			return (rv);
261 		rv = XDR_GETINT32(xdrs, i32p+1);
262 #else
263 		rv = XDR_GETINT32(xdrs, i32p+1);
264 		if (!rv)
265 			return (rv);
266 		rv = XDR_GETINT32(xdrs, i32p);
267 #endif
268 		return (rv);
269 #else
270 		lp = (int32_t *)&id;
271 		if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
272 			return (FALSE);
273 		for (i = 0, lim = dbl_limits;
274 			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
275 			i++, lim++) {
276 			if ((id.mantissa2 == lim->ieee.mantissa2) &&
277 				(id.mantissa1 == lim->ieee.mantissa1) &&
278 				(id.exp == lim->ieee.exp)) {
279 				vd = lim->d;
280 				goto doneit;
281 			}
282 		}
283 		vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
284 		vd.mantissa1 = (id.mantissa1 >> 13);
285 		vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
286 				(id.mantissa2 >> 29);
287 		vd.mantissa3 = (id.mantissa2 >> 13);
288 		vd.mantissa4 = (id.mantissa2 << 3);
289 	doneit:
290 		vd.sign = id.sign;
291 		*dp = *((double *)&vd);
292 		return (TRUE);
293 #endif
294 
295 	case XDR_FREE:
296 		return (TRUE);
297 	}
298 	/* NOTREACHED */
299 	return (FALSE);
300 }
301