Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/xdr_float.c

raw symbols SCIP
1
2 /*
3 * Copyright (c) 2009, Sun Microsystems, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * - Neither the name of Sun Microsystems, Inc. nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30
31 /*
32 * xdr_float.c, Generic XDR routines implementation.
33 *
34 * Copyright (C) 1984, Sun Microsystems, Inc.
35 *
36 * These are the "floating point" xdr routines used to (de)serialize
37 * most common data items. See xdr.h for more info on the interface to
38 * xdr.
39 */
40
41 #include <sys/types.h>
42 #include <sys/param.h>
43
44 #include <stdio.h>
45
46 #include <rpc/types.h>
47 #include <rpc/xdr.h>
48 #include "un-namespace.h"
49
50 /*
51 * NB: Not portable.
52 * This routine works on machines with IEEE754 FP and Vaxen.
53 */
54
55 #if defined(__vax__)
56
57 /* What IEEE single precision floating point looks like on a Vax */
58 struct ieee_single {
59 unsigned int mantissa: 23;
60 unsigned int exp : 8;
61 unsigned int sign : 1;
62 };
63
64 /* Vax single precision floating point */
65 struct vax_single {
66 unsigned int mantissa1 : 7;
67 unsigned int exp : 8;
68 unsigned int sign : 1;
69 unsigned int mantissa2 : 16;
70 };
71
72 #define VAX_SNG_BIAS 0x81
73 #define IEEE_SNG_BIAS 0x7f
74
75 static struct sgl_limits {
76 struct vax_single s;
77 struct ieee_single ieee;
78 } sgl_limits[2] = {
79 {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
80 { 0x0, 0xff, 0x0 }}, /* Max IEEE */
81 {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
82 { 0x0, 0x0, 0x0 }} /* Min IEEE */
83 };
84 #else
85
86 #include <endian.h>
87 #define IEEEFP
88
89 #endif /* vax */
90
91 bool_t
92 xdr_float(xdrs, fp)
93 XDR *xdrs;
94 float *fp;
95 {
96 #ifndef IEEEFP
97 struct ieee_single is;
98 struct vax_single vs, *vsp;
99 struct sgl_limits *lim;
100 int i;
101 #endif
102 switch (xdrs->x_op) {
103
104 case XDR_ENCODE:
105 #ifdef IEEEFP
106 return (XDR_PUTINT32(xdrs, (int32_t *)fp));
107 #else
108 vs = *((struct vax_single *)fp);
109 for (i = 0, lim = sgl_limits;
110 i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
111 i++, lim++) {
112 if ((vs.mantissa2 == lim->s.mantissa2) &&
113 (vs.exp == lim->s.exp) &&
114 (vs.mantissa1 == lim->s.mantissa1)) {
115 is = lim->ieee;
116 goto shipit;
117 }
118 }
119 is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
120 is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
121 shipit:
122 is.sign = vs.sign;
123 return (XDR_PUTINT32(xdrs, (int32_t *)&is));
124 #endif
125
126 case XDR_DECODE:
127 #ifdef IEEEFP
128 return (XDR_GETINT32(xdrs, (int32_t *)fp));
129 #else
130 vsp = (struct vax_single *)fp;
131 if (!XDR_GETINT32(xdrs, (int32_t *)&is))
132 return (FALSE);
133 for (i = 0, lim = sgl_limits;
134 i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
135 i++, lim++) {
136 if ((is.exp == lim->ieee.exp) &&
137 (is.mantissa == lim->ieee.mantissa)) {
138 *vsp = lim->s;
139 goto doneit;
140 }
141 }
142 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
143 vsp->mantissa2 = is.mantissa;
144 vsp->mantissa1 = (is.mantissa >> 16);
145 doneit:
146 vsp->sign = is.sign;
147 return (TRUE);
148 #endif
149
150 case XDR_FREE:
151 return (TRUE);
152 }
153 /* NOTREACHED */
154 return (FALSE);
155 }
156
157 #if defined(__vax__)
158 /* What IEEE double precision floating point looks like on a Vax */
159 struct ieee_double {
160 unsigned int mantissa1 : 20;
161 unsigned int exp : 11;
162 unsigned int sign : 1;
163 unsigned int mantissa2 : 32;
164 };
165
166 /* Vax double precision floating point */
167 struct vax_double {
168 unsigned int mantissa1 : 7;
169 unsigned int exp : 8;
170 unsigned int sign : 1;
171 unsigned int mantissa2 : 16;
172 unsigned int mantissa3 : 16;
173 unsigned int mantissa4 : 16;
174 };
175
176 #define VAX_DBL_BIAS 0x81
177 #define IEEE_DBL_BIAS 0x3ff
178 #define MASK(nbits) ((1 << nbits) - 1)
179
180 static struct dbl_limits {
181 struct vax_double d;
182 struct ieee_double ieee;
183 } dbl_limits[2] = {
184 {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
185 { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
186 {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
187 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
188 };
189
190 #endif /* vax */
191
192
193 bool_t
194 xdr_double(xdrs, dp)
195 XDR *xdrs;
196 double *dp;
197 {
198 #ifdef IEEEFP
199 int32_t *i32p;
200 bool_t rv;
201 #else
202 int32_t *lp;
203 struct ieee_double id;
204 struct vax_double vd;
205 struct dbl_limits *lim;
206 int i;
207 #endif
208
209 switch (xdrs->x_op) {
210
211 case XDR_ENCODE:
212 #ifdef IEEEFP
213 i32p = (int32_t *)(void *)dp;
214 #if BYTE_ORDER == BIG_ENDIAN
215 rv = XDR_PUTINT32(xdrs, i32p);
216 if (!rv)
217 return (rv);
218 rv = XDR_PUTINT32(xdrs, i32p+1);
219 #else
220 rv = XDR_PUTINT32(xdrs, i32p+1);
221 if (!rv)
222 return (rv);
223 rv = XDR_PUTINT32(xdrs, i32p);
224 #endif
225 return (rv);
226 #else
227 vd = *((struct vax_double *)dp);
228 for (i = 0, lim = dbl_limits;
229 i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
230 i++, lim++) {
231 if ((vd.mantissa4 == lim->d.mantissa4) &&
232 (vd.mantissa3 == lim->d.mantissa3) &&
233 (vd.mantissa2 == lim->d.mantissa2) &&
234 (vd.mantissa1 == lim->d.mantissa1) &&
235 (vd.exp == lim->d.exp)) {
236 id = lim->ieee;
237 goto shipit;
238 }
239 }
240 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
241 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
242 id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
243 (vd.mantissa3 << 13) |
244 ((vd.mantissa4 >> 3) & MASK(13));
245 shipit:
246 id.sign = vd.sign;
247 lp = (int32_t *)&id;
248 return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
249 #endif
250
251 case XDR_DECODE:
252 #ifdef IEEEFP
253 i32p = (int32_t *)(void *)dp;
254 #if BYTE_ORDER == BIG_ENDIAN
255 rv = XDR_GETINT32(xdrs, i32p);
256 if (!rv)
257 return (rv);
258 rv = XDR_GETINT32(xdrs, i32p+1);
259 #else
260 rv = XDR_GETINT32(xdrs, i32p+1);
261 if (!rv)
262 return (rv);
263 rv = XDR_GETINT32(xdrs, i32p);
264 #endif
265 return (rv);
266 #else
267 lp = (int32_t *)&id;
268 if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
269 return (FALSE);
270 for (i = 0, lim = dbl_limits;
271 i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
272 i++, lim++) {
273 if ((id.mantissa2 == lim->ieee.mantissa2) &&
274 (id.mantissa1 == lim->ieee.mantissa1) &&
275 (id.exp == lim->ieee.exp)) {
276 vd = lim->d;
277 goto doneit;
278 }
279 }
280 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
281 vd.mantissa1 = (id.mantissa1 >> 13);
282 vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
283 (id.mantissa2 >> 29);
284 vd.mantissa3 = (id.mantissa2 >> 13);
285 vd.mantissa4 = (id.mantissa2 << 3);
286 doneit:
287 vd.sign = id.sign;
288 *dp = *((double *)&vd);
289 return (TRUE);
290 #endif
291
292 case XDR_FREE:
293 return (TRUE);
294 }
295 /* NOTREACHED */
296 return (FALSE);
297 }