xdr_float.c revision 7f93f875323d3870ccfa2f3df6aad7f2edecbafe
/*
* 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.
*/
/*
* Copyright 2011 Jason King. All rights reserved
*/
/*
* 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>
#include <values.h>
#include <sys/byteorder.h>
#ifdef _IEEE_754
/*
* The OTW format is IEEE 754 with big endian ordering.
*/
{
case XDR_ENCODE:
case XDR_DECODE:
case XDR_FREE:
return (TRUE);
}
return (FALSE);
}
{
case XDR_ENCODE:
case XDR_DECODE:
if (ret)
return (ret);
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
}
#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;
case XDR_ENCODE:
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);
}
case XDR_DECODE:
/*
* 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.
*/
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);
case XDR_FREE:
return (TRUE);
}
return (FALSE);
}
{
/*
* Every machine can do this, its just not very efficient.
* In addtion, some rounding errors may occur do to the
* calculations involved.
*/
int *lp;
double d;
int neg = 0;
int exp = 0;
case XDR_ENCODE:
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 */
case XDR_DECODE:
/*
* 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.
*/
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;
return (TRUE);
case XDR_FREE:
return (TRUE);
}
return (FALSE);
}
{
return (FALSE);
}
#endif /* _IEEE_754 */