Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / xdr / xdr_float_vax.c
blob09c24b03ed330407b5235f738a27881be899da51
2 /*
3 * Copyright (c) 2009, Sun Microsystems, Inc.
4 * All rights reserved.
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.
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.
31 * xdr_float_vax.c, XDR floating point routines for vax.
33 * Copyright (C) 1984, Sun Microsystems, Inc.
35 * These are the "floating point" xdr routines used to (de)serialize
36 * most common data items. See xdr.h for more info on the interface to
37 * xdr.
39 #ifndef XDR_FLOAT_C
40 #error "Must be included from xdr_float.c"
41 #endif
43 /* What IEEE single precision floating point looks like on a Vax */
44 struct ieee_single
46 unsigned int mantissa:23;
47 unsigned int exp:8;
48 unsigned int sign:1;
51 /* Vax single precision floating point */
52 struct vax_single
54 unsigned int mantissa1:7;
55 unsigned int exp:8;
56 unsigned int sign:1;
57 unsigned int mantissa2:16;
60 # define VAX_SNG_BIAS 0x81
61 # define IEEE_SNG_BIAS 0x7f
63 /* *INDENT-OFF*
65 static struct sgl_limits
67 struct vax_single s;
68 struct ieee_single ieee;
69 } sgl_limits[2] =
72 {0x7f, 0xff, 0x0, 0xffff}, /* Max Vax */
73 {0x0, 0xff, 0x0} /* Max IEEE */
76 {0x0, 0x0, 0x0, 0x0}, /* Min Vax */
77 {0x0, 0x0, 0x0} /* Min IEEE */
80 /* *INDENT-ON*
83 bool_t
84 xdr_float (XDR * xdrs,
85 float *fp)
87 struct ieee_single is;
88 struct vax_single vs, *vsp;
89 struct sgl_limits *lim;
90 int i;
91 switch (xdrs->x_op)
94 case XDR_ENCODE:
95 vs = *((struct vax_single *) fp);
96 for (i = 0, lim = sgl_limits;
97 i < sizeof (sgl_limits) / sizeof (struct sgl_limits); i++, lim++)
99 if ((vs.mantissa2 == lim->s.mantissa2) &&
100 (vs.exp == lim->s.exp) && (vs.mantissa1 == lim->s.mantissa1))
102 is = lim->ieee;
103 goto shipit;
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_PUTINT32 (xdrs, (int32_t *) & is));
112 case XDR_DECODE:
113 vsp = (struct vax_single *) fp;
114 if (!XDR_GETINT32 (xdrs, (int32_t *) & is))
115 return FALSE;
116 for (i = 0, lim = sgl_limits;
117 i < sizeof (sgl_limits) / sizeof (struct sgl_limits); i++, lim++)
119 if ((is.exp == lim->ieee.exp) &&
120 (is.mantissa == lim->ieee.mantissa))
122 *vsp = lim->s;
123 goto doneit;
126 vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
127 vsp->mantissa2 = is.mantissa;
128 vsp->mantissa1 = (is.mantissa >> 16);
129 doneit:
130 vsp->sign = is.sign;
131 return TRUE;
133 case XDR_FREE:
134 return TRUE;
136 return FALSE;
139 #if !defined(_DOUBLE_IS_32BITS)
141 /* What IEEE double precision floating point looks like on a Vax */
142 struct ieee_double
144 unsigned int mantissa1:20;
145 unsigned int exp:11;
146 unsigned int sign:1;
147 unsigned int mantissa2:32;
150 /* Vax double precision floating point */
151 struct vax_double
153 unsigned int mantissa1:7;
154 unsigned int exp:8;
155 unsigned int sign:1;
156 unsigned int mantissa2:16;
157 unsigned int mantissa3:16;
158 unsigned int mantissa4:16;
161 # define VAX_DBL_BIAS 0x81
162 # define IEEE_DBL_BIAS 0x3ff
163 # define MASK(nbits) ((1 << nbits) - 1)
165 /* *INDENT-OFF*
167 static struct dbl_limits
169 struct vax_double d;
170 struct ieee_double ieee;
171 } dbl_limits[2] =
174 {0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff}, /* Max Vax */
175 {0x0, 0x7ff, 0x0, 0x0} /* Max IEEE */
178 {0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
179 {0x0, 0x0, 0x0, 0x0} /* Min IEEE */
182 /* *INDENT-ON*
185 bool_t
186 xdr_double (XDR * xdrs,
187 double *dp)
189 int32_t *lp;
190 struct ieee_double id;
191 struct vax_double vd;
192 struct dbl_limits *lim;
193 int i;
195 switch (xdrs->x_op)
198 case XDR_ENCODE:
199 vd = *((struct vax_double *) dp);
200 for (i = 0, lim = dbl_limits;
201 i < sizeof (dbl_limits) / sizeof (struct dbl_limits); i++, lim++)
203 if ((vd.mantissa4 == lim->d.mantissa4) &&
204 (vd.mantissa3 == lim->d.mantissa3) &&
205 (vd.mantissa2 == lim->d.mantissa2) &&
206 (vd.mantissa1 == lim->d.mantissa1) && (vd.exp == lim->d.exp))
208 id = lim->ieee;
209 goto shipit;
212 id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
213 id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
214 id.mantissa2 = ((vd.mantissa2 & MASK (3)) << 29) |
215 (vd.mantissa3 << 13) | ((vd.mantissa4 >> 3) & MASK (13));
216 shipit:
217 id.sign = vd.sign;
218 lp = (int32_t *) & id;
219 return (XDR_PUTINT32 (xdrs, lp++) && XDR_PUTINT32 (xdrs, lp));
221 case XDR_DECODE:
222 lp = (int32_t *) & id;
223 if (!XDR_GETINT32 (xdrs, lp++) || !XDR_GETINT32 (xdrs, lp))
224 return FALSE;
225 for (i = 0, lim = dbl_limits;
226 i < sizeof (dbl_limits) / sizeof (struct dbl_limits); i++, lim++)
228 if ((id.mantissa2 == lim->ieee.mantissa2) &&
229 (id.mantissa1 == lim->ieee.mantissa1) &&
230 (id.exp == lim->ieee.exp))
232 vd = lim->d;
233 goto doneit;
236 vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
237 vd.mantissa1 = (id.mantissa1 >> 13);
238 vd.mantissa2 = ((id.mantissa1 & MASK (13)) << 3) | (id.mantissa2 >> 29);
239 vd.mantissa3 = (id.mantissa2 >> 13);
240 vd.mantissa4 = (id.mantissa2 << 3);
241 doneit:
242 vd.sign = id.sign;
243 *dp = *((double *) &vd);
244 return TRUE;
246 case XDR_FREE:
247 return TRUE;
249 return FALSE;
251 #endif /* !_DOUBLE_IS_32BITS */