1 /* @(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
35 * xdr_float.c, Generic XDR routines impelmentation.
37 * Copyright (C) 1984, Sun Microsystems, Inc.
39 * These are the "floating point" xdr routines used to (de)serialize
40 * most common data items. See xdr.h for more info on the interface to
46 #include <rpc/types.h>
49 #if defined(__CYGWIN32__) || defined(__MINGW32__)
55 * This routine works on Suns (Sky / 68000's) and Vaxen.
60 /* What IEEE single precision floating point looks like on a Vax */
62 unsigned int mantissa: 23;
64 unsigned int sign : 1;
67 /* Vax single precision floating point */
69 unsigned int mantissa1 : 7;
71 unsigned int sign : 1;
72 unsigned int mantissa2 : 16;
75 #define VAX_SNG_BIAS 0x81
76 #define IEEE_SNG_BIAS 0x7f
78 static struct sgl_limits {
80 struct ieee_single ieee;
82 {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
83 { 0x0, 0xff, 0x0 }}, /* Max IEEE */
84 {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
85 { 0x0, 0x0, 0x0 }} /* Min IEEE */
95 struct ieee_single is;
96 struct vax_single vs, *vsp;
97 struct sgl_limits *lim;
100 switch (xdrs->x_op) {
104 return (XDR_PUTLONG(xdrs, (long *)fp));
106 vs = *((struct vax_single *)fp);
107 for (i = 0, lim = sgl_limits;
108 i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
110 if ((vs.mantissa2 == lim->s.mantissa2) &&
111 (vs.exp == lim->s.exp) &&
112 (vs.mantissa1 == lim->s.mantissa1)) {
117 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
118 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
121 return (XDR_PUTLONG(xdrs, (long *)(void *)&is));
126 return (XDR_GETLONG(xdrs, (long *)fp));
128 vsp = (struct vax_single *)fp;
129 if (!XDR_GETLONG(xdrs, (long *)(void *)&is))
131 for (i = 0, lim = sgl_limits;
132 i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
134 if ((is.exp == lim->ieee.exp) &&
135 (is.mantissa == lim->ieee.mantissa)) {
140 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
141 vsp->mantissa2 = is.mantissa;
142 vsp->mantissa1 = (is.mantissa >> 16);
155 * This routine works on Suns (Sky / 68000's) and Vaxen.
159 /* What IEEE double precision floating point looks like on a Vax */
161 unsigned int mantissa1 : 20;
162 unsigned int exp : 11;
163 unsigned int sign : 1;
164 unsigned int mantissa2 : 32;
167 /* Vax double precision floating point */
169 unsigned int mantissa1 : 7;
170 unsigned int exp : 8;
171 unsigned int sign : 1;
172 unsigned int mantissa2 : 16;
173 unsigned int mantissa3 : 16;
174 unsigned int mantissa4 : 16;
177 #define VAX_DBL_BIAS 0x81
178 #define IEEE_DBL_BIAS 0x3ff
179 #define MASK(nbits) ((1 << nbits) - 1)
181 static struct dbl_limits {
183 struct ieee_double ieee;
185 {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
186 { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
187 {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
188 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
201 struct ieee_double id;
202 struct vax_double vd;
203 register struct dbl_limits *lim;
207 switch (xdrs->x_op) {
213 vd = *((struct vax_double *)dp);
214 for (i = 0, lim = dbl_limits;
215 i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
217 if ((vd.mantissa4 == lim->d.mantissa4) &&
218 (vd.mantissa3 == lim->d.mantissa3) &&
219 (vd.mantissa2 == lim->d.mantissa2) &&
220 (vd.mantissa1 == lim->d.mantissa1) &&
221 (vd.exp == lim->d.exp)) {
226 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
227 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
228 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
229 (vd.mantissa3 << 13) |
230 ((vd.mantissa4 >> 3) & MASK(13));
233 lp = (long *)(void *)&id;
235 #if defined(__CYGWIN32__) || defined(__MINGW32__)
236 return (XDR_PUTLONG(xdrs, lp+1) && XDR_PUTLONG(xdrs, lp));
238 return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
244 #if defined(__CYGWIN32__) || defined(__MINGW32__)
245 return (XDR_GETLONG(xdrs, lp+1) && XDR_GETLONG(xdrs, lp));
247 return (XDR_GETLONG(xdrs, lp++) && XDR_GETLONG(xdrs, lp));
250 lp = (long *)(void *)&id;
251 if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
253 for (i = 0, lim = dbl_limits;
254 i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
256 if ((id.mantissa2 == lim->ieee.mantissa2) &&
257 (id.mantissa1 == lim->ieee.mantissa1) &&
258 (id.exp == lim->ieee.exp)) {
263 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
264 vd.mantissa1 = (id.mantissa1 >> 13);
265 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
266 (id.mantissa2 >> 29);
267 vd.mantissa3 = (id.mantissa2 >> 13);
268 vd.mantissa4 = (id.mantissa2 << 3);
271 *dp = *((double *)(void *)&vd);