]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libc/inet/rpc/xdr_float.c
03632c5caa85c69ed61ce1e9e9e86cb66de510aa
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libc / inet / rpc / 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 0
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 implementation.
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 #define __FORCE_GLIBC
45 #include <features.h>
46
47 #include <stdio.h>
48 #include <endian.h>
49
50 #include <rpc/types.h>
51 #include <rpc/xdr.h>
52
53 /*
54  * NB: Not portable.
55  * This routine works on Suns (Sky / 68000's) and Vaxen.
56  */
57
58 #define LSW     (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
59
60 #ifdef vax
61
62 /* What IEEE single precision floating point looks like on a Vax */
63 struct  ieee_single {
64         unsigned int    mantissa: 23;
65         unsigned int    exp     : 8;
66         unsigned int    sign    : 1;
67 };
68
69 /* Vax single precision floating point */
70 struct  vax_single {
71         unsigned int    mantissa1 : 7;
72         unsigned int    exp       : 8;
73         unsigned int    sign      : 1;
74         unsigned int    mantissa2 : 16;
75 };
76
77 #define VAX_SNG_BIAS    0x81
78 #define IEEE_SNG_BIAS   0x7f
79
80 static struct sgl_limits {
81         struct vax_single s;
82         struct ieee_single ieee;
83 } sgl_limits[2] = {
84         {{ 0x7f, 0xff, 0x0, 0xffff },   /* Max Vax */
85         { 0x0, 0xff, 0x0 }},            /* Max IEEE */
86         {{ 0x0, 0x0, 0x0, 0x0 },        /* Min Vax */
87         { 0x0, 0x0, 0x0 }}              /* Min IEEE */
88 };
89 #endif /* vax */
90
91 bool_t
92 xdr_float(xdrs, fp)
93      XDR *xdrs;
94      float *fp;
95 {
96 #ifdef vax
97         struct ieee_single is;
98         struct vax_single vs, *vsp;
99         struct sgl_limits *lim;
100         int i;
101 #endif
102         switch (xdrs->x_op) {
103
104         case XDR_ENCODE:
105 #ifdef vax
106                 vs = *((struct vax_single *)fp);
107                 for (i = 0, lim = sgl_limits;
108                         i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
109                         i++, lim++) {
110                         if ((vs.mantissa2 == lim->s.mantissa2) &&
111                                 (vs.exp == lim->s.exp) &&
112                                 (vs.mantissa1 == lim->s.mantissa1)) {
113                                 is = lim->ieee;
114                                 goto shipit;
115                         }
116                 }
117                 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
118                 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
119         shipit:
120                 is.sign = vs.sign;
121                 return (XDR_PUTLONG(xdrs, (long *)&is));
122 #else
123                 if (sizeof(float) == sizeof(long))
124                         return (XDR_PUTLONG(xdrs, (long *)fp));
125                 else if (sizeof(float) == sizeof(int)) {
126                         long tmp = *(int *)fp;
127                         return (XDR_PUTLONG(xdrs, &tmp));
128                 }
129                 break;
130 #endif
131
132         case XDR_DECODE:
133 #ifdef vax
134                 vsp = (struct vax_single *)fp;
135                 if (!XDR_GETLONG(xdrs, (long *)&is))
136                         return (FALSE);
137                 for (i = 0, lim = sgl_limits;
138                         i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
139                         i++, lim++) {
140                         if ((is.exp == lim->ieee.exp) &&
141                                 (is.mantissa == lim->ieee.mantissa)) {
142                                 *vsp = lim->s;
143                                 goto doneit;
144                         }
145                 }
146                 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
147                 vsp->mantissa2 = is.mantissa;
148                 vsp->mantissa1 = (is.mantissa >> 16);
149         doneit:
150                 vsp->sign = is.sign;
151                 return (TRUE);
152 #else
153                 if (sizeof(float) == sizeof(long))
154                         return (XDR_GETLONG(xdrs, (long *)fp));
155                 else if (sizeof(float) == sizeof(int)) {
156                         long tmp;
157                         if (XDR_GETLONG(xdrs, &tmp)) {
158                                 *(int *)fp = tmp;
159                                 return (TRUE);
160                         }
161                 }
162                 break;
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 #ifdef 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      XDR *xdrs;
214      double *dp;
215 {
216 #ifdef vax
217         struct  ieee_double id;
218         struct  vax_double vd;
219         register struct dbl_limits *lim;
220         int i;
221 #endif
222
223         switch (xdrs->x_op) {
224
225         case XDR_ENCODE:
226 #ifdef vax
227                 vd = *((struct vax_double *)dp);
228                 for (i = 0, lim = dbl_limits;
229                         i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
230                         i++, lim++) {
231                         if ((vd.mantissa4 == lim->d.mantissa4) &&
232                                 (vd.mantissa3 == lim->d.mantissa3) &&
233                                 (vd.mantissa2 == lim->d.mantissa2) &&
234                                 (vd.mantissa1 == lim->d.mantissa1) &&
235                                 (vd.exp == lim->d.exp)) {
236                                 id = lim->ieee;
237                                 goto shipit;
238                         }
239                 }
240                 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
241                 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
242                 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
243                                 (vd.mantissa3 << 13) |
244                                 ((vd.mantissa4 >> 3) & MASK(13));
245         shipit:
246                 id.sign = vd.sign;
247                 dp = (double *)&id;
248 #endif
249                 if (2*sizeof(long) == sizeof(double)) {
250                         long *lp = (long *)dp;
251                         return (XDR_PUTLONG(xdrs, lp+!LSW) &&
252                                 XDR_PUTLONG(xdrs, lp+LSW));
253                 } else if (2*sizeof(int) == sizeof(double)) {
254                         int *ip = (int *)dp;
255                         long tmp[2];
256                         tmp[0] = ip[!LSW];
257                         tmp[1] = ip[LSW];
258                         return (XDR_PUTLONG(xdrs, tmp) &&
259                                 XDR_PUTLONG(xdrs, tmp+1));
260                 }
261                 break;
262
263         case XDR_DECODE:
264 #ifdef vax
265                 lp = (long *)&id;
266                 if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
267                         return (FALSE);
268                 for (i = 0, lim = dbl_limits;
269                         i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
270                         i++, lim++) {
271                         if ((id.mantissa2 == lim->ieee.mantissa2) &&
272                                 (id.mantissa1 == lim->ieee.mantissa1) &&
273                                 (id.exp == lim->ieee.exp)) {
274                                 vd = lim->d;
275                                 goto doneit;
276                         }
277                 }
278                 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
279                 vd.mantissa1 = (id.mantissa1 >> 13);
280                 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
281                                 (id.mantissa2 >> 29);
282                 vd.mantissa3 = (id.mantissa2 >> 13);
283                 vd.mantissa4 = (id.mantissa2 << 3);
284         doneit:
285                 vd.sign = id.sign;
286                 *dp = *((double *)&vd);
287                 return (TRUE);
288 #else
289                 if (2*sizeof(long) == sizeof(double)) {
290                         long *lp = (long *)dp;
291                         return (XDR_GETLONG(xdrs, lp+!LSW) &&
292                                 XDR_GETLONG(xdrs, lp+LSW));
293                 } else if (2*sizeof(int) == sizeof(double)) {
294                         int *ip = (int *)dp;
295                         long tmp[2];
296                         if (XDR_GETLONG(xdrs, tmp+!LSW) &&
297                             XDR_GETLONG(xdrs, tmp+LSW)) {
298                                 ip[0] = tmp[0];
299                                 ip[1] = tmp[1];
300                                 return (TRUE);
301                         }
302                 }
303                 break;
304 #endif
305
306         case XDR_FREE:
307                 return (TRUE);
308         }
309         return (FALSE);
310 }