1 /* $NetBSD: xdr_float.c,v 1.23 2000/07/17 04:59:51 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 #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";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * xdr_float.c, Generic XDR routines implementation.
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
49 #include "namespace.h"
50 #include <sys/param.h>
54 #include <rpc/types.h>
56 #include "un-namespace.h"
60 * This routine works on machines with IEEE754 FP and Vaxen.
63 #include <machine/endian.h>
68 /* What IEEE single precision floating point looks like on a Vax */
70 unsigned int mantissa: 23;
72 unsigned int sign : 1;
75 /* Vax single precision floating point */
77 unsigned int mantissa1 : 7;
79 unsigned int sign : 1;
80 unsigned int mantissa2 : 16;
83 #define VAX_SNG_BIAS 0x81
84 #define IEEE_SNG_BIAS 0x7f
86 static struct sgl_limits {
88 struct ieee_single ieee;
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 */
98 xdr_float(XDR *xdrs, float *fp)
101 struct ieee_single is;
102 struct vax_single vs, *vsp;
103 struct sgl_limits *lim;
106 switch (xdrs->x_op) {
110 return (XDR_PUTINT32(xdrs, (int32_t *)fp));
112 vs = *((struct vax_single *)fp);
113 for (i = 0, lim = sgl_limits; i < nitems(sgl_limits);
115 if ((vs.mantissa2 == lim->s.mantissa2) &&
116 (vs.exp == lim->s.exp) &&
117 (vs.mantissa1 == lim->s.mantissa1)) {
122 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
123 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
126 return (XDR_PUTINT32(xdrs, (int32_t *)&is));
131 return (XDR_GETINT32(xdrs, (int32_t *)fp));
133 vsp = (struct vax_single *)fp;
134 if (!XDR_GETINT32(xdrs, (int32_t *)&is))
136 for (i = 0, lim = sgl_limits; i < nitems(sgl_limits);
138 if ((is.exp == lim->ieee.exp) &&
139 (is.mantissa == lim->ieee.mantissa)) {
144 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
145 vsp->mantissa2 = is.mantissa;
146 vsp->mantissa1 = (is.mantissa >> 16);
160 /* What IEEE double precision floating point looks like on a Vax */
162 unsigned int mantissa1 : 20;
163 unsigned int exp : 11;
164 unsigned int sign : 1;
165 unsigned int mantissa2 : 32;
168 /* Vax double precision floating point */
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;
178 #define VAX_DBL_BIAS 0x81
179 #define IEEE_DBL_BIAS 0x3ff
180 #define MASK(nbits) ((1 << nbits) - 1)
182 static struct dbl_limits {
184 struct ieee_double ieee;
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 */
196 xdr_double(XDR *xdrs, double *dp)
203 struct ieee_double id;
204 struct vax_double vd;
205 struct dbl_limits *lim;
209 switch (xdrs->x_op) {
213 i32p = (int32_t *)(void *)dp;
214 #if BYTE_ORDER == BIG_ENDIAN
215 rv = XDR_PUTINT32(xdrs, i32p);
218 rv = XDR_PUTINT32(xdrs, i32p+1);
220 rv = XDR_PUTINT32(xdrs, i32p+1);
223 rv = XDR_PUTINT32(xdrs, i32p);
227 vd = *((struct vax_double *)dp);
228 for (i = 0, lim = dbl_limits; i < nitems(dbl_limits);
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)) {
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));
247 return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
252 i32p = (int32_t *)(void *)dp;
253 #if BYTE_ORDER == BIG_ENDIAN
254 rv = XDR_GETINT32(xdrs, i32p);
257 rv = XDR_GETINT32(xdrs, i32p+1);
259 rv = XDR_GETINT32(xdrs, i32p+1);
262 rv = XDR_GETINT32(xdrs, i32p);
267 if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
269 for (i = 0, lim = dbl_limits; i < nitems(dbl_limits);
271 if ((id.mantissa2 == lim->ieee.mantissa2) &&
272 (id.mantissa1 == lim->ieee.mantissa1) &&
273 (id.exp == lim->ieee.exp)) {
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);
286 *dp = *((double *)&vd);