1 /* $NetBSD: xdr_float.c,v 1.38 2013/03/11 20:19:29 tron Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
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.
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.
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
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";
40 __RCSID("$NetBSD: xdr_float.c,v 1.38 2013/03/11 20:19:29 tron Exp $");
45 * xdr_float.c, Generic XDR routines implementation.
47 * Copyright (C) 1984, Sun Microsystems, Inc.
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
54 #include "namespace.h"
56 #include <sys/types.h>
57 #include <sys/param.h>
61 #include <rpc/types.h>
65 __weak_alias(xdr_double
,_xdr_double
)
66 __weak_alias(xdr_float
,_xdr_float
)
71 * This routine works on machines with IEEE754 FP and Vaxen.
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>
84 /* What IEEE single precision floating point looks like on a Vax */
86 unsigned int mantissa
: 23;
88 unsigned int sign
: 1;
91 /* Vax single precision floating point */
93 unsigned int mantissa1
: 7;
95 unsigned int sign
: 1;
96 unsigned int mantissa2
: 16;
99 #define VAX_SNG_BIAS 0x81
100 #define IEEE_SNG_BIAS 0x7f
102 static struct sgl_limits
{
104 struct ieee_single ieee
;
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 */
114 xdr_float(XDR
*xdrs
, float *fp
)
117 struct ieee_single is
;
118 struct vax_single vs
, *vsp
;
119 struct sgl_limits
*lim
;
122 switch (xdrs
->x_op
) {
126 return (XDR_PUTINT32(xdrs
, (int32_t *)(void *)fp
));
128 vs
= *((struct vax_single
*)(void *)fp
);
129 for (i
= 0, lim
= sgl_limits
;
130 i
< sizeof(sgl_limits
)/sizeof(struct sgl_limits
);
132 if ((vs
.mantissa2
== lim
->s
.mantissa2
) &&
133 (vs
.exp
== lim
->s
.exp
) &&
134 (vs
.mantissa1
== lim
->s
.mantissa1
)) {
139 is
.exp
= vs
.exp
- VAX_SNG_BIAS
+ IEEE_SNG_BIAS
;
140 is
.mantissa
= (vs
.mantissa1
<< 16) | vs
.mantissa2
;
143 return (XDR_PUTINT32(xdrs
, (int32_t *)(void *)&is
));
148 return (XDR_GETINT32(xdrs
, (int32_t *)(void *)fp
));
150 vsp
= (struct vax_single
*)(void *)fp
;
151 if (!XDR_GETINT32(xdrs
, (int32_t *)(void *)&is
))
153 for (i
= 0, lim
= sgl_limits
;
154 i
< sizeof(sgl_limits
)/sizeof(struct sgl_limits
);
156 if ((is
.exp
== lim
->ieee
.exp
) &&
157 (is
.mantissa
== lim
->ieee
.mantissa
)) {
162 vsp
->exp
= is
.exp
- IEEE_SNG_BIAS
+ VAX_SNG_BIAS
;
163 vsp
->mantissa2
= is
.mantissa
;
164 vsp
->mantissa1
= ((unsigned int)is
.mantissa
>> 16);
178 /* What IEEE double precision floating point looks like on a Vax */
180 unsigned int mantissa1
: 20;
181 unsigned int exp
: 11;
182 unsigned int sign
: 1;
183 unsigned int mantissa2
: 32;
186 /* Vax double precision floating point */
188 unsigned int mantissa1
: 7;
189 unsigned int exp
: 8;
190 unsigned int sign
: 1;
191 unsigned int mantissa2
: 16;
192 unsigned int mantissa3
: 16;
193 unsigned int mantissa4
: 16;
196 #define VAX_DBL_BIAS 0x81
197 #define IEEE_DBL_BIAS 0x3ff
198 #define MASK(nbits) ((1 << nbits) - 1)
200 static struct dbl_limits
{
202 struct ieee_double ieee
;
204 {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
205 { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
206 {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
207 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
214 xdr_double(XDR
*xdrs
, double *dp
)
221 struct ieee_double id
;
222 struct vax_double vd
;
223 struct dbl_limits
*lim
;
227 switch (xdrs
->x_op
) {
231 i32p
= (int32_t *)(void *)dp
;
232 #if (BYTE_ORDER == BIG_ENDIAN) || \
233 (defined(__arm__) && !defined(__VFP_FP__))
234 rv
= XDR_PUTINT32(xdrs
, i32p
);
237 rv
= XDR_PUTINT32(xdrs
, i32p
+1);
239 rv
= XDR_PUTINT32(xdrs
, i32p
+1);
242 rv
= XDR_PUTINT32(xdrs
, i32p
);
246 vd
= *((struct vax_double
*)(void *)dp
);
247 for (i
= 0, lim
= dbl_limits
;
248 i
< sizeof(dbl_limits
)/sizeof(struct dbl_limits
);
250 if ((vd
.mantissa4
== lim
->d
.mantissa4
) &&
251 (vd
.mantissa3
== lim
->d
.mantissa3
) &&
252 (vd
.mantissa2
== lim
->d
.mantissa2
) &&
253 (vd
.mantissa1
== lim
->d
.mantissa1
) &&
254 (vd
.exp
== lim
->d
.exp
)) {
259 id
.exp
= vd
.exp
- VAX_DBL_BIAS
+ IEEE_DBL_BIAS
;
260 id
.mantissa1
= (vd
.mantissa1
<< 13) |
261 ((unsigned int)vd
.mantissa2
>> 3);
262 id
.mantissa2
= ((vd
.mantissa2
& MASK(3)) << 29) |
263 (vd
.mantissa3
<< 13) |
264 (((unsigned int)vd
.mantissa4
>> 3) & MASK(13));
267 lp
= (int32_t *)(void *)&id
;
268 return (XDR_PUTINT32(xdrs
, lp
++) && XDR_PUTINT32(xdrs
, lp
));
273 i32p
= (int32_t *)(void *)dp
;
274 #if BYTE_ORDER == BIG_ENDIAN || \
275 (defined(__arm__) && !defined(__VFP_FP__))
276 rv
= XDR_GETINT32(xdrs
, i32p
);
279 rv
= XDR_GETINT32(xdrs
, i32p
+1);
281 rv
= XDR_GETINT32(xdrs
, i32p
+1);
284 rv
= XDR_GETINT32(xdrs
, i32p
);
288 lp
= (int32_t *)(void *)&id
;
289 if (!XDR_GETINT32(xdrs
, lp
++) || !XDR_GETINT32(xdrs
, lp
))
291 for (i
= 0, lim
= dbl_limits
;
292 i
< sizeof(dbl_limits
)/sizeof(struct dbl_limits
);
294 if ((id
.mantissa2
== lim
->ieee
.mantissa2
) &&
295 (id
.mantissa1
== lim
->ieee
.mantissa1
) &&
296 (id
.exp
== lim
->ieee
.exp
)) {
301 vd
.exp
= id
.exp
- IEEE_DBL_BIAS
+ VAX_DBL_BIAS
;
302 vd
.mantissa1
= ((unsigned int)id
.mantissa1
>> 13);
303 vd
.mantissa2
= ((id
.mantissa1
& MASK(13)) << 3) |
304 ((unsigned int)id
.mantissa2
>> 29);
305 vd
.mantissa3
= ((unsigned int)id
.mantissa2
>> 13);
306 vd
.mantissa4
= (id
.mantissa2
<< 3);
309 *dp
= *((double *)(void *)&vd
);