blob: e9bb60121d0378a3134ec4f2df6c233619c3e8a1 [file] [log] [blame]
Thomas Gleixner9d5a6342019-05-31 01:09:55 -07001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* IEEE754 floating point arithmetic
3 * single precision square root
4 */
5/*
6 * MIPS floating point support
7 * Copyright (C) 1994-2000 Algorithmics Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "ieee754sp.h"
11
Ralf Baechle2209bcb2014-04-16 01:31:11 +020012union ieee754sp ieee754sp_sqrt(union ieee754sp x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013{
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 Baechle9e8bad12014-04-19 00:36:32 +020021 ieee754_clearcx();
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 FLUSHXSP;
23
24 /* x == INF or NAN? */
25 switch (xc) {
Maciej W. Rozyckid5afa7e2015-04-03 23:25:34 +010026 case IEEE754_CLASS_SNAN:
27 return ieee754sp_nanxcpt(x);
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 case IEEE754_CLASS_QNAN:
30 /* sqrt(Nan) = Nan */
Maciej W. Rozycki539bfb52015-04-03 23:25:30 +010031 return x;
Ralf Baechle3f7cac42014-04-26 01:49:14 +020032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 case IEEE754_CLASS_ZERO:
34 /* sqrt(0) = 0 */
35 return x;
Ralf Baechle3f7cac42014-04-26 01:49:14 +020036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 case IEEE754_CLASS_INF:
38 if (xs) {
39 /* sqrt(-Inf) = Nan */
Ralf Baechle9e8bad12014-04-19 00:36:32 +020040 ieee754_setcx(IEEE754_INVALID_OPERATION);
Maciej W. Rozycki539bfb52015-04-03 23:25:30 +010041 return ieee754sp_indef();
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 }
43 /* sqrt(+Inf) = Inf */
44 return x;
Ralf Baechle3f7cac42014-04-26 01:49:14 +020045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 case IEEE754_CLASS_DNORM:
47 case IEEE754_CLASS_NORM:
48 if (xs) {
49 /* sqrt(-x) = Nan */
Ralf Baechle9e8bad12014-04-19 00:36:32 +020050 ieee754_setcx(IEEE754_INVALID_OPERATION);
Maciej W. Rozycki539bfb52015-04-03 23:25:30 +010051 return ieee754sp_indef();
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 }
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 Markovic61100502017-11-02 12:14:04 +010073 s = 0;
74 q = 0; /* q = sqrt(x) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 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 Baechle9e8bad12014-04-19 00:36:32 +020089 ieee754_setcx(IEEE754_INEXACT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 switch (ieee754_csr.rm) {
Ralf Baechle56a64732014-04-30 11:21:55 +020091 case FPU_CSR_RU:
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 q += 2;
93 break;
Ralf Baechle56a64732014-04-30 11:21:55 +020094 case FPU_CSR_RN:
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 q += (q & 1);
96 break;
97 }
98 }
99 ix = (q >> 1) + 0x3f000000;
100 ix += (m << 23);
101 x.bits = ix;
102 return x;
103}