]> git.saurik.com Git - apple/libinfo.git/blame_incremental - rpc.subproj/xdr_float.c
Libinfo-459.40.1.tar.gz
[apple/libinfo.git] / rpc.subproj / xdr_float.c
... / ...
CommitLineData
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/* $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)
57static char *sccsid = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
58static char *sccsid = "@(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC";
59#endif
60#include <sys/cdefs.h>
61
62/*
63 * xdr_float.c, Generic XDR routines implementation.
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
72#include <sys/types.h>
73#include <sys/param.h>
74
75#include <stdio.h>
76
77#include <rpc/types.h>
78#include <rpc/xdr.h>
79
80/*
81 * NB: Not portable.
82 * This routine works on machines with IEEE754 FP and Vaxen.
83 */
84
85#if 1 /* Used to be long list of architectures */
86#include <machine/endian.h>
87#define IEEEFP
88#endif
89
90#if defined(__vax__)
91
92/* What IEEE single precision floating point looks like on a Vax */
93struct 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 */
100struct 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
110static 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
121bool_t
122xdr_float(xdrs, fp)
123 XDR *xdrs;
124 float *fp;
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:
135#ifdef IEEEFP
136 return (XDR_PUTINT32(xdrs, (int32_t *)fp));
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;
153 return (XDR_PUTINT32(xdrs, (int32_t *)&is));
154#endif
155
156 case XDR_DECODE:
157#ifdef IEEEFP
158 return (XDR_GETINT32(xdrs, (int32_t *)fp));
159#else
160 vsp = (struct vax_single *)fp;
161 if (!XDR_GETINT32(xdrs, (int32_t *)&is))
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 }
183 /* NOTREACHED */
184 return (FALSE);
185}
186
187#if defined(__vax__)
188/* What IEEE double precision floating point looks like on a Vax */
189struct 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 */
197struct 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
210static 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
223bool_t
224xdr_double(xdrs, dp)
225 XDR *xdrs;
226 double *dp;
227{
228#ifdef IEEEFP
229 int32_t *i32p;
230 bool_t rv;
231#else
232 int32_t *lp;
233 struct ieee_double id;
234 struct vax_double vd;
235 struct dbl_limits *lim;
236 int i;
237#endif
238
239 switch (xdrs->x_op) {
240
241 case XDR_ENCODE:
242#ifdef IEEEFP
243 i32p = (int32_t *)(void *)dp;
244#if BYTE_ORDER == BIG_ENDIAN
245 rv = XDR_PUTINT32(xdrs, i32p);
246 if (!rv)
247 return (rv);
248 rv = XDR_PUTINT32(xdrs, i32p+1);
249#else
250 rv = XDR_PUTINT32(xdrs, i32p+1);
251 if (!rv)
252 return (rv);
253 rv = XDR_PUTINT32(xdrs, i32p);
254#endif
255 return (rv);
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;
277 lp = (int32_t *)&id;
278 return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
279#endif
280
281 case XDR_DECODE:
282#ifdef IEEEFP
283 i32p = (int32_t *)(void *)dp;
284#if BYTE_ORDER == BIG_ENDIAN
285 rv = XDR_GETINT32(xdrs, i32p);
286 if (!rv)
287 return (rv);
288 rv = XDR_GETINT32(xdrs, i32p+1);
289#else
290 rv = XDR_GETINT32(xdrs, i32p+1);
291 if (!rv)
292 return (rv);
293 rv = XDR_GETINT32(xdrs, i32p);
294#endif
295 return (rv);
296#else
297 lp = (int32_t *)&id;
298 if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
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 }
325 /* NOTREACHED */
326 return (FALSE);
327}