xref: /netbsd-src/lib/libc/rpc/xdr_float.c (revision c63c52b238fd8e7e280488dccb1b8ba9adc4308b)
1 /*	$NetBSD: xdr_float.c,v 1.12 1997/07/13 20:13:30 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.12 1997/07/13 20:13:30 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 <stdio.h>
53 #include <sys/types.h>
54 #include <sys/param.h>
55 #include <rpc/types.h>
56 #include <rpc/xdr.h>
57 
58 /*
59  * NB: Not portable.
60  * This routine works on machines with IEEE754 FP and Vaxen.
61  */
62 
63 #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
64     defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
65     defined(__arm32__) || defined(__powerpc__)
66 #include <machine/endian.h>
67 #define IEEEFP
68 #endif
69 
70 #ifdef vax
71 
72 /* What IEEE single precision floating point looks like on a Vax */
73 struct	ieee_single {
74 	unsigned int	mantissa: 23;
75 	unsigned int	exp     : 8;
76 	unsigned int	sign    : 1;
77 };
78 
79 /* Vax single precision floating point */
80 struct	vax_single {
81 	unsigned int	mantissa1 : 7;
82 	unsigned int	exp       : 8;
83 	unsigned int	sign      : 1;
84 	unsigned int	mantissa2 : 16;
85 };
86 
87 #define VAX_SNG_BIAS	0x81
88 #define IEEE_SNG_BIAS	0x7f
89 
90 static struct sgl_limits {
91 	struct vax_single s;
92 	struct ieee_single ieee;
93 } sgl_limits[2] = {
94 	{{ 0x7f, 0xff, 0x0, 0xffff },	/* Max Vax */
95 	{ 0x0, 0xff, 0x0 }},		/* Max IEEE */
96 	{{ 0x0, 0x0, 0x0, 0x0 },	/* Min Vax */
97 	{ 0x0, 0x0, 0x0 }}		/* Min IEEE */
98 };
99 #endif /* vax */
100 
101 bool_t
102 xdr_float(xdrs, fp)
103 	register XDR *xdrs;
104 	register float *fp;
105 {
106 #ifdef IEEEFP
107 	bool_t rv;
108 	long tmpl;
109 #else
110 	struct ieee_single is;
111 	struct vax_single vs, *vsp;
112 	struct sgl_limits *lim;
113 	int i;
114 #endif
115 	switch (xdrs->x_op) {
116 
117 	case XDR_ENCODE:
118 #ifdef IEEEFP
119 		tmpl = *(int32_t *)fp;
120 		return (XDR_PUTLONG(xdrs, &tmpl));
121 #else
122 		vs = *((struct vax_single *)fp);
123 		for (i = 0, lim = sgl_limits;
124 			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
125 			i++, lim++) {
126 			if ((vs.mantissa2 == lim->s.mantissa2) &&
127 				(vs.exp == lim->s.exp) &&
128 				(vs.mantissa1 == lim->s.mantissa1)) {
129 				is = lim->ieee;
130 				goto shipit;
131 			}
132 		}
133 		is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
134 		is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
135 	shipit:
136 		is.sign = vs.sign;
137 		return (XDR_PUTLONG(xdrs, (long *)&is));
138 #endif
139 
140 	case XDR_DECODE:
141 #ifdef IEEEFP
142 		rv = XDR_GETLONG(xdrs, &tmpl);
143 		*(int32_t *)fp = tmpl;
144 		return (rv);
145 #else
146 		vsp = (struct vax_single *)fp;
147 		if (!XDR_GETLONG(xdrs, (long *)&is))
148 			return (FALSE);
149 		for (i = 0, lim = sgl_limits;
150 			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
151 			i++, lim++) {
152 			if ((is.exp == lim->ieee.exp) &&
153 				(is.mantissa == lim->ieee.mantissa)) {
154 				*vsp = lim->s;
155 				goto doneit;
156 			}
157 		}
158 		vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
159 		vsp->mantissa2 = is.mantissa;
160 		vsp->mantissa1 = (is.mantissa >> 16);
161 	doneit:
162 		vsp->sign = is.sign;
163 		return (TRUE);
164 #endif
165 
166 	case XDR_FREE:
167 		return (TRUE);
168 	}
169 	return (FALSE);
170 }
171 
172 #ifdef vax
173 /* What IEEE double precision floating point looks like on a Vax */
174 struct	ieee_double {
175 	unsigned int	mantissa1 : 20;
176 	unsigned int	exp       : 11;
177 	unsigned int	sign      : 1;
178 	unsigned int	mantissa2 : 32;
179 };
180 
181 /* Vax double precision floating point */
182 struct  vax_double {
183 	unsigned int	mantissa1 : 7;
184 	unsigned int	exp       : 8;
185 	unsigned int	sign      : 1;
186 	unsigned int	mantissa2 : 16;
187 	unsigned int	mantissa3 : 16;
188 	unsigned int	mantissa4 : 16;
189 };
190 
191 #define VAX_DBL_BIAS	0x81
192 #define IEEE_DBL_BIAS	0x3ff
193 #define MASK(nbits)	((1 << nbits) - 1)
194 
195 static struct dbl_limits {
196 	struct	vax_double d;
197 	struct	ieee_double ieee;
198 } dbl_limits[2] = {
199 	{{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },	/* Max Vax */
200 	{ 0x0, 0x7ff, 0x0, 0x0 }},			/* Max IEEE */
201 	{{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},		/* Min Vax */
202 	{ 0x0, 0x0, 0x0, 0x0 }}				/* Min IEEE */
203 };
204 
205 #endif /* vax */
206 
207 
208 bool_t
209 xdr_double(xdrs, dp)
210 	register XDR *xdrs;
211 	double *dp;
212 {
213 #ifdef IEEEFP
214 	register int32_t *i32p;
215 	bool_t rv;
216 	long tmpl;
217 #else
218 	register long *lp;
219 	struct	ieee_double id;
220 	struct	vax_double vd;
221 	register struct dbl_limits *lim;
222 	int i;
223 #endif
224 
225 	switch (xdrs->x_op) {
226 
227 	case XDR_ENCODE:
228 #ifdef IEEEFP
229 		i32p = (int32_t *)dp;
230 #if BYTE_ORDER == BIG_ENDIAN
231 		tmpl = *i32p++;
232 		rv = XDR_PUTLONG(xdrs, &tmpl);
233 		if (!rv)
234 			return (rv);
235 		tmpl = *i32p;
236 		rv = XDR_PUTLONG(xdrs, &tmpl);
237 #else
238 		tmpl = *(i32p+1);
239 		rv = XDR_PUTLONG(xdrs, &tmpl);
240 		if (!rv)
241 			return (rv);
242 		tmpl = *i32p;
243 		rv = XDR_PUTLONG(xdrs, &tmpl);
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 = (long *)&id;
268 		return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
269 #endif
270 
271 	case XDR_DECODE:
272 #ifdef IEEEFP
273 		i32p = (int32_t *)dp;
274 #if BYTE_ORDER == BIG_ENDIAN
275 		rv = XDR_GETLONG(xdrs, &tmpl);
276 		*i32p++ = tmpl;
277 		if (!rv)
278 			return (rv);
279 		rv = XDR_GETLONG(xdrs, &tmpl);
280 		*i32p = tmpl;
281 #else
282 		rv = XDR_GETLONG(xdrs, &tmpl);
283 		*(i32p+1) = tmpl;
284 		if (!rv)
285 			return (rv);
286 		rv = XDR_GETLONG(xdrs, &tmpl);
287 		*i32p = tmpl;
288 #endif
289 		return (rv);
290 #else
291 		lp = (long *)&id;
292 		if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
293 			return (FALSE);
294 		for (i = 0, lim = dbl_limits;
295 			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
296 			i++, lim++) {
297 			if ((id.mantissa2 == lim->ieee.mantissa2) &&
298 				(id.mantissa1 == lim->ieee.mantissa1) &&
299 				(id.exp == lim->ieee.exp)) {
300 				vd = lim->d;
301 				goto doneit;
302 			}
303 		}
304 		vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
305 		vd.mantissa1 = (id.mantissa1 >> 13);
306 		vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
307 				(id.mantissa2 >> 29);
308 		vd.mantissa3 = (id.mantissa2 >> 13);
309 		vd.mantissa4 = (id.mantissa2 << 3);
310 	doneit:
311 		vd.sign = id.sign;
312 		*dp = *((double *)&vd);
313 		return (TRUE);
314 #endif
315 
316 	case XDR_FREE:
317 		return (TRUE);
318 	}
319 	return (FALSE);
320 }
321