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

4.115
Last change on this file since 39655741 was 39655741, checked in by Hesham ALMatary <heshamelmatary@…>, on 03/03/15 at 18:48:14

librpc: Include or1knd part of the recongnized CPUs at xdr_float.c

Close #2256

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