summaryrefslogtreecommitdiff
path: root/src/math/scalbnl.c
diff options
context:
space:
mode:
authornsz <nsz@port70.net>2012-03-19 10:54:07 +0100
committernsz <nsz@port70.net>2012-03-19 10:54:07 +0100
commit8051e08e10d2b739fcfcbc6bc7466e8d77fa49f1 (patch)
treecebd3ef5505965af3d75f093cf646ea6b99da3bd /src/math/scalbnl.c
parentf767aba8afe84d6117c7c0376122a5ff704a69df (diff)
downloadmusl-8051e08e10d2b739fcfcbc6bc7466e8d77fa49f1.tar.gz
musl-8051e08e10d2b739fcfcbc6bc7466e8d77fa49f1.tar.bz2
musl-8051e08e10d2b739fcfcbc6bc7466e8d77fa49f1.tar.xz
musl-8051e08e10d2b739fcfcbc6bc7466e8d77fa49f1.zip
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)
Diffstat (limited to 'src/math/scalbnl.c')
-rw-r--r--src/math/scalbnl.c71
1 files changed, 22 insertions, 49 deletions
diff --git a/src/math/scalbnl.c b/src/math/scalbnl.c
index 0ed5b7fd..a5d0adba 100644
--- a/src/math/scalbnl.c
+++ b/src/math/scalbnl.c
@@ -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"
#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);
}
#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)
{
- union IEEEl2bits u;
- int k;
+ union IEEEl2bits scale;
- u.e = x;
- k = u.bits.exp; /* extract exponent */
- if (k == 0) { /* 0 or subnormal x */
- if ((u.bits.manh|u.bits.manl) == 0) /* +-0 */
- return x;
- u.e *= 0x1p128;
- k = u.bits.exp - 128;
- if (n < -50000)
- return tiny*x; /*underflow*/
- }
- if (k == 0x7fff) /* NaN or Inf */
- return x + x;
- k = k + n;
- if (k >= 0x7fff)
- return huge*copysignl(huge, x); /* overflow */
- if (k > 0) { /* normal result */
- u.bits.exp = k;
- return u.e;
+ if (n > 16383) {
+ x *= 0x1p16383L;
+ n -= 16383;
+ if (n > 16383) {
+ x *= 0x1p16383L;
+ n -= 16383;
+ if (n > 16383)
+ return x * 0x1p16383L;
+ }
+ } 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 <= -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;
+ scale.e = 1.0L;
+ scale.bits.exp = 0x3fff + n;
+ return x * scale.e;
}
#endif