Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* IEEE754 floating point arithmetic |
| 2 | * double precision: common utilities |
| 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 | * |
| 8 | * ######################################################################## |
| 9 | * |
| 10 | * This program is free software; you can distribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License (Version 2) as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 17 | * for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 22 | * |
| 23 | * ######################################################################## |
| 24 | */ |
| 25 | |
Ralf Baechle | 1eaf198 | 2014-04-20 01:20:08 +0200 | [diff] [blame] | 26 | #include <stdarg.h> |
Ralf Baechle | cae5506 | 2014-04-16 00:47:59 +0200 | [diff] [blame] | 27 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include "ieee754dp.h" |
| 30 | |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 31 | int ieee754dp_class(union ieee754dp x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
| 33 | COMPXDP; |
| 34 | EXPLODEXDP; |
| 35 | return xc; |
| 36 | } |
| 37 | |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 38 | int ieee754dp_isnan(union ieee754dp x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
| 40 | return ieee754dp_class(x) >= IEEE754_CLASS_SNAN; |
| 41 | } |
| 42 | |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 43 | int ieee754dp_issnan(union ieee754dp x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
| 45 | assert(ieee754dp_isnan(x)); |
Ralf Baechle | ad8fb553 | 2014-04-22 15:51:55 +0200 | [diff] [blame^] | 46 | return ((DPMANT(x) & DP_MBIT(DP_FBITS-1)) == DP_MBIT(DP_FBITS-1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 50 | union ieee754dp __cold ieee754dp_xcpt(union ieee754dp r, const char *op, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
| 52 | struct ieee754xctx ax; |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 53 | if (!ieee754_tstx()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | return r; |
| 55 | |
| 56 | ax.op = op; |
| 57 | ax.rt = IEEE754_RT_DP; |
| 58 | ax.rv.dp = r; |
| 59 | va_start(ax.ap, op); |
| 60 | ieee754_xcpt(&ax); |
Richard Knutsson | 8142294 | 2007-11-24 22:22:19 +0100 | [diff] [blame] | 61 | va_end(ax.ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | return ax.rv.dp; |
| 63 | } |
| 64 | |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 65 | union ieee754dp __cold ieee754dp_nanxcpt(union ieee754dp r, const char *op, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
| 67 | struct ieee754xctx ax; |
| 68 | |
| 69 | assert(ieee754dp_isnan(r)); |
| 70 | |
| 71 | if (!ieee754dp_issnan(r)) /* QNAN does not cause invalid op !! */ |
| 72 | return r; |
| 73 | |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 74 | if (!ieee754_setandtestcx(IEEE754_INVALID_OPERATION)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* not enabled convert to a quiet NaN */ |
Ralf Baechle | ad8fb553 | 2014-04-22 15:51:55 +0200 | [diff] [blame^] | 76 | DPMANT(r) &= (~DP_MBIT(DP_FBITS-1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | if (ieee754dp_isnan(r)) |
| 78 | return r; |
| 79 | else |
| 80 | return ieee754dp_indef(); |
| 81 | } |
| 82 | |
| 83 | ax.op = op; |
| 84 | ax.rt = 0; |
| 85 | ax.rv.dp = r; |
| 86 | va_start(ax.ap, op); |
| 87 | ieee754_xcpt(&ax); |
Richard Knutsson | 8142294 | 2007-11-24 22:22:19 +0100 | [diff] [blame] | 88 | va_end(ax.ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | return ax.rv.dp; |
| 90 | } |
| 91 | |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 92 | union ieee754dp ieee754dp_bestnan(union ieee754dp x, union ieee754dp y) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
| 94 | assert(ieee754dp_isnan(x)); |
| 95 | assert(ieee754dp_isnan(y)); |
| 96 | |
| 97 | if (DPMANT(x) > DPMANT(y)) |
| 98 | return x; |
| 99 | else |
| 100 | return y; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | static u64 get_rounding(int sn, u64 xm) |
| 105 | { |
| 106 | /* inexact must round of 3 bits |
| 107 | */ |
| 108 | if (xm & (DP_MBIT(3) - 1)) { |
| 109 | switch (ieee754_csr.rm) { |
| 110 | case IEEE754_RZ: |
| 111 | break; |
| 112 | case IEEE754_RN: |
| 113 | xm += 0x3 + ((xm >> 3) & 1); |
| 114 | /* xm += (xm&0x8)?0x4:0x3 */ |
| 115 | break; |
| 116 | case IEEE754_RU: /* toward +Infinity */ |
| 117 | if (!sn) /* ?? */ |
| 118 | xm += 0x8; |
| 119 | break; |
| 120 | case IEEE754_RD: /* toward -Infinity */ |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 121 | if (sn) /* ?? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | xm += 0x8; |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | return xm; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /* generate a normal/denormal number with over,under handling |
| 131 | * sn is sign |
| 132 | * xe is an unbiased exponent |
| 133 | * xm is 3bit extended precision value. |
| 134 | */ |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 135 | union ieee754dp ieee754dp_format(int sn, int xe, u64 xm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
| 137 | assert(xm); /* we don't gen exact zeros (probably should) */ |
| 138 | |
Ralf Baechle | ad8fb553 | 2014-04-22 15:51:55 +0200 | [diff] [blame^] | 139 | assert((xm >> (DP_FBITS + 1 + 3)) == 0); /* no execess */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | assert(xm & (DP_HIDDEN_BIT << 3)); |
| 141 | |
| 142 | if (xe < DP_EMIN) { |
| 143 | /* strip lower bits */ |
| 144 | int es = DP_EMIN - xe; |
| 145 | |
| 146 | if (ieee754_csr.nod) { |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 147 | ieee754_setcx(IEEE754_UNDERFLOW); |
| 148 | ieee754_setcx(IEEE754_INEXACT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | switch(ieee754_csr.rm) { |
| 151 | case IEEE754_RN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | case IEEE754_RZ: |
| 153 | return ieee754dp_zero(sn); |
| 154 | case IEEE754_RU: /* toward +Infinity */ |
Ralf Baechle | 47fa0c0 | 2014-04-16 11:00:12 +0200 | [diff] [blame] | 155 | if (sn == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | return ieee754dp_min(0); |
| 157 | else |
| 158 | return ieee754dp_zero(1); |
| 159 | case IEEE754_RD: /* toward -Infinity */ |
Ralf Baechle | 47fa0c0 | 2014-04-16 11:00:12 +0200 | [diff] [blame] | 160 | if (sn == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return ieee754dp_zero(0); |
| 162 | else |
| 163 | return ieee754dp_min(1); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (xe == DP_EMIN - 1 |
Ralf Baechle | ad8fb553 | 2014-04-22 15:51:55 +0200 | [diff] [blame^] | 168 | && get_rounding(sn, xm) >> (DP_FBITS + 1 + 3)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
| 170 | /* Not tiny after rounding */ |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 171 | ieee754_setcx(IEEE754_INEXACT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | xm = get_rounding(sn, xm); |
| 173 | xm >>= 1; |
| 174 | /* Clear grs bits */ |
| 175 | xm &= ~(DP_MBIT(3) - 1); |
| 176 | xe++; |
| 177 | } |
| 178 | else { |
| 179 | /* sticky right shift es bits |
| 180 | */ |
| 181 | xm = XDPSRS(xm, es); |
| 182 | xe += es; |
| 183 | assert((xm & (DP_HIDDEN_BIT << 3)) == 0); |
| 184 | assert(xe == DP_EMIN); |
| 185 | } |
| 186 | } |
| 187 | if (xm & (DP_MBIT(3) - 1)) { |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 188 | ieee754_setcx(IEEE754_INEXACT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | if ((xm & (DP_HIDDEN_BIT << 3)) == 0) { |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 190 | ieee754_setcx(IEEE754_UNDERFLOW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* inexact must round of 3 bits |
| 194 | */ |
| 195 | xm = get_rounding(sn, xm); |
| 196 | /* adjust exponent for rounding add overflowing |
| 197 | */ |
Ralf Baechle | ad8fb553 | 2014-04-22 15:51:55 +0200 | [diff] [blame^] | 198 | if (xm >> (DP_FBITS + 3 + 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | /* add causes mantissa overflow */ |
| 200 | xm >>= 1; |
| 201 | xe++; |
| 202 | } |
| 203 | } |
| 204 | /* strip grs bits */ |
| 205 | xm >>= 3; |
| 206 | |
Ralf Baechle | ad8fb553 | 2014-04-22 15:51:55 +0200 | [diff] [blame^] | 207 | assert((xm >> (DP_FBITS + 1)) == 0); /* no execess */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | assert(xe >= DP_EMIN); |
| 209 | |
| 210 | if (xe > DP_EMAX) { |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 211 | ieee754_setcx(IEEE754_OVERFLOW); |
| 212 | ieee754_setcx(IEEE754_INEXACT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | /* -O can be table indexed by (rm,sn) */ |
| 214 | switch (ieee754_csr.rm) { |
| 215 | case IEEE754_RN: |
| 216 | return ieee754dp_inf(sn); |
| 217 | case IEEE754_RZ: |
| 218 | return ieee754dp_max(sn); |
| 219 | case IEEE754_RU: /* toward +Infinity */ |
| 220 | if (sn == 0) |
| 221 | return ieee754dp_inf(0); |
| 222 | else |
| 223 | return ieee754dp_max(1); |
| 224 | case IEEE754_RD: /* toward -Infinity */ |
| 225 | if (sn == 0) |
| 226 | return ieee754dp_max(0); |
| 227 | else |
| 228 | return ieee754dp_inf(1); |
| 229 | } |
| 230 | } |
| 231 | /* gen norm/denorm/zero */ |
| 232 | |
| 233 | if ((xm & DP_HIDDEN_BIT) == 0) { |
| 234 | /* we underflow (tiny/zero) */ |
| 235 | assert(xe == DP_EMIN); |
| 236 | if (ieee754_csr.mx & IEEE754_UNDERFLOW) |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 237 | ieee754_setcx(IEEE754_UNDERFLOW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | return builddp(sn, DP_EMIN - 1 + DP_EBIAS, xm); |
| 239 | } else { |
Ralf Baechle | ad8fb553 | 2014-04-22 15:51:55 +0200 | [diff] [blame^] | 240 | assert((xm >> (DP_FBITS + 1)) == 0); /* no execess */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | assert(xm & DP_HIDDEN_BIT); |
| 242 | |
| 243 | return builddp(sn, xe + DP_EBIAS, xm & ~DP_HIDDEN_BIT); |
| 244 | } |
| 245 | } |