sp_sqrt.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* IEEE754 floating point arithmetic
  2. * single precision square root
  3. */
  4. /*
  5. * MIPS floating point support
  6. * Copyright (C) 1994-2000 Algorithmics Ltd.
  7. * http://www.algor.co.uk
  8. *
  9. * ########################################################################
  10. *
  11. * This program is free software; you can distribute it and/or modify it
  12. * under the terms of the GNU General Public License (Version 2) as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. * for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  23. *
  24. * ########################################################################
  25. */
  26. #include "ieee754sp.h"
  27. ieee754sp ieee754sp_sqrt(ieee754sp x)
  28. {
  29. int ix, s, q, m, t, i;
  30. unsigned int r;
  31. COMPXSP;
  32. /* take care of Inf and NaN */
  33. EXPLODEXSP;
  34. CLEARCX;
  35. FLUSHXSP;
  36. /* x == INF or NAN? */
  37. switch (xc) {
  38. case IEEE754_CLASS_QNAN:
  39. /* sqrt(Nan) = Nan */
  40. return ieee754sp_nanxcpt(x, "sqrt");
  41. case IEEE754_CLASS_SNAN:
  42. SETCX(IEEE754_INVALID_OPERATION);
  43. return ieee754sp_nanxcpt(ieee754sp_indef(), "sqrt");
  44. case IEEE754_CLASS_ZERO:
  45. /* sqrt(0) = 0 */
  46. return x;
  47. case IEEE754_CLASS_INF:
  48. if (xs) {
  49. /* sqrt(-Inf) = Nan */
  50. SETCX(IEEE754_INVALID_OPERATION);
  51. return ieee754sp_nanxcpt(ieee754sp_indef(), "sqrt");
  52. }
  53. /* sqrt(+Inf) = Inf */
  54. return x;
  55. case IEEE754_CLASS_DNORM:
  56. case IEEE754_CLASS_NORM:
  57. if (xs) {
  58. /* sqrt(-x) = Nan */
  59. SETCX(IEEE754_INVALID_OPERATION);
  60. return ieee754sp_nanxcpt(ieee754sp_indef(), "sqrt");
  61. }
  62. break;
  63. }
  64. ix = x.bits;
  65. /* normalize x */
  66. m = (ix >> 23);
  67. if (m == 0) { /* subnormal x */
  68. for (i = 0; (ix & 0x00800000) == 0; i++)
  69. ix <<= 1;
  70. m -= i - 1;
  71. }
  72. m -= 127; /* unbias exponent */
  73. ix = (ix & 0x007fffff) | 0x00800000;
  74. if (m & 1) /* odd m, double x to make it even */
  75. ix += ix;
  76. m >>= 1; /* m = [m/2] */
  77. /* generate sqrt(x) bit by bit */
  78. ix += ix;
  79. q = s = 0; /* q = sqrt(x) */
  80. r = 0x01000000; /* r = moving bit from right to left */
  81. while (r != 0) {
  82. t = s + r;
  83. if (t <= ix) {
  84. s = t + r;
  85. ix -= t;
  86. q += r;
  87. }
  88. ix += ix;
  89. r >>= 1;
  90. }
  91. if (ix != 0) {
  92. SETCX(IEEE754_INEXACT);
  93. switch (ieee754_csr.rm) {
  94. case IEEE754_RP:
  95. q += 2;
  96. break;
  97. case IEEE754_RN:
  98. q += (q & 1);
  99. break;
  100. }
  101. }
  102. ix = (q >> 1) + 0x3f000000;
  103. ix += (m << 23);
  104. x.bits = ix;
  105. return x;
  106. }