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.
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char *sccsid2 = "@(#)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 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * xdr_float.c, Generic XDR routines implementation.
46 * These are the "floating point" xdr routines used to (de)serialize
47 * most common data items. See xdr.h for more info on the interface to
51 #include "namespace.h"
52 #include <sys/param.h>
56 #include <rpc/types.h>
58 #include "un-namespace.h"
62 * This routine works on machines with IEEE754 FP and Vaxen.
65 #include <machine/endian.h>
70 /* What IEEE single precision floating point looks like on a Vax */
72 unsigned int mantissa: 23;
74 unsigned int sign : 1;
77 /* Vax single precision floating point */
79 unsigned int mantissa1 : 7;
81 unsigned int sign : 1;
82 unsigned int mantissa2 : 16;
85 #define VAX_SNG_BIAS 0x81
86 #define IEEE_SNG_BIAS 0x7f
88 static struct sgl_limits {
90 struct ieee_single ieee;
92 {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
93 { 0x0, 0xff, 0x0 }}, /* Max IEEE */
94 {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
95 { 0x0, 0x0, 0x0 }} /* Min IEEE */
100 xdr_float(XDR *xdrs, float *fp)
103 struct ieee_single is;
104 struct vax_single vs, *vsp;
105 struct sgl_limits *lim;
108 switch (xdrs->x_op) {
112 return (XDR_PUTINT32(xdrs, (int32_t *)fp));
114 vs = *((struct vax_single *)fp);
115 for (i = 0, lim = sgl_limits; i < nitems(sgl_limits);
117 if ((vs.mantissa2 == lim->s.mantissa2) &&
118 (vs.exp == lim->s.exp) &&
119 (vs.mantissa1 == lim->s.mantissa1)) {
124 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
125 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
128 return (XDR_PUTINT32(xdrs, (int32_t *)&is));
133 return (XDR_GETINT32(xdrs, (int32_t *)fp));
135 vsp = (struct vax_single *)fp;
136 if (!XDR_GETINT32(xdrs, (int32_t *)&is))
138 for (i = 0, lim = sgl_limits; i < nitems(sgl_limits);
140 if ((is.exp == lim->ieee.exp) &&
141 (is.mantissa == lim->ieee.mantissa)) {
146 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
147 vsp->mantissa2 = is.mantissa;
148 vsp->mantissa1 = (is.mantissa >> 16);
162 /* What IEEE double precision floating point looks like on a Vax */
164 unsigned int mantissa1 : 20;
165 unsigned int exp : 11;
166 unsigned int sign : 1;
167 unsigned int mantissa2 : 32;
170 /* Vax double precision floating point */
172 unsigned int mantissa1 : 7;
173 unsigned int exp : 8;
174 unsigned int sign : 1;
175 unsigned int mantissa2 : 16;
176 unsigned int mantissa3 : 16;
177 unsigned int mantissa4 : 16;
180 #define VAX_DBL_BIAS 0x81
181 #define IEEE_DBL_BIAS 0x3ff
182 #define MASK(nbits) ((1 << nbits) - 1)
184 static struct dbl_limits {
186 struct ieee_double ieee;
188 {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
189 { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
190 {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
191 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
198 xdr_double(XDR *xdrs, double *dp)
205 struct ieee_double id;
206 struct vax_double vd;
207 struct dbl_limits *lim;
211 switch (xdrs->x_op) {
215 i32p = (int32_t *)(void *)dp;
216 #if BYTE_ORDER == BIG_ENDIAN
217 rv = XDR_PUTINT32(xdrs, i32p);
220 rv = XDR_PUTINT32(xdrs, i32p+1);
222 rv = XDR_PUTINT32(xdrs, i32p+1);
225 rv = XDR_PUTINT32(xdrs, i32p);
229 vd = *((struct vax_double *)dp);
230 for (i = 0, lim = dbl_limits; i < nitems(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; i < nitems(dbl_limits);
273 if ((id.mantissa2 == lim->ieee.mantissa2) &&
274 (id.mantissa1 == lim->ieee.mantissa1) &&
275 (id.exp == lim->ieee.exp)) {
280 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
281 vd.mantissa1 = (id.mantissa1 >> 13);
282 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
283 (id.mantissa2 >> 29);
284 vd.mantissa3 = (id.mantissa2 >> 13);
285 vd.mantissa4 = (id.mantissa2 << 3);
288 *dp = *((double *)&vd);