dp_sqrt.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* IEEE754 floating point arithmetic
  3. * double precision square root
  4. */
  5. /*
  6. * MIPS floating point support
  7. * Copyright (C) 1994-2000 Algorithmics Ltd.
  8. */
  9. #include "ieee754dp.h"
  10. static const unsigned int table[] = {
  11. 0, 1204, 3062, 5746, 9193, 13348, 18162, 23592,
  12. 29598, 36145, 43202, 50740, 58733, 67158, 75992,
  13. 85215, 83599, 71378, 60428, 50647, 41945, 34246,
  14. 27478, 21581, 16499, 12183, 8588, 5674, 3403,
  15. 1742, 661, 130
  16. };
  17. union ieee754dp ieee754dp_sqrt(union ieee754dp x)
  18. {
  19. struct _ieee754_csr oldcsr;
  20. union ieee754dp y, z, t;
  21. unsigned int scalx, yh;
  22. COMPXDP;
  23. EXPLODEXDP;
  24. ieee754_clearcx();
  25. FLUSHXDP;
  26. /* x == INF or NAN? */
  27. switch (xc) {
  28. case IEEE754_CLASS_SNAN:
  29. return ieee754dp_nanxcpt(x);
  30. case IEEE754_CLASS_QNAN:
  31. /* sqrt(Nan) = Nan */
  32. return x;
  33. case IEEE754_CLASS_ZERO:
  34. /* sqrt(0) = 0 */
  35. return x;
  36. case IEEE754_CLASS_INF:
  37. if (xs) {
  38. /* sqrt(-Inf) = Nan */
  39. ieee754_setcx(IEEE754_INVALID_OPERATION);
  40. return ieee754dp_indef();
  41. }
  42. /* sqrt(+Inf) = Inf */
  43. return x;
  44. case IEEE754_CLASS_DNORM:
  45. DPDNORMX;
  46. fallthrough;
  47. case IEEE754_CLASS_NORM:
  48. if (xs) {
  49. /* sqrt(-x) = Nan */
  50. ieee754_setcx(IEEE754_INVALID_OPERATION);
  51. return ieee754dp_indef();
  52. }
  53. break;
  54. }
  55. /* save old csr; switch off INX enable & flag; set RN rounding */
  56. oldcsr = ieee754_csr;
  57. ieee754_csr.mx &= ~IEEE754_INEXACT;
  58. ieee754_csr.sx &= ~IEEE754_INEXACT;
  59. ieee754_csr.rm = FPU_CSR_RN;
  60. /* adjust exponent to prevent overflow */
  61. scalx = 0;
  62. if (xe > 512) { /* x > 2**-512? */
  63. xe -= 512; /* x = x / 2**512 */
  64. scalx += 256;
  65. } else if (xe < -512) { /* x < 2**-512? */
  66. xe += 512; /* x = x * 2**512 */
  67. scalx -= 256;
  68. }
  69. x = builddp(0, xe + DP_EBIAS, xm & ~DP_HIDDEN_BIT);
  70. y = x;
  71. /* magic initial approximation to almost 8 sig. bits */
  72. yh = y.bits >> 32;
  73. yh = (yh >> 1) + 0x1ff80000;
  74. yh = yh - table[(yh >> 15) & 31];
  75. y.bits = ((u64) yh << 32) | (y.bits & 0xffffffff);
  76. /* Heron's rule once with correction to improve to ~18 sig. bits */
  77. /* t=x/y; y=y+t; py[n0]=py[n0]-0x00100006; py[n1]=0; */
  78. t = ieee754dp_div(x, y);
  79. y = ieee754dp_add(y, t);
  80. y.bits -= 0x0010000600000000LL;
  81. y.bits &= 0xffffffff00000000LL;
  82. /* triple to almost 56 sig. bits: y ~= sqrt(x) to within 1 ulp */
  83. /* t=y*y; z=t; pt[n0]+=0x00100000; t+=z; z=(x-z)*y; */
  84. t = ieee754dp_mul(y, y);
  85. z = t;
  86. t.bexp += 0x001;
  87. t = ieee754dp_add(t, z);
  88. z = ieee754dp_mul(ieee754dp_sub(x, z), y);
  89. /* t=z/(t+x) ; pt[n0]+=0x00100000; y+=t; */
  90. t = ieee754dp_div(z, ieee754dp_add(t, x));
  91. t.bexp += 0x001;
  92. y = ieee754dp_add(y, t);
  93. /* twiddle last bit to force y correctly rounded */
  94. /* set RZ, clear INEX flag */
  95. ieee754_csr.rm = FPU_CSR_RZ;
  96. ieee754_csr.sx &= ~IEEE754_INEXACT;
  97. /* t=x/y; ...chopped quotient, possibly inexact */
  98. t = ieee754dp_div(x, y);
  99. if (ieee754_csr.sx & IEEE754_INEXACT || t.bits != y.bits) {
  100. if (!(ieee754_csr.sx & IEEE754_INEXACT))
  101. /* t = t-ulp */
  102. t.bits -= 1;
  103. /* add inexact to result status */
  104. oldcsr.cx |= IEEE754_INEXACT;
  105. oldcsr.sx |= IEEE754_INEXACT;
  106. switch (oldcsr.rm) {
  107. case FPU_CSR_RU:
  108. y.bits += 1;
  109. fallthrough;
  110. case FPU_CSR_RN:
  111. t.bits += 1;
  112. break;
  113. }
  114. /* y=y+t; ...chopped sum */
  115. y = ieee754dp_add(y, t);
  116. /* adjust scalx for correctly rounded sqrt(x) */
  117. scalx -= 1;
  118. }
  119. /* py[n0]=py[n0]+scalx; ...scale back y */
  120. y.bexp += scalx;
  121. /* restore rounding mode, possibly set inexact */
  122. ieee754_csr = oldcsr;
  123. return y;
  124. }