source: rtems/cpukit/librpc/src/xdr/xdr_float.c @ 0c5b59de

4.104.115
Last change on this file since 0c5b59de was 0c5b59de, checked in by Joel Sherrill <joel.sherrill@…>, on 01/08/09 at 15:02:25

2009-01-08 Joel Sherrill <joel.sherrill@…>

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