~ubuntu-branches/ubuntu/karmic/scilab/karmic

« back to all changes in this revision

Viewing changes to routines/xdr/xdr_float.c

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2002-03-21 16:57:43 UTC
  • Revision ID: james.westby@ubuntu.com-20020321165743-e9mv12c1tb1plztg
Tags: upstream-2.6
ImportĀ upstreamĀ versionĀ 2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* @(#)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 !defined(lint) && defined(SCCSIDS)
 
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 impelmentation.
 
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
#include <stdio.h>
 
45
#ifndef __ABSC__
 
46
#include <sys/types.h>
 
47
#endif
 
48
 
 
49
#ifdef __MINGW32__
 
50
#define __MSC__
 
51
#endif 
 
52
 
 
53
#if !(defined __MSC__) && !(defined __ABSC__)
 
54
#include <sys/param.h>
 
55
#include <rpc/types.h> 
 
56
#include <rpc/xdr.h>
 
57
#else
 
58
#include "rpc/types.h" 
 
59
#include "rpc/xdr.h"
 
60
#endif
 
61
 
 
62
 
 
63
/*
 
64
 * NB: Not portable.
 
65
 * This routine works on Suns (Sky / 68000's) and Vaxen.
 
66
 */
 
67
 
 
68
#if defined(vax) || defined(WIN32)
 
69
 
 
70
/* What IEEE single precision floating point looks like on a Vax */
 
71
struct  ieee_single {
 
72
        unsigned int    mantissa: 23;
 
73
        unsigned int    exp     : 8;
 
74
        unsigned int    sign    : 1;
 
75
};
 
76
 
 
77
/* Vax single precision floating point */
 
78
struct  vax_single {
 
79
        unsigned int    mantissa1 : 7;
 
80
        unsigned int    exp       : 8;
 
81
        unsigned int    sign      : 1;
 
82
        unsigned int    mantissa2 : 16;
 
83
};
 
84
 
 
85
#define VAX_SNG_BIAS    0x81
 
86
#define IEEE_SNG_BIAS   0x7f
 
87
 
 
88
static struct sgl_limits {
 
89
        struct vax_single s;
 
90
        struct ieee_single ieee;
 
91
} sgl_limits[2] = {
 
92
        {{ 0x7f, 0xff, 0x0, 0xffff },   /* Max Vax */
 
93
        { 0x0, 0xff, 0x0 }},            /* Max IEEE */
 
94
        {{ 0x0, 0x0, 0x0, 0x0 },        /* Min Vax */
 
95
        { 0x0, 0x0, 0x0 }}              /* Min IEEE */
 
96
};
 
97
#endif /* vax */
 
98
 
 
99
bool_t
 
100
xdr_float(xdrs, fp)
 
101
        register XDR *xdrs;
 
102
        register float *fp;
 
103
{
 
104
 
 
105
#if !defined(mc68000) && !defined(sparc) && !defined(mips) && !defined(mmax) && !defined(_X86_)
 
106
        struct ieee_single is;
 
107
        struct vax_single vs, *vsp;
 
108
        struct sgl_limits *lim;
 
109
        int i;
 
110
#endif
 
111
        switch (xdrs->x_op) {
 
112
 
 
113
        case XDR_ENCODE:
 
114
#if defined(WIN32) || defined(mc68000) || defined(sparc) || defined(mips) || defined(mmax) || defined(_X86_)
 
115
                return (XDR_PUTLONG(xdrs, (long *)fp));
 
116
#else
 
117
                vs = *((struct vax_single *)fp);
 
118
                for (i = 0, lim = sgl_limits;
 
119
                        i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
 
120
                        i++, lim++) {
 
121
                        if ((vs.mantissa2 == lim->s.mantissa2) &&
 
122
                                (vs.exp == lim->s.exp) &&
 
123
                                (vs.mantissa1 == lim->s.mantissa1)) {
 
124
                                is = lim->ieee;
 
125
                                goto shipit;
 
126
                        }
 
127
                }
 
128
                is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
 
129
                is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
 
130
        shipit:
 
131
                is.sign = vs.sign;
 
132
                return (XDR_PUTLONG(xdrs, (long *)&is));
 
133
#endif
 
134
 
 
135
        case XDR_DECODE:
 
136
#if defined(WIN32) || defined(mc68000) || defined(sparc) || defined(mips) || defined(mmax) || defined(_X86_)
 
137
                return (XDR_GETLONG(xdrs, (long *)fp));
 
138
#else
 
139
                vsp = (struct vax_single *)fp;
 
140
                if (!XDR_GETLONG(xdrs, (long *)&is))
 
141
                        return (FALSE);
 
142
                for (i = 0, lim = sgl_limits;
 
143
                        i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
 
144
                        i++, lim++) {
 
145
                        if ((is.exp == lim->ieee.exp) &&
 
146
                                (is.mantissa == lim->ieee.mantissa)) {
 
147
                                *vsp = lim->s;
 
148
                                goto doneit;
 
149
                        }
 
150
                }
 
151
                vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
 
152
                vsp->mantissa2 = is.mantissa;
 
153
                vsp->mantissa1 = (is.mantissa >> 16);
 
154
        doneit:
 
155
                vsp->sign = is.sign;
 
156
                return (TRUE);
 
157
#endif
 
158
 
 
159
        case XDR_FREE:
 
160
                return (TRUE);
 
161
        }
 
162
        return (FALSE);
 
163
}
 
164
 
 
165
/*
 
166
 * This routine works on Suns (Sky / 68000's) and Vaxen.
 
167
 */
 
168
 
 
169
#ifdef vax
 
170
/* What IEEE double precision floating point looks like on a Vax */
 
171
struct  ieee_double {
 
172
        unsigned int    mantissa1 : 20;
 
173
        unsigned int    exp       : 11;
 
174
        unsigned int    sign      : 1;
 
175
        unsigned int    mantissa2 : 32;
 
176
};
 
177
 
 
178
/* Vax double precision floating point */
 
179
struct  vax_double {
 
180
        unsigned int    mantissa1 : 7;
 
181
        unsigned int    exp       : 8;
 
182
        unsigned int    sign      : 1;
 
183
        unsigned int    mantissa2 : 16;
 
184
        unsigned int    mantissa3 : 16;
 
185
        unsigned int    mantissa4 : 16;
 
186
};
 
187
 
 
188
#define VAX_DBL_BIAS    0x81
 
189
#define IEEE_DBL_BIAS   0x3ff
 
190
#define MASK(nbits)     ((1 << nbits) - 1)
 
191
 
 
192
static struct dbl_limits {
 
193
        struct  vax_double d;
 
194
        struct  ieee_double ieee;
 
195
} dbl_limits[2] = {
 
196
        {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },   /* Max Vax */
 
197
        { 0x0, 0x7ff, 0x0, 0x0 }},                      /* Max IEEE */
 
198
        {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},               /* Min Vax */
 
199
        { 0x0, 0x0, 0x0, 0x0 }}                         /* Min IEEE */
 
