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