xdr_float.c (a9bdcd3711af3df1131ef95d45b810c28b3941f6) xdr_float.c (eae561b30ec984ce171d99b1fd182575acc2c639)
1/* $NetBSD: xdr_float.c,v 1.23 2000/07/17 04:59:51 matt Exp $ */
2
3/*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
1/*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
10 *
8 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
12 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
18 *
16 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
22 *
20 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
26 *
24 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32#if defined(LIBC_SCCS) && !defined(lint)
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
28 */
29
30#if defined(LIBC_SCCS) && !defined(lint)
33static char *sccsid2 = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
34static char *sccsid = "@(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC";
31/*static char *sccsid = "from: @(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";*/
32/*static char *sccsid = "from: @(#)xdr_float.c 2.1 88/07/29 4.0 RPCSRC";*/
33static char *rcsid = "$Id: xdr_float.c,v 1.1 1993/10/27 05:41:10 paul Exp $";
35#endif
34#endif
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD$");
38
39/*
35
36/*
40 * xdr_float.c, Generic XDR routines implementation.
37 * xdr_float.c, Generic XDR routines impelmentation.
41 *
42 * Copyright (C) 1984, Sun Microsystems, Inc.
43 *
44 * These are the "floating point" xdr routines used to (de)serialize
45 * most common data items. See xdr.h for more info on the interface to
46 * xdr.
47 */
48
38 *
39 * Copyright (C) 1984, Sun Microsystems, Inc.
40 *
41 * These are the "floating point" xdr routines used to (de)serialize
42 * most common data items. See xdr.h for more info on the interface to
43 * xdr.
44 */
45
49#include "namespace.h"
46#include <stdio.h>
50#include <sys/types.h>
51#include <sys/param.h>
47#include <sys/types.h>
48#include <sys/param.h>
52
53#include <stdio.h>
54
55#include <rpc/types.h>
56#include <rpc/xdr.h>
49#include <rpc/types.h>
50#include <rpc/xdr.h>
57#include "un-namespace.h"
58
59/*
60 * NB: Not portable.
51
52/*
53 * NB: Not portable.
61 * This routine works on machines with IEEE754 FP and Vaxen.
54 * This routine works on Suns (Sky / 68000's), i386's, MIPS, NS32k and Vaxen.
62 */
63
55 */
56
64#if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
65 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
66 defined(__arm__) || defined(__ppc__) || defined(__ia64__) || \
67 defined(__arm26__) || defined(__sparc64__) || defined(__amd64__)
68#include <machine/endian.h>
57#if defined(mc68000)||defined(sparc)||defined(i386)||defined(mips)||defined(ns32000)
69#define IEEEFP
70#endif
71
58#define IEEEFP
59#endif
60
72#if defined(__vax__)
61#ifdef vax
73
74/* What IEEE single precision floating point looks like on a Vax */
75struct ieee_single {
76 unsigned int mantissa: 23;
77 unsigned int exp : 8;
78 unsigned int sign : 1;
79};
80

--- 16 unchanged lines hidden (view full) ---

97 { 0x0, 0xff, 0x0 }}, /* Max IEEE */
98 {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
99 { 0x0, 0x0, 0x0 }} /* Min IEEE */
100};
101#endif /* vax */
102
103bool_t
104xdr_float(xdrs, fp)
62
63/* What IEEE single precision floating point looks like on a Vax */
64struct ieee_single {
65 unsigned int mantissa: 23;
66 unsigned int exp : 8;
67 unsigned int sign : 1;
68};
69

--- 16 unchanged lines hidden (view full) ---

