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