xdr_float.c revision 74462
150476Speter/*	$NetBSD: xdr_float.c,v 1.23 2000/07/17 04:59:51 matt Exp $	*/
215353Swosch
318566Sbde/*
431859Sbde * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
515353Swosch * unrestricted use provided that this legend is included on all tape
615353Swosch * media and as a part of the software program in whole or part.  Users
715353Swosch * may copy or modify Sun RPC without charge, but are not authorized
833815Sbde * to license or distribute it to anyone else except as part of a product or
915948Swosch * program developed by the user.
1033815Sbde *
1133815Sbde * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1218566Sbde * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1318566Sbde * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1418566Sbde *
1518566Sbde * Sun RPC is provided with no support and without any obligation on the
1615353Swosch * part of Sun Microsystems, Inc. to assist in its use, correction,
1718566Sbde * modification or enhancement.
1818566Sbde *
1918566Sbde * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
2018566Sbde * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2118566Sbde * OR ANY PART THEREOF.
2218566Sbde *
2318566Sbde * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2418340Sswallace * or profits or other special, indirect and consequential damages, even if
2515948Swosch * Sun has been advised of the possibility of such damages.
2618566Sbde *
2718566Sbde * Sun Microsystems, Inc.
2815353Swosch * 2550 Garcia Avenue
2915353Swosch * Mountain View, California  94043
3015353Swosch */
3115353Swosch
3233815Sbde#include <sys/cdefs.h>
3315353Swosch#if defined(LIBC_SCCS) && !defined(lint)
3415353Swoschstatic char *sccsid = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
3516663Sjkhstatic char *sccsid = "@(#)xdr_float.c	2.1 88/07/29 4.0 RPCSRC";
3615353Swoschstatic char *rcsid = "$FreeBSD: head/lib/libc/xdr/xdr_float.c 74462 2001-03-19 12:50:13Z alfred $";
3715353Swosch#endif
3816663Sjkh
3915353Swosch/*
4014801Swosch * xdr_float.c, Generic XDR routines implementation.
4194940Sru *
4294940Sru * Copyright (C) 1984, Sun Microsystems, Inc.
4394940Sru *
4494940Sru * These are the "floating point" xdr routines used to (de)serialize
4564784Ssheldonh * most common data items.  See xdr.h for more info on the interface to
4618340Sswallace * xdr.
4718340Sswallace */
4818340Sswallace
4918340Sswallace#include "namespace.h"
5014801Swosch#include <sys/types.h>
5118340Sswallace#include <sys/param.h>
5276896Sru
5318340Sswallace#include <stdio.h>
5476896Sru
5576896Sru#include <rpc/types.h>
5676896Sru#include <rpc/xdr.h>
5776896Sru#include "un-namespace.h"
5876896Sru
5976896Sru/*
6076896Sru * NB: Not portable.
6176896Sru * This routine works on machines with IEEE754 FP and Vaxen.
6276896Sru */
6376896Sru
6476896Sru#if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
6576896Sru    defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
6676896Sru    defined(__arm32__) || defined(__ppc__) || defined(__ia64__) || \
6718340Sswallace    defined(__arm26__)
6876896Sru#include <machine/endian.h>
6976896Sru#define IEEEFP
7076896Sru#endif
7118340Sswallace
7218373Speter#if defined(__vax__)
7376896Sru
7418373Speter/* What IEEE single precision floating point looks like on a Vax */
7518340Sswallacestruct	ieee_single {
7618340Sswallace	unsigned int	mantissa: 23;
7718340Sswallace	unsigned int	exp     : 8;
7818340Sswallace	unsigned int	sign    : 1;
7995306Sru};
8014801Swosch
8125316Sbde/* Vax single precision floating point */
8218340Sswallacestruct	vax_single {
8325316Sbde	unsigned int	mantissa1 : 7;
8418340Sswallace	unsigned int	exp       : 8;
8516663Sjkh	unsigned int	sign      : 1;
8616663Sjkh	unsigned int	mantissa2 : 16;
8718340Sswallace};
8816663Sjkh
8914801Swosch#define VAX_SNG_BIAS	0x81
9014801Swosch#define IEEE_SNG_BIAS	0x7f
9144922Sbde
9295306Srustatic struct sgl_limits {
9344922Sbde	struct vax_single s;
9444922Sbde	struct ieee_single ieee;
9544922Sbde} sgl_limits[2] = {
9644922Sbde	{{ 0x7f, 0xff, 0x0, 0xffff },	/* Max Vax */
9744922Sbde	{ 0x0, 0xff, 0x0 }},		/* Max IEEE */
9844922Sbde	{{ 0x0, 0x0, 0x0, 0x0 },	/* Min Vax */
9944922Sbde	{ 0x0, 0x0, 0x0 }}		/* Min IEEE */
10044922Sbde};
10115166Swosch#endif /* vax */
10217116Spst
10317116Spstbool_t
10417116Spstxdr_float(xdrs, fp)
10517116Spst	XDR *xdrs;
10636641Speter	float *fp;
10717116Spst{
10817116Spst#ifndef IEEEFP
10916663Sjkh	struct ieee_single is;
11076021Sru	struct vax_single vs, *vsp;
11176021Sru	struct sgl_limits *lim;
11276021Sru	int i;
11376021Sru#endif
11476021Sru	switch (xdrs->x_op) {
11516663Sjkh
11615166Swosch	case XDR_ENCODE:
11731859Sbde#ifdef IEEEFP
11895306Sru		return (XDR_PUTINT32(xdrs, (int32_t *)fp));
11931859Sbde#else
12031859Sbde		vs = *((struct vax_single *)fp);
12116663Sjkh		for (i = 0, lim = sgl_limits;
12233815Sbde			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
12333815Sbde			i++, lim++) {
12415166Swosch			if ((vs.mantissa2 == lim->s.mantissa2) &&
12533815Sbde				(vs.exp == lim->s.exp) &&
12615166Swosch				(vs.mantissa1 == lim->s.mantissa1)) {
12718427Sbde				is = lim->ieee;
12895306Sru				goto shipit;
12918427Sbde			}
13018427Sbde		}
13138655Sjb		is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
13234576Sbde		is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
13334576Sbde	shipit:
13434576Sbde		is.sign = vs.sign;
13534576Sbde		return (XDR_PUTINT32(xdrs, (int32_t *)&is));
13634576Sbde#endif
13718427Sbde
13834576Sbde	case XDR_DECODE:
13934576Sbde#ifdef IEEEFP
14018427Sbde		return (XDR_GETINT32(xdrs, (int32_t *)fp));
14131859Sbde#else
14294410Sru		vsp = (struct vax_single *)fp;
14331859Sbde		if (!XDR_GETINT32(xdrs, (int32_t *)&is))
14431859Sbde			return (FALSE);
14534576Sbde		for (i = 0, lim = sgl_limits;
14634576Sbde			i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
14731859Sbde			i++, lim++) {
14818427Sbde			if ((is.exp == lim->ieee.exp) &&
14918427Sbde				(is.mantissa == lim->ieee.mantissa)) {
15031859Sbde				*vsp = lim->s;
15118427Sbde				goto doneit;
15295306Sru			}
15335789Sbde		}
15495306Sru		vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
15535789Sbde		vsp->mantissa2 = is.mantissa;
15694940Sru		vsp->mantissa1 = (is.mantissa >> 16);
157	doneit:
158		vsp->sign = is.sign;
159		return (TRUE);
160#endif
161
162	case XDR_FREE:
163		return (TRUE);
164	}
165	/* NOTREACHED */
166	return (FALSE);
167}
168
169#if defined(__vax__)
170/* What IEEE double precision floating point looks like on a Vax */
171struct	ieee_double {
172	unsigned int	mantissa1 : 20;
173	unsigned int	exp       : 11;
174	unsigned int	sign      : 1;
175	unsigned int	mantissa2 : 32;
176};
177
178/* Vax double precision floating point */
179struct  vax_double {
180	unsigned int	mantissa1 : 7;
181	unsigned int	exp       : 8;
182	unsigned int	sign      : 1;
183	unsigned int	mantissa2 : 16;
184	unsigned int	mantissa3 : 16;
185	unsigned int	mantissa4 : 16;
186};
187
188#define VAX_DBL_BIAS	0x81
189#define IEEE_DBL_BIAS	0x3ff
190#define MASK(nbits)	((1 << nbits) - 1)
191
192static struct dbl_limits {
193	struct	vax_double d;
194	struct	ieee_double ieee;
195} dbl_limits[2] = {
196	{{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },	/* Max Vax */
197	{ 0x0, 0x7ff, 0x0, 0x0 }},			/* Max IEEE */
198	{{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},		/* Min Vax */
199	{ 0x0, 0x0, 0x0, 0x0 }}				/* Min IEEE */
200};
201
202#endif /* vax */
203
204
205bool_t
206xdr_double(xdrs, dp)
207	XDR *xdrs;
208	double *dp;
209{
210#ifdef IEEEFP
211	int32_t *i32p;
212	bool_t rv;
213#else
214	int32_t *lp;
215	struct	ieee_double id;
216	struct	vax_double vd;
217	struct dbl_limits *lim;
218	int i;
219#endif
220
221	switch (xdrs->x_op) {
222
223	case XDR_ENCODE:
224#ifdef IEEEFP
225		i32p = (int32_t *)(void *)dp;
226#if BYTE_ORDER == BIG_ENDIAN
227		rv = XDR_PUTINT32(xdrs, i32p);
228		if (!rv)
229			return (rv);
230		rv = XDR_PUTINT32(xdrs, i32p+1);
231#else
232		rv = XDR_PUTINT32(xdrs, i32p+1);
233		if (!rv)
234			return (rv);
235		rv = XDR_PUTINT32(xdrs, i32p);
236#endif
237		return (rv);
238#else
239		vd = *((struct vax_double *)dp);
240		for (i = 0, lim = dbl_limits;
241			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
242			i++, lim++) {
243			if ((vd.mantissa4 == lim->d.mantissa4) &&
244				(vd.mantissa3 == lim->d.mantissa3) &&
245				(vd.mantissa2 == lim->d.mantissa2) &&
246				(vd.mantissa1 == lim->d.mantissa1) &&
247				(vd.exp == lim->d.exp)) {
248				id = lim->ieee;
249				goto shipit;
250			}
251		}
252		id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
253		id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
254		id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
255				(vd.mantissa3 << 13) |
256				((vd.mantissa4 >> 3) & MASK(13));
257	shipit:
258		id.sign = vd.sign;
259		lp = (int32_t *)&id;
260		return (XDR_PUTINT32(xdrs, lp++) && XDR_PUTINT32(xdrs, lp));
261#endif
262
263	case XDR_DECODE:
264#ifdef IEEEFP
265		i32p = (int32_t *)(void *)dp;
266#if BYTE_ORDER == BIG_ENDIAN
267		rv = XDR_GETINT32(xdrs, i32p);
268		if (!rv)
269			return (rv);
270		rv = XDR_GETINT32(xdrs, i32p+1);
271#else
272		rv = XDR_GETINT32(xdrs, i32p+1);
273		if (!rv)
274			return (rv);
275		rv = XDR_GETINT32(xdrs, i32p);
276#endif
277		return (rv);
278#else
279		lp = (int32_t *)&id;
280		if (!XDR_GETINT32(xdrs, lp++) || !XDR_GETINT32(xdrs, lp))
281			return (FALSE);
282		for (i = 0, lim = dbl_limits;
283			i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
284			i++, lim++) {
285			if ((id.mantissa2 == lim->ieee.mantissa2) &&
286				(id.mantissa1 == lim->ieee.mantissa1) &&
287				(id.exp == lim->ieee.exp)) {
288				vd = lim->d;
289				goto doneit;
290			}
291		}
292		vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
293		vd.mantissa1 = (id.mantissa1 >> 13);
294		vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
295				(id.mantissa2 >> 29);
296		vd.mantissa3 = (id.mantissa2 >> 13);
297		vd.mantissa4 = (id.mantissa2 << 3);
298	doneit:
299		vd.sign = id.sign;
300		*dp = *((double *)&vd);
301		return (TRUE);
302#endif
303
304	case XDR_FREE:
305		return (TRUE);
306	}
307	/* NOTREACHED */
308	return (FALSE);
309}
310