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