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