[df49c60] | 1 | /* |
---|
| 2 | * Sun RPC is a product of Sun Microsystems, Inc. and is provided for |
---|
| 3 | * unrestricted use provided that this legend is included on all tape |
---|
| 4 | * media and as a part of the software program in whole or part. Users |
---|
| 5 | * may copy or modify Sun RPC without charge, but are not authorized |
---|
| 6 | * to license or distribute it to anyone else except as part of a product or |
---|
| 7 | * program developed by the user. |
---|
| 8 | * |
---|
| 9 | * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE |
---|
| 10 | * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR |
---|
| 11 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. |
---|
| 12 | * |
---|
| 13 | * Sun RPC is provided with no support and without any obligation on the |
---|
| 14 | * part of Sun Microsystems, Inc. to assist in its use, correction, |
---|
| 15 | * modification or enhancement. |
---|
| 16 | * |
---|
| 17 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE |
---|
| 18 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC |
---|
| 19 | * OR ANY PART THEREOF. |
---|
| 20 | * |
---|
| 21 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue |
---|
| 22 | * or profits or other special, indirect and consequential damages, even if |
---|
| 23 | * Sun has been advised of the possibility of such damages. |
---|
| 24 | * |
---|
| 25 | * Sun Microsystems, Inc. |
---|
| 26 | * 2550 Garcia Avenue |
---|
| 27 | * Mountain View, California 94043 |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
| 31 | /*static char *sccsid = "from: @(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";*/ |
---|
| 32 | /*static char *sccsid = "from: @(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC";*/ |
---|
| 33 | static char *rcsid = "$FreeBSD: src/lib/libc/xdr/xdr_float.c,v 1.7 1999/08/28 00:02:55 peter Exp $"; |
---|
| 34 | #endif |
---|
| 35 | |
---|
| 36 | /* |
---|
| 37 | * xdr_float.c, Generic XDR routines impelmentation. |
---|
| 38 | * |
---|
| 39 | * Copyright (C) 1984, Sun Microsystems, Inc. |
---|
| 40 | * |
---|
| 41 | * These are the "floating point" xdr routines used to (de)serialize |
---|
| 42 | * most common data items. See xdr.h for more info on the interface to |
---|
| 43 | * xdr. |
---|
| 44 | */ |
---|
| 45 | |
---|
| 46 | #include <stdio.h> |
---|
| 47 | #include <sys/types.h> |
---|
| 48 | #include <sys/param.h> |
---|
| 49 | #include <rpc/types.h> |
---|
| 50 | #include <rpc/xdr.h> |
---|
| 51 | |
---|
| 52 | /* |
---|
| 53 | * NB: Not portable. |
---|
| 54 | * This routine works on machines with IEEE754 FP and Vaxen. |
---|
| 55 | */ |
---|
| 56 | |
---|
| 57 | #if defined(__alpha__) || \ |
---|
[a74e94a2] | 58 | defined(_AM29K) || \ |
---|
[2d354ea6] | 59 | defined(__arm__) || \ |
---|
[bbc38ba] | 60 | defined(__H8300__) || defined(__H8300H__) || defined(__H8300S__) || \ |
---|
[df49c60] | 61 | defined(__hppa__) || \ |
---|
| 62 | defined(__i386__) || \ |
---|
| 63 | defined(__m68k__) || defined(__mc68000__) || \ |
---|
| 64 | defined(__mips__) || \ |
---|
[60eae27d] | 65 | defined(__nios2__) || \ |
---|
[df49c60] | 66 | defined(__ns32k__) || \ |
---|
| 67 | defined(__sparc__) || \ |
---|
| 68 | defined(__ppc__) || defined(__PPC__) || \ |
---|
[e5c8049] | 69 | defined(__sh__) || \ |
---|
[e4fec90] | 70 | defined(__AVR__) || \ |
---|
| 71 | defined(__BFIN__) |
---|
[df49c60] | 72 | |
---|
[f75082d] | 73 | #include <rtems/endian.h> |
---|
[df49c60] | 74 | #if !defined(IEEEFP) |
---|
| 75 | #define IEEEFP |
---|
| 76 | #endif |
---|
| 77 | |
---|
| 78 | #elif defined(_TMS320C3x) || defined(_TMS320C4x) |
---|
| 79 | #error "Texas Instruments C3x/C4x Not supported." |
---|
| 80 | |
---|
| 81 | #elif defined(vax) |
---|
| 82 | |
---|
| 83 | /* What IEEE single precision floating point looks like on a Vax */ |
---|
| 84 | struct ieee_single { |
---|
| 85 | unsigned int mantissa: 23; |
---|
| 86 | unsigned int exp : 8; |
---|
| 87 | unsigned int sign : 1; |
---|
| 88 | }; |
---|
| 89 | |
---|
| 90 | /* Vax single precision floating point */ |
---|
| 91 | struct vax_single { |
---|
| 92 | unsigned int mantissa1 : 7; |
---|
| 93 | unsigned int exp : 8; |
---|
| 94 | unsigned int sign : 1; |
---|
| 95 | unsigned int mantissa2 : 16; |
---|
| 96 | }; |
---|
| 97 | |
---|
| 98 | #define VAX_SNG_BIAS 0x81 |
---|
| 99 | #define IEEE_SNG_BIAS 0x7f |
---|
| 100 | |
---|
| 101 | static struct sgl_limits { |
---|
| 102 | struct vax_single s; |
---|
| 103 | struct ieee_single ieee; |
---|
| 104 | } sgl_limits[2] = { |
---|
| 105 | {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */ |
---|
| 106 | { 0x0, 0xff, 0x0 }}, /* Max IEEE */ |
---|
| 107 | {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */ |
---|
| 108 | { 0x0, 0x0, 0x0 }} /* Min IEEE */ |
---|
| 109 | }; |
---|
| 110 | /* end of vax */ |
---|
| 111 | #else |
---|
| 112 | #error "xdr_float.c: unknown CPU" |
---|
| 113 | #endif |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | bool_t |
---|
[a66bc0f] | 117 | xdr_float( |
---|
| 118 | register XDR *xdrs, |
---|
| 119 | register float *fp) |
---|
[df49c60] | 120 | { |
---|
| 121 | #ifdef IEEEFP |
---|
| 122 | bool_t rv; |
---|
| 123 | long tmpl; |
---|
| 124 | #else |
---|
| 125 | struct ieee_single is; |
---|
| 126 | struct vax_single vs, *vsp; |
---|
| 127 | struct sgl_limits *lim; |
---|
| 128 | int i; |
---|
| 129 | #endif |
---|
| 130 | switch (xdrs->x_op) { |
---|
| 131 | |
---|
| 132 | case XDR_ENCODE: |
---|
| 133 | #ifdef IEEEFP |
---|
| 134 | tmpl = *(int32_t *)fp; |
---|
| 135 | return (XDR_PUTLONG(xdrs, &tmpl)); |
---|
| 136 | #else |
---|
| 137 | vs = *((struct vax_single *)fp); |
---|
| 138 | for (i = 0, lim = sgl_limits; |
---|
| 139 | i < sizeof(sgl_limits)/sizeof(struct sgl_limits); |
---|
| 140 | i++, lim++) { |
---|
| 141 | if ((vs.mantissa2 == lim->s.mantissa2) && |
---|
| 142 | (vs.exp == lim->s.exp) && |
---|
| 143 | (vs.mantissa1 == lim->s.mantissa1)) { |
---|
| 144 | is = lim->ieee; |
---|
| 145 | goto shipit; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS; |
---|
| 149 | is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2; |
---|
| 150 | shipit: |
---|
| 151 | is.sign = vs.sign; |
---|
| 152 | return (XDR_PUTLONG(xdrs, (long *)&is)); |
---|
| 153 | #endif |
---|
| 154 | |
---|
| 155 | case XDR_DECODE: |
---|
| 156 | #ifdef IEEEFP |
---|
| 157 | rv = XDR_GETLONG(xdrs, &tmpl); |
---|
| 158 | *(int32_t *)fp = tmpl; |
---|
| 159 | return (rv); |
---|
| 160 | #else |
---|
| 161 | vsp = (struct vax_single *)fp; |
---|
| 162 | if (!XDR_GETLONG(xdrs, (long *)&is)) |
---|
| 163 | return (FALSE); |
---|
| 164 | for (i = 0, lim = sgl_limits; |
---|
| 165 | i < sizeof(sgl_limits)/sizeof(struct sgl_limits); |
---|
| 166 | i++, lim++) { |
---|
| 167 | if ((is.exp == lim->ieee.exp) && |
---|
| 168 | (is.mantissa == lim->ieee.mantissa)) { |
---|
| 169 | *vsp = lim->s; |
---|
| 170 | goto doneit; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS; |
---|
| 174 | vsp->mantissa2 = is.mantissa; |
---|
| 175 | vsp->mantissa1 = (is.mantissa >> 16); |
---|
| 176 | doneit: |
---|
| 177 | vsp->sign = is.sign; |
---|
| 178 | return (TRUE); |
---|
| 179 | #endif |
---|
| 180 | |
---|
| 181 | case XDR_FREE: |
---|
| 182 | return (TRUE); |
---|
| 183 | } |
---|
| 184 | return (FALSE); |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | #ifdef vax |
---|
| 188 | /* What IEEE double precision floating point looks like on a Vax */ |
---|
| 189 | struct ieee_double { |
---|
| 190 | unsigned int mantissa1 : 20; |
---|
| 191 | unsigned int exp : 11; |
---|
| 192 | unsigned int sign : 1; |
---|
| 193 | unsigned int mantissa2 : 32; |
---|
| 194 | }; |
---|
| 195 | |
---|
| 196 | /* Vax double precision floating point */ |
---|
| 197 | struct vax_double { |
---|
| 198 | unsigned int mantissa1 : 7; |
---|
| 199 | unsigned int exp : 8; |
---|
| 200 | unsigned int sign : 1; |
---|
| 201 | unsigned int mantissa2 : 16; |
---|
| 202 | unsigned int mantissa3 : 16; |
---|
| 203 | unsigned int mantissa4 : 16; |
---|
| 204 | }; |
---|
| 205 | |
---|
| 206 | #define VAX_DBL_BIAS 0x81 |
---|
| 207 | #define IEEE_DBL_BIAS 0x3ff |
---|
| 208 | #define MASK(nbits) ((1 << nbits) - 1) |
---|
| 209 | |
---|
| 210 | static struct dbl_limits { |
---|
| 211 | struct vax_double d; |
---|
| 212 | struct ieee_double ieee; |
---|
| 213 | } dbl_limits[2] = { |
---|
| 214 | {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */ |
---|
| 215 | { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */ |
---|
| 216 | {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */ |
---|
| 217 | { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */ |
---|
| 218 | }; |
---|
| 219 | |
---|
| 220 | #endif /* vax */ |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | bool_t |
---|
[a66bc0f] | 224 | xdr_double( |
---|
| 225 | register XDR *xdrs, |
---|
| 226 | double *dp) |
---|
[df49c60] | 227 | { |
---|
| 228 | #ifdef IEEEFP |
---|
| 229 | register int32_t *i32p; |
---|
| 230 | bool_t rv; |
---|
| 231 | long tmpl; |
---|
| 232 | #else |
---|
| 233 | register long *lp; |
---|
| 234 | struct ieee_double id; |
---|
| 235 | struct vax_double vd; |
---|
| 236 | register struct dbl_limits *lim; |
---|
| 237 | int i; |
---|
| 238 | #endif |
---|
| 239 | |
---|
| 240 | switch (xdrs->x_op) { |
---|
| 241 | |
---|
| 242 | case XDR_ENCODE: |
---|
| 243 | #ifdef IEEEFP |
---|
| 244 | i32p = (int32_t *)dp; |
---|
| 245 | #if BYTE_ORDER == BIG_ENDIAN |
---|
| 246 | tmpl = *i32p++; |
---|
| 247 | rv = XDR_PUTLONG(xdrs, &tmpl); |
---|
| 248 | if (!rv) |
---|
| 249 | return (rv); |
---|
| 250 | tmpl = *i32p; |
---|
| 251 | rv = XDR_PUTLONG(xdrs, &tmpl); |
---|
| 252 | #else |
---|
| 253 | tmpl = *(i32p+1); |
---|
| 254 | rv = XDR_PUTLONG(xdrs, &tmpl); |
---|
| 255 | if (!rv) |
---|
| 256 | return (rv); |
---|
| 257 | tmpl = *i32p; |
---|
| 258 | rv = XDR_PUTLONG(xdrs, &tmpl); |
---|
| 259 | #endif |
---|
| 260 | return (rv); |
---|
| 261 | #else |
---|
| 262 | vd = *((struct vax_double *)dp); |
---|
| 263 | for (i = 0, lim = dbl_limits; |
---|
| 264 | i < sizeof(dbl_limits)/sizeof(struct dbl_limits); |
---|
| 265 | i++, lim++) { |
---|
| 266 | if ((vd.mantissa4 == lim->d.mantissa4) && |
---|
| 267 | (vd.mantissa3 == lim->d.mantissa3) && |
---|
| 268 | (vd.mantissa2 == lim->d.mantissa2) && |
---|
| 269 | (vd.mantissa1 == lim->d.mantissa1) && |
---|
| 270 | (vd.exp == lim->d.exp)) { |
---|
| 271 | id = lim->ieee; |
---|
| 272 | goto shipit; |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS; |
---|
| 276 | id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3); |
---|
| 277 | id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) | |
---|
| 278 | (vd.mantissa3 << 13) | |
---|
| 279 | ((vd.mantissa4 >> 3) & MASK(13)); |
---|
| 280 | shipit: |
---|
| 281 | id.sign = vd.sign; |
---|
| 282 | lp = (long *)&id; |
---|
| 283 | return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp)); |
---|
| 284 | #endif |
---|
| 285 | |
---|
| 286 | case XDR_DECODE: |
---|
| 287 | #ifdef IEEEFP |
---|
| 288 | i32p = (int32_t *)dp; |
---|
| 289 | #if BYTE_ORDER == BIG_ENDIAN |
---|
| 290 | rv = XDR_GETLONG(xdrs, &tmpl); |
---|
| 291 | *i32p++ = tmpl; |
---|
| 292 | if (!rv) |
---|
| 293 | return (rv); |
---|
| 294 | rv = XDR_GETLONG(xdrs, &tmpl); |
---|
| 295 | *i32p = tmpl; |
---|
| 296 | #else |
---|
| 297 | rv = XDR_GETLONG(xdrs, &tmpl); |
---|
| 298 | *(i32p+1) = tmpl; |
---|
| 299 | if (!rv) |
---|
| 300 | return (rv); |
---|
| 301 | rv = XDR_GETLONG(xdrs, &tmpl); |
---|
| 302 | *i32p = tmpl; |
---|
| 303 | #endif |
---|
| 304 | return (rv); |
---|
| 305 | #else |
---|
| 306 | lp = (long *)&id; |
---|
| 307 | if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp)) |
---|
| 308 | return (FALSE); |
---|
| 309 | for (i = 0, lim = dbl_limits; |
---|
| 310 | i < sizeof(dbl_limits)/sizeof(struct dbl_limits); |
---|
| 311 | i++, lim++) { |
---|
| 312 | if ((id.mantissa2 == lim->ieee.mantissa2) && |
---|
| 313 | (id.mantissa1 == lim->ieee.mantissa1) && |
---|
| 314 | (id.exp == lim->ieee.exp)) { |
---|
| 315 | vd = lim->d; |
---|
| 316 | goto doneit; |
---|
| 317 | } |
---|
| 318 | } |
---|
| 319 | vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS; |
---|
| 320 | vd.mantissa1 = (id.mantissa1 >> 13); |
---|
| 321 | vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) | |
---|
| 322 | (id.mantissa2 >> 29); |
---|
| 323 | vd.mantissa3 = (id.mantissa2 >> 13); |
---|
| 324 | vd.mantissa4 = (id.mantissa2 << 3); |
---|
| 325 | doneit: |
---|
| 326 | vd.sign = id.sign; |
---|
| 327 | *dp = *((double *)&vd); |
---|
| 328 | return (TRUE); |
---|
| 329 | #endif |
---|
| 330 | |
---|
| 331 | case XDR_FREE: |
---|
| 332 | return (TRUE); |
---|
| 333 | } |
---|
| 334 | return (FALSE); |
---|
| 335 | } |
---|