projects
/
musl
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
math.h: make __FLOAT_BITS and __DOUBLE_BITS C89
[musl]
/
src
/
math
/
fma.c
diff --git
a/src/math/fma.c
b/src/math/fma.c
index
89def79
..
02f5c86
100644
(file)
--- a/
src/math/fma.c
+++ b/
src/math/fma.c
@@
-2,16
+2,6
@@
#include "libm.h"
#if LDBL_MANT_DIG==64 && LDBL_MAX_EXP==16384
#include "libm.h"
#if LDBL_MANT_DIG==64 && LDBL_MAX_EXP==16384
-union ld80 {
- long double x;
- struct {
- uint64_t m;
- uint16_t e : 15;
- uint16_t s : 1;
- uint16_t pad;
- } bits;
-};
-
/* exact add, assumes exponent_x >= exponent_y */
static void add(long double *hi, long double *lo, long double x, long double y)
{
/* exact add, assumes exponent_x >= exponent_y */
static void add(long double *hi, long double *lo, long double x, long double y)
{
@@
-45,25
+35,25
@@
return an adjusted hi so that rounding it to double (or less) precision is corre
*/
static long double adjust(long double hi, long double lo)
{
*/
static long double adjust(long double hi, long double lo)
{
- union ld
80
uhi, ulo;
+ union ld
shape
uhi, ulo;
if (lo == 0)
return hi;
if (lo == 0)
return hi;
- uhi.
x
= hi;
- if (uhi.
bits
.m & 0x3ff)
+ uhi.
f
= hi;
+ if (uhi.
i
.m & 0x3ff)
return hi;
return hi;
- ulo.
x
= lo;
- if (
uhi.bits.s == ulo.bits.s
)
- uhi.
bits
.m++;
+ ulo.
f
= lo;
+ if (
(uhi.i.se & 0x8000) == (ulo.i.se & 0x8000)
)
+ uhi.
i
.m++;
else {
else {
- uhi.bits.m--;
/* handle underflow and take care of ld80 implicit msb */
/* handle underflow and take care of ld80 implicit msb */
- if (uhi.
bits.m == (uint64_t)-1/2
) {
- uhi.
bits.m *= 2
;
- uhi.
bits.
e--;
+ if (uhi.
i.m << 1 == 0
) {
+ uhi.
i.m = 0
;
+ uhi.
i.s
e--;
}
}
+ uhi.i.m--;
}
}
- return uhi.
x
;
+ return uhi.
f
;
}
/* adjusted add so the result is correct when rounded to double (or less) precision */
}
/* adjusted add so the result is correct when rounded to double (or less) precision */
@@
-82,9
+72,9
@@
static long double dmul(long double x, long double y)
static int getexp(long double x)
{
static int getexp(long double x)
{
- union ld
80
u;
- u.
x
= x;
- return u.
bits.e
;
+ union ld
shape
u;
+ u.
f
= x;
+ return u.
i.se & 0x7fff
;
}
double fma(double x, double y, double z)
}
double fma(double x, double y, double z)
@@
-242,16
+232,16
@@
static inline struct dd dd_add(double a, double b)
static inline double add_adjusted(double a, double b)
{
struct dd sum;
static inline double add_adjusted(double a, double b)
{
struct dd sum;
- u
int64_t hibits, lobits
;
+ u
nion {double f; uint64_t i;} uhi, ulo
;
sum = dd_add(a, b);
if (sum.lo != 0) {
sum = dd_add(a, b);
if (sum.lo != 0) {
-
EXTRACT_WORD64(hibits, sum.hi)
;
- if ((
hibits
& 1) == 0) {
+
uhi.f = sum.hi
;
+ if ((
uhi.i
& 1) == 0) {
/* hibits += (int)copysign(1.0, sum.hi * sum.lo) */
/* hibits += (int)copysign(1.0, sum.hi * sum.lo) */
-
EXTRACT_WORD64(lobits, sum.lo)
;
-
hibits += 1 - ((hibits ^ lobits
) >> 62);
-
INSERT_WORD64(sum.hi, hibits)
;
+
ulo.f = sum.lo
;
+
uhi.i += 1 - ((uhi.i ^ ulo.i
) >> 62);
+
sum.hi = uhi.f
;
}
}
return (sum.hi);
}
}
return (sum.hi);
@@
-265,7
+255,7
@@
static inline double add_adjusted(double a, double b)
static inline double add_and_denormalize(double a, double b, int scale)
{
struct dd sum;
static inline double add_and_denormalize(double a, double b, int scale)
{
struct dd sum;
- u
int64_t hibits, lobits
;
+ u
nion {double f; uint64_t i;} uhi, ulo
;
int bits_lost;
sum = dd_add(a, b);
int bits_lost;
sum = dd_add(a, b);
@@
-281,13
+271,13
@@
static inline double add_and_denormalize(double a, double b, int scale)
* break the ties manually.
*/
if (sum.lo != 0) {
* break the ties manually.
*/
if (sum.lo != 0) {
-
EXTRACT_WORD64(hibits, sum.hi)
;
- bits_lost = -((int)(
hibits
>> 52) & 0x7ff) - scale + 1;
- if (bits_lost != 1 ^ (int)(
hibits
& 1)) {
+
uhi.f = sum.hi
;
+ bits_lost = -((int)(
uhi.i
>> 52) & 0x7ff) - scale + 1;
+ if (bits_lost != 1 ^ (int)(
uhi.i
& 1)) {
/* hibits += (int)copysign(1.0, sum.hi * sum.lo) */
/* hibits += (int)copysign(1.0, sum.hi * sum.lo) */
-
EXTRACT_WORD64(lobits, sum.lo)
;
-
hibits += 1 - (((hibits ^ lobits
) >> 62) & 2);
-
INSERT_WORD64(sum.hi, hibits)
;
+
ulo.f = sum.lo
;
+
uhi.i += 1 - (((uhi.i ^ ulo.i
) >> 62) & 2);
+
sum.hi = uhi.f
;
}
}
return scalbn(sum.hi, scale);
}
}
return scalbn(sum.hi, scale);
@@
-441,10
+431,24
@@
double fma(double x, double y, double z)
/*
* There is no need to worry about double rounding in directed
* rounding modes.
/*
* There is no need to worry about double rounding in directed
* rounding modes.
+ * But underflow may not be raised properly, example in downward rounding:
+ * fma(0x1.000000001p-1000, 0x1.000000001p-30, -0x1p-1066)
*/
*/
+ double ret;
+#if defined(FE_INEXACT) && defined(FE_UNDERFLOW)
+ int e = fetestexcept(FE_INEXACT);
+ feclearexcept(FE_INEXACT);
+#endif
fesetround(oround);
adj = r.lo + xy.lo;
fesetround(oround);
adj = r.lo + xy.lo;
- return scalbn(r.hi + adj, spread);
+ ret = scalbn(r.hi + adj, spread);
+#if defined(FE_INEXACT) && defined(FE_UNDERFLOW)
+ if (ilogb(ret) < -1022 && fetestexcept(FE_INEXACT))
+ feraiseexcept(FE_UNDERFLOW);
+ else if (e)
+ feraiseexcept(FE_INEXACT);
+#endif
+ return ret;
}
adj = add_adjusted(r.lo, xy.lo);
}
adj = add_adjusted(r.lo, xy.lo);