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