* misc/sys/cdefs.h (__warndecl, __errordecl): For GCC 4.3+ define
[glibc.git] / sunrpc / xdr_float.c
blob0a7d9707d5993ec8c2fb6d6394002517afa8a226
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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
32 #endif
35 * xdr_float.c, Generic XDR routines implementation.
37 * Copyright (C) 1984, Sun Microsystems, Inc.
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.
44 #include <stdio.h>
45 #include <endian.h>
47 #include <rpc/types.h>
48 #include <rpc/xdr.h>
51 * NB: Not portable.
52 * This routine works on Suns (Sky / 68000's) and Vaxen.
55 #define LSW (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
57 #ifdef vax
59 /* What IEEE single precision floating point looks like on a Vax */
60 struct ieee_single {
61 unsigned int mantissa: 23;
62 unsigned int exp : 8;
63 unsigned int sign : 1;
66 /* Vax single precision floating point */
67 struct vax_single {
68 unsigned int mantissa1 : 7;
69 unsigned int exp : 8;
70 unsigned int sign : 1;
71 unsigned int mantissa2 : 16;
74 #define VAX_SNG_BIAS 0x81
75 #define IEEE_SNG_BIAS 0x7f
77 static struct sgl_limits {
78 struct vax_single s;
79 struct ieee_single ieee;
80 } sgl_limits[2] = {
81 {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
82 { 0x0, 0xff, 0x0 }}, /* Max IEEE */
83 {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
84 { 0x0, 0x0, 0x0 }} /* Min IEEE */
86 #endif /* vax */
88 bool_t
89 xdr_float(xdrs, fp)
90 XDR *xdrs;
91 float *fp;
93 #ifdef vax
94 struct ieee_single is;
95 struct vax_single vs, *vsp;
96 struct sgl_limits *lim;
97 int i;
98 #endif
99 switch (xdrs->x_op) {
101 case XDR_ENCODE:
102 #ifdef vax
103 vs = *((struct vax_single *)fp);
104 for (i = 0, lim = sgl_limits;
105 i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
106 i++, lim++) {
107 if ((vs.mantissa2 == lim->s.mantissa2) &&
108 (vs.exp == lim->s.exp) &&
109 (vs.mantissa1 == lim->s.mantissa1)) {
110 is = lim->ieee;
111 goto shipit;
114 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
115 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
116 shipit:
117 is.sign = vs.sign;
118 return (XDR_PUTLONG(xdrs, (long *)&is));
119 #else
120 if (sizeof(float) == sizeof(long))
121 return (XDR_PUTLONG(xdrs, (long *)fp));
122 else if (sizeof(float) == sizeof(int)) {
123 long tmp = *(int *)fp;
124 return (XDR_PUTLONG(xdrs, &tmp));
126 break;
127 #endif
129 case XDR_DECODE:
130 #ifdef vax
131 vsp = (struct vax_single *)fp;
132 if (!XDR_GETLONG(xdrs, (long *)&is))
133 return (FALSE);
134 for (i = 0, lim = sgl_limits;
135 i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
136 i++, lim++) {
137 if ((is.exp == lim->ieee.exp) &&
138 (is.mantissa == lim->ieee.mantissa)) {
139 *vsp = lim->s;
140 goto doneit;
143 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
144 vsp->mantissa2 = is.mantissa;
145 vsp->mantissa1 = (is.mantissa >> 16);
146 doneit:
147 vsp->sign = is.sign;
148 return (TRUE);
149 #else
150 if (sizeof(float) == sizeof(long))
151 return (XDR_GETLONG(xdrs, (long *)fp));
152 else if (sizeof(float) == sizeof(int)) {
153 long tmp;
154 if (XDR_GETLONG(xdrs, &tmp)) {
155 *(int *)fp = tmp;
156 return (TRUE);
159 break;
160 #endif
162 case XDR_FREE:
163 return (TRUE);
165 return (FALSE);
169 * This routine works on Suns (Sky / 68000's) and Vaxen.
172 #ifdef vax
173 /* What IEEE double precision floating point looks like on a Vax */
174 struct ieee_double {
175 unsigned int mantissa1 : 20;
176 unsigned int exp : 11;
177 unsigned int sign : 1;
178 unsigned int mantissa2 : 32;
181 /* Vax double precision floating point */
182 struct vax_double {
183 unsigned int mantissa1 : 7;
184 unsigned int exp : 8;
185 unsigned int sign : 1;
186 unsigned int mantissa2 : 16;
187 unsigned int mantissa3 : 16;
188 unsigned int mantissa4 : 16;
191 #define VAX_DBL_BIAS 0x81
192 #define IEEE_DBL_BIAS 0x3ff
193 #define MASK(nbits) ((1 << nbits) - 1)
195 static struct dbl_limits {
196 struct vax_double d;
197 struct ieee_double ieee;
198 } dbl_limits[2] = {
199 {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
200 { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
201 {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
202 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
205 #endif /* vax */
208 bool_t
209 xdr_double(xdrs, dp)
210 XDR *xdrs;
211 double *dp;
213 #ifdef vax
214 struct ieee_double id;
215 struct vax_double vd;
216 register struct dbl_limits *lim;
217 int i;
218 #endif
220 switch (xdrs->x_op) {
222 case XDR_ENCODE:
223 #ifdef vax
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;
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 dp = (double *)&id;
245 #endif
246 if (2*sizeof(long) == sizeof(double)) {
247 long *lp = (long *)dp;
248 return (XDR_PUTLONG(xdrs, lp+!LSW) &&
249 XDR_PUTLONG(xdrs, lp+LSW));
250 } else if (2*sizeof(int) == sizeof(double)) {
251 int *ip = (int *)dp;
252 long tmp[2];
253 tmp[0] = ip[!LSW];
254 tmp[1] = ip[LSW];
255 return (XDR_PUTLONG(xdrs, tmp) &&
256 XDR_PUTLONG(xdrs, tmp+1));
258 break;
260 case XDR_DECODE:
261 #ifdef vax
262 lp = (long *)&id;
263 if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
264 return (FALSE);
265 for (i = 0, lim = dbl_limits;
266 i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
267 i++, lim++) {
268 if ((id.mantissa2 == lim->ieee.mantissa2) &&
269 (id.mantissa1 == lim->ieee.mantissa1) &&
270 (id.exp == lim->ieee.exp)) {
271 vd = lim->d;
272 goto doneit;
275 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
276 vd.mantissa1 = (id.mantissa1 >> 13);
277 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
278 (id.mantissa2 >> 29);
279 vd.mantissa3 = (id.mantissa2 >> 13);
280 vd.mantissa4 = (id.mantissa2 << 3);
281 doneit:
282 vd.sign = id.sign;
283 *dp = *((double *)&vd);
284 return (TRUE);
285 #else
286 if (2*sizeof(long) == sizeof(double)) {
287 long *lp = (long *)dp;
288 return (XDR_GETLONG(xdrs, lp+!LSW) &&
289 XDR_GETLONG(xdrs, lp+LSW));
290 } else if (2*sizeof(int) == sizeof(double)) {
291 int *ip = (int *)dp;
292 long tmp[2];
293 if (XDR_GETLONG(xdrs, tmp+!LSW) &&
294 XDR_GETLONG(xdrs, tmp+LSW)) {
295 ip[0] = tmp[0];
296 ip[1] = tmp[1];
297 return (TRUE);
300 break;
301 #endif
303 case XDR_FREE:
304 return (TRUE);
306 return (FALSE);