]>
git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/xdr_float.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
25 /* $NetBSD: xdr_float.c,v 1.23 2000/07/17 04:59:51 matt Exp $ */
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.
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.
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.
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.
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.
51 * Sun Microsystems, Inc.
53 * Mountain View, California 94043
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";
60 #include <sys/cdefs.h>
63 * xdr_float.c, Generic XDR routines implementation.
65 * Copyright (C) 1984, Sun Microsystems, Inc.
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
72 #include <sys/types.h>
73 #include <sys/param.h>
77 #include <rpc/types.h>
82 * This routine works on machines with IEEE754 FP and Vaxen.
85 #if 1 /* Used to be long list of architectures */
86 #include <machine/endian.h>
92 /* What IEEE single precision floating point looks like on a Vax */
94 unsigned int mantissa
: 23;
96 unsigned int sign
: 1;
99 /* Vax single precision floating point */
101 unsigned int mantissa1
: 7;
102 unsigned int exp
: 8;
103 unsigned int sign
: 1;
104 unsigned int mantissa2
: 16;
107 #define VAX_SNG_BIAS 0x81
108 #define IEEE_SNG_BIAS 0x7f
110 static struct sgl_limits
{
112 struct ieee_single ieee
;
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 */
127 struct ieee_single is
;
128 struct vax_single vs
, *vsp
;
129 struct sgl_limits
*lim
;
132 switch (xdrs
->x_op
) {
136 return (XDR_PUTINT32(xdrs
, (int32_t *)fp
));
138 vs
= *((struct vax_single
*)fp
);
139 for (i
= 0, lim
= sgl_limits
;
140 i
< sizeof(sgl_limits
)/sizeof(struct sgl_limits
);
142 if ((vs
.mantissa2
== lim
->s
.mantissa2
) &&
143 (vs
.exp
== lim
->s
.exp
) &&
144 (vs
.mantissa1
== lim
->s
.mantissa1
)) {
149 is
.exp
= vs
.exp
- VAX_SNG_BIAS
+ IEEE_SNG_BIAS
;
150 is
.mantissa
= (vs
.mantissa1
<< 16) | vs
.mantissa2
;
153 return (XDR_PUTINT32(xdrs
, (int32_t *)&is
));
158 return (XDR_GETINT32(xdrs
, (int32_t *)fp
));
160 vsp
= (struct vax_single
*)fp
;
161 if (!XDR_GETINT32(xdrs
, (int32_t *)&is
))
163 for (i
= 0, lim
= sgl_limits
;
164 i
< sizeof(sgl_limits
)/sizeof(struct sgl_limits
);
166 if ((is
.exp
== lim
->ieee
.exp
) &&
167 (is
.mantissa
== lim
->ieee
.mantissa
)) {
172 vsp
->exp
= is
.exp
- IEEE_SNG_BIAS
+ VAX_SNG_BIAS
;
173 vsp
->mantissa2
= is
.mantissa
;
174 vsp
->mantissa1
= (is
.mantissa
>> 16);
188 /* What IEEE double precision floating point looks like on a Vax */
190 unsigned int mantissa1
: 20;
191 unsigned int exp
: 11;
192 unsigned int sign
: 1;
193 unsigned int mantissa2
: 32;
196 /* Vax double precision floating point */
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;
206 #define VAX_DBL_BIAS 0x81
207 #define IEEE_DBL_BIAS 0x3ff
208 #define MASK(nbits) ((1 << nbits) - 1)
210 static struct dbl_limits
{
212 struct ieee_double ieee
;
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 */
233 struct ieee_double id
;
234 struct vax_double vd
;
235 struct dbl_limits
*lim
;
239 switch (xdrs
->x_op
) {
243 i32p
= (int32_t *)(void *)dp
;
244 #if BYTE_ORDER == BIG_ENDIAN
245 rv
= XDR_PUTINT32(xdrs
, i32p
);
248 rv
= XDR_PUTINT32(xdrs
, i32p
+1);
250 rv
= XDR_PUTINT32(xdrs
, i32p
+1);
253 rv
= XDR_PUTINT32(xdrs
, i32p
);
257 vd
= *((struct vax_double
*)dp
);
258 for (i
= 0, lim
= dbl_limits
;
259 i
< sizeof(dbl_limits
)/sizeof(struct dbl_limits
);
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
)) {
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));
278 return (XDR_PUTINT32(xdrs
, lp
++) && XDR_PUTINT32(xdrs
, lp
));
283 i32p
= (int32_t *)(void *)dp
;
284 #if BYTE_ORDER == BIG_ENDIAN
285 rv
= XDR_GETINT32(xdrs
, i32p
);
288 rv
= XDR_GETINT32(xdrs
, i32p
+1);
290 rv
= XDR_GETINT32(xdrs
, i32p
+1);
293 rv
= XDR_GETINT32(xdrs
, i32p
);
298 if (!XDR_GETINT32(xdrs
, lp
++) || !XDR_GETINT32(xdrs
, lp
))
300 for (i
= 0, lim
= dbl_limits
;
301 i
< sizeof(dbl_limits
)/sizeof(struct dbl_limits
);
303 if ((id
.mantissa2
== lim
->ieee
.mantissa2
) &&
304 (id
.mantissa1
== lim
->ieee
.mantissa1
) &&
305 (id
.exp
== lim
->ieee
.exp
)) {
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);
318 *dp
= *((double *)&vd
);