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 |
| 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_add(union ieee754sp x, union ieee754sp y) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | { |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 14 | int s; |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | COMPXSP; |
| 17 | COMPYSP; |
| 18 | |
| 19 | EXPLODEXSP; |
| 20 | EXPLODEYSP; |
| 21 | |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 22 | ieee754_clearcx(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | FLUSHXSP; |
| 25 | FLUSHYSP; |
| 26 | |
| 27 | switch (CLPAIR(xc, yc)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN): |
| 30 | case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN): |
| 31 | case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN): |
| 32 | case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN): |
Maciej W. Rozycki | d5afa7e | 2015-04-03 23:25:34 +0100 | [diff] [blame] | 33 | return ieee754sp_nanxcpt(y); |
| 34 | |
| 35 | case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN): |
| 36 | case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO): |
| 38 | case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM): |
| 39 | case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM): |
| 40 | case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF): |
Maciej W. Rozycki | d5afa7e | 2015-04-03 23:25:34 +0100 | [diff] [blame] | 41 | return ieee754sp_nanxcpt(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN): |
| 44 | case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN): |
| 45 | case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN): |
| 46 | case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN): |
| 47 | return y; |
| 48 | |
| 49 | case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN): |
| 50 | case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO): |
| 51 | case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM): |
| 52 | case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM): |
| 53 | case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF): |
| 54 | return x; |
| 55 | |
| 56 | |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 57 | /* |
| 58 | * Infinity handling |
| 59 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF): |
| 61 | if (xs == ys) |
| 62 | return x; |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 63 | ieee754_setcx(IEEE754_INVALID_OPERATION); |
Ralf Baechle | 90efba3 | 2014-04-25 03:19:57 +0200 | [diff] [blame] | 64 | return ieee754sp_indef(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF): |
| 67 | case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF): |
| 68 | case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF): |
| 69 | return y; |
| 70 | |
| 71 | case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO): |
| 72 | case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM): |
| 73 | case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM): |
| 74 | return x; |
| 75 | |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 76 | /* |
| 77 | * Zero handling |
| 78 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO): |
| 80 | if (xs == ys) |
| 81 | return x; |
| 82 | else |
Ralf Baechle | 56a6473 | 2014-04-30 11:21:55 +0200 | [diff] [blame] | 83 | return ieee754sp_zero(ieee754_csr.rm == FPU_CSR_RD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO): |
| 86 | case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO): |
| 87 | return x; |
| 88 | |
| 89 | case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM): |
| 90 | case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM): |
| 91 | return y; |
| 92 | |
| 93 | case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM): |
| 94 | SPDNORMX; |
Liangliang Huang | c9b0299 | 2020-05-04 16:51:29 +0800 | [diff] [blame] | 95 | fallthrough; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM): |
| 97 | SPDNORMY; |
| 98 | break; |
| 99 | |
| 100 | case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM): |
| 101 | SPDNORMX; |
| 102 | break; |
| 103 | |
| 104 | case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM): |
| 105 | break; |
| 106 | } |
| 107 | assert(xm & SP_HIDDEN_BIT); |
| 108 | assert(ym & SP_HIDDEN_BIT); |
| 109 | |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 110 | /* |
| 111 | * Provide guard, round and stick bit space. |
| 112 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | xm <<= 3; |
| 114 | ym <<= 3; |
| 115 | |
| 116 | if (xe > ye) { |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 117 | /* |
| 118 | * Have to shift y fraction right to align. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | */ |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 120 | s = xe - ye; |
Paul Burton | db57f29 | 2016-04-21 14:04:54 +0100 | [diff] [blame] | 121 | ym = XSPSRS(ym, s); |
| 122 | ye += s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } else if (ye > xe) { |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 124 | /* |
| 125 | * Have to shift x fraction right to align. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | */ |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 127 | s = ye - xe; |
Paul Burton | db57f29 | 2016-04-21 14:04:54 +0100 | [diff] [blame] | 128 | xm = XSPSRS(xm, s); |
| 129 | xe += s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | assert(xe == ye); |
| 132 | assert(xe <= SP_EMAX); |
| 133 | |
| 134 | if (xs == ys) { |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 135 | /* |
| 136 | * Generate 28 bit result of adding two 27 bit numbers |
| 137 | * leaving result in xm, xs and xe. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | */ |
| 139 | xm = xm + ym; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Ralf Baechle | ad8fb553 | 2014-04-22 15:51:55 +0200 | [diff] [blame] | 141 | if (xm >> (SP_FBITS + 1 + 3)) { /* carry out */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | SPXSRSX1(); |
| 143 | } |
| 144 | } else { |
| 145 | if (xm >= ym) { |
| 146 | xm = xm - ym; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } else { |
| 148 | xm = ym - xm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | xs = ys; |
| 150 | } |
| 151 | if (xm == 0) |
Ralf Baechle | 56a6473 | 2014-04-30 11:21:55 +0200 | [diff] [blame] | 152 | return ieee754sp_zero(ieee754_csr.rm == FPU_CSR_RD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame] | 154 | /* |
| 155 | * Normalize in extended single precision |
| 156 | */ |
Ralf Baechle | ad8fb553 | 2014-04-22 15:51:55 +0200 | [diff] [blame] | 157 | while ((xm >> (SP_FBITS + 3)) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | xm <<= 1; |
| 159 | xe--; |
| 160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
Ralf Baechle | 90efba3 | 2014-04-25 03:19:57 +0200 | [diff] [blame] | 162 | |
| 163 | return ieee754sp_format(xs, xe, xm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |