First step to clean up the variety of frexp(), ldexp() and modf()

implementations.
Long term goal is to use the implementation in libm and get rid of
the libc instances. For now, we need to keep one in libc for compatibility,
it belongs into the compat subtree.
The switch is per-arch. Should be painless for ieee754 boxes.
If all ieee754 archs are switched, libc/gen/{frexp,ldexp,modf}_ieee754.c
can be removed.
This commit is contained in:
drochner 2006-06-27 18:16:47 +00:00
parent 4e40f16c41
commit 0048377426
4 changed files with 335 additions and 3 deletions

View File

@ -0,0 +1,83 @@
/* $NetBSD: compat_frexp_ieee754.c,v 1.1 2006/06/27 18:16:47 drochner Exp $ */
/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* from: Header: frexp.c,v 1.1 91/07/07 04:45:01 torek Exp
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
#if 0
static char sccsid[] = "@(#)frexp.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: compat_frexp_ieee754.c,v 1.1 2006/06/27 18:16:47 drochner Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <machine/ieee.h>
#include <math.h>
/*
* Split the given value into a fraction in the range [0.5, 1.0) and
* an exponent, such that frac * (2^exp) == value. If value is 0,
* return 0.
*/
double
frexp(double value, int *eptr)
{
union ieee_double_u u;
if (value) {
/*
* Fractions in [0.5..1.0) have an exponent of 2^-1.
* Leave Inf and NaN alone, however.
* WHAT ABOUT DENORMS?
*/
u.dblu_d = value;
if (u.dblu_dbl.dbl_exp != DBL_EXP_INFNAN) {
*eptr = 0;
if (u.dblu_dbl.dbl_exp == 0) {
/* denormal, scale out of mantissa */
*eptr = -DBL_FRACBITS;
u.dblu_d *= 4.50359962737049600000e+15;
}
*eptr += u.dblu_dbl.dbl_exp - (DBL_EXP_BIAS - 1);
u.dblu_dbl.dbl_exp = DBL_EXP_BIAS - 1;
}
return (u.dblu_d);
} else {
*eptr = 0;
return (0.0);
}
}

View File

@ -0,0 +1,149 @@
/* $NetBSD: compat_ldexp_ieee754.c,v 1.1 2006/06/27 18:16:47 drochner Exp $ */
/*-
* Copyright (c) 1999 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Charles M. Hannum.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: compat_ldexp_ieee754.c,v 1.1 2006/06/27 18:16:47 drochner Exp $");
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <machine/ieee.h>
#include <errno.h>
#include <math.h>
/*
* Multiply the given value by 2^expon.
*/
double
ldexp(double val, int expon)
{
int oldexp, newexp;
union ieee_double_u u, mul;
u.dblu_d = val;
oldexp = u.dblu_dbl.dbl_exp;
/*
* If input is zero, Inf or NaN, just return it.
*/
if (u.dblu_d == 0.0 || oldexp == DBL_EXP_INFNAN)
return (val);
if (oldexp == 0) {
/*
* u.v is denormal. We must adjust it so that the exponent
* arithmetic below will work.
*/
if (expon <= DBL_EXP_BIAS) {
/*
* Optimization: if the scaling can be done in a single
* multiply, or underflows, just do it now.
*/
if (expon <= -DBL_FRACBITS) {
errno = ERANGE;
return (val < 0.0 ? -0.0 : 0.0);
}
mul.dblu_d = 0.0;
mul.dblu_dbl.dbl_exp = expon + DBL_EXP_BIAS;
u.dblu_d *= mul.dblu_d;
if (u.dblu_d == 0.0) {
errno = ERANGE;
return (val < 0.0 ? -0.0 : 0.0);
}
return (u.dblu_d);
} else {
/*
* We know that expon is very large, and therefore the
* result cannot be denormal (though it may be Inf).
* Shift u.v by just enough to make it normal.
*/
mul.dblu_d = 0.0;
mul.dblu_dbl.dbl_exp = DBL_FRACBITS + DBL_EXP_BIAS;
u.dblu_d *= mul.dblu_d;
expon -= DBL_FRACBITS;
oldexp = u.dblu_dbl.dbl_exp;
}
}
/*
* u.v is now normalized and oldexp has been adjusted if necessary.
* Calculate the new exponent and check for underflow and overflow.
*/
newexp = oldexp + expon;
if (newexp <= 0) {
/*
* The output number is either denormal or underflows (see
* comments in machine/ieee.h).
*/
if (newexp <= -DBL_FRACBITS) {
errno = ERANGE;
return (val < 0.0 ? -0.0 : 0.0);
}
/*
* Denormalize the result. We do this with a multiply. If
* expon is very large, it won't fit in a double, so we have
* to adjust the exponent first. This is safe because we know
* that u.v is normal at this point.
*/
if (expon <= -DBL_EXP_BIAS) {
u.dblu_dbl.dbl_exp = 1;
expon += oldexp - 1;
}
mul.dblu_d = 0.0;
mul.dblu_dbl.dbl_exp = expon + DBL_EXP_BIAS;
u.dblu_d *= mul.dblu_d;
return (u.dblu_d);
} else if (newexp >= DBL_EXP_INFNAN) {
/*
* The result overflowed; return +/-Inf.
*/
u.dblu_dbl.dbl_exp = DBL_EXP_INFNAN;
u.dblu_dbl.dbl_frach = 0;
u.dblu_dbl.dbl_fracl = 0;
errno = ERANGE;
return (u.dblu_d);
} else {
/*
* The result is normal; just replace the old exponent with the
* new one.
*/
u.dblu_dbl.dbl_exp = newexp;
return (u.dblu_d);
}
}

