]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/xdr/xdr_float.c
Merge OpenSSL 1.0.2l.
[FreeBSD/FreeBSD.git] / lib / libc / xdr / xdr_float.c
1 /*      $NetBSD: xdr_float.c,v 1.23 2000/07/17 04:59:51 matt Exp $      */
2
3 /*-
4  * Copyright (c) 2010, Oracle America, 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 the "Oracle America, 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
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char *sccsid2 = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)xdr_float.c  2.1 88/07/29 4.0 RPCSRC";
37 #endif
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 /*
42  * xdr_float.c, Generic XDR routines implementation.
43  *
44  * These are the "floating point" xdr routines used to (de)serialize
45  * most common data items.  See xdr.h for more info on the interface to
46  * xdr.
47  */
48
49 #include "namespace.h"
50 #include <sys/param.h>
51
52 #include <stdio.h>
53
54 #include <rpc/types.h>
55 #include <rpc/xdr.h>
56 #include "un-namespace.h"
57
58 /*
59  * NB: Not portable.
60  * This routine works on machines with IEEE754 FP and Vaxen.
61  */
62
63 #include <machine/endian.h>
64 #define IEEEFP
65
66 #if defined(__vax__)
67
68 /* What IEEE single precision floating point looks like on a Vax */
69 struct  ieee_single {
70         unsigned int    mantissa: 23;
71         unsigned int    exp     : 8;
72         unsigned int    sign    : 1;
73 };
74
75 /* Vax single precision floating point */
76 struct  vax_single {
77         unsigned int    mantissa1 : 7;
78         unsigned int    exp       : 8;
79         unsigned int    sign      : 1;
80         unsigned int    mantissa2 : 16;
81 };
82
83 #define VAX_SNG_BIAS    0x81
84 #define IEEE_SNG_BIAS   0x7f
85
86 static struct sgl_limits {
87         struct vax_single s;
88         struct ieee_single ieee;
89 } sgl_limits[2] = {
90         {{ 0x7f, 0xff, 0x0, 0xffff },   /* Max Vax */
91         { 0x0, 0xff, 0x0 }},            /* Max IEEE */
92         {{ 0x0, 0x0, 0x0, 0x0 },        /* Min Vax */
93         { 0x0, 0x0, 0x0 }}              /* Min IEEE */
94 };
95 #endif /* vax */
96
97 bool_t
98 xdr_float(XDR *xdrs, float *fp)
99 {
100 #ifndef IEEEFP
101         struct ieee_single is;
102         struct vax_single vs, *vsp;
103         struct sgl_limits *lim;
104         u_int i;
105 #endif
106         switch (xdrs->x_op) {
107
108         case XDR_ENCODE:
109 #ifdef IEEEFP
110                 return (XDR_PUTINT32(xdrs, (int32_t *)fp));
111 #else
112                 vs = *((struct vax_single *)fp);
113                 for (i = 0, lim = sgl_limits; i < nitems(sgl_limits);
114                     i++, lim++) {
115                         if ((vs.mantissa2 == lim->s.mantissa2) &&
116                                 (vs.exp == lim->s.exp) &&
117                                 (vs.mantissa1 == lim->s.mantissa1)) {
118                                 is = lim->ieee;
119                                 goto shipit;
120                         }
121                 }
122                 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
123                 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
124         shipit:
125                 is.sign = vs.sign;
126                 return (XDR_PUTINT32(xdrs, (int32_t *)&is));
127 #endif
128
129         case XDR_DECODE:
130 #ifdef IEEEFP
131                 return (XDR_GETINT32(xdrs, (int32_t *)fp));
132 #else
133                 vsp = (struct vax_single *)fp;
134                 if (!XDR_GETINT32(xdrs, (int32_t *)&is))
135                         return (FALSE);
136                 for (i = 0, lim = sgl_limits; i < nitems(sgl_limits);
137                     i++, lim++) {
138                         if ((is.exp == lim->ieee.exp) &&
139                                 (is.mantissa == lim->ieee.mantissa)) {
140                                 *vsp = lim->s;
141                                 goto doneit;
142                         }
143                 }
144                 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
145                 vsp->mantissa2 = is.mantissa;
146                 vsp->mantissa1 = (is.mantissa >> 16);
147         doneit:
148                 vsp->sign = is.sign;
149                 return (TRUE);
150 #endif
151
152         case XDR_FREE:
153                 return (TRUE);
154         }
155         /* NOTREACHED */
156         return (FALSE);
157 }
158
159 #if defined(__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(XDR *xdrs, double *dp)
197 {
198 #ifdef IEEEFP
199         int32_t *i32p;
200         bool_t rv;
201 #else
202         int32_t *lp;
203         struct  ieee_double id;
204         struct  vax_double vd;
205         struct dbl_limits *lim;
206         u_int i;
207 #endif
208
209         switch (xdrs->x_op) {
210
211         case XDR_ENCODE:
212 #ifdef IEEEFP
213                 i32p = (int32_t *)(void *)dp;
214 #if BYTE_ORDER == BIG_ENDIAN
215                 rv = XDR_PUTINT32(xdrs, i32p);
216                 if (!rv)
217                         return (rv);
218                 rv = XDR_PUTINT32(xdrs, i32p+1);
219 #else
220                 rv = XDR_PUTINT32(xdrs, i32p+1);
221                 if (!rv)
222                         return (rv);
223                 rv = XDR_PUTINT32(xdrs, i32p);
224 #endif
225                 return (rv);
226 #else
227                 vd = *((struct vax_double *)dp);
228                 for (i = 0, lim = dbl_limits; i < nitems(dbl_limits);
229                     i++, lim++) {
230                         if ((vd.mantissa4 == lim->d.mantissa4) &&
231                                 (vd.mantissa3 == lim->d.mantissa3) &&
232                                 (vd.mantissa2 == lim->d.mantissa2) &&
233                                 (vd.mantissa1 == lim->d.mantissa1) &&
234                                 (vd.exp == lim->d.exp)) {
235                                 id = lim->ieee;
236                                 goto shipit;
237                         }
238                 }
239                 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
240                 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
241                 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
242                                 (vd.mantissa3 << 13) |
243                                 ((vd.mantissa4 >> 3) & MASK(13));
244         shipit:
245                 id.sign = vd.sign;
246                 lp = (int32_t *)&id;
247                 return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
248 #endif
249
250         case XDR_DECODE:
251 #ifdef IEEEFP
252                 i32p = (int32_t *)(void *)dp;
253 #if BYTE_ORDER == BIG_ENDIAN
254                 rv = XDR_GETINT32(xdrs, i32p);
255                 if (!rv)
256                         return (rv);
257                 rv = XDR_GETINT32(xdrs, i32p+1);
258 #else
259                 rv = XDR_GETINT32(xdrs, i32p+1);
260                 if (!rv)
261                         return (rv);
262                 rv = XDR_GETINT32(xdrs, i32p);
263 #endif
264                 return (rv);
265 #else
266                 lp = (int32_t *)&id;
267                 if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
268                         return (FALSE);
269                 for (i = 0, lim = dbl_limits; i < nitems(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 #endif
289
290         case XDR_FREE:
291                 return (TRUE);
292         }
293         /* NOTREACHED */
294         return (FALSE);
295 }