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