1 /* $NetBSD: xdr_float.c,v 1.40 2014/08/24 17:07:00 matt 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.40 2014/08/24 17:07:00 matt 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.
75 #include <machine/endian.h>
81 /* What IEEE single precision floating point looks like on a Vax */
83 unsigned int mantissa
: 23;
85 unsigned int sign
: 1;
88 /* Vax single precision floating point */
90 unsigned int mantissa1
: 7;
92 unsigned int sign
: 1;
93 unsigned int mantissa2
: 16;
96 #define VAX_SNG_BIAS 0x81
97 #define IEEE_SNG_BIAS 0x7f
99 static struct sgl_limits
{
101 struct ieee_single ieee
;
103 {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
104 { 0x0, 0xff, 0x0 }}, /* Max IEEE */
105 {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
106 { 0x0, 0x0, 0x0 }} /* Min IEEE */
111 xdr_float(XDR
*xdrs
, float *fp
)
114 struct ieee_single is
;
115 struct vax_single vs
, *vsp
;
116 struct sgl_limits
*lim
;
119 switch (xdrs
->x_op
) {
123 return (XDR_PUTINT32(xdrs
, (int32_t *)(void *)fp
));
125 vs
= *((struct vax_single
*)(void *)fp
);
126 for (i
= 0, lim
= sgl_limits
;
127 i
< sizeof(sgl_limits
)/sizeof(struct sgl_limits
);
129 if ((vs
.mantissa2
== lim
->s
.mantissa2
) &&
130 (vs
.exp
== lim
->s
.exp
) &&
131 (vs
.mantissa1
== lim
->s
.mantissa1
)) {
136 is
.exp
= vs
.exp
- VAX_SNG_BIAS
+ IEEE_SNG_BIAS
;
137 is
.mantissa
= (vs
.mantissa1
<< 16) | vs
.mantissa2
;
140 return (XDR_PUTINT32(xdrs
, (int32_t *)(void *)&is
));
145 return (XDR_GETINT32(xdrs
, (int32_t *)(void *)fp
));
147 vsp
= (struct vax_single
*)(void *)fp
;
148 if (!XDR_GETINT32(xdrs
, (int32_t *)(void *)&is
))
150 for (i
= 0, lim
= sgl_limits
;
151 i
< sizeof(sgl_limits
)/sizeof(struct sgl_limits
);
153 if ((is
.exp
== lim
->ieee
.exp
) &&
154 (is
.mantissa
== lim
->ieee
.mantissa
)) {
159 vsp
->exp
= is
.exp
- IEEE_SNG_BIAS
+ VAX_SNG_BIAS
;
160 vsp
->mantissa2
= is
.mantissa
;
161 vsp
->mantissa1
= ((unsigned int)is
.mantissa
>> 16);
175 /* What IEEE double precision floating point looks like on a Vax */
177 unsigned int mantissa1
: 20;
178 unsigned int exp
: 11;
179 unsigned int sign
: 1;
180 unsigned int mantissa2
: 32;
183 /* Vax double precision floating point */
185 unsigned int mantissa1
: 7;
186 unsigned int exp
: 8;
187 unsigned int sign
: 1;
188 unsigned int mantissa2
: 16;
189 unsigned int mantissa3
: 16;
190 unsigned int mantissa4
: 16;
193 #define VAX_DBL_BIAS 0x81
194 #define IEEE_DBL_BIAS 0x3ff
195 #define MASK(nbits) ((1 << nbits) - 1)
197 static struct dbl_limits
{
199 struct ieee_double ieee
;
201 {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
202 { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
203 {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
204 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
211 xdr_double(XDR
*xdrs
, double *dp
)
218 struct ieee_double id
;
219 struct vax_double vd
;
220 struct dbl_limits
*lim
;
224 switch (xdrs
->x_op
) {
228 i32p
= (int32_t *)(void *)dp
;
229 #if (BYTE_ORDER == BIG_ENDIAN) || \
230 (defined(__arm__) && !defined(__VFP_FP__))
231 rv
= XDR_PUTINT32(xdrs
, i32p
);
234 rv
= XDR_PUTINT32(xdrs
, i32p
+1);
236 rv
= XDR_PUTINT32(xdrs
, i32p
+1);
239 rv
= XDR_PUTINT32(xdrs
, i32p
);
243 vd
= *((struct vax_double
*)(void *)dp
);
244 for (i
= 0, lim
= dbl_limits
;
245 i
< sizeof(dbl_limits
)/sizeof(struct dbl_limits
);
247 if ((vd
.mantissa4
== lim
->d
.mantissa4
) &&
248 (vd
.mantissa3
== lim
->d
.mantissa3
) &&
249 (vd
.mantissa2
== lim
->d
.mantissa2
) &&
250 (vd
.mantissa1
== lim
->d
.mantissa1
) &&
251 (vd
.exp
== lim
->d
.exp
)) {
256 id
.exp
= vd
.exp
- VAX_DBL_BIAS
+ IEEE_DBL_BIAS
;
257 id
.mantissa1
= (vd
.mantissa1
<< 13) |
258 ((unsigned int)vd
.mantissa2
>> 3);
259 id
.mantissa2
= ((vd
.mantissa2
& MASK(3)) << 29) |
260 (vd
.mantissa3
<< 13) |
261 (((unsigned int)vd
.mantissa4
>> 3) & MASK(13));
264 lp
= (int32_t *)(void *)&id
;
265 return (XDR_PUTINT32(xdrs
, lp
++) && XDR_PUTINT32(xdrs
, lp
));
270 i32p
= (int32_t *)(void *)dp
;
271 #if BYTE_ORDER == BIG_ENDIAN || \
272 (defined(__arm__) && !defined(__VFP_FP__))
273 rv
= XDR_GETINT32(xdrs
, i32p
);
276 rv
= XDR_GETINT32(xdrs
, i32p
+1);
278 rv
= XDR_GETINT32(xdrs
, i32p
+1);
281 rv
= XDR_GETINT32(xdrs
, i32p
);
285 lp
= (int32_t *)(void *)&id
;
286 if (!XDR_GETINT32(xdrs
, lp
++) || !XDR_GETINT32(xdrs
, lp
))
288 for (i
= 0, lim
= dbl_limits
;
289 i
< sizeof(dbl_limits
)/sizeof(struct dbl_limits
);
291 if ((id
.mantissa2
== lim
->ieee
.mantissa2
) &&
292 (id
.mantissa1
== lim
->ieee
.mantissa1
) &&
293 (id
.exp
== lim
->ieee
.exp
)) {
298 vd
.exp
= id
.exp
- IEEE_DBL_BIAS
+ VAX_DBL_BIAS
;
299 vd
.mantissa1
= ((unsigned int)id
.mantissa1
>> 13);
300 vd
.mantissa2
= ((id
.mantissa1
& MASK(13)) << 3) |
301 ((unsigned int)id
.mantissa2
>> 29);
302 vd
.mantissa3
= ((unsigned int)id
.mantissa2
>> 13);
303 vd
.mantissa4
= (id
.mantissa2
<< 3);
306 *dp
= *((double *)(void *)&vd
);