source: rtems/cpukit/librpc/src/xdr/xdr_float.c @ 08330bf

4.104.114.84.95
Last change on this file since 08330bf was bbc38ba, checked in by Joel Sherrill <joel.sherrill@…>, on 06/29/00 at 22:35:17

Added H8 as IEEE whether this is true or not.

  • Property mode set to 100644
File size: 8.0 KB
Line 
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";*/
33static 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__) || \
58    defined(__arm32__) || \
59    defined(__H8300__) || defined(__H8300H__) || defined(__H8300S__) || \
60    defined(__hppa__) || \
61    defined(__i386__) || \
62    defined(__i960__) || \
63    defined(__m68k__) || defined(__mc68000__) || \
64    defined(__mips__) || \
65    defined(__ns32k__) || \
66    defined(__sparc__) || \
67    defined(__ppc__) || defined(__PPC__) || \
68    defined(__sh__)
69
70#include <machine/endian.h>
71#if !defined(IEEEFP)
72#define IEEEFP
73#endif
74
75#elif defined(_TMS320C3x) || defined(_TMS320C4x)
76#error "Texas Instruments C3x/C4x Not supported."
77
78#elif defined(vax)
79
80/* What IEEE single precision floating point looks like on a Vax */
81struct  ieee_single {
82        unsigned int    mantissa: 23;
83        unsigned int    exp     : 8;
84        unsigned int    sign    : 1;
85};
86
87/* Vax single precision floating point */
88struct  vax_single {
89        unsigned int    mantissa1 : 7;
90        unsigned int    exp       : 8;
91        unsigned int    sign      : 1;
92        unsigned int    mantissa2 : 16;
93};
94
95#define VAX_SNG_BIAS    0x81
96#define IEEE_SNG_BIAS   0x7f
97
98static struct sgl_limits {
99        struct vax_single s;
100        struct ieee_single ieee;
101} sgl_limits[2] = {
102        {{ 0x7f, 0xff, 0x0, 0xffff },   /* Max Vax */
103        { 0x0, 0xff, 0x0 }},            /* Max IEEE */
104        {{ 0x0, 0x0, 0x0, 0x0 },        /* Min Vax */
105        { 0x0, 0x0, 0x0 }}              /* Min IEEE */
106};
107/* end of vax */
108#else
109#error "xdr_float.c: unknown CPU"
110#endif
111
112
113bool_t
114xdr_float(xdrs, fp)
115        register XDR *xdrs;
116        register float *fp;
117{
118#ifdef IEEEFP
119        bool_t rv;
120        long tmpl;
121#else
122        struct ieee_single is;
123        struct vax_single vs, *vsp;
124        struct sgl_limits *lim;
125        int i;
126#endif
127        switch (xdrs->x_op) {
128
129        case XDR_ENCODE:
130#ifdef IEEEFP
131                tmpl = *(int32_t *)fp;
132                return (XDR_PUTLONG(xdrs, &tmpl));
133#else
134                vs = *((struct vax_single *)fp);
135                for (i = 0, lim = sgl_limits;
136                        i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
137                        i++, lim++) {
138                        if ((vs.mantissa2 == lim->s.mantissa2) &&
139                                (vs.exp == lim->s.exp) &&
140                                (vs.mantissa1 == lim->s.mantissa1)) {
141                                is = lim->ieee;
142                                goto shipit;
143                        }
144                }
145                is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
146                is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
147        shipit:
148                is.sign = vs.sign;
149                return (XDR_PUTLONG(xdrs, (long *)&is));
150#endif
151
152        case XDR_DECODE:
153#ifdef IEEEFP
154                rv = XDR_GETLONG(xdrs, &tmpl);
155                *(int32_t *)fp = tmpl;
156                return (rv);
157#else
158                vsp = (struct vax_single *)fp;
159                if (!XDR_GETLONG(xdrs, (long *)&is))
160                        return (FALSE);
161                for (i = 0, lim = sgl_limits;
162                        i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
163                        i++, lim++) {
164                        if ((is.exp == lim->ieee.exp) &&
165                                (is.mantissa == lim->ieee.mantissa)) {
166                                *vsp = lim->s;
167                                goto doneit;
168                        }
169                }
170                vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
171                vsp->mantissa2 = is.mantissa;
172                vsp->mantissa1 = (is.mantissa >> 16);
173        doneit:
174                vsp->sign = is.sign;
175                return (TRUE);
176#endif
177
178        case XDR_FREE:
179                return (TRUE);
180        }
181        return (FALSE);
182}
183
184#ifdef vax
185/* What IEEE double precision floating point looks like on a Vax */
186struct  ieee_double {
187        unsigned int    mantissa1 : 20;
188        unsigned int    exp       : 11;
189        unsigned int    sign      : 1;
190        unsigned int    mantissa2 : 32;
191};
192
193/* Vax double precision floating point */
194struct  vax_double {
195        unsigned int    mantissa1 : 7;
196        unsigned int    exp       : 8;
197        unsigned int    sign      : 1;
198        unsigned int    mantissa2 : 16;
199        unsigned int    mantissa3 : 16;
200        unsigned int    mantissa4 : 16;
201};
202
203#define VAX_DBL_BIAS    0x81
204#define IEEE_DBL_BIAS   0x3ff
205#define MASK(nbits)     ((1 << nbits) - 1)
206
207static struct dbl_limits {
208        struct  vax_double d;
209        struct  ieee_double ieee;
210} dbl_limits[2] = {
211        {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },   /* Max Vax */
212        { 0x0, 0x7ff, 0x0, 0x0 }},                      /* Max IEEE */
213        {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},               /* Min Vax */
214        { 0x0, 0x0, 0x0, 0x0 }}                         /* Min IEEE */
215};
216
217#endif /* vax */
218
219
220bool_t
221xdr_double(xdrs, dp)
222        register XDR *xdrs;
223        double *dp;
224{
225#ifdef IEEEFP
226        register int32_t *i32p;
227        bool_t rv;
228        long tmpl;
229#else
230        register long *lp;
231        struct  ieee_double id;
232        struct  vax_double vd;
233        register struct dbl_limits *lim;
234        int i;
235#endif
236
237        switch (xdrs->x_op) {
238
239        case XDR_ENCODE:
240#ifdef IEEEFP
241                i32p = (int32_t *)dp;
242#if BYTE_ORDER == BIG_ENDIAN
243                tmpl = *i32p++;
244                rv = XDR_PUTLONG(xdrs, &tmpl);
245                if (!rv)
246                        return (rv);
247                tmpl = *i32p;
248                rv = XDR_PUTLONG(xdrs, &tmpl);
249#else
250                tmpl = *(i32p+1);
251                rv = XDR_PUTLONG(xdrs, &tmpl);
252                if (!rv)
253                        return (rv);
254                tmpl = *i32p;
255                rv = XDR_PUTLONG(xdrs, &tmpl);
256#endif
257                return (rv);
258#else
259                vd = *((struct vax_double *)dp);
260                for (i = 0, lim = dbl_limits;
261                        i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
262                        i++, lim++) {
263                        if ((vd.mantissa4 == lim->d.mantissa4) &&
264                                (vd.mantissa3 == lim->d.mantissa3) &&
265                                (vd.mantissa2 == lim->d.mantissa2) &&
266                                (vd.mantissa1 == lim->d.mantissa1) &&
267                                (vd.exp == lim->d.exp)) {
268                                id = lim->ieee;
269                                goto shipit;
270                        }
271                }
272                id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
273                id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
274                id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
275                                (vd.mantissa3 << 13) |
276                                ((vd.mantissa4 >> 3) & MASK(13));
277        shipit:
278                id.sign = vd.sign;
279                lp = (long *)&id;
280                return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
281#endif
282
283        case XDR_DECODE:
284#ifdef IEEEFP
285                i32p = (int32_t *)dp;
286#if BYTE_ORDER == BIG_ENDIAN
287                rv = XDR_GETLONG(xdrs, &tmpl);
288                *i32p++ = tmpl;
289                if (!rv)
290                        return (rv);
291                rv = XDR_GETLONG(xdrs, &tmpl);
292                *i32p = tmpl;
293#else
294                rv = XDR_GETLONG(xdrs, &tmpl);
295                *(i32p+1) = tmpl;
296                if (!rv)
297                        return (rv);
298                rv = XDR_GETLONG(xdrs, &tmpl);
299                *i32p = tmpl;
300#endif
301                return (rv);
302#else
303                lp = (long *)&id;
304                if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
305                        return (FALSE);
306                for (i = 0, lim = dbl_limits;
307                        i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
308                        i++, lim++) {
309                        if ((id.mantissa2 == lim->ieee.mantissa2) &&
310                                (id.mantissa1 == lim->ieee.mantissa1) &&
311                                (id.exp == lim->ieee.exp)) {
312                                vd = lim->d;
313                                goto doneit;
314                        }
315                }
316                vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
317                vd.mantissa1 = (id.mantissa1 >> 13);
318                vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
319                                (id.mantissa2 >> 29);
320                vd.mantissa3 = (id.mantissa2 >> 13);
321                vd.mantissa4 = (id.mantissa2 << 3);
322        doneit:
323                vd.sign = id.sign;
324                *dp = *((double *)&vd);
325                return (TRUE);
326#endif
327
328        case XDR_FREE:
329                return (TRUE);
330        }
331        return (FALSE);
332}
Note: See TracBrowser for help on using the repository browser.