Reinitialize _create_xid state after fork.
[platform/upstream/glibc.git] / sunrpc / xdr_float.c
1 /*
2  * xdr_float.c, Generic XDR routines implementation.
3  *
4  * Copyright (C) 1984, Sun Microsystems, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  *       copyright notice, this list of conditions and the following
14  *       disclaimer in the documentation and/or other materials
15  *       provided with the distribution.
16  *     * Neither the name of Sun Microsystems, Inc. nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * These are the "floating point" xdr routines used to (de)serialize
34  * most common data items.  See xdr.h for more info on the interface to
35  * xdr.
36  */
37
38 #include <stdio.h>
39 #include <endian.h>
40
41 #include <rpc/types.h>
42 #include <rpc/xdr.h>
43
44 /*
45  * NB: Not portable.
46  * This routine works on Suns (Sky / 68000's) and Vaxen.
47  */
48
49 #define LSW     (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
50
51 #ifdef vax
52
53 /* What IEEE single precision floating point looks like on a Vax */
54 struct  ieee_single {
55         unsigned int    mantissa: 23;
56         unsigned int    exp     : 8;
57         unsigned int    sign    : 1;
58 };
59
60 /* Vax single precision floating point */
61 struct  vax_single {
62         unsigned int    mantissa1 : 7;
63         unsigned int    exp       : 8;
64         unsigned int    sign      : 1;
65         unsigned int    mantissa2 : 16;
66 };
67
68 #define VAX_SNG_BIAS    0x81
69 #define IEEE_SNG_BIAS   0x7f
70
71 static struct sgl_limits {
72         struct vax_single s;
73         struct ieee_single ieee;
74 } sgl_limits[2] = {
75         {{ 0x7f, 0xff, 0x0, 0xffff },   /* Max Vax */
76         { 0x0, 0xff, 0x0 }},            /* Max IEEE */
77         {{ 0x0, 0x0, 0x0, 0x0 },        /* Min Vax */
78         { 0x0, 0x0, 0x0 }}              /* Min IEEE */
79 };
80 #endif /* vax */
81
82 bool_t
83 xdr_float(xdrs, fp)
84      XDR *xdrs;
85      float *fp;
86 {
87 #ifdef vax
88         struct ieee_single is;
89         struct vax_single vs, *vsp;
90         struct sgl_limits *lim;
91         int i;
92 #endif
93         switch (xdrs->x_op) {
94
95         case XDR_ENCODE:
96 #ifdef vax
97                 vs = *((struct vax_single *)fp);
98                 for (i = 0, lim = sgl_limits;
99                         i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
100                         i++, lim++) {
101                         if ((vs.mantissa2 == lim->s.mantissa2) &&
102                                 (vs.exp == lim->s.exp) &&
103                                 (vs.mantissa1 == lim->s.mantissa1)) {
104                                 is = lim->ieee;
105                                 goto shipit;
106                         }
107                 }
108                 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
109                 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
110         shipit:
111                 is.sign = vs.sign;
112                 return (XDR_PUTLONG(xdrs, (long *)&is));
113 #else
114                 if (sizeof(float) == sizeof(long))
115                         return (XDR_PUTLONG(xdrs, (long *)fp));
116                 else if (sizeof(float) == sizeof(int)) {
117                         long tmp = *(int *)fp;
118                         return (XDR_PUTLONG(xdrs, &tmp));
119                 }
120                 break;
121 #endif
122
123         case XDR_DECODE:
124 #ifdef vax
125                 vsp = (struct vax_single *)fp;
126                 if (!XDR_GETLONG(xdrs, (long *)&is))
127                         return (FALSE);
128                 for (i = 0, lim = sgl_limits;
129                         i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
130                         i++, lim++) {
131                         if ((is.exp == lim->ieee.exp) &&
132                                 (is.mantissa == lim->ieee.mantissa)) {
133                                 *vsp = lim->s;
134                                 goto doneit;
135                         }
136                 }
137                 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
138                 vsp->mantissa2 = is.mantissa;
139                 vsp->mantissa1 = (is.mantissa >> 16);
140         doneit:
141                 vsp->sign = is.sign;
142                 return (TRUE);
143 #else
144                 if (sizeof(float) == sizeof(long))
145                         return (XDR_GETLONG(xdrs, (long *)fp));
146                 else if (sizeof(float) == sizeof(int)) {
147                         long tmp;
148                         if (XDR_GETLONG(xdrs, &tmp)) {
149                                 *(int *)fp = tmp;
150                                 return (TRUE);
151                         }
152                 }
153                 break;
154 #endif
155
156         case XDR_FREE:
157                 return (TRUE);
158         }
159         return (FALSE);
160 }
161
162 /*
163  * This routine works on Suns (Sky / 68000's) and Vaxen.
164  */
165
166 #ifdef vax
167 /* What IEEE double precision floating point looks like on a Vax */
168 struct  ieee_double {
169         unsigned int    mantissa1 : 20;
170         unsigned int    exp       : 11;
171         unsigned int    sign      : 1;
172         unsigned int    mantissa2 : 32;
173 };
174
175 /* Vax double precision floating point */
176 struct  vax_double {
177         unsigned int    mantissa1 : 7;
178         unsigned int    exp       : 8;
179         unsigned int    sign      : 1;
180         unsigned int    mantissa2 : 16;
181         unsigned int    mantissa3 : 16;
182         unsigned int    mantissa4 : 16;
183 };
184
185 #define VAX_DBL_BIAS    0x81
186 #define IEEE_DBL_BIAS   0x3ff
187 #define MASK(nbits)     ((1 << nbits) - 1)
188
189 static struct dbl_limits {
190         struct  vax_double d;
191         struct  ieee_double ieee;
192 } dbl_limits[2] = {
193         {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },   /* Max Vax */
194         { 0x0, 0x7ff, 0x0, 0x0 }},                      /* Max IEEE */
195         {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},               /* Min Vax */
196         { 0x0, 0x0, 0x0, 0x0 }}                         /* Min IEEE */
197 };
198
199 #endif /* vax */
200
201
202 bool_t
203 xdr_double(xdrs, dp)
204      XDR *xdrs;
205      double *dp;
206 {
207 #ifdef vax
208         struct  ieee_double id;
209         struct  vax_double vd;
210         register struct dbl_limits *lim;
211         int i;
212 #endif
213
214         switch (xdrs->x_op) {
215
216         case XDR_ENCODE:
217 #ifdef vax
218                 vd = *((struct vax_double *)dp);
219                 for (i = 0, lim = dbl_limits;
220                         i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
221                         i++, lim++) {
222                         if ((vd.mantissa4 == lim->d.mantissa4) &&
223                                 (vd.mantissa3 == lim->d.mantissa3) &&
224                                 (vd.mantissa2 == lim->d.mantissa2) &&
225                                 (vd.mantissa1 == lim->d.mantissa1) &&
226                                 (vd.exp == lim->d.exp)) {
227                                 id = lim->ieee;
228                                 goto shipit;
229                         }
230                 }
231                 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
232                 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
233                 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
234                                 (vd.mantissa3 << 13) |
235                                 ((vd.mantissa4 >> 3) & MASK(13));
236         shipit:
237                 id.sign = vd.sign;
238                 dp = (double *)&id;
239 #endif
240                 if (2*sizeof(long) == sizeof(double)) {
241                         long *lp = (long *)dp;
242                         return (XDR_PUTLONG(xdrs, lp+!LSW) &&
243                                 XDR_PUTLONG(xdrs, lp+LSW));
244                 } else if (2*sizeof(int) == sizeof(double)) {
245                         int *ip = (int *)dp;
246                         long tmp[2];
247                         tmp[0] = ip[!LSW];
248                         tmp[1] = ip[LSW];
249                         return (XDR_PUTLONG(xdrs, tmp) &&
250                                 XDR_PUTLONG(xdrs, tmp+1));
251                 }
252                 break;
253
254         case XDR_DECODE:
255 #ifdef vax
256                 lp = (long *)&id;
257                 if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
258                         return (FALSE);
259                 for (i = 0, lim = dbl_limits;
260                         i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
261                         i++, lim++) {
262                         if ((id.mantissa2 == lim->ieee.mantissa2) &&
263                                 (id.mantissa1 == lim->ieee.mantissa1) &&
264                                 (id.exp == lim->ieee.exp)) {
265                                 vd = lim->d;
266                                 goto doneit;
267                         }
268                 }
269                 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
270                 vd.mantissa1 = (id.mantissa1 >> 13);
271                 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
272                                 (id.mantissa2 >> 29);
273                 vd.mantissa3 = (id.mantissa2 >> 13);
274                 vd.mantissa4 = (id.mantissa2 << 3);
275         doneit:
276                 vd.sign = id.sign;
277                 *dp = *((double *)&vd);
278                 return (TRUE);
279 #else
280                 if (2*sizeof(long) == sizeof(double)) {
281                         long *lp = (long *)dp;
282                         return (XDR_GETLONG(xdrs, lp+!LSW) &&
283                                 XDR_GETLONG(xdrs, lp+LSW));
284                 } else if (2*sizeof(int) == sizeof(double)) {
285                         int *ip = (int *)dp;
286                         long tmp[2];
287                         if (XDR_GETLONG(xdrs, tmp+!LSW) &&
288                             XDR_GETLONG(xdrs, tmp+LSW)) {
289                                 ip[0] = tmp[0];
290                                 ip[1] = tmp[1];
291                                 return (TRUE);
292                         }
293                 }
294                 break;
295 #endif
296
297         case XDR_FREE:
298                 return (TRUE);
299         }
300         return (FALSE);
301 }