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