86 { 0x0, 0xff, 0x0 }}, /* Max IEEE */
87 {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
88 { 0x0, 0x0, 0x0 }} /* Min IEEE */
89};
90#endif /* vax */
91
92bool_t
93xdr_float(xdrs, fp)
105 XDR *xdrs;
106 float *fp;
94 register XDR *xdrs;
95 register float *fp;
107{
108#ifndef IEEEFP
109 struct ieee_single is;
110 struct vax_single vs, *vsp;
111 struct sgl_limits *lim;
112 int i;
113#endif
114 switch (xdrs->x_op) {
115
116 case XDR_ENCODE:
96{
97#ifndef IEEEFP
98 struct ieee_single is;
99 struct vax_single vs, *vsp;
100 struct sgl_limits *lim;
101 int i;
102#endif
103 switch (xdrs->x_op) {
104
105 case XDR_ENCODE:
117#ifdef IEEEFP
118 return (XDR_PUTINT32(xdrs, (int32_t *)fp));
106#ifdef IEEEFP
107 return (XDR_PUTLONG(xdrs, (long *)fp));
119#else
120 vs = *((struct vax_single *)fp);
121 for (i = 0, lim = sgl_limits;
122 i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
123 i++, lim++) {
124 if ((vs.mantissa2 == lim->s.mantissa2) &&
125 (vs.exp == lim->s.exp) &&
126 (vs.mantissa1 == lim->s.mantissa1)) {
127 is = lim->ieee;
128 goto shipit;
129 }
130 }
131 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
132 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
133 shipit:
134 is.sign = vs.sign;
108#else
109 vs = *((struct vax_single *)fp);
110 for (i = 0, lim = sgl_limits;
111 i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
112 i++, lim++) {
113 if ((vs.mantissa2 == lim->s.mantissa2) &&
114 (vs.exp == lim->s.exp) &&
115 (vs.mantissa1 == lim->s.mantissa1)) {
116 is = lim->ieee;
117 goto shipit;
118 }
119 }
120 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
121 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
122 shipit:
123 is.sign = vs.sign;
135 return (XDR_PUTINT32(xdrs, (int32_t *)&is));
124 return (XDR_PUTLONG(xdrs, (long *)&is));
136#endif
137
138 case XDR_DECODE:
139#ifdef IEEEFP
125#endif
126
127 case XDR_DECODE:
128#ifdef IEEEFP
140 return (XDR_GETINT32(xdrs, (int32_t *)fp));
129 return (XDR_GETLONG(xdrs, (long *)fp));
141#else
142 vsp = (struct vax_single *)fp;
130#else
131 vsp = (struct vax_single *)fp;
143 if (!XDR_GETINT32(xdrs, (int32_t *)&is))
132 if (!XDR_GETLONG(xdrs, (long *)&is))
144 return (FALSE);
145 for (i = 0, lim = sgl_limits;
146 i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
147 i++, lim++) {
148 if ((is.exp == lim->ieee.exp) &&
149 (is.mantissa == lim->ieee.mantissa)) {
150 *vsp = lim->s;
151 goto doneit;

--- 5 unchanged lines hidden (view full) ---

157 doneit:
158 vsp->sign = is.sign;
159 return (TRUE);
160#endif
161
162 case XDR_FREE:
163 return (TRUE);
164 }
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;

--- 5 unchanged lines hidden (view full) ---

146 doneit:
147 vsp->sign = is.sign;
148 return (TRUE);
149#endif
150
151 case XDR_FREE:
152 return (TRUE);
153 }
165 /* NOTREACHED */
166 return (FALSE);
167}
168
154 return (FALSE);
155}
156
169#if defined(__vax__)
157/*
158 * This routine works on Suns (Sky / 68000's), i386's, MIPS and Vaxen.
159 */
160
161#ifdef vax
170/* What IEEE double precision floating point looks like on a Vax */
171struct ieee_double {
172 unsigned int mantissa1 : 20;
173 unsigned int exp : 11;
174 unsigned int sign : 1;
175 unsigned int mantissa2 : 32;
176};
177

--- 21 unchanged lines hidden (view full) ---

199 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
200};
201
202#endif /* vax */
203
204
205bool_t
206xdr_double(xdrs, dp)
162/* What IEEE double precision floating point looks like on a Vax */
163struct ieee_double {
164 unsigned int mantissa1 : 20;
165 unsigned int exp : 11;
166 unsigned int sign : 1;
167 unsigned int mantissa2 : 32;
168};
169

--- 21 unchanged lines hidden (view full) ---

191 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
192};
193
194#endif /* vax */
195
196
197bool_t
198xdr_double(xdrs, dp)
207 XDR *xdrs;
199 register XDR *xdrs;
208 double *dp;
209{
200 double *dp;
201{
210#ifdef IEEEFP
211 int32_t *i32p;
212 bool_t rv;
213#else
214 int32_t *lp;
202 register long *lp;
203#ifndef IEEEFP
215 struct ieee_double id;
216 struct vax_double vd;
204 struct ieee_double id;
205 struct vax_double vd;
217 struct dbl_limits *lim;
206 register struct dbl_limits *lim;
218 int i;
219#endif
220
221 switch (xdrs->x_op) {
222
223 case XDR_ENCODE:
224#ifdef IEEEFP
207 int i;
208#endif
209
210 switch (xdrs->x_op) {
211
212 case XDR_ENCODE:
213#ifdef IEEEFP
225 i32p = (int32_t *)(void *)dp;
214 lp = (long *)dp;
226#if BYTE_ORDER == BIG_ENDIAN
215#if BYTE_ORDER == BIG_ENDIAN
227 rv = XDR_PUTINT32(xdrs, i32p);
228 if (!rv)
229 return (rv);
230 rv = XDR_PUTINT32(xdrs, i32p+1);
216 return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
231#else
217#else
232 rv = XDR_PUTINT32(xdrs, i32p+1);
233 if (!rv)
234 return (rv);
235 rv = XDR_PUTINT32(xdrs, i32p);
218 return (XDR_PUTLONG(xdrs, lp+1) && XDR_PUTLONG(xdrs, lp));
236#endif
219#endif
237 return (rv);
238#else
239 vd = *((struct vax_double *)dp);
240 for (i = 0, lim = dbl_limits;
241 i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
242 i++, lim++) {
243 if ((vd.mantissa4 == lim->d.mantissa4) &&
244 (vd.mantissa3 == lim->d.mantissa3) &&
245 (vd.mantissa2 == lim->d.mantissa2) &&

--- 5 unchanged lines hidden (view full) ---

251 }
252 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
253 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
254 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
255 (vd.mantissa3 << 13) |
256 ((vd.mantissa4 >> 3) & MASK(13));
257 shipit:
258 id.sign = vd.sign;
220#else
221 vd = *((struct vax_double *)dp);
222 for (i = 0, lim = dbl_limits;
223 i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
224 i++, lim++) {
225 if ((vd.mantissa4 == lim->d.mantissa4) &&
226 (vd.mantissa3 == lim->d.mantissa3) &&
227 (vd.mantissa2 == lim->d.mantissa2) &&

--- 5 unchanged lines hidden (view full) ---

233 }
234 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
235 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
236 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
237 (vd.mantissa3 << 13) |
238 ((vd.mantissa4 >> 3) & MASK(13));
239 shipit:
240 id.sign = vd.sign;
259 lp = (int32_t *)&id;
260 return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
241 lp = (long *)&id;
242 return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
261#endif
262
263 case XDR_DECODE:
264#ifdef IEEEFP
243#endif
244
245 case XDR_DECODE:
246#ifdef IEEEFP
265 i32p = (int32_t *)(void *)dp;
247 lp = (long *)dp;
266#if BYTE_ORDER == BIG_ENDIAN
248#if BYTE_ORDER == BIG_ENDIAN
267 rv = XDR_GETINT32(xdrs, i32p);
268 if (!rv)
269 return (rv);
270 rv = XDR_GETINT32(xdrs, i32p+1);
249 return (XDR_GETLONG(xdrs, lp++) && XDR_GETLONG(xdrs, lp));
271#else
250#else
272 rv = XDR_GETINT32(xdrs, i32p+1);
273 if (!rv)
274 return (rv);
275 rv = XDR_GETINT32(xdrs, i32p);
251 return (XDR_GETLONG(xdrs, lp+1) && XDR_GETLONG(xdrs, lp));
276#endif
252#endif
277 return (rv);
278#else
253#else
279 lp = (int32_t *)&id;
280 if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
254 lp = (long *)&id;
255 if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
281 return (FALSE);
282 for (i = 0, lim = dbl_limits;
283 i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
284 i++, lim++) {
285 if ((id.mantissa2 == lim->ieee.mantissa2) &&
286 (id.mantissa1 == lim->ieee.mantissa1) &&
287 (id.exp == lim->ieee.exp)) {
288 vd = lim->d;

--- 10 unchanged lines hidden (view full) ---

299 vd.sign = id.sign;
300 *dp = *((double *)&vd);
301 return (TRUE);
302#endif
303
304 case XDR_FREE:
305 return (TRUE);
306 }
256 return (FALSE);
257 for (i = 0, lim = dbl_limits;
258 i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
259 i++, lim++) {
260 if ((id.mantissa2 == lim->ieee.mantissa2) &&
261 (id.mantissa1 == lim->ieee.mantissa1) &&
262 (id.exp == lim->ieee.exp)) {
263 vd = lim->d;

--- 10 unchanged lines hidden (view full) ---

274 vd.sign = id.sign;
275 *dp = *((double *)&vd);
276 return (TRUE);
277#endif
278
279 case XDR_FREE:
280 return (TRUE);
281 }
307 /* NOTREACHED */
308 return (FALSE);
309}
282 return (FALSE);
283}