source: rtems-libbsd/freebsd/lib/libc/xdr/xdr_float.c @ f41a394

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f41a394 was f41a394, checked in by Sebastian Huber <sebastian.huber@…>, on 06/09/16 at 09:46:09

XDR(3): Import from FreeBSD

  • Property mode set to 100644
File size: 7.7 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*      $NetBSD: xdr_float.c,v 1.23 2000/07/17 04:59:51 matt Exp $      */
4
5/*
6 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
7 * unrestricted use provided that this legend is included on all tape
8 * media and as a part of the software program in whole or part.  Users
9 * may copy or modify Sun RPC without charge, but are not authorized
10 * to license or distribute it to anyone else except as part of a product or
11 * program developed by the user.
12 *
13 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
14 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
15 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
16 *
17 * Sun RPC is provided with no support and without any obligation on the
18 * part of Sun Microsystems, Inc. to assist in its use, correction,
19 * modification or enhancement.
20 *
21 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
22 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
23 * OR ANY PART THEREOF.
24 *
25 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
26 * or profits or other special, indirect and consequential damages, even if
27 * Sun has been advised of the possibility of such damages.
28 *
29 * Sun Microsystems, Inc.
30 * 2550 Garcia Avenue
31 * Mountain View, California  94043
32 */
33
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *sccsid2 = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
36static char *sccsid = "@(#)xdr_float.c  2.1 88/07/29 4.0 RPCSRC";
37#endif
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD$");
40
41/*
42 * xdr_float.c, Generic XDR routines implementation.
43 *
44 * Copyright (C) 1984, Sun Microsystems, Inc.
45 *
46 * These are the "floating point" xdr routines used to (de)serialize
47 * most common data items.  See xdr.h for more info on the interface to
48 * xdr.
49 */
50
51#include "namespace.h"
52#include <sys/types.h>
53#include <rtems/bsd/sys/param.h>
54
55#include <stdio.h>
56
57#include <rpc/types.h>
58#include <rpc/xdr.h>
59#include "un-namespace.h"
60
61/*
62 * NB: Not portable.
63 * This routine works on machines with IEEE754 FP and Vaxen.
64 */
65
66#if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
67    defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
68    defined(__arm__) || defined(__ppc__) || defined(__ia64__) || \
69    defined(__arm26__) || defined(__sparc64__) || defined(__amd64__)
70#include <machine/endian.h>
71#define IEEEFP
72#endif
73
74#if defined(__vax__)
75
76/* What IEEE single precision floating point looks like on a Vax */
77struct  ieee_single {
78        unsigned int    mantissa: 23;
79        unsigned int    exp     : 8;
80        unsigned int    sign    : 1;
81};
82
83/* Vax single precision floating point */
84struct  vax_single {
85        unsigned int    mantissa1 : 7;
86        unsigned int    exp       : 8;
87        unsigned int    sign      : 1;
88        unsigned int    mantissa2 : 16;
89};
90
91#define VAX_SNG_BIAS    0x81
92#define IEEE_SNG_BIAS   0x7f
93
94static struct sgl_limits {
95        struct vax_single s;
96        struct ieee_single ieee;
97} sgl_limits[2] = {
98        {{ 0x7f, 0xff, 0x0, 0xffff },   /* Max Vax */
99        { 0x0, 0xff, 0x0 }},            /* Max IEEE */
100        {{ 0x0, 0x0, 0x0, 0x0 },        /* Min Vax */
101        { 0x0, 0x0, 0x0 }}              /* Min IEEE */
102};
103#endif /* vax */
104
105bool_t
106xdr_float(xdrs, fp)
107        XDR *xdrs;
108        float *fp;
109{
110#ifndef IEEEFP
111        struct ieee_single is;
112        struct vax_single vs, *vsp;
113        struct sgl_limits *lim;
114        int i;
115#endif
116        switch (xdrs->x_op) {
117
118        case XDR_ENCODE:
119#ifdef IEEEFP
120                return (XDR_PUTINT32(xdrs, (int32_t *)fp));
121#else
122                vs = *((struct vax_single *)fp);
123                for (i = 0, lim = sgl_limits;
124                        i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
125                        i++, lim++) {
126                        if ((vs.mantissa2 == lim->s.mantissa2) &&
127                                (vs.exp == lim->s.exp) &&
128                                (vs.mantissa1 == lim->s.mantissa1)) {
129                                is = lim->ieee;
130                                goto shipit;
131                        }
132                }
133                is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
134                is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
135        shipit:
136                is.sign = vs.sign;
137                return (XDR_PUTINT32(xdrs, (int32_t *)&is));
138#endif
139
140        case XDR_DECODE:
141#ifdef IEEEFP
142                return (XDR_GETINT32(xdrs, (int32_t *)fp));
143#else
144                vsp = (struct vax_single *)fp;
145                if (!XDR_GETINT32(xdrs, (int32_t *)&is))
146                        return (FALSE);
147                for (i = 0, lim = sgl_limits;
148                        i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
149                        i++, lim++) {
150                        if ((is.exp == lim->ieee.exp) &&
151                                (is.mantissa == lim->ieee.mantissa)) {
152                                *vsp = lim->s;
153                                goto doneit;
154                        }
155                }
156                vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
157                vsp->mantissa2 = is.mantissa;
158                vsp->mantissa1 = (is.mantissa >> 16);
159        doneit:
160                vsp->sign = is.sign;
161                return (TRUE);
162#endif
163
164        case XDR_FREE:
165                return (TRUE);
166        }
167        /* NOTREACHED */
168        return (FALSE);
169}
170
171#if defined(__vax__)
172/* What IEEE double precision floating point looks like on a Vax */
173struct  ieee_double {
174        unsigned int    mantissa1 : 20;
175        unsigned int    exp       : 11;
176        unsigned int    sign      : 1;
177        unsigned int    mantissa2 : 32;
178};
179
180/* Vax double precision floating point */
181struct  vax_double {
182        unsigned int    mantissa1 : 7;
183        unsigned int    exp       : 8;
184        unsigned int    sign      : 1;
185        unsigned int    mantissa2 : 16;
186        unsigned int    mantissa3 : 16;
187        unsigned int    mantissa4 : 16;
188};
189
190#define VAX_DBL_BIAS    0x81
191#define IEEE_DBL_BIAS   0x3ff
192#define MASK(nbits)     ((1 << nbits) - 1)
193
194static struct dbl_limits {
195        struct  vax_double d;
196        struct  ieee_double ieee;
197} dbl_limits[2] = {
198        {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },   /* Max Vax */
199        { 0x0, 0x7ff, 0x0, 0x0 }},                      /* Max IEEE */
200        {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},               /* Min Vax */
201        { 0x0, 0x0, 0x0, 0x0 }}                         /* Min IEEE */
202};
203
204#endif /* vax */
205
206
207bool_t
208xdr_double(xdrs, dp)
209        XDR *xdrs;
210        double *dp;
211{
212#ifdef IEEEFP
213        int32_t *i32p;
214        bool_t rv;
215#else
216        int32_t *lp;
217        struct  ieee_double id;
218        struct  vax_double vd;
219        struct dbl_limits *lim;
220        int i;
221#endif
222
223        switch (xdrs->x_op) {
224
225        case XDR_ENCODE:
226#ifdef IEEEFP
227                i32p = (int32_t *)(void *)dp;
228#if BYTE_ORDER == BIG_ENDIAN
229                rv = XDR_PUTINT32(xdrs, i32p);
230                if (!rv)
231                        return (rv);
232                rv = XDR_PUTINT32(xdrs, i32p+1);
233#else
234                rv = XDR_PUTINT32(xdrs, i32p+1);
235                if (!rv)
236                        return (rv);
237                rv = XDR_PUTINT32(xdrs, i32p);
238#endif
239                return (rv);
240#else
241                vd = *((struct vax_double *)dp);
242                for (i = 0, lim = dbl_limits;
243                        i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
244                        i++, lim++) {
245                        if ((vd.mantissa4 == lim->d.mantissa4) &&
246                                (vd.mantissa3 == lim->d.mantissa3) &&
247                                (vd.mantissa2 == lim->d.mantissa2) &&
248                                (vd.mantissa1 == lim->d.mantissa1) &&
249                                (vd.exp == lim->d.exp)) {
250                                id = lim->ieee;
251                                goto shipit;
252                        }
253                }
254                id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
255                id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
256                id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
257                                (vd.mantissa3 << 13) |
258                                ((vd.mantissa4 >> 3) & MASK(13));
259        shipit:
260                id.sign = vd.sign;
261                lp = (int32_t *)&id;
262                return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
263#endif
264
265        case XDR_DECODE:
266#ifdef IEEEFP
267                i32p = (int32_t *)(void *)dp;
268#if BYTE_ORDER == BIG_ENDIAN
269                rv = XDR_GETINT32(xdrs, i32p);
270                if (!rv)
271                        return (rv);
272                rv = XDR_GETINT32(xdrs, i32p+1);
273#else
274                rv = XDR_GETINT32(xdrs, i32p+1);
275                if (!rv)
276                        return (rv);
277                rv = XDR_GETINT32(xdrs, i32p);
278#endif
279                return (rv);
280#else
281                lp = (int32_t *)&id;
282                if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
283                        return (FALSE);
284                for (i = 0, lim = dbl_limits;
285                        i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
286                        i++, lim++) {
287                        if ((id.mantissa2 == lim->ieee.mantissa2) &&
288                                (id.mantissa1 == lim->ieee.mantissa1) &&
289                                (id.exp == lim->ieee.exp)) {
290                                vd = lim->d;
291                                goto doneit;
292                        }
293                }
294                vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
295                vd.mantissa1 = (id.mantissa1 >> 13);
296                vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
297                                (id.mantissa2 >> 29);
298                vd.mantissa3 = (id.mantissa2 >> 13);
299                vd.mantissa4 = (id.mantissa2 << 3);
300        doneit:
301                vd.sign = id.sign;
302                *dp = *((double *)&vd);
303                return (TRUE);
304#endif
305
306        case XDR_FREE:
307                return (TRUE);
308        }
309        /* NOTREACHED */
310        return (FALSE);
311}
Note: See TracBrowser for help on using the repository browser.