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