View File

@ -0,0 +1,100 @@
/* $NetBSD: compat_modf_ieee754.c,v 1.1 2006/06/27 18:16:47 drochner Exp $ */
/*
* Copyright (c) 1994, 1995 Carnegie-Mellon University.
* All rights reserved.
*
* Author: Chris G. Demetriou
*
* Permission to use, copy, modify and distribute this software and
* its documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
* FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie the
* rights to redistribute these changes.
*/
#include <sys/types.h>
#include <machine/ieee.h>
#include <errno.h>
#include <math.h>
/*
* double modf(double val, double *iptr)
* returns: f and i such that |f| < 1.0, (f + i) = val, and
* sign(f) == sign(i) == sign(val).
*
* Beware signedness when doing subtraction, and also operand size!
*/
double
modf(double val, double *iptr)
{
union ieee_double_u u, v;
u_int64_t frac;
/*
* If input is Inf or NaN, return it and leave i alone.
*/
u.dblu_d = val;
if (u.dblu_dbl.dbl_exp == DBL_EXP_INFNAN)
return (u.dblu_d);
/*
* If input can't have a fractional part, return
* (appropriately signed) zero, and make i be the input.
*/
if ((int)u.dblu_dbl.dbl_exp - DBL_EXP_BIAS > DBL_FRACBITS - 1) {
*iptr = u.dblu_d;
v.dblu_d = 0.0;
v.dblu_dbl.dbl_sign = u.dblu_dbl.dbl_sign;
return (v.dblu_d);
}
/*
* If |input| < 1.0, return it, and set i to the appropriately
* signed zero.
*/
if (u.dblu_dbl.dbl_exp < DBL_EXP_BIAS) {
v.dblu_d = 0.0;
v.dblu_dbl.dbl_sign = u.dblu_dbl.dbl_sign;
*iptr = v.dblu_d;
return (u.dblu_d);
}
/*
* There can be a fractional part of the input.
* If you look at the math involved for a few seconds, it's
* plain to see that the integral part is the input, with the
* low (DBL_FRACBITS - (exponent - DBL_EXP_BIAS)) bits zeroed,
* the fractional part is the part with the rest of the
* bits zeroed. Just zeroing the high bits to get the
* fractional part would yield a fraction in need of
* normalization. Therefore, we take the easy way out, and
* just use subtraction to get the fractional part.
*/
v.dblu_d = u.dblu_d;
/* Zero the low bits of the fraction, the sleazy way. */
frac = ((u_int64_t)v.dblu_dbl.dbl_frach << 32) + v.dblu_dbl.dbl_fracl;
frac >>= DBL_FRACBITS - (u.dblu_dbl.dbl_exp - DBL_EXP_BIAS);
frac <<= DBL_FRACBITS - (u.dblu_dbl.dbl_exp - DBL_EXP_BIAS);
v.dblu_dbl.dbl_fracl = (unsigned int)frac & 0xffffffff;
v.dblu_dbl.dbl_frach = (unsigned int)(frac >> 32);
*iptr = v.dblu_d;
u.dblu_d -= v.dblu_d;
u.dblu_dbl.dbl_sign = v.dblu_dbl.dbl_sign;
return (u.dblu_d);
}

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile.inc,v 1.149 2006/04/17 23:29:21 salo Exp $
# $NetBSD: Makefile.inc,v 1.150 2006/06/27 18:16:47 drochner Exp $
# from: @(#)Makefile.inc 8.6 (Berkeley) 5/4/95
# gen sources
@ -39,8 +39,8 @@ SRCS+= _err.c _errx.c \
# machine-dependent gen sources
# m-d Makefile.inc must include sources for:
# _setjmp() bswap16() bswap32() bswap64() fabs() frexp() infinity
# isinf() ldexp() modf() setjmp() sigsetjmp()
# _setjmp() bswap16() bswap32() bswap64() fabs() infinity
# isinf() setjmp() sigsetjmp()
.include "${ARCHDIR}/gen/Makefile.inc"