xdr_float.c revision 5f9e186f08c9119f60f4f357c8905e75244ee0aa
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley
* 4.3 BSD under license from the Regents of the University of
* California.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Generic XDR routines impelmentation.
*
* These are the "floating point" xdr routines used to (de)serialize
* most common data items. See xdr.h for more info on the interface to
* xdr.
*/
#include "mt.h"
#include <stdio.h>
/*
* This routine works on Suns, 3b2, 68000s, 386 and Vaxen in a manner
* which is very efficient as bit twiddling is all that is needed. All
* other machines can use this code but the code is inefficient as
* various mathematical operations are used to generate the ieee format.
* In addition rounding errors may occur due to the calculations involved.
* To be most efficient, new machines should have their own ifdefs.
* The encoding routines will fail if the machines try to encode a
* ieee largest float = (2 ^ 128) * 0x1.fffff
* ieee smallest float = (2 ^ -127) * 0x1.00000
* ieee largest double = (2 ^ 1024) * 0x1.fffff
* ieee smallest double = (2 ^ -1023) * 0x1.00000
* The decoding routines assumes that the receiving machine can handle
* use a machine which can not represent these values, you will need
* to put ifdefs in the decode sections to identify areas of failure.
*/
#if defined(vax)
/*
* What IEEE single precision floating point looks like this on a
* vax.
*/
struct ieee_single {
unsigned int mantissa: 23;
unsigned int exp : 8;
unsigned int sign : 1;
};
#define IEEE_SNG_BIAS 0x7f
#define VAX_SNG_BIAS 0x81
/* Vax single precision floating point */
struct vax_single {
unsigned int mantissa1 : 7;
unsigned int exp : 8;
unsigned int sign : 1;
unsigned int mantissa2 : 16;
};
#define VAX_SNG_BIAS 0x81
static struct sgl_limits {
struct vax_single s;
struct ieee_single ieee;
} sgl_limits[2] = {
{{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */
{ 0x0, 0xff, 0x0 }}, /* Max IEEE */
{{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */
{ 0x0, 0x0, 0x0 }} /* Min IEEE */
};
#endif /* vax */
{
#if defined(vax)
struct ieee_single is;
struct sgl_limits *lim;
size_t i;
#endif
case XDR_ENCODE:
#else
#if defined(vax)
/* map these to subnormals */
/* lose some precision */
goto shipit;
}
for (i = 0, lim = sgl_limits;
i < (int)(sizeof (sgl_limits) /
sizeof (struct sgl_limits));
i++, lim++) {
goto shipit;
}
}
#else
{
/*
* Every machine can do this, its just not very efficient.
* In addtion, some rounding errors may occur do to the
* calculations involved.
*/
float f;
int neg = 0;
int exp = 0;
f = *fp;
if (f == 0) {
val = 0;
}
if (f < 0) {
f = 0 - f;
neg = 1;
}
while (f < 1) {
f = f * 2;
--exp;
}
while (f >= 2) {
f = f/2;
++exp;
}
/* over or under flowing ieee exponent */
return (FALSE);
}
}
#endif
#endif
case XDR_DECODE:
#else
#if defined(vax)
return (FALSE);
for (i = 0, lim = sgl_limits;
i < (int)(sizeof (sgl_limits) /
sizeof (struct sgl_limits));
i++, lim++) {
goto doneit;
/* Special Case */
if (tmp >= 4) {
} else if (tmp >= 2) {
} else {
break;
} /* else */
goto doneit;
}
return (TRUE);
#else
{
/*
* Every machine can do this, its just not very
* efficient. It assumes that the decoding machine's
* float can represent any value in the range of
* ieee largest float = (2 ^ 128) * 0x1.fffff
* to
* ieee smallest float = (2 ^ -127) * 0x1.00000
* In addtion, some rounding errors may occur do to the
* calculations involved.
*/
float f;
int neg = 0;
int exp = 0;
return (FALSE);
/* 2 ^ -23 */
f++;
while (exp != 0) {
if (exp < 0) {
f = f/2.0;
++exp;
} else {
f = f * 2.0;
--exp;
}
}
if (neg)
f = 0 - f;
*fp = f;
}
return (TRUE);
#endif
#endif
case XDR_FREE:
return (TRUE);
}
return (FALSE);
}
/*
* This routine works on Suns (Sky / 68000's) and Vaxen.
*/
#if defined(vax)
/* What IEEE double precision floating point looks like on a Vax */
struct ieee_double {
unsigned int mantissa1 : 20;
unsigned int exp : 11;
unsigned int sign : 1;
unsigned int mantissa2 : 32;
};
/* Vax double precision floating point */
struct vax_double {
unsigned int mantissa1 : 7;
unsigned int exp : 8;
unsigned int sign : 1;
unsigned int mantissa2 : 16;
unsigned int mantissa3 : 16;
unsigned int mantissa4 : 16;
};
#define VAX_DBL_BIAS 0x81
#define IEEE_DBL_BIAS 0x3ff
static struct dbl_limits {
struct vax_double d;
struct ieee_double ieee;
} dbl_limits[2] = {
{{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */
{ 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */
{{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */
{ 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */
};
#endif /* vax */
{
int *lp;
#if defined(vax)
struct ieee_double id;
struct vax_double vd;
struct dbl_limits *lim;
size_t i;
#endif
case XDR_ENCODE:
defined(_LONG_LONG_HTOL)
#else
#if defined(_LONG_LONG_LTOH)
lp++;
#else
#if defined(vax)
for (i = 0, lim = dbl_limits;
i < (int)(sizeof (dbl_limits) /
sizeof (struct dbl_limits));
i++, lim++) {
goto shipit;
}
}
#else
{
/*
* Every machine can do this, its just not very efficient.
* In addtion, some rounding errors may occur do to the
* calculations involved.
*/
double d;
int neg = 0;
int exp = 0;
d = *dp;
if (d == 0) {
val[0] = 0;
val[1] = 0;
}
if (d < 0) {
d = 0 - d;
neg = 1;
}
while (d < 1) {
d = d * 2;
--exp;
}
while (d >= 2) {
d = d/2;
++exp;
}
/* over or under flowing ieee exponent */
return (FALSE);
}
* 4294967296);
/* 2 ^ 32 */
}
#endif
#endif
#endif
case XDR_DECODE:
defined(_LONG_LONG_HTOL)
#else
#if defined(_LONG_LONG_LTOH)
lp++;
#else
#if defined(vax)
return (FALSE);
for (i = 0, lim = dbl_limits;
i < sizeof (dbl_limits)/sizeof (struct dbl_limits);
i++, lim++) {
goto doneit;
}
}
return (TRUE);
#else
{
/*
* Every machine can do this, its just not very
* efficient. It assumes that the decoding machine's
* double can represent any value in the range of
* ieee largest double = (2 ^ 1024) * 0x1.fffffffffffff
* to
* ieee smallest double = (2 ^ -1023) * 0x1.0000000000000
* In addtion, some rounding errors may occur do to the
* calculations involved.
*/
double d;
int neg = 0;
int exp = 0;
return (FALSE);
/* 2 ^ -20 */
/* 2 ^ -52 */
d++;
while (exp != 0) {
if (exp < 0) {
d = d/2.0;
++exp;
} else {
d = d * 2.0;
--exp;
}
}
if (neg)
d = 0 - d;
*dp = d;
}
#endif
#endif
#endif
case XDR_FREE:
return (TRUE);
}
return (FALSE);
}
/* ARGSUSED */
{
/*
* The Sparc uses IEEE FP encoding, so just do a byte copy
*/
#if !defined(sparc)
return (FALSE);
#else
case XDR_ENCODE:
case XDR_DECODE:
case XDR_FREE:
return (TRUE);
}
return (FALSE);
#endif
}