Merge up from branch.
[shibboleth/cpp-sp.git] / oncrpc / xdr_floa.c
1 /*********************************************************************
2  * RPC for the Windows NT Operating System
3  * 1993 by Martin F. Gergeleit
4  * Users may use, copy or modify Sun RPC for the Windows NT Operating 
5  * System according to the Sun copyright below.
6  *
7  * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO 
8  * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE 
9  * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
10  *********************************************************************/
11
12 /* @(#)xdr_float.c      2.1 88/07/29 4.0 RPCSRC */
13 /*
14  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
15  * unrestricted use provided that this legend is included on all tape
16  * media and as a part of the software program in whole or part.  Users
17  * may copy or modify Sun RPC without charge, but are not authorized
18  * to license or distribute it to anyone else except as part of a product or
19  * program developed by the user.
20  * 
21  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
22  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
24  * 
25  * Sun RPC is provided with no support and without any obligation on the
26  * part of Sun Microsystems, Inc. to assist in its use, correction,
27  * modification or enhancement.
28  * 
29  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
30  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
31  * OR ANY PART THEREOF.
32  * 
33  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
34  * or profits or other special, indirect and consequential damages, even if
35  * Sun has been advised of the possibility of such damages.
36  * 
37  * Sun Microsystems, Inc.
38  * 2550 Garcia Avenue
39  * Mountain View, California  94043
40  */
41 #if !defined(lint) && defined(SCCSIDS)
42 static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
43 #endif
44
45 /*
46  * xdr_float.c, Generic XDR routines impelmentation.
47  *
48  * Copyright (C) 1984, Sun Microsystems, Inc.
49  *
50  * These are the "floating point" xdr routines used to (de)serialize
51  * most common data items.  See xdr.h for more info on the interface to
52  * xdr.
53  */
54
55 #include <stdio.h>
56
57 #include <rpc/rpc.h>
58 #include <rpc/types.h>
59 #include <rpc/xdr.h>
60
61 /*
62  * NB: Not portable.
63  * This routine works on Suns (Sky / 68000's) and Vaxen.
64  */
65
66 #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
67     defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
68     defined(__arm__) || defined(__ppc__) || defined(__ia64__) || \
69     defined(__arm26__) || defined(__sparc64__) || defined(__amd64__) || \
70     defined(__sparc) || defined(WIN32)
71 #define IEEEFP
72 #endif
73
74 #if defined(__vax__)
75
76 /* What IEEE single precision floating point looks like on a Vax */
77 struct  ieee_single {
78         unsigned int    mantissa: 23;
79         unsigned int    exp     : 8;
80         unsigned int    sign    : 1;
81 };
82
83 /* Vax single precision floating point */
84 struct  vax_single {
85         unsigned int    mantissa1 : 7;
86         unsigned int    exp       : 8;
87         unsigned int    sign      : 1;
88         unsigned int    mantissa2 : 16;
89 };
90
91 #define VAX_SNG_BIAS    0x81
92 #define IEEE_SNG_BIAS   0x7f
93
94 static struct sgl_limits {
95         struct vax_single s;
96         struct ieee_single ieee;
97 } sgl_limits[2] = {
98         {{ 0x7f, 0xff, 0x0, 0xffff },   /* Max Vax */
99         { 0x0, 0xff, 0x0 }},            /* Max IEEE */
100         {{ 0x0, 0x0, 0x0, 0x0 },        /* Min Vax */
101         { 0x0, 0x0, 0x0 }}              /* Min IEEE */
102 };
103 #endif /* vax */
104
105 bool_t
106 xdr_float(xdrs, fp)
107         register XDR *xdrs;
108         register float *fp;
109 {
110
111 #ifndef IEEEFP
112         struct ieee_single is;
113         struct vax_single vs, *vsp;
114         struct sgl_limits *lim;
115         int i;
116 #endif
117         switch (xdrs->x_op) {
118
119         case XDR_ENCODE:
120 #ifdef IEEEFP
121      return (XDR_PUTLONG(xdrs, (int32_t *)fp));
122 #else
123                 vs = *((struct vax_single *)fp);
124                 for (i = 0, lim = sgl_limits;
125                         i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
126                         i++, lim++) {
127                         if ((vs.mantissa2 == lim->s.mantissa2) &&
128                                 (vs.exp == lim->s.exp) &&
129                                 (vs.mantissa1 == lim->s.mantissa1)) {
130                                 is = lim->ieee;
131                                 goto shipit;
132                         }
133                 }
134                 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
135                 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
136         shipit:
137                 is.sign = vs.sign;
138                 return (XDR_PUTLONG(xdrs, (long *)&is));
139 #endif
140
141         case XDR_DECODE:
142 #ifdef IEEEFP
143      return (XDR_GETLONG(xdrs, (int32_t *)fp));
144 #else
145                 vsp = (struct vax_single *)fp;
146                 if (!XDR_GETLONG(xdrs, (long *)&is))
147                         return (FALSE);
148                 for (i = 0, lim = sgl_limits;
149                         i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
150                         i++, lim++) {
151                         if ((is.exp == lim->ieee.exp) &&
152                                 (is.mantissa == lim->ieee.mantissa)) {
153                                 *vsp = lim->s;
154                                 goto doneit;
155                         }
156                 }
157                 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
158                 vsp->mantissa2 = is.mantissa;
159                 vsp->mantissa1 = (is.mantissa >> 16);
160         doneit:
161                 vsp->sign = is.sign;
162                 return (TRUE);
163 #endif
164
165         case XDR_FREE:
166                 return (TRUE);
167         }
168         return (FALSE);
169 }
170
171 /*
172  * This routine works on Suns (Sky / 68000's) and Vaxen.
173  */
174
175 #if defined(__vax__)
176 /* What IEEE double precision floating point looks like on a Vax */
177 struct  ieee_double {
178         unsigned int    mantissa1 : 20;
179         unsigned int    exp       : 11;
180         unsigned int    sign      : 1;
181         unsigned int    mantissa2 : 32;
182 };
183
184 /* Vax double precision floating point */
185 struct  vax_double {
186         unsigned int    mantissa1 : 7;
187         unsigned int    exp       : 8;
188         unsigned int    sign      : 1;
189         unsigned int    mantissa2 : 16;
190         unsigned int    mantissa3 : 16;
191         unsigned int    mantissa4 : 16;
192 };
193
194 #define VAX_DBL_BIAS    0x81
195 #define IEEE_DBL_BIAS   0x3ff
196 #define MASK(nbits)     ((1 << nbits) - 1)
197
198 static struct dbl_limits {
199         struct  vax_double d;
200         struct  ieee_double ieee;
201 } dbl_limits[2] = {
202         {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },   /* Max Vax */
203         { 0x0, 0x7ff, 0x0, 0x0 }},                      /* Max IEEE */
204         {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},               /* Min Vax */
205         { 0x0, 0x0, 0x0, 0x0 }}                         /* Min IEEE */
206 };
207
208 #endif /* vax */
209
210
211 bool_t
212 xdr_double(xdrs, dp)
213         register XDR *xdrs;
214         double *dp;
215 {
216 #ifdef IEEEFP
217     int32_t *i32p;
218     bool_t rv;
219 #else
220     int32_t *lp;
221     struct  ieee_double id;
222     struct  vax_double vd;
223     struct dbl_limits *lim;
224     int i;
225 #endif
226         switch (xdrs->x_op) {
227
228         case XDR_ENCODE:
229 #ifdef IEEEFP
230         i32p = (int32_t *)(void *)dp;
231 #if BYTE_ORDER == BIG_ENDIAN
232         rv = XDR_PUTLONG(xdrs, i32p);
233         if (!rv)
234             return (rv);
235         rv = XDR_PUTLONG(xdrs, i32p+1);
236 #else
237         rv = XDR_PUTLONG(xdrs, i32p+1);
238         if (!rv)
239             return (rv);
240         rv = XDR_PUTLONG(xdrs, i32p);
241 #endif
242         return (rv);
243 #else
244                 vd = *((struct vax_double *)dp);
245                 for (i = 0, lim = dbl_limits;
246                         i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
247                         i++, lim++) {
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)) {
253                                 id = lim->ieee;
254                                 goto shipit;
255                         }
256                 }
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));
262         shipit:
263                 id.sign = vd.sign;
264                 lp = (long *)&id;
265         return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
266 #endif
267         case XDR_DECODE:
268 #ifdef IEEEFP
269         i32p = (int32_t *)(void *)dp;
270 #if BYTE_ORDER == BIG_ENDIAN
271         rv = XDR_GETLONG(xdrs, i32p);
272         if (!rv)
273             return (rv);
274         rv = XDR_GETLONG(xdrs, i32p+1);
275 #else
276         rv = XDR_GETLONG(xdrs, i32p+1);
277         if (!rv)
278             return (rv);
279         rv = XDR_GETLONG(xdrs, i32p);
280 #endif
281         return (rv);
282 #else
283                 lp = (long *)&id;
284                 if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
285                         return (FALSE);
286                 for (i = 0, lim = dbl_limits;
287                         i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
288                         i++, lim++) {
289                         if ((id.mantissa2 == lim->ieee.mantissa2) &&
290                                 (id.mantissa1 == lim->ieee.mantissa1) &&
291                                 (id.exp == lim->ieee.exp)) {
292                                 vd = lim->d;
293                                 goto doneit;
294                         }
295                 }
296                 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
297                 vd.mantissa1 = (id.mantissa1 >> 13);
298                 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
299                                 (id.mantissa2 >> 29);
300                 vd.mantissa3 = (id.mantissa2 >> 13);
301                 vd.mantissa4 = (id.mantissa2 << 3);
302         doneit:
303                 vd.sign = id.sign;
304                 *dp = *((double *)&vd);
305                 return (TRUE);
306 #endif
307
308         case XDR_FREE:
309                 return (TRUE);
310         }
311         return (FALSE);
312 }