union ieee754dp ieee754dp_sqrt(union ieee754dp x)
{ struct _ieee754_csr oldcsr; union ieee754dp y, z, t; unsignedint scalx, yh;
COMPXDP;
EXPLODEXDP;
ieee754_clearcx();
FLUSHXDP;
/* x == INF or NAN? */ switch (xc) { case IEEE754_CLASS_SNAN: return ieee754dp_nanxcpt(x);
case IEEE754_CLASS_QNAN: /* sqrt(Nan) = Nan */ return x;
case IEEE754_CLASS_ZERO: /* sqrt(0) = 0 */ return x;
case IEEE754_CLASS_INF: if (xs) { /* sqrt(-Inf) = Nan */
ieee754_setcx(IEEE754_INVALID_OPERATION); return ieee754dp_indef();
} /* sqrt(+Inf) = Inf */ return x;
case IEEE754_CLASS_DNORM:
DPDNORMX;
fallthrough; case IEEE754_CLASS_NORM: if (xs) { /* sqrt(-x) = Nan */
ieee754_setcx(IEEE754_INVALID_OPERATION); return ieee754dp_indef();
} break;
}
/* save old csr; switch off INX enable & flag; set RN rounding */
oldcsr = ieee754_csr;
ieee754_csr.mx &= ~IEEE754_INEXACT;
ieee754_csr.sx &= ~IEEE754_INEXACT;
ieee754_csr.rm = FPU_CSR_RN;
/* adjust exponent to prevent overflow */
scalx = 0; if (xe > 512) { /* x > 2**-512? */
xe -= 512; /* x = x / 2**512 */
scalx += 256;
} elseif (xe < -512) { /* x < 2**-512? */
xe += 512; /* x = x * 2**512 */
scalx -= 256;
}
x = builddp(0, xe + DP_EBIAS, xm & ~DP_HIDDEN_BIT);
y = x;
/* Heron's rule once with correction to improve to ~18 sig. bits */ /* t=x/y; y=y+t; py[n0]=py[n0]-0x00100006; py[n1]=0; */
t = ieee754dp_div(x, y);
y = ieee754dp_add(y, t);
y.bits -= 0x0010000600000000LL;
y.bits &= 0xffffffff00000000LL;
/* triple to almost 56 sig. bits: y ~= sqrt(x) to within 1 ulp */ /* t=y*y; z=t; pt[n0]+=0x00100000; t+=z; z=(x-z)*y; */
t = ieee754dp_mul(y, y);
z = t;
t.bexp += 0x001;
t = ieee754dp_add(t, z);
z = ieee754dp_mul(ieee754dp_sub(x, z), y);
/* t=z/(t+x) ; pt[n0]+=0x00100000; y+=t; */
t = ieee754dp_div(z, ieee754dp_add(t, x));
t.bexp += 0x001;
y = ieee754dp_add(y, t);
/* twiddle last bit to force y correctly rounded */
/* set RZ, clear INEX flag */
ieee754_csr.rm = FPU_CSR_RZ;
ieee754_csr.sx &= ~IEEE754_INEXACT;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.