source: trunk/libs/newlib/src/newlib/libc/xdr/xdr_float_vax.c @ 567

Last change on this file since 567 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 6.9 KB
Line 
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 * xdr_float_vax.c, XDR floating point routines for vax.
32 *
33 * Copyright (C) 1984, Sun Microsystems, Inc.
34 *
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.
38 */
39#ifndef XDR_FLOAT_C
40#error "Must be included from xdr_float.c"
41#endif
42
43/* What IEEE single precision floating point looks like on a Vax */
44struct ieee_single
45{
46  unsigned int mantissa:23;
47  unsigned int exp:8;
48  unsigned int sign:1;
49};
50
51/* Vax single precision floating point */
52struct vax_single
53{
54  unsigned int mantissa1:7;
55  unsigned int exp:8;
56  unsigned int sign:1;
57  unsigned int mantissa2:16;
58};
59
60# define VAX_SNG_BIAS   0x81
61# define IEEE_SNG_BIAS  0x7f
62
63/* *INDENT-OFF*
64*/
65static struct sgl_limits
66{
67  struct vax_single s;
68  struct ieee_single ieee;
69} sgl_limits[2] =
70{
71  {
72    {0x7f, 0xff, 0x0, 0xffff},   /* Max Vax */
73    {0x0, 0xff, 0x0}             /* Max IEEE */
74  },
75  {
76    {0x0, 0x0, 0x0, 0x0},        /* Min Vax */
77    {0x0, 0x0, 0x0}              /* Min IEEE */
78  }
79};
80/* *INDENT-ON*
81*/
82
83bool_t
84xdr_float (XDR * xdrs,
85       float *fp)
86{
87  struct ieee_single is;
88  struct vax_single vs, *vsp;
89  struct sgl_limits *lim;
90  int i;
91  switch (xdrs->x_op)
92    {
93
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++)
98        {
99          if ((vs.mantissa2 == lim->s.mantissa2) &&
100              (vs.exp == lim->s.exp) && (vs.mantissa1 == lim->s.mantissa1))
101            {
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_PUTINT32 (xdrs, (int32_t *) & is));
111
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++)
118        {
119          if ((is.exp == lim->ieee.exp) &&
120              (is.mantissa == lim->ieee.mantissa))
121            {
122              *vsp = lim->s;
123              goto doneit;
124            }
125        }
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;
132
133    case XDR_FREE:
134      return TRUE;
135    }
136  return FALSE;
137}
138
139#if !defined(_DOUBLE_IS_32BITS)
140
141/* What IEEE double precision floating point looks like on a Vax */
142struct ieee_double
143{
144  unsigned int mantissa1:20;
145  unsigned int exp:11;
146  unsigned int sign:1;
147  unsigned int mantissa2:32;
148};
149
150/* Vax double precision floating point */
151struct vax_double
152{
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;
159};
160
161# define VAX_DBL_BIAS   0x81
162# define IEEE_DBL_BIAS  0x3ff
163# define MASK(nbits)    ((1 << nbits) - 1)
164
165/* *INDENT-OFF*
166*/
167static struct dbl_limits
168{
169  struct vax_double d;
170  struct ieee_double ieee;
171} dbl_limits[2] =
172{
173  {
174    {0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff},  /* Max Vax */
175    {0x0, 0x7ff, 0x0, 0x0}                      /* Max IEEE */
176  },
177  {
178    {0x0, 0x0, 0x0, 0x0, 0x0, 0x0},             /* Min Vax */
179    {0x0, 0x0, 0x0, 0x0}                        /* Min IEEE */
180  }
181};
182/* *INDENT-ON*
183*/
184
185bool_t
186xdr_double (XDR * xdrs,
187        double *dp)
188{
189  int32_t *lp;
190  struct ieee_double id;
191  struct vax_double vd;
192  struct dbl_limits *lim;
193  int i;
194
195  switch (xdrs->x_op)
196    {
197
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++)
202        {
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))
207            {
208              id = lim->ieee;
209              goto shipit;
210            }
211        }
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));
220
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++)
227        {
228          if ((id.mantissa2 == lim->ieee.mantissa2) &&
229              (id.mantissa1 == lim->ieee.mantissa1) &&
230              (id.exp == lim->ieee.exp))
231            {
232              vd = lim->d;
233              goto doneit;
234            }
235        }
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;
245
246    case XDR_FREE:
247      return TRUE;
248    }
249  return FALSE;
250}
251#endif /* !_DOUBLE_IS_32BITS */
252
Note: See TracBrowser for help on using the repository browser.