1 /* $NetBSD: xdr_float.c,v 1.23 2000/07/17 04:59:51 matt Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * Copyright (c) 2010, Oracle America, Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are 10 * met: 11 * 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. 21 * 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. 34 */ 35 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"; 39 #endif 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 /* 44 * xdr_float.c, Generic XDR routines implementation. 45 * 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 48 * xdr. 49 */ 50 51 #include "namespace.h" 52 #include <sys/param.h> 53 54 #include <stdio.h> 55 56 #include <rpc/types.h> 57 #include <rpc/xdr.h> 58 #include "un-namespace.h" 59 60 /* 61 * NB: Not portable. 62 * This routine works on machines with IEEE754 FP and Vaxen. 63 */ 64 65 #include <machine/endian.h> 66 #define IEEEFP 67 68 #if defined(__vax__) 69 70 /* What IEEE single precision floating point looks like on a Vax */ 71 struct ieee_single { 72 unsigned int mantissa: 23; 73 unsigned int exp : 8; 74 unsigned int sign : 1; 75 }; 76 77 /* Vax single precision floating point */ 78 struct vax_single { 79 unsigned int mantissa1 : 7; 80 unsigned int exp : 8; 81 unsigned int sign : 1; 82 unsigned int mantissa2 : 16; 83 }; 84 85 #define VAX_SNG_BIAS 0x81 86 #define IEEE_SNG_BIAS 0x7f 87 88 static struct sgl_limits { 89 struct vax_single s; 90 struct ieee_single ieee; 91 } sgl_limits[2] = { 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 */ 96 }; 97 #endif /* vax */ 98 99 bool_t 100 xdr_float(XDR *xdrs, float *fp) 101 { 102 #ifndef IEEEFP 103 struct ieee_single is; 104 struct vax_single vs, *vsp; 105 struct sgl_limits *lim; 106 u_int i; 107 #endif 108 switch (xdrs->x_op) { 109 110 case XDR_ENCODE: 111 #ifdef IEEEFP 112 return (XDR_PUTINT32(xdrs, (int32_t *)fp)); 113 #else 114 vs = *((struct vax_single *)fp); 115 for (i = 0, lim = sgl_limits; i < nitems(sgl_limits); 116 i++, lim++) { 117 if ((vs.mantissa2 == lim->s.mantissa2) && 118 (vs.exp == lim->s.exp) && 119 (vs.mantissa1 == lim->s.mantissa1)) { 120 is = lim->ieee; 121 goto shipit; 122 } 123 } 124 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS; 125 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2; 126 shipit: 127 is.sign = vs.sign; 128 return (XDR_PUTINT32(xdrs, (int32_t *)&is)); 129 #endif 130 131 case XDR_DECODE: 132 #ifdef IEEEFP 133 return (XDR_GETINT32(xdrs, (int32_t *)fp)); 134 #else 135 vsp = (struct vax_single *)fp; 136 if (!XDR_GETINT32(xdrs, (int32_t *)&is)) 137 return (FALSE); 138 for (i = 0, lim = sgl_limits; i < nitems(sgl_limits); 139 i++, lim++) { 140 if ((is.exp == lim->ieee.exp) && 141 (is.mantissa == lim->ieee.mantissa)) { 142 *vsp = lim->s; 143 goto doneit; 144 } 145 } 146 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS; 147 vsp->mantissa2 = is.mantissa; 148 vsp->mantissa1 = (is.mantissa >> 16); 149 doneit: 150 vsp->sign = is.sign; 151 return (TRUE); 152 #endif 153 154 case XDR_FREE: 155 return (TRUE); 156 } 157 /* NOTREACHED */ 158 return (FALSE); 159 } 160 161 #if defined(__vax__) 162 /* What IEEE double precision floating point looks like on a Vax */ 163 struct ieee_double { 164 unsigned int mantissa1 : 20; 165 unsigned int exp : 11; 166 unsigned int sign : 1; 167 unsigned int mantissa2 : 32; 168 }; 169 170 /* Vax double precision floating point */ 171 struct vax_double { 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; 178 }; 179 180 #define VAX_DBL_BIAS 0x81 181 #define IEEE_DBL_BIAS 0x3ff 182 #define MASK(nbits) ((1 << nbits) - 1) 183 184 static struct dbl_limits { 185 struct vax_double d; 186 struct ieee_double ieee; 187 } dbl_limits[2] = { 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 */ 192 }; 193 194 #endif /* vax */ 195 196 197 bool_t 198 xdr_double(XDR *xdrs, double *dp) 199 { 200 #ifdef IEEEFP 201 int32_t *i32p; 202 bool_t rv; 203 #else 204 int32_t *lp; 205 struct ieee_double id; 206 struct vax_double vd; 207 struct dbl_limits *lim; 208 u_int i; 209 #endif 210 211 switch (xdrs->x_op) { 212 213 case XDR_ENCODE: 214 #ifdef IEEEFP 215 i32p = (int32_t *)(void *)dp; 216 #if BYTE_ORDER == BIG_ENDIAN 217 rv = XDR_PUTINT32(xdrs, i32p); 218 if (!rv) 219 return (rv); 220 rv = XDR_PUTINT32(xdrs, i32p+1); 221 #else 222 rv = XDR_PUTINT32(xdrs, i32p+1); 223 if (!rv) 224 return (rv); 225 rv = XDR_PUTINT32(xdrs, i32p); 226 #endif 227 return (rv); 228 #else 229 vd = *((struct vax_double *)dp); 230 for (i = 0, lim = dbl_limits; i < nitems(dbl_limits); 231 i++, lim++) { 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)) { 237 id = lim->ieee; 238 goto shipit; 239 } 240 } 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)); 246 shipit: 247 id.sign = vd.sign; 248 lp = (int32_t *)&id; 249 return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp)); 250 #endif 251 252 case XDR_DECODE: 253 #ifdef IEEEFP 254 i32p = (int32_t *)(void *)dp; 255 #if BYTE_ORDER == BIG_ENDIAN 256 rv = XDR_GETINT32(xdrs, i32p); 257 if (!rv) 258 return (rv); 259 rv = XDR_GETINT32(xdrs, i32p+1); 260 #else 261 rv = XDR_GETINT32(xdrs, i32p+1); 262 if (!rv) 263 return (rv); 264 rv = XDR_GETINT32(xdrs, i32p); 265 #endif 266 return (rv); 267 #else 268 lp = (int32_t *)&id; 269 if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp)) 270 return (FALSE); 271 for (i = 0, lim = dbl_limits; i < nitems(dbl_limits); 272 i++, lim++) { 273 if ((id.mantissa2 == lim->ieee.mantissa2) && 274 (id.mantissa1 == lim->ieee.mantissa1) && 275 (id.exp == lim->ieee.exp)) { 276 vd = lim->d; 277 goto doneit; 278 } 279 } 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); 286 doneit: 287 vd.sign = id.sign; 288 *dp = *((double *)&vd); 289 return (TRUE); 290 #endif 291 292 case XDR_FREE: 293 return (TRUE); 294 } 295 /* NOTREACHED */ 296 return (FALSE); 297 } 298