fma.c revision 25c28e83beb90e7c80452a7c818c5e6f73a07dc8
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#if defined(ELFOBJ)
#endif
#include "libm.h"
#include "fma.h"
#include "fenv_inlines.h"
#if defined(__sparc)
static const union {
unsigned i[2];
double d;
} C[] = {
{ 0x3fe00000u, 0 },
{ 0x40000000u, 0 },
{ 0x43300000u, 0 },
{ 0x41a00000u, 0 },
{ 0x3e500000u, 0 },
{ 0x3df00000u, 0 },
{ 0x3bf00000u, 0 },
{ 0x7fe00000u, 0 },
{ 0x00100000u, 0 },
{ 0x00100001u, 0 }
};
#define half C[0].d
#define two C[1].d
#define two52 C[2].d
#define two27 C[3].d
#define twom26 C[4].d
#define twom32 C[5].d
#define twom64 C[6].d
#define huge C[7].d
#define tiny C[8].d
#define tiny2 C[9].d
static const unsigned int fsr_rm = 0xc0000000u;
/*
* fma for SPARC: 64-bit double precision, big-endian
*/
double
__fma(double x, double y, double z) {
union {
unsigned i[2];
double d;
volatile double dummy;
/* extract the high order words of the arguments */
xx.d = x;
yy.d = y;
zz.d = z;
/* dispense with inf, nan, and zero cases */
return (x * y + z);
/*
* x * y isn't zero but could underflow to zero,
* so don't add z, lest we perturb the sign
*/
return (x * y);
/*
* now x, y, and z are all finite and nonzero; save the fsr and
* set round-to-negative-infinity mode (and clear nonstandard
* mode before we try to scale subnormal operands)
*/
/* extract signs and exponents, and normalize subnormals */
if (!ex) {
}
if (!ey) {
}
if (!ez) {
}
/* multiply x*y to 106 bits */
x = xx.d;
y = yy.d;
x *= y;
if (x >= two) {
x *= half;
y *= half;
exy++;
}
/* extract the significands */
xx.d = x;
/*
* now x*y is represented by sxy, exy, and xy[0-3], and z is
* represented likewise; swap if need be so |xy| <= |z|
*/
}
/* shift the significand of xy keeping a sticky bit */
if (e > 116) {
xy3 = 1;
} else if (e >= 96) {
if (sticky)
xy3 |= 1;
} else if (e >= 64) {
if (sticky)
xy3 |= 1;
} else if (e >= 32) {
if (sticky)
xy3 |= 1;
xy0 = 0;
} else if (e) {
if (sticky)
xy3 |= 1;
xy0 >>= e;
}
/* if this is a magnitude subtract, negate the significand of xy */
if (xy3 == 0)
if (++xy2 == 0)
if (++xy1 == 0)
xy0++;
}
/* add, propagating carries */
if (e) {
z2++;
} else
if (e) {
z1++;
} else
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) {
} else if (e >= 32) {
if (sticky)
z2 |= 1;
z0 = 0;
} else {
if (sticky)
z2 |= 1;
z0 >>= e;
}
ez = 1;
} else if (z0 >= 0x200000) {
/* carry out; shift right by one */
if (sticky)
z2 |= 1;
z0 >>= 1;
ez++;
} else {
/*
* borrow/cancellation; shift left as much as
* exponent allows
*/
z3 = 0;
ez -= 32;
}
z3 <<= 1;
ez--;
}
}
if (z3)
z2 |= 1;
}
/* get the rounding mode and clear current exceptions */
/* strip off the integer bit, if there is one */
if (ibit)
z0 -= 0x100000;
else {
ez = 0;
zz.i[1] = 0;
return (zz.d);
}
}
/*
* flip the sense of directed roundings if the result is negative;
* the logic below applies to a positive result
*/
if (sz)
/* round and raise exceptions */
if (z2) {
/* decide whether to round the fraction up */
/* round up and renormalize if necessary */
if (++z1 == 0) {
if (++z0 == 0x100000) {
z0 = 0;
ez++;
}
}
}
}
if (ez >= 0x7ff) {
zz.i[1] = 0;
} else {
}
} else {
/*
* !ibit => exact result was tiny before rounding,
* z2 nonzero => result delivered is inexact
*/
if (!ibit) {
if (z2)
}
}
/* restore the fsr and emulate exceptions as needed */
else
} else {
}
} else {
}
return (zz.d);
}
#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) {
union {
unsigned i[NI];
long double e;
/* convert the operands to double extended */
xx.e = (long double) x;
yy.e = (long double) y;
zz.e = (long double) z;
/* extract the exponents of the arguments */
/* dispense with inf, nan, and zero cases */
/* x or y is inf, nan, or zero */
/* 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
*/
/* multiply x*y to 106 bits */
xx.i[0] = 0;
yy.i[0] = 0;
/* distill the sum of xe, ye, and z */
/* now (xhi,xlo) = ye + z */
/* perturb yy.e if its least significant 10 bits are zero */
if (!(yy.i[0] & 0x3ff)) {
if (xx.i[1] != 0) {
xx.i[0] = 0;
}
}
} else {
/* set sign of zero result according to rounding direction */
}
/*
* restore the control and status words and convert the result
* to double
*/
return ((double) yy.e);
}
#else
#endif