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