Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* IEEE754 floating point arithmetic |
| 2 | * single precision square root |
| 3 | */ |
| 4 | /* |
| 5 | * MIPS floating point support |
| 6 | * Copyright (C) 1994-2000 Algorithmics Ltd. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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., |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 19 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "ieee754sp.h" |
| 23 | |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 24 | union ieee754sp ieee754sp_sqrt(union ieee754sp x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | { |
| 26 | int ix, s, q, m, t, i; |
| 27 | unsigned int r; |
| 28 | COMPXSP; |
| 29 | |
| 30 | /* take care of Inf and NaN */ |
| 31 | |
| 32 | EXPLODEXSP; |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 33 | ieee754_clearcx(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | FLUSHXSP; |
| 35 | |
| 36 | /* x == INF or NAN? */ |
| 37 | switch (xc) { |
Maciej W. Rozycki | d5afa7e | 2015-04-03 23:25:34 +0100 | [diff] [blame] | 38 | case IEEE754_CLASS_SNAN: |
| 39 | return ieee754sp_nanxcpt(x); |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | case IEEE754_CLASS_QNAN: |
| 42 | /* sqrt(Nan) = Nan */ |
Maciej W. Rozycki | 539bfb5 | 2015-04-03 23:25:30 +0100 | [diff] [blame] | 43 | return x; |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | case IEEE754_CLASS_ZERO: |
| 46 | /* sqrt(0) = 0 */ |
| 47 | return x; |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | case IEEE754_CLASS_INF: |
| 50 | if (xs) { |
| 51 | /* sqrt(-Inf) = Nan */ |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 52 | ieee754_setcx(IEEE754_INVALID_OPERATION); |
Maciej W. Rozycki | 539bfb5 | 2015-04-03 23:25:30 +0100 | [diff] [blame] | 53 | return ieee754sp_indef(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | /* sqrt(+Inf) = Inf */ |
| 56 | return x; |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | case IEEE754_CLASS_DNORM: |
| 59 | case IEEE754_CLASS_NORM: |
| 60 | if (xs) { |
| 61 | /* sqrt(-x) = Nan */ |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 62 | ieee754_setcx(IEEE754_INVALID_OPERATION); |
Maciej W. Rozycki | 539bfb5 | 2015-04-03 23:25:30 +0100 | [diff] [blame] | 63 | return ieee754sp_indef(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | ix = x.bits; |
| 69 | |
| 70 | /* normalize x */ |
| 71 | m = (ix >> 23); |
| 72 | if (m == 0) { /* subnormal x */ |
| 73 | for (i = 0; (ix & 0x00800000) == 0; i++) |
| 74 | ix <<= 1; |
| 75 | m -= i - 1; |
| 76 | } |
| 77 | m -= 127; /* unbias exponent */ |
| 78 | ix = (ix & 0x007fffff) | 0x00800000; |
| 79 | if (m & 1) /* odd m, double x to make it even */ |
| 80 | ix += ix; |
| 81 | m >>= 1; /* m = [m/2] */ |
| 82 | |
| 83 | /* generate sqrt(x) bit by bit */ |
| 84 | ix += ix; |
| 85 | q = s = 0; /* q = sqrt(x) */ |
| 86 | r = 0x01000000; /* r = moving bit from right to left */ |
| 87 | |
| 88 | while (r != 0) { |
| 89 | t = s + r; |
| 90 | if (t <= ix) { |
| 91 | s = t + r; |
| 92 | ix -= t; |
| 93 | q += r; |
| 94 | } |
| 95 | ix += ix; |
| 96 | r >>= 1; |
| 97 | } |
| 98 | |
| 99 | if (ix != 0) { |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 100 | ieee754_setcx(IEEE754_INEXACT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | switch (ieee754_csr.rm) { |
Ralf Baechle | 56a6473 | 2014-04-30 11:21:55 +0200 | [diff] [blame] | 102 | case FPU_CSR_RU: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | q += 2; |
| 104 | break; |
Ralf Baechle | 56a6473 | 2014-04-30 11:21:55 +0200 | [diff] [blame] | 105 | case FPU_CSR_RN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | q += (q & 1); |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | ix = (q >> 1) + 0x3f000000; |
| 111 | ix += (m << 23); |
| 112 | x.bits = ix; |
| 113 | return x; |
| 114 | } |