xdr_float.c revision 1.35.8.1 1 /* $NetBSD: xdr_float.c,v 1.35.8.1 2013/03/14 22:03:15 riz 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 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #if 0
37 static char *sccsid = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
38 static char *sccsid = "@(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC";
39 #else
40 __RCSID("$NetBSD: xdr_float.c,v 1.35.8.1 2013/03/14 22:03:15 riz Exp $");
41 #endif
42 #endif
43
44 /*
45 * xdr_float.c, Generic XDR routines implementation.
46 *
47 * Copyright (C) 1984, Sun Microsystems, Inc.
48 *
49 * These are the "floating point" xdr routines used to (de)serialize
50 * most common data items. See xdr.h for more info on the interface to
51 * xdr.
52 */
53
54 #include "namespace.h"
55
56 #include <sys/types.h>
57 #include <sys/param.h>
58
59 #include <stdio.h>
60
61 #include <rpc/types.h>
62 #include <rpc/xdr.h>
63
64 #ifdef __weak_alias
65 __weak_alias(xdr_double,_xdr_double)
66 __weak_alias(xdr_float,_xdr_float)
67 #endif
68
69 /*
70 * NB: Not portable.
71 * This routine works on machines with IEEE754 FP and Vaxen.
72 */
73
74 #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
75 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
76 defined(__arm__) || defined(__powerpc__) || defined(__sh__) || \
77 defined(__x86_64__) || defined(__hppa__) || defined(__ia64__)
78 #include <machine/endian.h>
79 #define IEEEFP
80 #endif
81
82 #if defined(__vax__)
83
84 /* What IEEE single precision floating point looks like on a Vax */
85 struct ieee_single {
86 unsigned int mantissa: 23;
87 unsigned int exp : 8;
88 unsigned int sign : 1;
89 };
90
91 /* Vax single precision floating point */
92 struct vax_single {
93 unsigned int mantissa1 : 7;
94 unsigned int exp : 8;
95 unsigned int sign : 1;
96 unsigned int mantissa2 : 16;
97 };
98
99 #define VAX_SNG_BIAS 0x81
100 #define IEEE_SNG_BIAS 0x7f
101
102 static struct sgl_limits {
103 struct vax_single s;
104 struct ieee_single ieee;
105 } sgl_limits[2] = {
106 {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
107 { 0x0, 0xff, 0x0 }}, /* Max IEEE */
108 {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
109 { 0x0, 0x0, 0x0 }} /* Min IEEE */
110 };
111 #endif /* vax */
112
113 bool_t
114 xdr_float(xdrs, fp)
115 XDR *xdrs;
116 float *fp;
117 {
118 #ifndef IEEEFP
119 struct ieee_single is;
120 struct vax_single vs, *vsp;
121 struct sgl_limits *lim;
122 size_t i;
123 #endif
124 switch (xdrs->x_op) {
125
126 case XDR_ENCODE:
127 #ifdef IEEEFP
128 return (XDR_PUTINT32(xdrs, (int32_t *)(void *)fp));
129 #else
130 vs = *((struct vax_single *)fp);
131 for (i = 0, lim = sgl_limits;
132 i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
133 i++, lim++) {
134 if ((vs.mantissa2 == lim->s.mantissa2) &&
135 (vs.exp == lim->s.exp) &&
136 (vs.mantissa1 == lim->s.mantissa1)) {
137 is = lim->ieee;
138 goto shipit;
139 }
140 }
141 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
142 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
143 shipit:
144 is.sign = vs.sign;
145 return (XDR_PUTINT32(xdrs, (int32_t *)(void *)&is));
146 #endif
147
148 case XDR_DECODE:
149 #ifdef IEEEFP
150 return (XDR_GETINT32(xdrs, (int32_t *)(void *)fp));
151 #else
152 vsp = (struct vax_single *)fp;
153 if (!XDR_GETINT32(xdrs, (int32_t *)(void *)&is))
154 return (FALSE);
155 for (i = 0, lim = sgl_limits;
156 i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
157 i++, lim++) {
158 if ((is.exp == lim->ieee.exp) &&
159 (is.mantissa == lim->ieee.mantissa)) {
160 *vsp = lim->s;
161 goto doneit;
162 }
163 }
164 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
165 vsp->mantissa2 = is.mantissa;
166 vsp->mantissa1 = (is.mantissa >> 16);
167 doneit:
168 vsp->sign = is.sign;
169 return (TRUE);
170 #endif
171
172 case XDR_FREE:
173 return (TRUE);
174 }
175 /* NOTREACHED */
176 return (FALSE);
177 }
178
179 #if defined(__vax__)
180 /* What IEEE double precision floating point looks like on a Vax */
181 struct ieee_double {
182 unsigned int mantissa1 : 20;
183 unsigned int exp : 11;
184 unsigned int sign : 1;
185 unsigned int mantissa2 : 32;
186 };
187
188 /* Vax double precision floating point */
189 struct vax_double {
190 unsigned int mantissa1 : 7;
191 unsigned int exp : 8;
192 unsigned int sign : 1;
193 unsigned int mantissa2 : 16;
194 unsigned int mantissa3 : 16;
195 unsigned int mantissa4 : 16;
196 };
197
198 #define VAX_DBL_BIAS 0x81
199 #define IEEE_DBL_BIAS 0x3ff
200 #define MASK(nbits) ((1 << nbits) - 1)
201
202 static struct dbl_limits {
203 struct vax_double d;
204 struct ieee_double ieee;
205 } dbl_limits[2] = {
206 {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
207 { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
208 {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
209 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
210 };
211
212 #endif /* vax */
213
214
215 bool_t
216 xdr_double(xdrs, dp)
217 XDR *xdrs;
218 double *dp;
219 {
220 #ifdef IEEEFP
221 int32_t *i32p;
222 bool_t rv;
223 #else
224 int32_t *lp;
225 struct ieee_double id;
226 struct vax_double vd;
227 struct dbl_limits *lim;
228 size_t i;
229 #endif
230
231 switch (xdrs->x_op) {
232
233 case XDR_ENCODE:
234 #ifdef IEEEFP
235 i32p = (int32_t *)(void *)dp;
236 #if (BYTE_ORDER == BIG_ENDIAN) || \
237 (defined(__arm__) && !defined(__VFP_FP__))
238 rv = XDR_PUTINT32(xdrs, i32p);
239 if (!rv)
240 return (rv);
241 rv = XDR_PUTINT32(xdrs, i32p+1);
242 #else
243 rv = XDR_PUTINT32(xdrs, i32p+1);
244 if (!rv)
245 return (rv);
246 rv = XDR_PUTINT32(xdrs, i32p);
247 #endif
248 return (rv);
249 #else
250 vd = *((struct vax_double *)dp);
251 for (i = 0, lim = dbl_limits;
252 i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
253 i++, lim++) {
254 if ((vd.mantissa4 == lim->d.mantissa4) &&
255 (vd.mantissa3 == lim->d.mantissa3) &&
256 (vd.mantissa2 == lim->d.mantissa2) &&
257 (vd.mantissa1 == lim->d.mantissa1) &&
258 (vd.exp == lim->d.exp)) {
259 id = lim->ieee;
260 goto shipit;
261 }
262 }
263 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
264 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
265 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
266 (vd.mantissa3 << 13) |
267 ((vd.mantissa4 >> 3) & MASK(13));
268 shipit:
269 id.sign = vd.sign;
270 lp = (int32_t *)(void *)&id;
271 return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
272 #endif
273
274 case XDR_DECODE:
275 #ifdef IEEEFP
276 i32p = (int32_t *)(void *)dp;
277 #if BYTE_ORDER == BIG_ENDIAN || \
278 (defined(__arm__) && !defined(__VFP_FP__))
279 rv = XDR_GETINT32(xdrs, i32p);
280 if (!rv)
281 return (rv);
282 rv = XDR_GETINT32(xdrs, i32p+1);
283 #else
284 rv = XDR_GETINT32(xdrs, i32p+1);
285 if (!rv)
286 return (rv);
287 rv = XDR_GETINT32(xdrs, i32p);
288 #endif
289 return (rv);
290 #else
291 lp = (int32_t *)(void *)&id;
292 if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
293 return (FALSE);
294 for (i = 0, lim = dbl_limits;
295 i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
296 i++, lim++) {
297 if ((id.mantissa2 == lim->ieee.mantissa2) &&
298 (id.mantissa1 == lim->ieee.mantissa1) &&
299 (id.exp == lim->ieee.exp)) {
300 vd = lim->d;
301 goto doneit;
302 }
303 }
304 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
305 vd.mantissa1 = (id.mantissa1 >> 13);
306 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
307 (id.mantissa2 >> 29);
308 vd.mantissa3 = (id.mantissa2 >> 13);
309 vd.mantissa4 = (id.mantissa2 << 3);
310 doneit:
311 vd.sign = id.sign;
312 *dp = *((double *)(void *)&vd);
313 return (TRUE);
314 #endif
315
316 case XDR_FREE:
317 return (TRUE);
318 }
319 /* NOTREACHED */
320 return (FALSE);
321 }
322