200
};
 
201
 
 
202
#endif /* vax */
 
203
 
 
204
 
 
205
bool_t
 
206
xdr_double(xdrs, dp)
 
207
        register XDR *xdrs;
 
208
        double *dp;
 
209
{
 
210
        register long *lp;
 
211
#if !defined(WIN32) && !defined(mc68000) && !defined(sparc) && !defined(mips) && !defined(mmax) && !defined(_X86_)
 
212
        struct  ieee_double id;
 
213
        struct  vax_double vd;
 
214
        register struct dbl_limits *lim;
 
215
        int i;
 
216
#endif
 
217
 
 
218
        switch (xdrs->x_op) {
 
219
 
 
220
        case XDR_ENCODE:
 
221
#if defined(WIN32) || defined(mc68000) || defined(sparc) || defined(mips) || defined(mmax) || defined(_X86_)
 
222
                lp = (long *)dp;
 
223
#else
 
224
                vd = *((struct vax_double *)dp);
 
225
                for (i = 0, lim = dbl_limits;
 
226
                        i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
 
227
                        i++, lim++) {
 
228
                        if ((vd.mantissa4 == lim->d.mantissa4) &&
 
229
                                (vd.mantissa3 == lim->d.mantissa3) &&
 
230
                                (vd.mantissa2 == lim->d.mantissa2) &&
 
231
                                (vd.mantissa1 == lim->d.mantissa1) &&
 
232
                                (vd.exp == lim->d.exp)) {
 
233
                                id = lim->ieee;
 
234
                                goto shipit;
 
235
                        }
 
236
                }
 
237
                id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
 
238
                id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
 
239
                id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
 
240
                                (vd.mantissa3 << 13) |
 
241
                                ((vd.mantissa4 >> 3) & MASK(13));
 
242
        shipit:
 
243
                id.sign = vd.sign;
 
244
                lp = (long *)&id;
 
245
#endif
 
246
#if defined(WIN32) || defined(_X86_) 
 
247
                return (XDR_PUTLONG(xdrs, lp+1) && XDR_PUTLONG(xdrs, lp));
 
248
#else
 
249
                return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
 
250
#endif
 
251
        case XDR_DECODE:
 
252
#if defined(WIN32) || defined(mc68000) || defined(sparc) || defined(mips) || defined(mmax) || defined(_X86_)
 
253
                lp = (long *)dp;
 
254
#if defined(WIN32) || defined(_X86_)
 
255
                return (XDR_GETLONG(xdrs, lp+1) && XDR_GETLONG(xdrs, lp));
 
256
#else
 
257
                return (XDR_GETLONG(xdrs, lp++) && XDR_GETLONG(xdrs, lp));
 
258
#endif
 
259
#else
 
260
                lp = (long *)&id;
 
261
                if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
 
262
                        return (FALSE);
 
263
                for (i = 0, lim = dbl_limits;
 
264
                        i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
 
265
                        i++, lim++) {
 
266
                        if ((id.mantissa2 == lim->ieee.mantissa2) &&
 
267
                                (id.mantissa1 == lim->ieee.mantissa1) &&
 
268
                                (id.exp == lim->ieee.exp)) {
 
269
                                vd = lim->d;
 
270
                                goto doneit;
 
271
                        }
 
272
                }
 
273
                vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
 
274
                vd.mantissa1 = (id.mantissa1 >> 13);
 
275
                vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
 
276
                                (id.mantissa2 >> 29);
 
277
                vd.mantissa3 = (id.mantissa2 >> 13);
 
278
                vd.mantissa4 = (id.mantissa2 << 3);
 
279
        doneit:
 
280
                vd.sign = id.sign;
 
281
                *dp = *((double *)&vd);
 
282
                return (TRUE);
 
283
#endif
 
284
 
 
285
        case XDR_FREE:
 
286
                return (TRUE);
 
287
        }
 
288
        return (FALSE);
 
289
}