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