Print this page
11210 libm should be cstyle(1ONBLD) clean
@@ -20,10 +20,11 @@
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
*/
+
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
@@ -32,11 +33,10 @@
#include "libm.h"
#include "fma.h"
#include "fenv_inlines.h"
#if defined(__sparc)
-
static const union {
unsigned i[2];
double d;
} C[] = {
{ 0x3fe00000u, 0 },
@@ -66,15 +66,17 @@
/*
* fma for SPARC: 64-bit double precision, big-endian
*/
double
-__fma(double x, double y, double z) {
+__fma(double x, double y, double z)
+{
union {
unsigned i[2];
double d;
} xx, yy, zz;
+
double xhi, yhi, xlo, ylo, t;
unsigned int xy0, xy1, xy2, xy3, z0, z1, z2, z3, fsr, rm, sticky;
int hx, hy, hz, ex, ey, ez, exy, sxy, sz, e, ibit;
volatile double dummy;
@@ -85,12 +87,12 @@
hx = xx.i[0] & ~0x80000000;
hy = yy.i[0] & ~0x80000000;
hz = zz.i[0] & ~0x80000000;
/* dispense with inf, nan, and zero cases */
- if (hx >= 0x7ff00000 || hy >= 0x7ff00000 || (hx | xx.i[1]) == 0 ||
- (hy | yy.i[1]) == 0) /* x or y is inf, nan, or zero */
+ if (hx >= 0x7ff00000 || hy >= 0x7ff00000 || (hx | xx.i[1]) == 0 || (hy |
+ yy.i[1]) == 0) /* x or y is inf, nan, or zero */
return (x * y + z);
if (hz >= 0x7ff00000) /* z is inf or nan */
return (x + z); /* avoid spurious under/overflow in x * y */
@@ -111,20 +113,25 @@
/* extract signs and exponents, and normalize subnormals */
sxy = (xx.i[0] ^ yy.i[0]) & 0x80000000;
sz = zz.i[0] & 0x80000000;
ex = hx >> 20;
+
if (!ex) {
xx.d = x * two52;
ex = ((xx.i[0] & ~0x80000000) >> 20) - 52;
}
+
ey = hy >> 20;
+
if (!ey) {
yy.d = y * two52;
ey = ((yy.i[0] & ~0x80000000) >> 20) - 52;
}
+
ez = hz >> 20;
+
if (!ez) {
zz.d = z * two52;
ez = ((zz.i[0] & ~0x80000000) >> 20) - 52;
}
@@ -138,10 +145,11 @@
yhi = ((y + twom26) + two27) - two27;
xlo = x - xhi;
ylo = y - yhi;
x *= y;
y = ((xhi * yhi - x) + xhi * ylo + xlo * yhi) + xlo * ylo;
+
if (x >= two) {
x *= half;
y *= half;
exy++;
}
@@ -160,51 +168,70 @@
/*
* now x*y is represented by sxy, exy, and xy[0-3], and z is
* represented likewise; swap if need be so |xy| <= |z|
*/
- if (exy > ez || (exy == ez && (xy0 > z0 || (xy0 == z0 &&
- (xy1 > z1 || (xy1 == z1 && (xy2 | xy3) != 0)))))) {
- e = sxy; sxy = sz; sz = e;
- e = exy; exy = ez; ez = e;
- e = xy0; xy0 = z0; z0 = e;
- e = xy1; xy1 = z1; z1 = e;
- z2 = xy2; xy2 = 0;
- z3 = xy3; xy3 = 0;
+ if (exy > ez || (exy == ez && (xy0 > z0 || (xy0 == z0 && (xy1 > z1 ||
+ (xy1 == z1 && (xy2 | xy3) != 0)))))) {
+ e = sxy;
+ sxy = sz;
+ sz = e;
+ e = exy;
+ exy = ez;
+ ez = e;
+ e = xy0;
+ xy0 = z0;
+ z0 = e;
+ e = xy1;
+ xy1 = z1;
+ z1 = e;
+ z2 = xy2;
+ xy2 = 0;
+ z3 = xy3;
+ xy3 = 0;
}
/* shift the significand of xy keeping a sticky bit */
e = ez - exy;
+
if (e > 116) {
xy0 = xy1 = xy2 = 0;
xy3 = 1;
} else if (e >= 96) {
sticky = xy3 | xy2 | xy1 | ((xy0 << 1) << (127 - e));
xy3 = xy0 >> (e - 96);
+
if (sticky)
xy3 |= 1;
+
xy0 = xy1 = xy2 = 0;
} else if (e >= 64) {
sticky = xy3 | xy2 | ((xy1 << 1) << (95 - e));
xy3 = (xy1 >> (e - 64)) | ((xy0 << 1) << (95 - e));
+
if (sticky)
xy3 |= 1;
+
xy2 = xy0 >> (e - 64);
xy0 = xy1 = 0;
} else if (e >= 32) {
sticky = xy3 | ((xy2 << 1) << (63 - e));
xy3 = (xy2 >> (e - 32)) | ((xy1 << 1) << (63 - e));
+
if (sticky)
xy3 |= 1;
+
xy2 = (xy1 >> (e - 32)) | ((xy0 << 1) << (63 - e));
xy1 = xy0 >> (e - 32);
xy0 = 0;
} else if (e) {
sticky = (xy3 << 1) << (31 - e);
xy3 = (xy3 >> e) | ((xy2 << 1) << (31 - e));
+
if (sticky)
xy3 |= 1;
+
xy2 = (xy2 >> e) | ((xy1 << 1) << (31 - e));
xy1 = (xy1 >> e) | ((xy0 << 1) << (31 - e));
xy0 >>= e;
}
@@ -212,64 +239,80 @@
if (sxy ^ sz) {
xy0 = ~xy0;
xy1 = ~xy1;
xy2 = ~xy2;
xy3 = -xy3;
+
if (xy3 == 0)
if (++xy2 == 0)
if (++xy1 == 0)
xy0++;
}
/* add, propagating carries */
z3 += xy3;
e = (z3 < xy3);
z2 += xy2;
+
if (e) {
z2++;
e = (z2 <= xy2);
- } else
+ } else {
e = (z2 < xy2);
+ }
+
z1 += xy1;
+
if (e) {
z1++;
e = (z1 <= xy1);
- } else
+ } else {
e = (z1 < xy1);
+ }
+
z0 += xy0;
+
if (e)
z0++;
/* postnormalize and collect rounding information into z2 */
if (ez < 1) {
/* result is tiny; shift right until exponent is within range */
e = 1 - ez;
+
if (e > 56) {
z2 = 1; /* result can't be exactly zero */
z0 = z1 = 0;
} else if (e >= 32) {
sticky = z3 | z2 | ((z1 << 1) << (63 - e));
z2 = (z1 >> (e - 32)) | ((z0 << 1) << (63 - e));
+
if (sticky)
z2 |= 1;
+
z1 = z0 >> (e - 32);
z0 = 0;
} else {
sticky = z3 | (z2 << 1) << (31 - e);
z2 = (z2 >> e) | ((z1 << 1) << (31 - e));
+
if (sticky)
z2 |= 1;
+
z1 = (z1 >> e) | ((z0 << 1) << (31 - e));
z0 >>= e;
}
+
ez = 1;
} else if (z0 >= 0x200000) {
/* carry out; shift right by one */
sticky = (z2 & 1) | z3;
z2 = (z2 >> 1) | (z1 << 31);
+
if (sticky)
z2 |= 1;
+
z1 = (z1 >> 1) | (z0 << 31);
z0 >>= 1;
ez++;
} else {
if (z0 < 0x100000 && (z0 | z1 | z2 | z3) != 0) {
@@ -282,32 +325,36 @@
z1 = z2;
z2 = z3;
z3 = 0;
ez -= 32;
}
+
while (z0 < 0x100000 && ez > 1) {
z0 = (z0 << 1) | (z1 >> 31);
z1 = (z1 << 1) | (z2 >> 31);
z2 = (z2 << 1) | (z3 >> 31);
z3 <<= 1;
ez--;
}
}
+
if (z3)
z2 |= 1;
}
/* get the rounding mode and clear current exceptions */
rm = fsr >> 30;
fsr &= ~FSR_CEXC;
/* strip off the integer bit, if there is one */
ibit = z0 & 0x100000;
- if (ibit)
+
+ if (ibit) {
z0 -= 0x100000;
- else {
+ } else {
ez = 0;
+
if (!(z0 | z1 | z2)) { /* exact zero */
zz.i[0] = rm == FSR_RM ? 0x80000000 : 0;
zz.i[1] = 0;
__fenv_setfsr32(&fsr);
return (zz.d);
@@ -324,12 +371,12 @@
/* round and raise exceptions */
if (z2) {
fsr |= FSR_NXC;
/* decide whether to round the fraction up */
- if (rm == FSR_RP || (rm == FSR_RN && (z2 > 0x80000000u ||
- (z2 == 0x80000000u && (z1 & 1))))) {
+ if (rm == FSR_RP || (rm == FSR_RN && (z2 > 0x80000000u || (z2 ==
+ 0x80000000u && (z1 & 1))))) {
/* round up and renormalize if necessary */
if (++z1 == 0) {
if (++z0 == 0x100000) {
z0 = 0;
ez++;
@@ -345,10 +392,11 @@
zz.i[1] = 0;
} else {
zz.i[0] = sz | 0x7fefffff;
zz.i[1] = 0xffffffff;
}
+
fsr |= FSR_OFC | FSR_NXC;
} else {
zz.i[0] = sz | (ez << 20) | z0;
zz.i[1] = z1;
@@ -365,15 +413,17 @@
}
/* restore the fsr and emulate exceptions as needed */
if ((fsr & FSR_CEXC) & (fsr >> 23)) {
__fenv_setfsr32(&fsr);
+
if (fsr & FSR_OFC) {
dummy = huge;
dummy *= huge;
} else if (fsr & FSR_UFC) {
dummy = tiny;
+
if (fsr & FSR_NXC)
dummy *= tiny;
else
dummy -= tiny2;
} else {
@@ -382,51 +432,53 @@
}
} else {
fsr |= (fsr & 0x1f) << 5;
__fenv_setfsr32(&fsr);
}
+
return (zz.d);
}
-
#elif defined(__x86)
-
#if defined(__amd64)
#define NI 4
#else
#define NI 3
#endif
/*
* fma for x86: 64-bit double precision, little-endian
*/
double
-__fma(double x, double y, double z) {
+__fma(double x, double y, double z)
+{
union {
unsigned i[NI];
long double e;
} xx, yy, zz;
+
long double xe, ye, xhi, xlo, yhi, ylo;
int ex, ey, ez;
unsigned cwsw, oldcwsw, rm;
/* convert the operands to double extended */
- xx.e = (long double) x;
- yy.e = (long double) y;
- zz.e = (long double) z;
+ xx.e = (long double)x;
+ yy.e = (long double)y;
+ zz.e = (long double)z;
/* extract the exponents of the arguments */
ex = xx.i[2] & 0x7fff;
ey = yy.i[2] & 0x7fff;
ez = zz.i[2] & 0x7fff;
/* dispense with inf, nan, and zero cases */
if (ex == 0x7fff || ey == 0x7fff || ex == 0 || ey == 0)
/* x or y is inf, nan, or zero */
- return ((double) (xx.e * yy.e + zz.e));
+ return ((double)(xx.e * yy.e + zz.e));
if (ez >= 0x7fff) /* z is inf or nan */
- return ((double) (xx.e + zz.e));
+ return ((double)(xx.e + zz.e));
+
/* avoid spurious inexact in x * y */
/*
* save the control and status words, mask all exceptions, and
* set rounding to 64-bit precision and to-nearest
@@ -466,30 +518,30 @@
if (yy.i[1] != 0) { /* yy.e is nonzero */
/* perturb yy.e if its least significant 10 bits are zero */
if (!(yy.i[0] & 0x3ff)) {
xx.e = ylo + xlo;
+
if (xx.i[1] != 0) {
- xx.i[2] = (xx.i[2] & 0x8000) |
- ((yy.i[2] & 0x7fff) - 63);
+ xx.i[2] = (xx.i[2] & 0x8000) | ((yy.i[2] &
+ 0x7fff) - 63);
xx.i[1] = 0x80000000;
xx.i[0] = 0;
yy.e += xx.e;
}
}
} else {
/* set sign of zero result according to rounding direction */
rm = oldcwsw & 0x0c000000;
- yy.i[2] = ((rm == FCW_RM)? 0x8000 : 0);
+ yy.i[2] = ((rm == FCW_RM) ? 0x8000 : 0);
}
/*
* restore the control and status words and convert the result
* to double
*/
__fenv_setcwsw(&oldcwsw);
- return ((double) yy.e);
+ return ((double)yy.e);
}
-
#else
#error Unknown architecture
#endif