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