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 | * |
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 "ieee754dp.h" |
| 23 | |
Ralf Baechle | 2209bcb | 2014-04-16 01:31:11 +0200 | [diff] [blame] | 24 | union ieee754dp ieee754dp_div(union ieee754dp x, union ieee754dp y) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | { |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame^] | 26 | u64 rm; |
| 27 | int re; |
| 28 | u64 bm; |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | COMPXDP; |
| 31 | COMPYDP; |
| 32 | |
| 33 | EXPLODEXDP; |
| 34 | EXPLODEYDP; |
| 35 | |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 36 | ieee754_clearcx(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | FLUSHXDP; |
| 39 | FLUSHYDP; |
| 40 | |
| 41 | switch (CLPAIR(xc, yc)) { |
| 42 | case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN): |
| 43 | case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN): |
| 44 | case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN): |
| 45 | case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN): |
| 46 | case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN): |
| 47 | case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN): |
| 48 | case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN): |
| 49 | case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO): |
| 50 | case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM): |
| 51 | case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM): |
| 52 | case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF): |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 53 | ieee754_setcx(IEEE754_INVALID_OPERATION); |
Ralf Baechle | 90efba3 | 2014-04-25 03:19:57 +0200 | [diff] [blame] | 54 | return ieee754dp_nanxcpt(ieee754dp_indef()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN): |
| 57 | case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN): |
| 58 | case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN): |
| 59 | case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN): |
| 60 | return y; |
| 61 | |
| 62 | case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN): |
| 63 | case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO): |
| 64 | case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM): |
| 65 | case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM): |
| 66 | case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF): |
| 67 | return x; |
| 68 | |
| 69 | |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame^] | 70 | /* |
| 71 | * Infinity handling |
| 72 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF): |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 74 | ieee754_setcx(IEEE754_INVALID_OPERATION); |
Ralf Baechle | 90efba3 | 2014-04-25 03:19:57 +0200 | [diff] [blame] | 75 | return ieee754dp_indef(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF): |
| 78 | case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF): |
| 79 | case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF): |
| 80 | return ieee754dp_zero(xs ^ ys); |
| 81 | |
| 82 | case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO): |
| 83 | case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM): |
| 84 | case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM): |
| 85 | return ieee754dp_inf(xs ^ ys); |
| 86 | |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame^] | 87 | /* |
| 88 | * Zero handling |
| 89 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO): |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 91 | ieee754_setcx(IEEE754_INVALID_OPERATION); |
Ralf Baechle | 90efba3 | 2014-04-25 03:19:57 +0200 | [diff] [blame] | 92 | return ieee754dp_indef(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO): |
| 95 | case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO): |
Ralf Baechle | 9e8bad1 | 2014-04-19 00:36:32 +0200 | [diff] [blame] | 96 | ieee754_setcx(IEEE754_ZERO_DIVIDE); |
Ralf Baechle | 90efba3 | 2014-04-25 03:19:57 +0200 | [diff] [blame] | 97 | return ieee754dp_inf(xs ^ ys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM): |
| 100 | case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM): |
| 101 | return ieee754dp_zero(xs == ys ? 0 : 1); |
| 102 | |
| 103 | case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM): |
| 104 | DPDNORMX; |
| 105 | |
| 106 | case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM): |
| 107 | DPDNORMY; |
| 108 | break; |
| 109 | |
| 110 | case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM): |
| 111 | DPDNORMX; |
| 112 | break; |
| 113 | |
| 114 | case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM): |
| 115 | break; |
| 116 | } |
| 117 | assert(xm & DP_HIDDEN_BIT); |
| 118 | assert(ym & DP_HIDDEN_BIT); |
| 119 | |
| 120 | /* provide rounding space */ |
| 121 | xm <<= 3; |
| 122 | ym <<= 3; |
| 123 | |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame^] | 124 | /* now the dirty work */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame^] | 126 | rm = 0; |
| 127 | re = xe - ye; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame^] | 129 | for (bm = DP_MBIT(DP_FBITS + 2); bm; bm >>= 1) { |
| 130 | if (xm >= ym) { |
| 131 | xm -= ym; |
| 132 | rm |= bm; |
| 133 | if (xm == 0) |
| 134 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame^] | 136 | xm <<= 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
Ralf Baechle | 3f7cac4 | 2014-04-26 01:49:14 +0200 | [diff] [blame^] | 138 | |
| 139 | rm <<= 1; |
| 140 | if (xm) |
| 141 | rm |= 1; /* have remainder, set sticky */ |
| 142 | |
| 143 | assert(rm); |
| 144 | |
| 145 | /* |
| 146 | * Normalise rm to rounding precision ? |
| 147 | */ |
| 148 | while ((rm >> (DP_FBITS + 3)) == 0) { |
| 149 | rm <<= 1; |
| 150 | re--; |
| 151 | } |
| 152 | |
| 153 | return ieee754dp_format(xs == ys ? 0 : 1, re, rm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |