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