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