2 * xdr_float.c, Generic XDR routines implementation.
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
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.
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.
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
41 #include <rpc/types.h>
46 * This routine works on Suns (Sky / 68000's) and Vaxen.
49 #define LSW (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
53 /* What IEEE single precision floating point looks like on a Vax */
55 unsigned int mantissa
: 23;
57 unsigned int sign
: 1;
60 /* Vax single precision floating point */
62 unsigned int mantissa1
: 7;
64 unsigned int sign
: 1;
65 unsigned int mantissa2
: 16;
68 #define VAX_SNG_BIAS 0x81
69 #define IEEE_SNG_BIAS 0x7f
71 static struct sgl_limits
{
73 struct ieee_single ieee
;
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 */
88 struct ieee_single is
;
89 struct vax_single vs
, *vsp
;
90 struct sgl_limits
*lim
;
97 vs
= *((struct vax_single
*)fp
);
98 for (i
= 0, lim
= sgl_limits
;
99 i
< sizeof(sgl_limits
)/sizeof(struct sgl_limits
);
101 if ((vs
.mantissa2
== lim
->s
.mantissa2
) &&
102 (vs
.exp
== lim
->s
.exp
) &&
103 (vs
.mantissa1
== lim
->s
.mantissa1
)) {
108 is
.exp
= vs
.exp
- VAX_SNG_BIAS
+ IEEE_SNG_BIAS
;
109 is
.mantissa
= (vs
.mantissa1
<< 16) | vs
.mantissa2
;
112 return (XDR_PUTLONG(xdrs
, (long *)&is
));
114 if (sizeof(float) == sizeof(long))
115 return (XDR_PUTLONG(xdrs
, (long *)fp
));
116 else if (sizeof(float) == sizeof(int)) {
117 long tmp
= *(int *)fp
;
118 return (XDR_PUTLONG(xdrs
, &tmp
));
125 vsp
= (struct vax_single
*)fp
;
126 if (!XDR_GETLONG(xdrs
, (long *)&is
))
128 for (i
= 0, lim
= sgl_limits
;
129 i
< sizeof(sgl_limits
)/sizeof(struct sgl_limits
);
131 if ((is
.exp
== lim
->ieee
.exp
) &&
132 (is
.mantissa
== lim
->ieee
.mantissa
)) {
137 vsp
->exp
= is
.exp
- IEEE_SNG_BIAS
+ VAX_SNG_BIAS
;
138 vsp
->mantissa2
= is
.mantissa
;
139 vsp
->mantissa1
= (is
.mantissa
>> 16);
144 if (sizeof(float) == sizeof(long))
145 return (XDR_GETLONG(xdrs
, (long *)fp
));
146 else if (sizeof(float) == sizeof(int)) {
148 if (XDR_GETLONG(xdrs
, &tmp
)) {
161 libc_hidden_nolink_sunrpc (xdr_float
, GLIBC_2_0
)
164 * This routine works on Suns (Sky / 68000's) and Vaxen.
168 /* What IEEE double precision floating point looks like on a Vax */
170 unsigned int mantissa1
: 20;
171 unsigned int exp
: 11;
172 unsigned int sign
: 1;
173 unsigned int mantissa2
: 32;
176 /* Vax double precision floating point */
178 unsigned int mantissa1
: 7;
179 unsigned int exp
: 8;
180 unsigned int sign
: 1;
181 unsigned int mantissa2
: 16;
182 unsigned int mantissa3
: 16;
183 unsigned int mantissa4
: 16;
186 #define VAX_DBL_BIAS 0x81
187 #define IEEE_DBL_BIAS 0x3ff
188 #define MASK(nbits) ((1 << nbits) - 1)
190 static struct dbl_limits
{
192 struct ieee_double ieee
;
194 {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
195 { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
196 {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
197 { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
209 struct ieee_double id
;
210 struct vax_double vd
;
211 register struct dbl_limits
*lim
;
215 switch (xdrs
->x_op
) {
219 vd
= *((struct vax_double
*)dp
);
220 for (i
= 0, lim
= dbl_limits
;
221 i
< sizeof(dbl_limits
)/sizeof(struct dbl_limits
);
223 if ((vd
.mantissa4
== lim
->d
.mantissa4
) &&
224 (vd
.mantissa3
== lim
->d
.mantissa3
) &&
225 (vd
.mantissa2
== lim
->d
.mantissa2
) &&
226 (vd
.mantissa1
== lim
->d
.mantissa1
) &&
227 (vd
.exp
== lim
->d
.exp
)) {
232 id
.exp
= vd
.exp
- VAX_DBL_BIAS
+ IEEE_DBL_BIAS
;
233 id
.mantissa1
= (vd
.mantissa1
<< 13) | (vd
.mantissa2
>> 3);
234 id
.mantissa2
= ((vd
.mantissa2
& MASK(3)) << 29) |
235 (vd
.mantissa3
<< 13) |
236 ((vd
.mantissa4
>> 3) & MASK(13));
241 if (2*sizeof(long) == sizeof(double)) {
242 long *lp
= (long *)dp
;
243 return (XDR_PUTLONG(xdrs
, lp
+!LSW
) &&
244 XDR_PUTLONG(xdrs
, lp
+LSW
));
245 } else if (2*sizeof(int) == sizeof(double)) {
250 return (XDR_PUTLONG(xdrs
, tmp
) &&
251 XDR_PUTLONG(xdrs
, tmp
+1));
258 if (!XDR_GETLONG(xdrs
, lp
++) || !XDR_GETLONG(xdrs
, lp
))
260 for (i
= 0, lim
= dbl_limits
;
261 i
< sizeof(dbl_limits
)/sizeof(struct dbl_limits
);
263 if ((id
.mantissa2
== lim
->ieee
.mantissa2
) &&
264 (id
.mantissa1
== lim
->ieee
.mantissa1
) &&
265 (id
.exp
== lim
->ieee
.exp
)) {
270 vd
.exp
= id
.exp
- IEEE_DBL_BIAS
+ VAX_DBL_BIAS
;
271 vd
.mantissa1
= (id
.mantissa1
>> 13);
272 vd
.mantissa2
= ((id
.mantissa1
& MASK(13)) << 3) |
273 (id
.mantissa2
>> 29);
274 vd
.mantissa3
= (id
.mantissa2
>> 13);
275 vd
.mantissa4
= (id
.mantissa2
<< 3);
278 *dp
= *((double *)&vd
);
281 if (2*sizeof(long) == sizeof(double)) {
282 long *lp
= (long *)dp
;
283 return (XDR_GETLONG(xdrs
, lp
+!LSW
) &&
284 XDR_GETLONG(xdrs
, lp
+LSW
));
285 } else if (2*sizeof(int) == sizeof(double)) {
288 if (XDR_GETLONG(xdrs
, tmp
+!LSW
) &&
289 XDR_GETLONG(xdrs
, tmp
+LSW
)) {
303 libc_hidden_nolink_sunrpc (xdr_double
, GLIBC_2_0
)