3 * Copyright (c) 2009, Sun Microsystems, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * - Neither the name of Sun Microsystems, Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
33 * xdr_float.c, Generic XDR routines implementation.
35 * Copyright (C) 1984, Sun Microsystems, Inc.
37 * These are the "floating point" xdr routines used to (de)serialize
38 * most common data items. See xdr.h for more info on the interface to
42 #include <sys/types.h>
43 #include <sys/param.h>
47 #include <rpc/types.h>
49 #include "un-namespace.h"
53 * This routine works on machines with IEEE754 FP and Vaxen.
58 /* What IEEE single precision floating point looks like on a Vax */
60 unsigned int mantissa: 23;
62 unsigned int sign : 1;
65 /* Vax single precision floating point */
67 unsigned int mantissa1 : 7;
69 unsigned int sign : 1;
70 unsigned int mantissa2 : 16;
73 #define VAX_SNG_BIAS 0x81
74 #define IEEE_SNG_BIAS 0x7f
76 static struct sgl_limits {
78 struct ieee_single ieee;
80 {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
81 { 0x0, 0xff, 0x0 }}, /* Max IEEE */
82 {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
83 { 0x0, 0x0, 0x0 }} /* Min IEEE */
87 #include <bits/endian.h>
98 struct ieee_single is;
99 struct vax_single vs, *vsp;
100 struct sgl_limits *lim;
103 switch (xdrs->x_op) {
107 return (XDR_PUTINT32(xdrs, (int32_t *)fp));
109 vs = *((struct vax_single *)fp);
110 for (i = 0, lim = sgl_limits;
111 i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
113 if ((vs.mantissa2 == lim->s.mantissa2) &&
114 (vs.exp == lim->s.exp) &&
115 (vs.mantissa1 == lim->s.mantissa1)) {
120 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
121 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
124 return (XDR_PUTINT32(xdrs, (int32_t *)&is));
129 return (XDR_GETINT32(xdrs, (int32_t *)fp));
131 vsp = (struct vax_single *)fp;
132 if (!XDR_GETINT32(xdrs, (int32_t *)&is))
134 for (i = 0, lim = sgl_limits;
135 i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
137 if ((is.exp == lim->ieee.exp) &&
138 (is.mantissa == lim->ieee.mantissa)) {
143 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
144 vsp->mantissa2 = is.mantissa;
145 vsp->mantissa1 = (is.mantissa >> 16);
159 /* What IEEE double precision floating point looks like on a Vax */
161 unsigned int mantissa1 : 20;
162 unsigned int exp : 11;
163 unsigned int sign : 1;
164 unsigned int mantissa2 : 32;
167 /* Vax double precision floating point */
169 unsigned int mantissa1 : 7;
170 unsigned int exp : 8;
171 unsigned int sign : 1;
172 unsigned int mantissa2 : 16;
173 unsigned int mantissa3 : 16;
174 unsigned int mantissa4 : 16;
177 #define VAX_DBL_BIAS 0x81
178 #define IEEE_DBL_BIAS 0x3ff
179 #define MASK(nbits) ((1 << nbits) - 1)
181 static struct dbl_limits {
183 struct ieee_double ieee;
185 {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
186 { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
187 {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
188 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
204 struct ieee_double id;
205 struct vax_double vd;
206 struct dbl_limits *lim;
210 switch (xdrs->x_op) {
214 i32p = (int32_t *)(void *)dp;
215 #if BYTE_ORDER == BIG_ENDIAN
216 rv = XDR_PUTINT32(xdrs, i32p);
219 rv = XDR_PUTINT32(xdrs, i32p+1);
221 rv = XDR_PUTINT32(xdrs, i32p+1);
224 rv = XDR_PUTINT32(xdrs, i32p);
228 vd = *((struct vax_double *)dp);
229 for (i = 0, lim = dbl_limits;
230 i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
232 if ((vd.mantissa4 == lim->d.mantissa4) &&
233 (vd.mantissa3 == lim->d.mantissa3) &&
234 (vd.mantissa2 == lim->d.mantissa2) &&
235 (vd.mantissa1 == lim->d.mantissa1) &&
236 (vd.exp == lim->d.exp)) {
241 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
242 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
243 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
244 (vd.mantissa3 << 13) |
245 ((vd.mantissa4 >> 3) & MASK(13));
249 return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
254 i32p = (int32_t *)(void *)dp;
255 #if BYTE_ORDER == BIG_ENDIAN
256 rv = XDR_GETINT32(xdrs, i32p);
259 rv = XDR_GETINT32(xdrs, i32p+1);
261 rv = XDR_GETINT32(xdrs, i32p+1);
264 rv = XDR_GETINT32(xdrs, i32p);
269 if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
271 for (i = 0, lim = dbl_limits;
272 i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
274 if ((id.mantissa2 == lim->ieee.mantissa2) &&
275 (id.mantissa1 == lim->ieee.mantissa1) &&
276 (id.exp == lim->ieee.exp)) {
281 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
282 vd.mantissa1 = (id.mantissa1 >> 13);
283 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
284 (id.mantissa2 >> 29);
285 vd.mantissa3 = (id.mantissa2 >> 13);
286 vd.mantissa4 = (id.mantissa2 << 3);
289 *dp = *((double *)&vd);