1 /* $NetBSD: xdr_float.c,v 1.23 2000/07/17 04:59:51 matt Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2010, Oracle America, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials
17 * provided with the distribution.
18 * * Neither the name of the "Oracle America, Inc." nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * xdr_float.c, Generic XDR routines implementation.
39 * These are the "floating point" xdr routines used to (de)serialize
40 * most common data items. See xdr.h for more info on the interface to
44 #include "namespace.h"
45 #include <sys/param.h>
49 #include <rpc/types.h>
51 #include "un-namespace.h"
55 * This routine works on machines with IEEE754 FP and Vaxen.
58 #include <machine/endian.h>
63 /* What IEEE single precision floating point looks like on a Vax */
65 unsigned int mantissa: 23;
67 unsigned int sign : 1;
70 /* Vax single precision floating point */
72 unsigned int mantissa1 : 7;
74 unsigned int sign : 1;
75 unsigned int mantissa2 : 16;
78 #define VAX_SNG_BIAS 0x81
79 #define IEEE_SNG_BIAS 0x7f
81 static struct sgl_limits {
83 struct ieee_single ieee;
85 {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
86 { 0x0, 0xff, 0x0 }}, /* Max IEEE */
87 {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
88 { 0x0, 0x0, 0x0 }} /* Min IEEE */
93 xdr_float(XDR *xdrs, float *fp)
96 struct ieee_single is;
97 struct vax_single vs, *vsp;
98 struct sgl_limits *lim;
101 switch (xdrs->x_op) {
105 return (XDR_PUTINT32(xdrs, (int32_t *)fp));
107 vs = *((struct vax_single *)fp);
108 for (i = 0, lim = sgl_limits; i < nitems(sgl_limits);
110 if ((vs.mantissa2 == lim->s.mantissa2) &&
111 (vs.exp == lim->s.exp) &&
112 (vs.mantissa1 == lim->s.mantissa1)) {
117 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
118 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
121 return (XDR_PUTINT32(xdrs, (int32_t *)&is));
126 return (XDR_GETINT32(xdrs, (int32_t *)fp));
128 vsp = (struct vax_single *)fp;
129 if (!XDR_GETINT32(xdrs, (int32_t *)&is))
131 for (i = 0, lim = sgl_limits; i < nitems(sgl_limits);
133 if ((is.exp == lim->ieee.exp) &&
134 (is.mantissa == lim->ieee.mantissa)) {
139 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
140 vsp->mantissa2 = is.mantissa;
141 vsp->mantissa1 = (is.mantissa >> 16);
155 /* What IEEE double precision floating point looks like on a Vax */
157 unsigned int mantissa1 : 20;
158 unsigned int exp : 11;
159 unsigned int sign : 1;
160 unsigned int mantissa2 : 32;
163 /* Vax double precision floating point */
165 unsigned int mantissa1 : 7;
166 unsigned int exp : 8;
167 unsigned int sign : 1;
168 unsigned int mantissa2 : 16;
169 unsigned int mantissa3 : 16;
170 unsigned int mantissa4 : 16;
173 #define VAX_DBL_BIAS 0x81
174 #define IEEE_DBL_BIAS 0x3ff
175 #define MASK(nbits) ((1 << nbits) - 1)
177 static struct dbl_limits {
179 struct ieee_double ieee;
181 {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
182 { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
183 {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
184 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
191 xdr_double(XDR *xdrs, double *dp)
198 struct ieee_double id;
199 struct vax_double vd;
200 struct dbl_limits *lim;
204 switch (xdrs->x_op) {
208 i32p = (int32_t *)(void *)dp;
209 #if BYTE_ORDER == BIG_ENDIAN
210 rv = XDR_PUTINT32(xdrs, i32p);
213 rv = XDR_PUTINT32(xdrs, i32p+1);
215 rv = XDR_PUTINT32(xdrs, i32p+1);
218 rv = XDR_PUTINT32(xdrs, i32p);
222 vd = *((struct vax_double *)dp);
223 for (i = 0, lim = dbl_limits; i < nitems(dbl_limits);
225 if ((vd.mantissa4 == lim->d.mantissa4) &&
226 (vd.mantissa3 == lim->d.mantissa3) &&
227 (vd.mantissa2 == lim->d.mantissa2) &&
228 (vd.mantissa1 == lim->d.mantissa1) &&
229 (vd.exp == lim->d.exp)) {
234 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
235 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
236 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
237 (vd.mantissa3 << 13) |
238 ((vd.mantissa4 >> 3) & MASK(13));
242 return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
247 i32p = (int32_t *)(void *)dp;
248 #if BYTE_ORDER == BIG_ENDIAN
249 rv = XDR_GETINT32(xdrs, i32p);
252 rv = XDR_GETINT32(xdrs, i32p+1);
254 rv = XDR_GETINT32(xdrs, i32p+1);
257 rv = XDR_GETINT32(xdrs, i32p);
262 if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
264 for (i = 0, lim = dbl_limits; i < nitems(dbl_limits);
266 if ((id.mantissa2 == lim->ieee.mantissa2) &&
267 (id.mantissa1 == lim->ieee.mantissa1) &&
268 (id.exp == lim->ieee.exp)) {
273 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
274 vd.mantissa1 = (id.mantissa1 >> 13);
275 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
276 (id.mantissa2 >> 29);
277 vd.mantissa3 = (id.mantissa2 >> 13);
278 vd.mantissa4 = (id.mantissa2 << 3);
281 *dp = *((double *)&vd);