]>
Commit | Line | Data |
---|---|---|
03fb6eb0 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
ad21edcc A |
6 | * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights |
7 | * Reserved. This file contains Original Code and/or Modifications of | |
8 | * Original Code as defined in and that are subject to the Apple Public | |
9 | * Source License Version 1.1 (the "License"). You may not use this file | |
10 | * except in compliance with the License. Please obtain a copy of the | |
11 | * License at http://www.apple.com/publicsource and read it before using | |
12 | * this file. | |
03fb6eb0 A |
13 | * |
14 | * The Original Code and all software distributed under the License are | |
ad21edcc | 15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
03fb6eb0 A |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
ad21edcc A |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the |
19 | * License for the specific language governing rights and limitations | |
20 | * under the License. | |
03fb6eb0 A |
21 | * |
22 | * @APPLE_LICENSE_HEADER_END@ | |
23 | */ | |
c29f2fcc A |
24 | |
25 | /* $NetBSD: xdr_float.c,v 1.23 2000/07/17 04:59:51 matt Exp $ */ | |
26 | ||
03fb6eb0 A |
27 | /* |
28 | * Sun RPC is a product of Sun Microsystems, Inc. and is provided for | |
29 | * unrestricted use provided that this legend is included on all tape | |
30 | * media and as a part of the software program in whole or part. Users | |
31 | * may copy or modify Sun RPC without charge, but are not authorized | |
32 | * to license or distribute it to anyone else except as part of a product or | |
33 | * program developed by the user. | |
c29f2fcc | 34 | * |
03fb6eb0 A |
35 | * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE |
36 | * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR | |
37 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. | |
c29f2fcc | 38 | * |
03fb6eb0 A |
39 | * Sun RPC is provided with no support and without any obligation on the |
40 | * part of Sun Microsystems, Inc. to assist in its use, correction, | |
41 | * modification or enhancement. | |
c29f2fcc | 42 | * |
03fb6eb0 A |
43 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE |
44 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC | |
45 | * OR ANY PART THEREOF. | |
c29f2fcc | 46 | * |
03fb6eb0 A |
47 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue |
48 | * or profits or other special, indirect and consequential damages, even if | |
49 | * Sun has been advised of the possibility of such damages. | |
c29f2fcc | 50 | * |
03fb6eb0 A |
51 | * Sun Microsystems, Inc. |
52 | * 2550 Garcia Avenue | |
53 | * Mountain View, California 94043 | |
54 | */ | |
55 | ||
56 | #if defined(LIBC_SCCS) && !defined(lint) | |
c29f2fcc A |
57 | static char *sccsid = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro"; |
58 | static char *sccsid = "@(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC"; | |
03fb6eb0 | 59 | #endif |
c29f2fcc | 60 | #include <sys/cdefs.h> |
03fb6eb0 A |
61 | |
62 | /* | |
c29f2fcc | 63 | * xdr_float.c, Generic XDR routines implementation. |
03fb6eb0 A |
64 | * |
65 | * Copyright (C) 1984, Sun Microsystems, Inc. | |
66 | * | |
67 | * These are the "floating point" xdr routines used to (de)serialize | |
68 | * most common data items. See xdr.h for more info on the interface to | |
69 | * xdr. | |
70 | */ | |
71 | ||
03fb6eb0 A |
72 | #include <sys/types.h> |
73 | #include <sys/param.h> | |
c29f2fcc A |
74 | |
75 | #include <stdio.h> | |
76 | ||
03fb6eb0 A |
77 | #include <rpc/types.h> |
78 | #include <rpc/xdr.h> | |
79 | ||
80 | /* | |
81 | * NB: Not portable. | |
c29f2fcc | 82 | * This routine works on machines with IEEE754 FP and Vaxen. |
03fb6eb0 A |
83 | */ |
84 | ||
c29f2fcc A |
85 | #if 1 /* Used to be long list of architectures */ |
86 | #include <machine/endian.h> | |
03fb6eb0 A |
87 | #define IEEEFP |
88 | #endif | |
89 | ||
c29f2fcc | 90 | #if defined(__vax__) |
03fb6eb0 A |
91 | |
92 | /* What IEEE single precision floating point looks like on a Vax */ | |
93 | struct ieee_single { | |
94 | unsigned int mantissa: 23; | |
95 | unsigned int exp : 8; | |
96 | unsigned int sign : 1; | |
97 | }; | |
98 | ||
99 | /* Vax single precision floating point */ | |
100 | struct vax_single { | |
101 | unsigned int mantissa1 : 7; | |
102 | unsigned int exp : 8; | |
103 | unsigned int sign : 1; | |
104 | unsigned int mantissa2 : 16; | |
105 | }; | |
106 | ||
107 | #define VAX_SNG_BIAS 0x81 | |
108 | #define IEEE_SNG_BIAS 0x7f | |
109 | ||
110 | static struct sgl_limits { | |
111 | struct vax_single s; | |
112 | struct ieee_single ieee; | |
113 | } sgl_limits[2] = { | |
114 | {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */ | |
115 | { 0x0, 0xff, 0x0 }}, /* Max IEEE */ | |
116 | {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */ | |
117 | { 0x0, 0x0, 0x0 }} /* Min IEEE */ | |
118 | }; | |
119 | #endif /* vax */ | |
120 | ||
121 | bool_t | |
122 | xdr_float(xdrs, fp) | |
c29f2fcc A |
123 | XDR *xdrs; |
124 | float *fp; | |
03fb6eb0 A |
125 | { |
126 | #ifndef IEEEFP | |
127 | struct ieee_single is; | |
128 | struct vax_single vs, *vsp; | |
129 | struct sgl_limits *lim; | |
130 | int i; | |
131 | #endif | |
132 | switch (xdrs->x_op) { | |
133 | ||
134 | case XDR_ENCODE: | |
c29f2fcc A |
135 | #ifdef IEEEFP |
136 | return (XDR_PUTINT32(xdrs, (int32_t *)fp)); | |
03fb6eb0 A |
137 | #else |
138 | vs = *((struct vax_single *)fp); | |
139 | for (i = 0, lim = sgl_limits; | |
140 | i < sizeof(sgl_limits)/sizeof(struct sgl_limits); | |
141 | i++, lim++) { | |
142 | if ((vs.mantissa2 == lim->s.mantissa2) && | |
143 | (vs.exp == lim->s.exp) && | |
144 | (vs.mantissa1 == lim->s.mantissa1)) { | |
145 | is = lim->ieee; | |
146 | goto shipit; | |
147 | } | |
148 | } | |
149 | is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS; | |
150 | is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2; | |
151 | shipit: | |
152 | is.sign = vs.sign; | |
c29f2fcc | 153 | return (XDR_PUTINT32(xdrs, (int32_t *)&is)); |
03fb6eb0 A |
154 | #endif |
155 | ||
156 | case XDR_DECODE: | |
157 | #ifdef IEEEFP | |
c29f2fcc | 158 | return (XDR_GETINT32(xdrs, (int32_t *)fp)); |
03fb6eb0 A |
159 | #else |
160 | vsp = (struct vax_single *)fp; | |
c29f2fcc | 161 | if (!XDR_GETINT32(xdrs, (int32_t *)&is)) |
03fb6eb0 A |
162 | return (FALSE); |
163 | for (i = 0, lim = sgl_limits; | |
164 | i < sizeof(sgl_limits)/sizeof(struct sgl_limits); | |
165 | i++, lim++) { | |
166 | if ((is.exp == lim->ieee.exp) && | |
167 | (is.mantissa == lim->ieee.mantissa)) { | |
168 | *vsp = lim->s; | |
169 | goto doneit; | |
170 | } | |
171 | } | |
172 | vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS; | |
173 | vsp->mantissa2 = is.mantissa; | |
174 | vsp->mantissa1 = (is.mantissa >> 16); | |
175 | doneit: | |
176 | vsp->sign = is.sign; | |
177 | return (TRUE); | |
178 | #endif | |
179 | ||
180 | case XDR_FREE: | |
181 | return (TRUE); | |
182 | } | |
c29f2fcc | 183 | /* NOTREACHED */ |
03fb6eb0 A |
184 | return (FALSE); |
185 | } | |
186 | ||
c29f2fcc | 187 | #if defined(__vax__) |
03fb6eb0 A |
188 | /* What IEEE double precision floating point looks like on a Vax */ |
189 | struct ieee_double { | |
190 | unsigned int mantissa1 : 20; | |
191 | unsigned int exp : 11; | |
192 | unsigned int sign : 1; | |
193 | unsigned int mantissa2 : 32; | |
194 | }; | |
195 | ||
196 | /* Vax double precision floating point */ | |
197 | struct vax_double { | |
198 | unsigned int mantissa1 : 7; | |
199 | unsigned int exp : 8; | |
200 | unsigned int sign : 1; | |
201 | unsigned int mantissa2 : 16; | |
202 | unsigned int mantissa3 : 16; | |
203 | unsigned int mantissa4 : 16; | |
204 | }; | |
205 | ||
206 | #define VAX_DBL_BIAS 0x81 | |
207 | #define IEEE_DBL_BIAS 0x3ff | |
208 | #define MASK(nbits) ((1 << nbits) - 1) | |
209 | ||
210 | static struct dbl_limits { | |
211 | struct vax_double d; | |
212 | struct ieee_double ieee; | |
213 | } dbl_limits[2] = { | |
214 | {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */ | |
215 | { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */ | |
216 | {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */ | |
217 | { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */ | |
218 | }; | |
219 | ||
220 | #endif /* vax */ | |
221 | ||
222 | ||
223 | bool_t | |
224 | xdr_double(xdrs, dp) | |
c29f2fcc | 225 | XDR *xdrs; |
03fb6eb0 A |
226 | double *dp; |
227 | { | |
c29f2fcc A |
228 | #ifdef IEEEFP |
229 | int32_t *i32p; | |
230 | bool_t rv; | |
231 | #else | |
232 | int32_t *lp; | |
03fb6eb0 A |
233 | struct ieee_double id; |
234 | struct vax_double vd; | |
c29f2fcc | 235 | struct dbl_limits *lim; |
03fb6eb0 A |
236 | int i; |
237 | #endif | |
238 | ||
239 | switch (xdrs->x_op) { | |
240 | ||
241 | case XDR_ENCODE: | |
242 | #ifdef IEEEFP | |
c29f2fcc | 243 | i32p = (int32_t *)(void *)dp; |
03fb6eb0 | 244 | #if BYTE_ORDER == BIG_ENDIAN |
c29f2fcc A |
245 | rv = XDR_PUTINT32(xdrs, i32p); |
246 | if (!rv) | |
247 | return (rv); | |
248 | rv = XDR_PUTINT32(xdrs, i32p+1); | |
03fb6eb0 | 249 | #else |
c29f2fcc A |
250 | rv = XDR_PUTINT32(xdrs, i32p+1); |
251 | if (!rv) | |
252 | return (rv); | |
253 | rv = XDR_PUTINT32(xdrs, i32p); | |
03fb6eb0 | 254 | #endif |
c29f2fcc | 255 | return (rv); |
03fb6eb0 A |
256 | #else |
257 | vd = *((struct vax_double *)dp); | |
258 | for (i = 0, lim = dbl_limits; | |
259 | i < sizeof(dbl_limits)/sizeof(struct dbl_limits); | |
260 | i++, lim++) { | |
261 | if ((vd.mantissa4 == lim->d.mantissa4) && | |
262 | (vd.mantissa3 == lim->d.mantissa3) && | |
263 | (vd.mantissa2 == lim->d.mantissa2) && | |
264 | (vd.mantissa1 == lim->d.mantissa1) && | |
265 | (vd.exp == lim->d.exp)) { | |
266 | id = lim->ieee; | |
267 | goto shipit; | |
268 | } | |
269 | } | |
270 | id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS; | |
271 | id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3); | |
272 | id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) | | |
273 | (vd.mantissa3 << 13) | | |
274 | ((vd.mantissa4 >> 3) & MASK(13)); | |
275 | shipit: | |
276 | id.sign = vd.sign; | |
c29f2fcc A |
277 | lp = (int32_t *)&id; |
278 | return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp)); | |
03fb6eb0 A |
279 | #endif |
280 | ||
281 | case XDR_DECODE: | |
282 | #ifdef IEEEFP | |
c29f2fcc | 283 | i32p = (int32_t *)(void *)dp; |
03fb6eb0 | 284 | #if BYTE_ORDER == BIG_ENDIAN |
c29f2fcc A |
285 | rv = XDR_GETINT32(xdrs, i32p); |
286 | if (!rv) | |
287 | return (rv); | |
288 | rv = XDR_GETINT32(xdrs, i32p+1); | |
03fb6eb0 | 289 | #else |
c29f2fcc A |
290 | rv = XDR_GETINT32(xdrs, i32p+1); |
291 | if (!rv) | |
292 | return (rv); | |
293 | rv = XDR_GETINT32(xdrs, i32p); | |
03fb6eb0 | 294 | #endif |
c29f2fcc | 295 | return (rv); |
03fb6eb0 | 296 | #else |
c29f2fcc A |
297 | lp = (int32_t *)&id; |
298 | if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp)) | |
03fb6eb0 A |
299 | return (FALSE); |
300 | for (i = 0, lim = dbl_limits; | |
301 | i < sizeof(dbl_limits)/sizeof(struct dbl_limits); | |
302 | i++, lim++) { | |
303 | if ((id.mantissa2 == lim->ieee.mantissa2) && | |
304 | (id.mantissa1 == lim->ieee.mantissa1) && | |
305 | (id.exp == lim->ieee.exp)) { | |
306 | vd = lim->d; | |
307 | goto doneit; | |
308 | } | |
309 | } | |
310 | vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS; | |
311 | vd.mantissa1 = (id.mantissa1 >> 13); | |
312 | vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) | | |
313 | (id.mantissa2 >> 29); | |
314 | vd.mantissa3 = (id.mantissa2 >> 13); | |
315 | vd.mantissa4 = (id.mantissa2 << 3); | |
316 | doneit: | |
317 | vd.sign = id.sign; | |
318 | *dp = *((double *)&vd); | |
319 | return (TRUE); | |
320 | #endif | |
321 | ||
322 | case XDR_FREE: | |
323 | return (TRUE); | |
324 | } | |
c29f2fcc | 325 | /* NOTREACHED */ |
03fb6eb0 A |
326 | return (FALSE); |
327 | } |