1 /* IEEE754 floating point arithmetic 2 * single precision 3 */ 4 /* 5 * MIPS floating point support 6 * Copyright (C) 1994-2000 Algorithmics Ltd. 7 * 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., 19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 */ 21 22 #include "ieee754sp.h" 23 24 union ieee754sp ieee754sp_mul(union ieee754sp x, union ieee754sp y) 25 { 26 int re; 27 int rs; 28 unsigned rm; 29 unsigned short lxm; 30 unsigned short hxm; 31 unsigned short lym; 32 unsigned short hym; 33 unsigned lrm; 34 unsigned hrm; 35 unsigned t; 36 unsigned at; 37 38 COMPXSP; 39 COMPYSP; 40 41 EXPLODEXSP; 42 EXPLODEYSP; 43 44 ieee754_clearcx(); 45 46 FLUSHXSP; 47 FLUSHYSP; 48 49 switch (CLPAIR(xc, yc)) { 50 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_QNAN): 51 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_SNAN): 52 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_SNAN): 53 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_SNAN): 54 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_SNAN): 55 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_SNAN): 56 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_SNAN): 57 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_ZERO): 58 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_NORM): 59 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_DNORM): 60 case CLPAIR(IEEE754_CLASS_SNAN, IEEE754_CLASS_INF): 61 ieee754_setcx(IEEE754_INVALID_OPERATION); 62 return ieee754sp_nanxcpt(ieee754sp_indef()); 63 64 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_QNAN): 65 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_QNAN): 66 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_QNAN): 67 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_QNAN): 68 return y; 69 70 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_QNAN): 71 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_ZERO): 72 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_NORM): 73 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_DNORM): 74 case CLPAIR(IEEE754_CLASS_QNAN, IEEE754_CLASS_INF): 75 return x; 76 77 78 /* 79 * Infinity handling 80 */ 81 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_ZERO): 82 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_INF): 83 ieee754_setcx(IEEE754_INVALID_OPERATION); 84 return ieee754sp_indef(); 85 86 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_INF): 87 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_INF): 88 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_NORM): 89 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_DNORM): 90 case CLPAIR(IEEE754_CLASS_INF, IEEE754_CLASS_INF): 91 return ieee754sp_inf(xs ^ ys); 92 93 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_ZERO): 94 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_NORM): 95 case CLPAIR(IEEE754_CLASS_ZERO, IEEE754_CLASS_DNORM): 96 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_ZERO): 97 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_ZERO): 98 return ieee754sp_zero(xs ^ ys); 99 100 101 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_DNORM): 102 SPDNORMX; 103 104 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_DNORM): 105 SPDNORMY; 106 break; 107 108 case CLPAIR(IEEE754_CLASS_DNORM, IEEE754_CLASS_NORM): 109 SPDNORMX; 110 break; 111 112 case CLPAIR(IEEE754_CLASS_NORM, IEEE754_CLASS_NORM): 113 break; 114 } 115 /* rm = xm * ym, re = xe+ye basically */ 116 assert(xm & SP_HIDDEN_BIT); 117 assert(ym & SP_HIDDEN_BIT); 118 119 re = xe + ye; 120 rs = xs ^ ys; 121 122 /* shunt to top of word */ 123 xm <<= 32 - (SP_FBITS + 1); 124 ym <<= 32 - (SP_FBITS + 1); 125 126 /* 127 * Multiply 32 bits xm, ym to give high 32 bits rm with stickness. 128 */ 129 lxm = xm & 0xffff; 130 hxm = xm >> 16; 131 lym = ym & 0xffff; 132 hym = ym >> 16; 133 134 lrm = lxm * lym; /* 16 * 16 => 32 */ 135 hrm = hxm * hym; /* 16 * 16 => 32 */ 136 137 t = lxm * hym; /* 16 * 16 => 32 */ 138 at = lrm + (t << 16); 139 hrm += at < lrm; 140 lrm = at; 141 hrm = hrm + (t >> 16); 142 143 t = hxm * lym; /* 16 * 16 => 32 */ 144 at = lrm + (t << 16); 145 hrm += at < lrm; 146 lrm = at; 147 hrm = hrm + (t >> 16); 148 149 rm = hrm | (lrm != 0); 150 151 /* 152 * Sticky shift down to normal rounding precision. 153 */ 154 if ((int) rm < 0) { 155 rm = (rm >> (32 - (SP_FBITS + 1 + 3))) | 156 ((rm << (SP_FBITS + 1 + 3)) != 0); 157 re++; 158 } else { 159 rm = (rm >> (32 - (SP_FBITS + 1 + 3 + 1))) | 160 ((rm << (SP_FBITS + 1 + 3 + 1)) != 0); 161 } 162 assert(rm & (SP_HIDDEN_BIT << 3)); 163 164 return ieee754sp_format(rs, re, rm); 165 } 166