dp_sqrt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* IEEE754 floating point arithmetic
  2. * double precision square root
  3. */
  4. /*
  5. * MIPS floating point support
  6. * Copyright (C) 1994-2000 Algorithmics Ltd.
  7. *
  8. * ########################################################################
  9. *
  10. * This program is free software; you can distribute it and/or modify it
  11. * under the terms of the GNU General Public License (Version 2) as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. * for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * ########################################################################
  24. */
  25. #include "ieee754dp.h"
  26. static const unsigned table[] = {
  27. 0, 1204, 3062, 5746, 9193, 13348, 18162, 23592,
  28. 29598, 36145, 43202, 50740, 58733, 67158, 75992,
  29. 85215, 83599, 71378, 60428, 50647, 41945, 34246,
  30. 27478, 21581, 16499, 12183, 8588, 5674, 3403,
  31. 1742, 661, 130
  32. };
  33. ieee754dp ieee754dp_sqrt(ieee754dp x)
  34. {
  35. struct _ieee754_csr oldcsr;
  36. ieee754dp y, z, t;
  37. unsigned scalx, yh;
  38. COMPXDP;
  39. EXPLODEXDP;
  40. CLEARCX;
  41. FLUSHXDP;
  42. /* x == INF or NAN? */
  43. switch (xc) {
  44. case IEEE754_CLASS_QNAN:
  45. /* sqrt(Nan) = Nan */
  46. return ieee754dp_nanxcpt(x, "sqrt");
  47. case IEEE754_CLASS_SNAN:
  48. SETCX(IEEE754_INVALID_OPERATION);
  49. return ieee754dp_nanxcpt(ieee754dp_indef(), "sqrt");
  50. case IEEE754_CLASS_ZERO:
  51. /* sqrt(0) = 0 */
  52. return x;
  53. case IEEE754_CLASS_INF:
  54. if (xs) {
  55. /* sqrt(-Inf) = Nan */
  56. SETCX(IEEE754_INVALID_OPERATION);
  57. return ieee754dp_nanxcpt(ieee754dp_indef(), "sqrt");
  58. }
  59. /* sqrt(+Inf) = Inf */
  60. return x;
  61. case IEEE754_CLASS_DNORM:
  62. DPDNORMX;
  63. /* fall through */
  64. case IEEE754_CLASS_NORM:
  65. if (xs) {
  66. /* sqrt(-x) = Nan */
  67. SETCX(IEEE754_INVALID_OPERATION);
  68. return ieee754dp_nanxcpt(ieee754dp_indef(), "sqrt");
  69. }
  70. break;
  71. }
  72. /* save old csr; switch off INX enable & flag; set RN rounding */
  73. oldcsr = ieee754_csr;
  74. ieee754_csr.mx &= ~IEEE754_INEXACT;
  75. ieee754_csr.sx &= ~IEEE754_INEXACT;
  76. ieee754_csr.rm = IEEE754_RN;
  77. /* adjust exponent to prevent overflow */
  78. scalx = 0;
  79. if (xe > 512) { /* x > 2**-512? */
  80. xe -= 512; /* x = x / 2**512 */
  81. scalx += 256;
  82. } else if (xe < -512) { /* x < 2**-512? */
  83. xe += 512; /* x = x * 2**512 */
  84. scalx -= 256;
  85. }
  86. y = x = builddp(0, xe + DP_EBIAS, xm & ~DP_HIDDEN_BIT);
  87. /* magic initial approximation to almost 8 sig. bits */
  88. yh = y.bits >> 32;
  89. yh = (yh >> 1) + 0x1ff80000;
  90. yh = yh - table[(yh >> 15) & 31];
  91. y.bits = ((u64) yh << 32) | (y.bits & 0xffffffff);
  92. /* Heron's rule once with correction to improve to ~18 sig. bits */
  93. /* t=x/y; y=y+t; py[n0]=py[n0]-0x00100006; py[n1]=0; */
  94. t = ieee754dp_div(x, y);
  95. y = ieee754dp_add(y, t);
  96. y.bits -= 0x0010000600000000LL;
  97. y.bits &= 0xffffffff00000000LL;
  98. /* triple to almost 56 sig. bits: y ~= sqrt(x) to within 1 ulp */
  99. /* t=y*y; z=t; pt[n0]+=0x00100000; t+=z; z=(x-z)*y; */
  100. z = t = ieee754dp_mul(y, y);
  101. t.parts.bexp += 0x001;
  102. t = ieee754dp_add(t, z);
  103. z = ieee754dp_mul(ieee754dp_sub(x, z), y);
  104. /* t=z/(t+x) ; pt[n0]+=0x00100000; y+=t; */
  105. t = ieee754dp_div(z, ieee754dp_add(t, x));
  106. t.parts.bexp += 0x001;
  107. y = ieee754dp_add(y, t);
  108. /* twiddle last bit to force y correctly rounded */
  109. /* set RZ, clear INEX flag */
  110. ieee754_csr.rm = IEEE754_RZ;
  111. ieee754_csr.sx &= ~IEEE754_INEXACT;
  112. /* t=x/y; ...chopped quotient, possibly inexact */
  113. t = ieee754dp_div(x, y);
  114. if (ieee754_csr.sx & IEEE754_INEXACT || t.bits != y.bits) {
  115. if (!(ieee754_csr.sx & IEEE754_INEXACT))
  116. /* t = t-ulp */
  117. t.bits -= 1;
  118. /* add inexact to result status */
  119. oldcsr.cx |= IEEE754_INEXACT;
  120. oldcsr.sx |= IEEE754_INEXACT;
  121. switch (oldcsr.rm) {
  122. case IEEE754_RP:
  123. y.bits += 1;
  124. /* drop through */
  125. case IEEE754_RN:
  126. t.bits += 1;
  127. break;
  128. }
  129. /* y=y+t; ...chopped sum */
  130. y = ieee754dp_add(y, t);
  131. /* adjust scalx for correctly rounded sqrt(x) */
  132. scalx -= 1;
  133. }
  134. /* py[n0]=py[n0]+scalx; ...scale back y */
  135. y.parts.bexp += scalx;
  136. /* restore rounding mode, possibly set inexact */
  137. ieee754_csr = oldcsr;
  138. return y;
  139. }