source: rtems/cpukit/librpc/src/xdr/xdr_float.c @ 15e44fd

4.104.115
Last change on this file since 15e44fd was 15e44fd, checked in by Joel Sherrill <joel.sherrill@…>, on 12/04/08 at 22:53:33

2008-12-04 Jukka Pietarinen <jukka.pietarinen@…>

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