* mingwex/math/hypotl.c: Replace with version based on cephes
library.
This commit is contained in:
parent
c8bef40026
commit
169618f29f
|
@ -1,3 +1,8 @@
|
||||||
|
2002-09-02 Danny Smith <dannysmith@users.sourceforge.net>
|
||||||
|
|
||||||
|
* mingwex/math/hypotl.c: Replace with version based on cephes
|
||||||
|
library.
|
||||||
|
|
||||||
2002-08-28 Danny Smith <dannysmith@users.sourceforge.net>
|
2002-08-28 Danny Smith <dannysmith@users.sourceforge.net>
|
||||||
|
|
||||||
* include/sys/param.h: Add ENDIAN defines.
|
* include/sys/param.h: Add ENDIAN defines.
|
||||||
|
|
|
@ -1,58 +1,73 @@
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <float.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
typedef union
|
/*
|
||||||
{
|
This implementation is based largely on Cephes library
|
||||||
long double value;
|
function cabsl (cmplxl.c), which bears the following notice:
|
||||||
struct
|
|
||||||
{
|
|
||||||
unsigned mantissa[2];
|
|
||||||
unsigned sign_exponent : 16;
|
|
||||||
unsigned empty : 16;
|
|
||||||
} parts;
|
|
||||||
} ieee_long_double_shape_type;
|
|
||||||
|
|
||||||
|
Cephes Math Library Release 2.1: January, 1989
|
||||||
|
Copyright 1984, 1987, 1989 by Stephen L. Moshier
|
||||||
|
Direct inquiries to 30 Frost Street, Cambridge, MA 02140
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Modified for use in libmingwex.a
|
||||||
|
02 Sept 2002 Danny Smith <dannysmith@users.sourceforege.net>
|
||||||
|
Calls to ldexpl replaced by logbl and calls to frexpl replaced
|
||||||
|
by scalbnl to avoid duplicated range checks.
|
||||||
|
*/
|
||||||
|
|
||||||
/* Get int from the exponent of a long double. */
|
extern long double __INFL;
|
||||||
static __inline__ void
|
#define PRECL 32
|
||||||
get_ld_exp (unsigned exp,long double d)
|
|
||||||
{
|
|
||||||
ieee_long_double_shape_type u;
|
|
||||||
u.value = d;
|
|
||||||
exp = u.parts.sign_exponent;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Set exponent of a long double from an int. */
|
|
||||||
static __inline__ void
|
|
||||||
set_ld_exp (long double d,unsigned exp)
|
|
||||||
{
|
|
||||||
ieee_long_double_shape_type u;
|
|
||||||
u.value = d;
|
|
||||||
u.parts.sign_exponent = exp;
|
|
||||||
d = u.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
long double
|
long double
|
||||||
hypotl (long double x, long double y)
|
hypotl (long double x, long double y)
|
||||||
{
|
{
|
||||||
unsigned exx = 0U;
|
int exx;
|
||||||
unsigned eyy = 0U;
|
int eyy;
|
||||||
unsigned scale;
|
int scale;
|
||||||
long double xx =fabsl(x);
|
long double xx =fabsl(x);
|
||||||
long double yy =fabsl(y);
|
long double yy =fabsl(y);
|
||||||
if (!isfinite(xx) || !isfinite(yy))
|
if (!isfinite(xx) || !isfinite(yy))
|
||||||
return xx + yy; /* Return INF or NAN. */
|
return xx + yy; /* Return INF or NAN. */
|
||||||
|
|
||||||
/* Scale to avoid overflow.*/
|
if (xx == 0.0L)
|
||||||
get_ld_exp (exx, xx);
|
return yy;
|
||||||
get_ld_exp (eyy, yy);
|
if (yy == 0.0L)
|
||||||
scale = (exx > eyy ? exx : eyy);
|
return xx;
|
||||||
if (scale == 0)
|
|
||||||
|
/* Get exponents */
|
||||||
|
exx = logbl (xx);
|
||||||
|
eyy = logbl (yy);
|
||||||
|
|
||||||
|
/* Check if large differences in scale */
|
||||||
|
scale = exx - eyy;
|
||||||
|
if ( scale > PRECL)
|
||||||
|
return xx;
|
||||||
|
if ( scale < -PRECL)
|
||||||
|
return yy;
|
||||||
|
|
||||||
|
/* Exponent of approximate geometric mean (x 2) */
|
||||||
|
scale = (exx + eyy) >> 1;
|
||||||
|
|
||||||
|
/* Rescale: Geometric mean is now about 2 */
|
||||||
|
x = scalbnl(xx, -scale);
|
||||||
|
y = scalbnl(yy, -scale);
|
||||||
|
|
||||||
|
xx = sqrtl(x * x + y * y);
|
||||||
|
|
||||||
|
/* Check for overflow and underflow */
|
||||||
|
exx = logbl(xx);
|
||||||
|
exx += scale;
|
||||||
|
if (exx > LDBL_MAX_EXP)
|
||||||
|
{
|
||||||
|
errno = ERANGE;
|
||||||
|
return __INFL;
|
||||||
|
}
|
||||||
|
if (exx < LDBL_MIN_EXP)
|
||||||
return 0.0L;
|
return 0.0L;
|
||||||
set_ld_exp (xx, exx - scale);
|
|
||||||
set_ld_exp (yy, eyy - scale);
|
/* Undo scaling */
|
||||||
xx = sqrtl(xx * xx + yy * yy);
|
return (scalbnl (xx, scale));
|
||||||
get_ld_exp (exx,xx);
|
|
||||||
set_ld_exp (xx, exx + scale);
|
|
||||||
return xx;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue