]>
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 * 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.
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.
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.
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.
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.
48 * Sun Microsystems, Inc.
50 * Mountain View, California 94043
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 $";
60 * xdr_float.c, Generic XDR routines impelmentation.
62 * Copyright (C) 1984, Sun Microsystems, Inc.
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
70 #include <sys/types.h>
71 #include <sys/param.h>
72 #include <rpc/types.h>
77 * This routine works on Suns (Sky / 68000's), i386's, MIPS, NS32k and Vaxen.
80 #if defined(mc68000)||defined(sparc)||defined(i386)||defined(mips)||defined(ns32k)||defined(__APPLE__)
86 /* What IEEE single precision floating point looks like on a Vax */
88 unsigned int mantissa
: 23;
90 unsigned int sign
: 1;
93 /* Vax single precision floating point */
95 unsigned int mantissa1
: 7;
97 unsigned int sign
: 1;
98 unsigned int mantissa2
: 16;
101 #define VAX_SNG_BIAS 0x81
102 #define IEEE_SNG_BIAS 0x7f
104 static struct sgl_limits
{
106 struct ieee_single ieee
;
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 */
121 struct ieee_single is
;
122 struct vax_single vs
, *vsp
;
123 struct sgl_limits
*lim
;
126 switch (xdrs
->x_op
) {
130 return (XDR_PUTLONG(xdrs
, (long *)fp
));
132 vs
= *((struct vax_single
*)fp
);
133 for (i
= 0, lim
= sgl_limits
;
134 i
< sizeof(sgl_limits
)/sizeof(struct sgl_limits
);
136 if ((vs
.mantissa2
== lim
->s
.mantissa2
) &&
137 (vs
.exp
== lim
->s
.exp
) &&
138 (vs
.mantissa1
== lim
->s
.mantissa1
)) {
143 is
.exp
= vs
.exp
- VAX_SNG_BIAS
+ IEEE_SNG_BIAS
;
144 is
.mantissa
= (vs
.mantissa1
<< 16) | vs
.mantissa2
;
147 return (XDR_PUTLONG(xdrs
, (long *)&is
));
152 return (XDR_GETLONG(xdrs
, (long *)fp
));
154 vsp
= (struct vax_single
*)fp
;
155 if (!XDR_GETLONG(xdrs
, (long *)&is
))
157 for (i
= 0, lim
= sgl_limits
;
158 i
< sizeof(sgl_limits
)/sizeof(struct sgl_limits
);
160 if ((is
.exp
== lim
->ieee
.exp
) &&
161 (is
.mantissa
== lim
->ieee
.mantissa
)) {
166 vsp
->exp
= is
.exp
- IEEE_SNG_BIAS
+ VAX_SNG_BIAS
;
167 vsp
->mantissa2
= is
.mantissa
;
168 vsp
->mantissa1
= (is
.mantissa
>> 16);
181 * This routine works on Suns (Sky / 68000's), i386's, MIPS and Vaxen.
185 /* What IEEE double precision floating point looks like on a Vax */
187 unsigned int mantissa1
: 20;
188 unsigned int exp
: 11;
189 unsigned int sign
: 1;
190 unsigned int mantissa2
: 32;
193 /* Vax double precision floating point */
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;
203 #define VAX_DBL_BIAS 0x81
204 #define IEEE_DBL_BIAS 0x3ff
205 #define MASK(nbits) ((1 << nbits) - 1)
207 static struct dbl_limits
{
209 struct ieee_double ieee
;
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 */
227 struct ieee_double id
;
228 struct vax_double vd
;
229 register struct dbl_limits
*lim
;
233 switch (xdrs
->x_op
) {
238 #if BYTE_ORDER == BIG_ENDIAN
239 return (XDR_PUTLONG(xdrs
, lp
++) && XDR_PUTLONG(xdrs
, lp
));
241 return (XDR_PUTLONG(xdrs
, lp
+1) && XDR_PUTLONG(xdrs
, lp
));
244 vd
= *((struct vax_double
*)dp
);
245 for (i
= 0, lim
= dbl_limits
;
246 i
< sizeof(dbl_limits
)/sizeof(struct dbl_limits
);
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
)) {
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));
265 return (XDR_PUTLONG(xdrs
, lp
++) && XDR_PUTLONG(xdrs
, lp
));
271 #if BYTE_ORDER == BIG_ENDIAN
272 return (XDR_GETLONG(xdrs
, lp
++) && XDR_GETLONG(xdrs
, lp
));
274 return (XDR_GETLONG(xdrs
, lp
+1) && XDR_GETLONG(xdrs
, lp
));
278 if (!XDR_GETLONG(xdrs
, lp
++) || !XDR_GETLONG(xdrs
, lp
))
280 for (i
= 0, lim
= dbl_limits
;
281 i
< sizeof(dbl_limits
)/sizeof(struct dbl_limits
);
283 if ((id
.mantissa2
== lim
->ieee
.mantissa2
) &&
284 (id
.mantissa1
== lim
->ieee
.mantissa1
) &&
285 (id
.exp
== lim
->ieee
.exp
)) {
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);
298 *dp
= *((double *)&vd
);