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