1 /* $NetBSD: xdr_float.c,v 1.39 2014/08/10 05:57:31 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 #include <sys/cdefs.h> 35 #if defined(LIBC_SCCS) && !defined(lint) 36 #if 0 37 static char *sccsid = "@(#)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 #else 40 __RCSID("$NetBSD: xdr_float.c,v 1.39 2014/08/10 05:57:31 matt Exp $"); 41 #endif 42 #endif 43 44 /* 45 * xdr_float.c, Generic XDR routines implementation. 46 * 47 * Copyright (C) 1984, Sun Microsystems, Inc. 48 * 49 * These are the "floating point" xdr routines used to (de)serialize 50 * most common data items. See xdr.h for more info on the interface to 51 * xdr. 52 */ 53 54 #include "namespace.h" 55 56 #include <sys/types.h> 57 #include <sys/param.h> 58 59 #include <stdio.h> 60 61 #include <rpc/types.h> 62 #include <rpc/xdr.h> 63 64 #ifdef __weak_alias 65 __weak_alias(xdr_double,_xdr_double) 66 __weak_alias(xdr_float,_xdr_float) 67 #endif 68 69 /* 70 * NB: Not portable. 71 * This routine works on machines with IEEE754 FP and Vaxen. 72 */ 73 74 #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \ 75 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \ 76 defined(__arm__) || defined(__powerpc__) || defined(__sh__) || \ 77 defined(__x86_64__) || defined(__hppa__) || defined(__ia64__) || \ 78 defined(__aarch64__) 79 #include <machine/endian.h> 80 #define IEEEFP 81 #endif 82 83 #if defined(__vax__) 84 85 /* What IEEE single precision floating point looks like on a Vax */ 86 struct ieee_single { 87 unsigned int mantissa: 23; 88 unsigned int exp : 8; 89 unsigned int sign : 1; 90 }; 91 92 /* Vax single precision floating point */ 93 struct vax_single { 94 unsigned int mantissa1 : 7; 95 unsigned int exp : 8; 96 unsigned int sign : 1; 97 unsigned int mantissa2 : 16; 98 }; 99 100 #define VAX_SNG_BIAS 0x81 101 #define IEEE_SNG_BIAS 0x7f 102 103 static struct sgl_limits { 104 struct vax_single s; 105 struct ieee_single ieee; 106 } sgl_limits[2] = { 107 {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */ 108 { 0x0, 0xff, 0x0 }}, /* Max IEEE */ 109 {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */ 110 { 0x0, 0x0, 0x0 }} /* Min IEEE */ 111 }; 112 #endif /* vax */ 113 114 bool_t 115 xdr_float(XDR *xdrs, float *fp) 116 { 117 #ifndef IEEEFP 118 struct ieee_single is; 119 struct vax_single vs, *vsp; 120 struct sgl_limits *lim; 121 size_t i; 122 #endif 123 switch (xdrs->x_op) { 124 125 case XDR_ENCODE: 126 #ifdef IEEEFP 127 return (XDR_PUTINT32(xdrs, (int32_t *)(void *)fp)); 128 #else 129 vs = *((struct vax_single *)(void *)fp); 130 for (i = 0, lim = sgl_limits; 131 i < sizeof(sgl_limits)/sizeof(struct sgl_limits); 132 i++, lim++) { 133 if ((vs.mantissa2 == lim->s.mantissa2) && 134 (vs.exp == lim->s.exp) && 135 (vs.mantissa1 == lim->s.mantissa1)) { 136 is = lim->ieee; 137 goto shipit; 138 } 139 } 140 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS; 141 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2; 142 shipit: 143 is.sign = vs.sign; 144 return (XDR_PUTINT32(xdrs, (int32_t *)(void *)&is)); 145 #endif 146 147 case XDR_DECODE: 148 #ifdef IEEEFP 149 return (XDR_GETINT32(xdrs, (int32_t *)(void *)fp)); 150 #else 151 vsp = (struct vax_single *)(void *)fp; 152 if (!XDR_GETINT32(xdrs, (int32_t *)(void *)&is)) 153 return (FALSE); 154 for (i = 0, lim = sgl_limits; 155 i < sizeof(sgl_limits)/sizeof(struct sgl_limits); 156 i++, lim++) { 157 if ((is.exp == lim->ieee.exp) && 158 (is.mantissa == lim->ieee.mantissa)) { 159 *vsp = lim->s; 160 goto doneit; 161 } 162 } 163 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS; 164 vsp->mantissa2 = is.mantissa; 165 vsp->mantissa1 = ((unsigned int)is.mantissa >> 16); 166 doneit: 167 vsp->sign = is.sign; 168 return (TRUE); 169 #endif 170 171 case XDR_FREE: 172 return (TRUE); 173 } 174 /* NOTREACHED */ 175 return (FALSE); 176 } 177 178 #if defined(__vax__) 179 /* What IEEE double precision floating point looks like on a Vax */ 180 struct ieee_double { 181 unsigned int mantissa1 : 20; 182 unsigned int exp : 11; 183 unsigned int sign : 1; 184 unsigned int mantissa2 : 32; 185 }; 186 187 /* Vax double precision floating point */ 188 struct vax_double { 189 unsigned int mantissa1 : 7; 190 unsigned int exp : 8; 191 unsigned int sign : 1; 192 unsigned int mantissa2 : 16; 193 unsigned int mantissa3 : 16; 194 unsigned int mantissa4 : 16; 195 }; 196 197 #define VAX_DBL_BIAS 0x81 198 #define IEEE_DBL_BIAS 0x3ff 199 #define MASK(nbits) ((1 << nbits) - 1) 200 201 static struct dbl_limits { 202 struct vax_double d; 203 struct ieee_double ieee; 204 } dbl_limits[2] = { 205 {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */ 206 { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */ 207 {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */ 208 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */ 209 }; 210 211 #endif /* vax */ 212 213 214 bool_t 215 xdr_double(XDR *xdrs, double *dp) 216 { 217 #ifdef IEEEFP 218 int32_t *i32p; 219 bool_t rv; 220 #else 221 int32_t *lp; 222 struct ieee_double id; 223 struct vax_double vd; 224 struct dbl_limits *lim; 225 size_t i; 226 #endif 227 228 switch (xdrs->x_op) { 229 230 case XDR_ENCODE: 231 #ifdef IEEEFP 232 i32p = (int32_t *)(void *)dp; 233 #if (BYTE_ORDER == BIG_ENDIAN) || \ 234 (defined(__arm__) && !defined(__VFP_FP__)) 235 rv = XDR_PUTINT32(xdrs, i32p); 236 if (!rv) 237 return (rv); 238 rv = XDR_PUTINT32(xdrs, i32p+1); 239 #else 240 rv = XDR_PUTINT32(xdrs, i32p+1); 241 if (!rv) 242 return (rv); 243 rv = XDR_PUTINT32(xdrs, i32p); 244 #endif 245 return (rv); 246 #else 247 vd = *((struct vax_double *)(void *)dp); 248 for (i = 0, lim = dbl_limits; 249 i < sizeof(dbl_limits)/sizeof(struct dbl_limits); 250 i++, lim++) { 251 if ((vd.mantissa4 == lim->d.mantissa4) && 252 (vd.mantissa3 == lim->d.mantissa3) && 253 (vd.mantissa2 == lim->d.mantissa2) && 254 (vd.mantissa1 == lim->d.mantissa1) && 255 (vd.exp == lim->d.exp)) { 256 id = lim->ieee; 257 goto shipit; 258 } 259 } 260 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS; 261 id.mantissa1 = (vd.mantissa1 << 13) | 262 ((unsigned int)vd.mantissa2 >> 3); 263 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) | 264 (vd.mantissa3 << 13) | 265 (((unsigned int)vd.mantissa4 >> 3) & MASK(13)); 266 shipit: 267 id.sign = vd.sign; 268 lp = (int32_t *)(void *)&id; 269 return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp)); 270 #endif 271 272 case XDR_DECODE: 273 #ifdef IEEEFP 274 i32p = (int32_t *)(void *)dp; 275 #if BYTE_ORDER == BIG_ENDIAN || \ 276 (defined(__arm__) && !defined(__VFP_FP__)) 277 rv = XDR_GETINT32(xdrs, i32p); 278 if (!rv) 279 return (rv); 280 rv = XDR_GETINT32(xdrs, i32p+1); 281 #else 282 rv = XDR_GETINT32(xdrs, i32p+1); 283 if (!rv) 284 return (rv); 285 rv = XDR_GETINT32(xdrs, i32p); 286 #endif 287 return (rv); 288 #else 289 lp = (int32_t *)(void *)&id; 290 if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp)) 291 return (FALSE); 292 for (i = 0, lim = dbl_limits; 293 i < sizeof(dbl_limits)/sizeof(struct dbl_limits); 294 i++, lim++) { 295 if ((id.mantissa2 == lim->ieee.mantissa2) && 296 (id.mantissa1 == lim->ieee.mantissa1) && 297 (id.exp == lim->ieee.exp)) { 298 vd = lim->d; 299 goto doneit; 300 } 301 } 302 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS; 303 vd.mantissa1 = ((unsigned int)id.mantissa1 >> 13); 304 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) | 305 ((unsigned int)id.mantissa2 >> 29); 306 vd.mantissa3 = ((unsigned int)id.mantissa2 >> 13); 307 vd.mantissa4 = (id.mantissa2 << 3); 308 doneit: 309 vd.sign = id.sign; 310 *dp = *((double *)(void *)&vd); 311 return (TRUE); 312 #endif 313 314 case XDR_FREE: 315 return (TRUE); 316 } 317 /* NOTREACHED */ 318 return (FALSE); 319 } 320