xref: /netbsd-src/lib/libc/rpc/xdr_float.c (revision a31739bc0177e74b2925d120ee7e496081371af2)
1 /*	$NetBSD: xdr_float.c,v 1.14 1998/02/10 04:54:59 lukem 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.14 1998/02/10 04:54:59 lukem 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__)
75 #include <machine/endian.h>
76 #define IEEEFP
77 #endif
78 
79 #ifdef 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(xdrs, fp)
112 	XDR *xdrs;
113 	float *fp;
114 {
115 #ifdef IEEEFP
116 	bool_t rv;
117 	int32_t tmpl;
118 #else
119 	struct ieee_single is;
120 	struct vax_single vs, *vsp;
121 	struct sgl_limits *lim;
122 	int i;
123 #endif
124 	switch (xdrs->x_op) {
125 
126 	case XDR_ENCODE:
127 #ifdef IEEEFP
128 		tmpl = *(int32_t *)fp;
129 		return (XDR_PUTLONG(xdrs, &tmpl));
130 #else
131 		vs = *((struct vax_single *)fp);
132 		for (i = 0, lim = sgl_limits;
133 			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
134 			i++, lim++) {
135 			if ((vs.mantissa2 == lim->s.mantissa2) &&
136 				(vs.exp == lim->s.exp) &&
137 				(vs.mantissa1 == lim->s.mantissa1)) {
138 				is = lim->ieee;
139 				goto shipit;
140 			}
141 		}
142 		is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
143 		is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
144 	shipit:
145 		is.sign = vs.sign;
146 		return (XDR_PUTLONG(xdrs, (int32_t *)&is));
147 #endif
148 
149 	case XDR_DECODE:
150 #ifdef IEEEFP
151 		rv = XDR_GETLONG(xdrs, &tmpl);
152 		*(int32_t *)fp = tmpl;
153 		return (rv);
154 #else
155 		vsp = (struct vax_single *)fp;
156 		if (!XDR_GETLONG(xdrs, (int32_t *)&is))
157 			return (FALSE);
158 		for (i = 0, lim = sgl_limits;
159 			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
160 			i++, lim++) {
161 			if ((is.exp == lim->ieee.exp) &&
162 				(is.mantissa == lim->ieee.mantissa)) {
163 				*vsp = lim->s;
164 				goto doneit;
165 			}
166 		}
167 		vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
168 		vsp->mantissa2 = is.mantissa;
169 		vsp->mantissa1 = (is.mantissa >> 16);
170 	doneit:
171 		vsp->sign = is.sign;
172 		return (TRUE);
173 #endif
174 
175 	case XDR_FREE:
176 		return (TRUE);
177 	}
178 	return (FALSE);
179 }
180 
181 #ifdef vax
182 /* What IEEE double precision floating point looks like on a Vax */
183 struct	ieee_double {
184 	unsigned int	mantissa1 : 20;
185 	unsigned int	exp       : 11;
186 	unsigned int	sign      : 1;
187 	unsigned int	mantissa2 : 32;
188 };
189 
190 /* Vax double precision floating point */
191 struct  vax_double {
192 	unsigned int	mantissa1 : 7;
193 	unsigned int	exp       : 8;
194 	unsigned int	sign      : 1;
195 	unsigned int	mantissa2 : 16;
196 	unsigned int	mantissa3 : 16;
197 	unsigned int	mantissa4 : 16;
198 };
199 
200 #define VAX_DBL_BIAS	0x81
201 #define IEEE_DBL_BIAS	0x3ff
202 #define MASK(nbits)	((1 << nbits) - 1)
203 
204 static struct dbl_limits {
205 	struct	vax_double d;
206 	struct	ieee_double ieee;
207 } dbl_limits[2] = {
208 	{{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },	/* Max Vax */
209 	{ 0x0, 0x7ff, 0x0, 0x0 }},			/* Max IEEE */
210 	{{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},		/* Min Vax */
211 	{ 0x0, 0x0, 0x0, 0x0 }}				/* Min IEEE */
212 };
213 
214 #endif /* vax */
215 
216 
217 bool_t
218 xdr_double(xdrs, dp)
219 	XDR *xdrs;
220 	double *dp;
221 {
222 #ifdef IEEEFP
223 	int32_t *i32p;
224 	bool_t rv;
225 	int32_t tmpl;
226 #else
227 	int32_t *lp;
228 	struct	ieee_double id;
229 	struct	vax_double vd;
230 	struct dbl_limits *lim;
231 	int i;
232 #endif
233 
234 	switch (xdrs->x_op) {
235 
236 	case XDR_ENCODE:
237 #ifdef IEEEFP
238 		i32p = (int32_t *)dp;
239 #if BYTE_ORDER == BIG_ENDIAN
240 		tmpl = *i32p++;
241 		rv = XDR_PUTLONG(xdrs, &tmpl);
242 		if (!rv)
243 			return (rv);
244 		tmpl = *i32p;
245 		rv = XDR_PUTLONG(xdrs, &tmpl);
246 #else
247 		tmpl = *(i32p+1);
248 		rv = XDR_PUTLONG(xdrs, &tmpl);
249 		if (!rv)
250 			return (rv);
251 		tmpl = *i32p;
252 		rv = XDR_PUTLONG(xdrs, &tmpl);
253 #endif
254 		return (rv);
255 #else
256 		vd = *((struct vax_double *)dp);
257 		for (i = 0, lim = dbl_limits;
258 			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
259 			i++, lim++) {
260 			if ((vd.mantissa4 == lim->d.mantissa4) &&
261 				(vd.mantissa3 == lim->d.mantissa3) &&
262 				(vd.mantissa2 == lim->d.mantissa2) &&
263 				(vd.mantissa1 == lim->d.mantissa1) &&
264 				(vd.exp == lim->d.exp)) {
265 				id = lim->ieee;
266 				goto shipit;
267 			}
268 		}
269 		id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
270 		id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
271 		id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
272 				(vd.mantissa3 << 13) |
273 				((vd.mantissa4 >> 3) & MASK(13));
274 	shipit:
275 		id.sign = vd.sign;
276 		lp = (int32_t *)&id;
277 		return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
278 #endif
279 
280 	case XDR_DECODE:
281 #ifdef IEEEFP
282 		i32p = (int32_t *)dp;
283 #if BYTE_ORDER == BIG_ENDIAN
284 		rv = XDR_GETLONG(xdrs, &tmpl);
285 		*i32p++ = tmpl;
286 		if (!rv)
287 			return (rv);
288 		rv = XDR_GETLONG(xdrs, &tmpl);
289 		*i32p = tmpl;
290 #else
291 		rv = XDR_GETLONG(xdrs, &tmpl);
292 		*(i32p+1) = tmpl;
293 		if (!rv)
294 			return (rv);
295 		rv = XDR_GETLONG(xdrs, &tmpl);
296 		*i32p = tmpl;
297 #endif
298 		return (rv);
299 #else
300 		lp = (long *)&id;
301 		if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
302 			return (FALSE);
303 		for (i = 0, lim = dbl_limits;
304 			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
305 			i++, lim++) {
306 			if ((id.mantissa2 == lim->ieee.mantissa2) &&
307 				(id.mantissa1 == lim->ieee.mantissa1) &&
308 				(id.exp == lim->ieee.exp)) {
309 				vd = lim->d;
310 				goto doneit;
311 			}
312 		}
313 		vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
314 		vd.mantissa1 = (id.mantissa1 >> 13);
315 		vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
316 				(id.mantissa2 >> 29);
317 		vd.mantissa3 = (id.mantissa2 >> 13);
318 		vd.mantissa4 = (id.mantissa2 << 3);
319 	doneit:
320 		vd.sign = id.sign;
321 		*dp = *((double *)&vd);
322 		return (TRUE);
323 #endif
324 
325 	case XDR_FREE:
326 		return (TRUE);
327 	}
328 	return (FALSE);
329 }
330