OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [inet/] [rpc/] [xdr_float.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.