dp_sqrt.c 4.1 KB

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