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