Ported xdr-4.0-mingw5 back to Linux.
[portablexdr.git] / xdr_float.c
1 /* @(#)xdr_float.c      2.1 88/07/29 4.0 RPCSRC */
2 /*
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.
9  * 
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.
13  * 
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.
17  * 
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.
21  * 
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.
25  * 
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
32 #endif
33
34 /*
35  * xdr_float.c, Generic XDR routines impelmentation.
36  *
37  * Copyright (C) 1984, Sun Microsystems, Inc.
38  *
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
41  * xdr.
42  */
43
44 #include <stdio.h>
45
46 #include <rpc/types.h>
47 #include <rpc/xdr.h>
48
49 #if defined(__CYGWIN32__) || defined(__MINGW32__)
50 #define vax
51 #endif
52
53 /*
54  * NB: Not portable.
55  * This routine works on Suns (Sky / 68000's) and Vaxen.
56  */
57
58 #ifdef vax
59
60 /* What IEEE single precision floating point looks like on a Vax */
61 struct  ieee_single {
62         unsigned int    mantissa: 23;
63         unsigned int    exp     : 8;
64         unsigned int    sign    : 1;
65 };
66
67 /* Vax single precision floating point */
68 struct  vax_single {
69         unsigned int    mantissa1 : 7;
70         unsigned int    exp       : 8;
71         unsigned int    sign      : 1;
72         unsigned int    mantissa2 : 16;
73 };
74
75 #define VAX_SNG_BIAS    0x81
76 #define IEEE_SNG_BIAS   0x7f
77
78 static struct sgl_limits {
79         struct vax_single s;
80         struct ieee_single ieee;
81 } sgl_limits[2] = {
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 */
86 };
87 #endif /* vax */
88
89 bool_t
90 xdr_float(xdrs, fp)
91         register XDR *xdrs;
92         register float *fp;
93 {
94 #if !defined(mc68000) && !defined(sparc) && !defined(__CYGWIN32__) && !defined(__MINGW32__) && !defined(linux)
95
96         struct ieee_single is;
97         struct vax_single vs, *vsp;
98         struct sgl_limits *lim;
99         int i;
100 #endif
101         switch (xdrs->x_op) {
102
103         case XDR_ENCODE:
104 #if defined(mc68000) || defined(sparc) || defined(__CYGWIN32__) || defined(__MINGW32__) || defined(linux)
105                 return (XDR_PUTLONG(xdrs, (long *)fp));
106 #else
107                 vs = *((struct vax_single *)fp);
108                 for (i = 0, lim = sgl_limits;
109                         i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
110                         i++, lim++) {
111                         if ((vs.mantissa2 == lim->s.mantissa2) &&
112                                 (vs.exp == lim->s.exp) &&
113                                 (vs.mantissa1 == lim->s.mantissa1)) {
114                                 is = lim->ieee;
115                                 goto shipit;
116                         }
117                 }
118                 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
119                 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
120         shipit:
121                 is.sign = vs.sign;
122                 return (XDR_PUTLONG(xdrs, (long *)&is));
123 #endif
124
125         case XDR_DECODE:
126 #if defined(mc68000) || defined(sparc) || defined(__CYGWIN32__) || defined(__MINGW32__) || defined(linux)
127                 return (XDR_GETLONG(xdrs, (long *)fp));
128 #else
129                 vsp = (struct vax_single *)fp;
130                 if (!XDR_GETLONG(xdrs, (long *)&is))
131                         return (FALSE);
132                 for (i = 0, lim = sgl_limits;
133                         i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
134                         i++, lim++) {
135                         if ((is.exp == lim->ieee.exp) &&
136                                 (is.mantissa == lim->ieee.mantissa)) {
137                                 *vsp = lim->s;
138                                 goto doneit;
139                         }
140                 }
141                 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
142                 vsp->mantissa2 = is.mantissa;
143                 vsp->mantissa1 = (is.mantissa >> 16);
144         doneit:
145                 vsp->sign = is.sign;
146                 return (TRUE);
147 #endif
148
149         case XDR_FREE:
150                 return (TRUE);
151         }
152         return (FALSE);
153 }
154
155 /*
156  * This routine works on Suns (Sky / 68000's) and Vaxen.
157  */
158
159 #ifdef vax
160 /* What IEEE double precision floating point looks like on a Vax */
161 struct  ieee_double {
162         unsigned int    mantissa1 : 20;
163         unsigned int    exp       : 11;
164         unsigned int    sign      : 1;
165         unsigned int    mantissa2 : 32;
166 };
167
168 /* Vax double precision floating point */
169 struct  vax_double {
170         unsigned int    mantissa1 : 7;
171         unsigned int    exp       : 8;
172         unsigned int    sign      : 1;
173         unsigned int    mantissa2 : 16;
174         unsigned int    mantissa3 : 16;
175         unsigned int    mantissa4 : 16;
176 };
177
178 #define VAX_DBL_BIAS    0x81
179 #define IEEE_DBL_BIAS   0x3ff
180 #define MASK(nbits)     ((1 << nbits) - 1)
181
182 static struct dbl_limits {
183         struct  vax_double d;
184         struct  ieee_double ieee;
185 } dbl_limits[2] = {
186         {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },   /* Max Vax */
187         { 0x0, 0x7ff, 0x0, 0x0 }},                      /* Max IEEE */
188         {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},               /* Min Vax */
189         { 0x0, 0x0, 0x0, 0x0 }}                         /* Min IEEE */
190 };
191
192 #endif /* vax */
193
194
195 bool_t
196 xdr_double(xdrs, dp)
197         register XDR *xdrs;
198         double *dp;
199 {
200         register long *lp;
201 #if !defined(mc68000) && !defined(sparc) && !defined(__CYGWIN32__) && !defined(__MINGW32__) && !defined(linux)
202         struct  ieee_double id;
203         struct  vax_double vd;
204         register struct dbl_limits *lim;
205         int i;
206 #endif
207
208         switch (xdrs->x_op) {
209
210         case XDR_ENCODE:
211 #if defined(mc68000) || defined(sparc) || defined(__CYGWIN32__) || defined(__MINGW32__) || defined(linux)
212                 lp = (long *)dp;
213 #else
214                 vd = *((struct vax_double *)dp);
215                 for (i = 0, lim = dbl_limits;
216                         i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
217                         i++, lim++) {
218                         if ((vd.mantissa4 == lim->d.mantissa4) &&
219                                 (vd.mantissa3 == lim->d.mantissa3) &&
220                                 (vd.mantissa2 == lim->d.mantissa2) &&
221                                 (vd.mantissa1 == lim->d.mantissa1) &&
222                                 (vd.exp == lim->d.exp)) {
223                                 id = lim->ieee;
224                                 goto shipit;
225                         }
226                 }
227                 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
228                 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
229                 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
230                                 (vd.mantissa3 << 13) |
231                                 ((vd.mantissa4 >> 3) & MASK(13));
232         shipit:
233                 id.sign = vd.sign;
234                 lp = (long *)&id;
235 #endif
236 #if defined(__CYGWIN32__) || defined(__MINGW32__)
237                 return (XDR_PUTLONG(xdrs, lp+1) && XDR_PUTLONG(xdrs, lp));
238 #else
239                 return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
240 #endif
241
242         case XDR_DECODE:
243 #if defined(mc68000) || defined(sparc) || defined(__CYGWIN32__) || defined(__MINGW32__) || defined(linux)
244                 lp = (long *)dp;
245 #if defined(__CYGWIN32__) || defined(__MINGW32__)
246                 return (XDR_GETLONG(xdrs, lp+1) && XDR_GETLONG(xdrs, lp));
247 #else
248                 return (XDR_GETLONG(xdrs, lp++) && XDR_GETLONG(xdrs, lp));
249 #endif
250 #else
251                 lp = (long *)&id;
252                 if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
253                         return (FALSE);
254                 for (i = 0, lim = dbl_limits;
255                         i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
256                         i++, lim++) {
257                         if ((id.mantissa2 == lim->ieee.mantissa2) &&
258                                 (id.mantissa1 == lim->ieee.mantissa1) &&
259                                 (id.exp == lim->ieee.exp)) {
260                                 vd = lim->d;
261                                 goto doneit;
262                         }
263                 }
264                 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
265                 vd.mantissa1 = (id.mantissa1 >> 13);
266                 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
267                                 (id.mantissa2 >> 29);
268                 vd.mantissa3 = (id.mantissa2 >> 13);
269                 vd.mantissa4 = (id.mantissa2 << 3);
270         doneit:
271                 vd.sign = id.sign;
272                 *dp = *((double *)&vd);
273                 return (TRUE);
274 #endif
275
276         case XDR_FREE:
277                 return (TRUE);
278         }
279         return (FALSE);
280 }