mirror of
git://git.musl-libc.org/musl
synced 2025-04-26 21:08:19 +00:00
simplify scalbn*.c implementations
The old scalbn.c was wrong and slow, the new one is just slow. (scalbn(0x1p+1023,-2097) should give 0x1p-1074, but the old code gave 0)
This commit is contained in:
parent
f767aba8af
commit
8051e08e10
@ -1,62 +1,28 @@
|
|||||||
/* origin: FreeBSD /usr/src/lib/msun/src/s_scalbn.c */
|
|
||||||
/*
|
|
||||||
* ====================================================
|
|
||||||
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
|
||||||
*
|
|
||||||
* Developed at SunPro, a Sun Microsystems, Inc. business.
|
|
||||||
* Permission to use, copy, modify, and distribute this
|
|
||||||
* software is freely granted, provided that this notice
|
|
||||||
* is preserved.
|
|
||||||
* ====================================================
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
* scalbn (double x, int n)
|
|
||||||
* scalbn(x,n) returns x* 2**n computed by exponent
|
|
||||||
* manipulation rather than by actually performing an
|
|
||||||
* exponentiation or a multiplication.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "libm.h"
|
#include "libm.h"
|
||||||
|
|
||||||
static const double
|
|
||||||
two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
|
|
||||||
twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
|
|
||||||
huge = 1.0e+300,
|
|
||||||
tiny = 1.0e-300;
|
|
||||||
|
|
||||||
double scalbn(double x, int n)
|
double scalbn(double x, int n)
|
||||||
{
|
{
|
||||||
// FIXME: k+n check depends on signed int overflow.. use unsigned hx
|
double scale;
|
||||||
// TODO: when long != int:
|
|
||||||
// scalbln(x,long n) { if(n>9999)n=9999; else if(n<-9999)n=-9999; return scalbn(x,n); }
|
|
||||||
// TODO: n < -50000 ...
|
|
||||||
int32_t k,hx,lx;
|
|
||||||
|
|
||||||
EXTRACT_WORDS(hx, lx, x);
|
if (n > 1023) {
|
||||||
k = (hx&0x7ff00000)>>20; /* extract exponent */
|
x *= 0x1p1023;
|
||||||
if (k == 0) { /* 0 or subnormal x */
|
n -= 1023;
|
||||||
if ((lx|(hx&0x7fffffff)) == 0) /* +-0 */
|
if (n > 1023) {
|
||||||
return x;
|
x *= 0x1p1023;
|
||||||
x *= two54;
|
n -= 1023;
|
||||||
GET_HIGH_WORD(hx, x);
|
if (n > 1023)
|
||||||
k = ((hx&0x7ff00000)>>20) - 54;
|
return x * 0x1p1023;
|
||||||
if (n < -50000)
|
}
|
||||||
return tiny*x; /*underflow*/
|
} else if (n < -1022) {
|
||||||
|
x *= 0x1p-1022;
|
||||||
|
n += 1022;
|
||||||
|
if (n < -1022) {
|
||||||
|
x *= 0x1p-1022;
|
||||||
|
n += 1022;
|
||||||
|
if (n < -1022)
|
||||||
|
return x * 0x1p-1022;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (k == 0x7ff) /* NaN or Inf */
|
INSERT_WORDS(scale, (uint32_t)(0x3ff+n)<<20, 0);
|
||||||
return x + x;
|
return x * scale;
|
||||||
k = k + n;
|
|
||||||
if (k > 0x7fe)
|
|
||||||
return huge*copysign(huge, x); /* overflow */
|
|
||||||
if (k > 0) { /* normal result */
|
|
||||||
SET_HIGH_WORD(x, (hx&0x800fffff)|(k<<20));
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
if (k <= -54)
|
|
||||||
if (n > 50000) /* in case integer overflow in n+k */
|
|
||||||
return huge*copysign(huge, x); /*overflow*/
|
|
||||||
return tiny*copysign(tiny, x); /*underflow*/
|
|
||||||
k += 54; /* subnormal result */
|
|
||||||
SET_HIGH_WORD(x, (hx&0x800fffff)|(k<<20));
|
|
||||||
return x*twom54;
|
|
||||||
}
|
}
|
||||||
|
@ -1,54 +1,28 @@
|
|||||||
/* origin: FreeBSD /usr/src/lib/msun/src/s_scalbnf.c */
|
|
||||||
/*
|
|
||||||
* Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
* ====================================================
|
|
||||||
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
|
||||||
*
|
|
||||||
* Developed at SunPro, a Sun Microsystems, Inc. business.
|
|
||||||
* Permission to use, copy, modify, and distribute this
|
|
||||||
* software is freely granted, provided that this notice
|
|
||||||
* is preserved.
|
|
||||||
* ====================================================
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "libm.h"
|
#include "libm.h"
|
||||||
|
|
||||||
static const float
|
|
||||||
two25 = 3.355443200e+07, /* 0x4c000000 */
|
|
||||||
twom25 = 2.9802322388e-08, /* 0x33000000 */
|
|
||||||
huge = 1.0e+30,
|
|
||||||
tiny = 1.0e-30;
|
|
||||||
|
|
||||||
float scalbnf(float x, int n)
|
float scalbnf(float x, int n)
|
||||||
{
|
{
|
||||||
int32_t k, ix;
|
float scale;
|
||||||
GET_FLOAT_WORD(ix, x);
|
|
||||||
k = (ix&0x7f800000)>>23; /* extract exponent */
|
if (n > 127) {
|
||||||
if (k == 0) { /* 0 or subnormal x */
|
x *= 0x1p127f;
|
||||||
if ((ix&0x7fffffff) == 0) /* +-0 */
|
n -= 127;
|
||||||
return x;
|
if (n > 127) {
|
||||||
x *= two25;
|
x *= 0x1p127f;
|
||||||
GET_FLOAT_WORD(ix, x);
|
n -= 127;
|
||||||
k = ((ix&0x7f800000)>>23) - 25;
|
if (n > 127)
|
||||||
if (n < -50000)
|
return x * 0x1p127f;
|
||||||
return tiny*x; /*underflow*/
|
}
|
||||||
|
} else if (n < -126) {
|
||||||
|
x *= 0x1p-126f;
|
||||||
|
n += 126;
|
||||||
|
if (n < -126) {
|
||||||
|
x *= 0x1p-126f;
|
||||||
|
n += 126;
|
||||||
|
if (n < -126)
|
||||||
|
return x * 0x1p-126f;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (k == 0xff) /* NaN or Inf */
|
SET_FLOAT_WORD(scale, (uint32_t)(0x7f+n)<<23);
|
||||||
return x + x;
|
return x * scale;
|
||||||
k = k + n;
|
|
||||||
if (k > 0xfe)
|
|
||||||
return huge*copysignf(huge, x); /* overflow */
|
|
||||||
if (k > 0) { /* normal result */
|
|
||||||
SET_FLOAT_WORD(x, (ix&0x807fffff)|(k<<23));
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
if (k <= -25)
|
|
||||||
if (n > 50000) /* in case integer overflow in n+k */
|
|
||||||
return huge*copysignf(huge,x); /*overflow*/
|
|
||||||
return tiny*copysignf(tiny, x); /*underflow*/
|
|
||||||
k += 25; /* subnormal result */
|
|
||||||
SET_FLOAT_WORD(x, (ix&0x807fffff)|(k<<23));
|
|
||||||
return x*twom25;
|
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,3 @@
|
|||||||
/* origin: FreeBSD /usr/src/lib/msun/src/s_scalbnl.c */
|
|
||||||
/*
|
|
||||||
* ====================================================
|
|
||||||
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
|
||||||
*
|
|
||||||
* Developed at SunPro, a Sun Microsystems, Inc. business.
|
|
||||||
* Permission to use, copy, modify, and distribute this
|
|
||||||
* software is freely granted, provided that this notice
|
|
||||||
* is preserved.
|
|
||||||
* ====================================================
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
* scalbnl (long double x, int n)
|
|
||||||
* scalbnl(x,n) returns x* 2**n computed by exponent
|
|
||||||
* manipulation rather than by actually performing an
|
|
||||||
* exponentiation or a multiplication.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "libm.h"
|
#include "libm.h"
|
||||||
|
|
||||||
#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
|
#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
|
||||||
@ -24,40 +6,31 @@ long double scalbnl(long double x, int n)
|
|||||||
return scalbn(x, n);
|
return scalbn(x, n);
|
||||||
}
|
}
|
||||||
#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
|
#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
|
||||||
static const long double
|
|
||||||
huge = 0x1p16000L,
|
|
||||||
tiny = 0x1p-16000L;
|
|
||||||
|
|
||||||
long double scalbnl(long double x, int n)
|
long double scalbnl(long double x, int n)
|
||||||
{
|
{
|
||||||
union IEEEl2bits u;
|
union IEEEl2bits scale;
|
||||||
int k;
|
|
||||||
|
|
||||||
u.e = x;
|
if (n > 16383) {
|
||||||
k = u.bits.exp; /* extract exponent */
|
x *= 0x1p16383L;
|
||||||
if (k == 0) { /* 0 or subnormal x */
|
n -= 16383;
|
||||||
if ((u.bits.manh|u.bits.manl) == 0) /* +-0 */
|
if (n > 16383) {
|
||||||
return x;
|
x *= 0x1p16383L;
|
||||||
u.e *= 0x1p128;
|
n -= 16383;
|
||||||
k = u.bits.exp - 128;
|
if (n > 16383)
|
||||||
if (n < -50000)
|
return x * 0x1p16383L;
|
||||||
return tiny*x; /*underflow*/
|
}
|
||||||
|
} else if (n < -16382) {
|
||||||
|
x *= 0x1p-16382L;
|
||||||
|
n += 16382;
|
||||||
|
if (n < -16382) {
|
||||||
|
x *= 0x1p-16382L;
|
||||||
|
n += 16382;
|
||||||
|
if (n < -16382)
|
||||||
|
return x * 0x1p-16382L;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (k == 0x7fff) /* NaN or Inf */
|
scale.e = 1.0L;
|
||||||
return x + x;
|
scale.bits.exp = 0x3fff + n;
|
||||||
k = k + n;
|
return x * scale.e;
|
||||||
if (k >= 0x7fff)
|
|
||||||
return huge*copysignl(huge, x); /* overflow */
|
|
||||||
if (k > 0) { /* normal result */
|
|
||||||
u.bits.exp = k;
|
|
||||||
return u.e;
|
|
||||||
}
|
|
||||||
if (k <= -128)
|
|
||||||
if (n > 50000) /* in case integer overflow in n+k */
|
|
||||||
return huge*copysign(huge, x); /*overflow*/
|
|
||||||
return tiny*copysign(tiny, x); /*underflow*/
|
|
||||||
k += 128; /* subnormal result */
|
|
||||||
u.bits.exp = k;
|
|
||||||
return u.e*0x1p-128;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user