1 /* Software floating-point emulation. Common o 1 /* Software floating-point emulation. Common operations. 2 Copyright (C) 1997,1998,1999 Free Software 2 Copyright (C) 1997,1998,1999 Free Software Foundation, Inc. 3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library. 4 Contributed by Richard Henderson (rth@cygnu 4 Contributed by Richard Henderson (rth@cygnus.com), 5 Jakub Jelinek (jj@ultra.linu 5 Jakub Jelinek (jj@ultra.linux.cz), 6 David S. Miller (davem@redha 6 David S. Miller (davem@redhat.com) and 7 Peter Maydell (pmaydell@chia 7 Peter Maydell (pmaydell@chiark.greenend.org.uk). 8 8 9 The GNU C Library is free software; you can 9 The GNU C Library is free software; you can redistribute it and/or 10 modify it under the terms of the GNU Librar 10 modify it under the terms of the GNU Library General Public License as 11 published by the Free Software Foundation; 11 published by the Free Software Foundation; either version 2 of the 12 License, or (at your option) any later vers 12 License, or (at your option) any later version. 13 13 14 The GNU C Library is distributed in the hop 14 The GNU C Library is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 Library General Public License for more det 17 Library General Public License for more details. 18 18 19 You should have received a copy of the GNU 19 You should have received a copy of the GNU Library General Public 20 License along with the GNU C Library; see t 20 License along with the GNU C Library; see the file COPYING.LIB. If 21 not, write to the Free Software Foundation, 21 not, write to the Free Software Foundation, Inc., 22 59 Temple Place - Suite 330, Boston, MA 021 22 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 23 23 24 #ifndef __MATH_EMU_OP_COMMON_H__ 24 #ifndef __MATH_EMU_OP_COMMON_H__ 25 #define __MATH_EMU_OP_COMMON_H__ 25 #define __MATH_EMU_OP_COMMON_H__ 26 26 27 #define _FP_DECL(wc, X) \ 27 #define _FP_DECL(wc, X) \ 28 _FP_I_TYPE X##_c=0, X##_s=0, X##_e=0; \ 28 _FP_I_TYPE X##_c=0, X##_s=0, X##_e=0; \ 29 _FP_FRAC_DECL_##wc(X) 29 _FP_FRAC_DECL_##wc(X) 30 30 31 /* 31 /* 32 * Finish truly unpacking a native fp value by !! 32 * Finish truely unpacking a native fp value by classifying the kind 33 * of fp value and normalizing both the expone 33 * of fp value and normalizing both the exponent and the fraction. 34 */ 34 */ 35 35 36 #define _FP_UNPACK_CANONICAL(fs, wc, X) 36 #define _FP_UNPACK_CANONICAL(fs, wc, X) \ 37 do { 37 do { \ 38 switch (X##_e) 38 switch (X##_e) \ 39 { 39 { \ 40 default: 40 default: \ 41 _FP_FRAC_HIGH_RAW_##fs(X) |= _FP_IMPLBIT_# 41 _FP_FRAC_HIGH_RAW_##fs(X) |= _FP_IMPLBIT_##fs; \ 42 _FP_FRAC_SLL_##wc(X, _FP_WORKBITS); 42 _FP_FRAC_SLL_##wc(X, _FP_WORKBITS); \ 43 X##_e -= _FP_EXPBIAS_##fs; 43 X##_e -= _FP_EXPBIAS_##fs; \ 44 X##_c = FP_CLS_NORMAL; 44 X##_c = FP_CLS_NORMAL; \ 45 break; 45 break; \ 46 46 \ 47 case 0: 47 case 0: \ 48 if (_FP_FRAC_ZEROP_##wc(X)) 48 if (_FP_FRAC_ZEROP_##wc(X)) \ 49 X##_c = FP_CLS_ZERO; 49 X##_c = FP_CLS_ZERO; \ 50 else 50 else \ 51 { 51 { \ 52 /* a denormalized number */ 52 /* a denormalized number */ \ 53 _FP_I_TYPE _shift; 53 _FP_I_TYPE _shift; \ 54 _FP_FRAC_CLZ_##wc(_shift, X); 54 _FP_FRAC_CLZ_##wc(_shift, X); \ 55 _shift -= _FP_FRACXBITS_##fs; 55 _shift -= _FP_FRACXBITS_##fs; \ 56 _FP_FRAC_SLL_##wc(X, (_shift+_FP_WORKB 56 _FP_FRAC_SLL_##wc(X, (_shift+_FP_WORKBITS)); \ 57 X##_e -= _FP_EXPBIAS_##fs - 1 + _shift 57 X##_e -= _FP_EXPBIAS_##fs - 1 + _shift; \ 58 X##_c = FP_CLS_NORMAL; 58 X##_c = FP_CLS_NORMAL; \ 59 FP_SET_EXCEPTION(FP_EX_DENORM); 59 FP_SET_EXCEPTION(FP_EX_DENORM); \ 60 if (FP_DENORM_ZERO) 60 if (FP_DENORM_ZERO) \ 61 { 61 { \ 62 FP_SET_EXCEPTION(FP_EX_INEXACT); 62 FP_SET_EXCEPTION(FP_EX_INEXACT); \ 63 X##_c = FP_CLS_ZERO; 63 X##_c = FP_CLS_ZERO; \ 64 } 64 } \ 65 } 65 } \ 66 break; 66 break; \ 67 67 \ 68 case _FP_EXPMAX_##fs: 68 case _FP_EXPMAX_##fs: \ 69 if (_FP_FRAC_ZEROP_##wc(X)) 69 if (_FP_FRAC_ZEROP_##wc(X)) \ 70 X##_c = FP_CLS_INF; 70 X##_c = FP_CLS_INF; \ 71 else 71 else \ 72 { 72 { \ 73 X##_c = FP_CLS_NAN; 73 X##_c = FP_CLS_NAN; \ 74 /* Check for signaling NaN */ 74 /* Check for signaling NaN */ \ 75 if (!(_FP_FRAC_HIGH_RAW_##fs(X) & _FP_ 75 if (!(_FP_FRAC_HIGH_RAW_##fs(X) & _FP_QNANBIT_##fs)) \ 76 FP_SET_EXCEPTION(FP_EX_INVALID | FP_ !! 76 FP_SET_EXCEPTION(FP_EX_INVALID); \ 77 } 77 } \ 78 break; 78 break; \ 79 } 79 } \ 80 } while (0) 80 } while (0) 81 81 82 /* 82 /* 83 * Before packing the bits back into the nativ 83 * Before packing the bits back into the native fp result, take care 84 * of such mundane things as rounding and over 84 * of such mundane things as rounding and overflow. Also, for some 85 * kinds of fp values, the original parts may 85 * kinds of fp values, the original parts may not have been fully 86 * extracted -- but that is ok, we can regener 86 * extracted -- but that is ok, we can regenerate them now. 87 */ 87 */ 88 88 89 #define _FP_PACK_CANONICAL(fs, wc, X) 89 #define _FP_PACK_CANONICAL(fs, wc, X) \ 90 do { 90 do { \ 91 switch (X##_c) 91 switch (X##_c) \ 92 { 92 { \ 93 case FP_CLS_NORMAL: 93 case FP_CLS_NORMAL: \ 94 X##_e += _FP_EXPBIAS_##fs; 94 X##_e += _FP_EXPBIAS_##fs; \ 95 if (X##_e > 0) 95 if (X##_e > 0) \ 96 { 96 { \ 97 _FP_ROUND(wc, X); 97 _FP_ROUND(wc, X); \ 98 if (_FP_FRAC_OVERP_##wc(fs, X)) 98 if (_FP_FRAC_OVERP_##wc(fs, X)) \ 99 { 99 { \ 100 _FP_FRAC_CLEAR_OVERP_##wc(fs, X); 100 _FP_FRAC_CLEAR_OVERP_##wc(fs, X); \ 101 X##_e++; 101 X##_e++; \ 102 } 102 } \ 103 _FP_FRAC_SRL_##wc(X, _FP_WORKBITS); 103 _FP_FRAC_SRL_##wc(X, _FP_WORKBITS); \ 104 if (X##_e >= _FP_EXPMAX_##fs) 104 if (X##_e >= _FP_EXPMAX_##fs) \ 105 { 105 { \ 106 /* overflow */ 106 /* overflow */ \ 107 switch (FP_ROUNDMODE) 107 switch (FP_ROUNDMODE) \ 108 { 108 { \ 109 case FP_RND_NEAREST: 109 case FP_RND_NEAREST: \ 110 X##_c = FP_CLS_INF; 110 X##_c = FP_CLS_INF; \ 111 break; 111 break; \ 112 case FP_RND_PINF: 112 case FP_RND_PINF: \ 113 if (!X##_s) X##_c = FP_CLS_INF 113 if (!X##_s) X##_c = FP_CLS_INF; \ 114 break; 114 break; \ 115 case FP_RND_MINF: 115 case FP_RND_MINF: \ 116 if (X##_s) X##_c = FP_CLS_INF; 116 if (X##_s) X##_c = FP_CLS_INF; \ 117 break; 117 break; \ 118 } 118 } \ 119 if (X##_c == FP_CLS_INF) 119 if (X##_c == FP_CLS_INF) \ 120 { 120 { \ 121 /* Overflow to infinity */ 121 /* Overflow to infinity */ \ 122 X##_e = _FP_EXPMAX_##fs; 122 X##_e = _FP_EXPMAX_##fs; \ 123 _FP_FRAC_SET_##wc(X, _FP_ZEROF 123 _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \ 124 } 124 } \ 125 else 125 else \ 126 { 126 { \ 127 /* Overflow to maximum normal 127 /* Overflow to maximum normal */ \ 128 X##_e = _FP_EXPMAX_##fs - 1; 128 X##_e = _FP_EXPMAX_##fs - 1; \ 129 _FP_FRAC_SET_##wc(X, _FP_MAXFR 129 _FP_FRAC_SET_##wc(X, _FP_MAXFRAC_##wc); \ 130 } 130 } \ 131 FP_SET_EXCEPTION(FP_EX_OVERFLOW); 131 FP_SET_EXCEPTION(FP_EX_OVERFLOW); \ 132 FP_SET_EXCEPTION(FP_EX_INEXACT); 132 FP_SET_EXCEPTION(FP_EX_INEXACT); \ 133 } 133 } \ 134 } 134 } \ 135 else 135 else \ 136 { 136 { \ 137 /* we've got a denormalized number */ 137 /* we've got a denormalized number */ \ 138 X##_e = -X##_e + 1; 138 X##_e = -X##_e + 1; \ 139 if (X##_e <= _FP_WFRACBITS_##fs) 139 if (X##_e <= _FP_WFRACBITS_##fs) \ 140 { 140 { \ 141 _FP_FRAC_SRS_##wc(X, X##_e, _FP_WF 141 _FP_FRAC_SRS_##wc(X, X##_e, _FP_WFRACBITS_##fs); \ >> 142 _FP_ROUND(wc, X); \ 142 if (_FP_FRAC_HIGH_##fs(X) 143 if (_FP_FRAC_HIGH_##fs(X) \ 143 & (_FP_OVERFLOW_##fs >> 1)) 144 & (_FP_OVERFLOW_##fs >> 1)) \ 144 { 145 { \ 145 X##_e = 1; 146 X##_e = 1; \ 146 _FP_FRAC_SET_##wc(X, _FP_ZEROF 147 _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \ 147 } 148 } \ 148 else 149 else \ 149 { 150 { \ 150 _FP_ROUND(wc, X); !! 151 X##_e = 0; \ 151 if (_FP_FRAC_HIGH_##fs(X) !! 152 _FP_FRAC_SRL_##wc(X, _FP_WORKBITS); \ 152 & (_FP_OVERFLOW_##fs >> 1)) << 153 { << 154 X##_e = 1; << 155 _FP_FRAC_SET_##wc(X, _FP_Z << 156 FP_SET_EXCEPTION(FP_EX_INE << 157 } << 158 else << 159 { << 160 X##_e = 0; << 161 _FP_FRAC_SRL_##wc(X, _FP_W << 162 } << 163 } << 164 if ((FP_CUR_EXCEPTIONS & FP_EX_INE << 165 (FP_TRAPPING_EXCEPTIONS & FP_E << 166 FP_SET_EXCEPTION(FP_EX_UNDERFL 153 FP_SET_EXCEPTION(FP_EX_UNDERFLOW); \ >> 154 } \ 167 } 155 } \ 168 else 156 else \ 169 { 157 { \ 170 /* underflow to zero */ 158 /* underflow to zero */ \ 171 X##_e = 0; 159 X##_e = 0; \ 172 if (!_FP_FRAC_ZEROP_##wc(X)) 160 if (!_FP_FRAC_ZEROP_##wc(X)) \ 173 { 161 { \ 174 _FP_FRAC_SET_##wc(X, _FP_MINFR 162 _FP_FRAC_SET_##wc(X, _FP_MINFRAC_##wc); \ 175 _FP_ROUND(wc, X); 163 _FP_ROUND(wc, X); \ 176 _FP_FRAC_LOW_##wc(X) >>= (_FP_ 164 _FP_FRAC_LOW_##wc(X) >>= (_FP_WORKBITS); \ 177 } 165 } \ 178 FP_SET_EXCEPTION(FP_EX_UNDERFLOW); 166 FP_SET_EXCEPTION(FP_EX_UNDERFLOW); \ 179 } 167 } \ 180 } 168 } \ 181 break; 169 break; \ 182 170 \ 183 case FP_CLS_ZERO: 171 case FP_CLS_ZERO: \ 184 X##_e = 0; 172 X##_e = 0; \ 185 _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); 173 _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \ 186 break; 174 break; \ 187 175 \ 188 case FP_CLS_INF: 176 case FP_CLS_INF: \ 189 X##_e = _FP_EXPMAX_##fs; 177 X##_e = _FP_EXPMAX_##fs; \ 190 _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); 178 _FP_FRAC_SET_##wc(X, _FP_ZEROFRAC_##wc); \ 191 break; 179 break; \ 192 180 \ 193 case FP_CLS_NAN: 181 case FP_CLS_NAN: \ 194 X##_e = _FP_EXPMAX_##fs; 182 X##_e = _FP_EXPMAX_##fs; \ 195 if (!_FP_KEEPNANFRACP) 183 if (!_FP_KEEPNANFRACP) \ 196 { 184 { \ 197 _FP_FRAC_SET_##wc(X, _FP_NANFRAC_##fs) 185 _FP_FRAC_SET_##wc(X, _FP_NANFRAC_##fs); \ 198 X##_s = _FP_NANSIGN_##fs; 186 X##_s = _FP_NANSIGN_##fs; \ 199 } 187 } \ 200 else 188 else \ 201 _FP_FRAC_HIGH_RAW_##fs(X) |= _FP_QNANBIT 189 _FP_FRAC_HIGH_RAW_##fs(X) |= _FP_QNANBIT_##fs; \ 202 break; 190 break; \ 203 } 191 } \ 204 } while (0) 192 } while (0) 205 193 206 /* This one accepts raw argument and not cooke 194 /* This one accepts raw argument and not cooked, returns 207 * 1 if X is a signaling NaN. 195 * 1 if X is a signaling NaN. 208 */ 196 */ 209 #define _FP_ISSIGNAN(fs, wc, X) 197 #define _FP_ISSIGNAN(fs, wc, X) \ 210 ({ 198 ({ \ 211 int __ret = 0; 199 int __ret = 0; \ 212 if (X##_e == _FP_EXPMAX_##fs) 200 if (X##_e == _FP_EXPMAX_##fs) \ 213 { 201 { \ 214 if (!_FP_FRAC_ZEROP_##wc(X) 202 if (!_FP_FRAC_ZEROP_##wc(X) \ 215 && !(_FP_FRAC_HIGH_RAW_##fs(X) & _FP 203 && !(_FP_FRAC_HIGH_RAW_##fs(X) & _FP_QNANBIT_##fs)) \ 216 __ret = 1; 204 __ret = 1; \ 217 } 205 } \ 218 __ret; 206 __ret; \ 219 }) 207 }) 220 208 221 209 222 210 223 211 224 212 225 /* 213 /* 226 * Main addition routine. The input values sh 214 * Main addition routine. The input values should be cooked. 227 */ 215 */ 228 216 229 #define _FP_ADD_INTERNAL(fs, wc, R, X, Y, OP) 217 #define _FP_ADD_INTERNAL(fs, wc, R, X, Y, OP) \ 230 do { 218 do { \ 231 switch (_FP_CLS_COMBINE(X##_c, Y##_c)) 219 switch (_FP_CLS_COMBINE(X##_c, Y##_c)) \ 232 { 220 { \ 233 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NO 221 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL): \ 234 { 222 { \ 235 /* shift the smaller number so that its 223 /* shift the smaller number so that its exponent matches the larger */ \ 236 _FP_I_TYPE diff = X##_e - Y##_e; 224 _FP_I_TYPE diff = X##_e - Y##_e; \ 237 225 \ 238 if (diff < 0) 226 if (diff < 0) \ 239 { 227 { \ 240 diff = -diff; 228 diff = -diff; \ 241 if (diff <= _FP_WFRACBITS_##fs) 229 if (diff <= _FP_WFRACBITS_##fs) \ 242 _FP_FRAC_SRS_##wc(X, diff, _FP_WFR 230 _FP_FRAC_SRS_##wc(X, diff, _FP_WFRACBITS_##fs); \ 243 else if (!_FP_FRAC_ZEROP_##wc(X)) 231 else if (!_FP_FRAC_ZEROP_##wc(X)) \ 244 _FP_FRAC_SET_##wc(X, _FP_MINFRAC_# 232 _FP_FRAC_SET_##wc(X, _FP_MINFRAC_##wc); \ 245 R##_e = Y##_e; 233 R##_e = Y##_e; \ 246 } 234 } \ 247 else 235 else \ 248 { 236 { \ 249 if (diff > 0) 237 if (diff > 0) \ 250 { 238 { \ 251 if (diff <= _FP_WFRACBITS_##fs) 239 if (diff <= _FP_WFRACBITS_##fs) \ 252 _FP_FRAC_SRS_##wc(Y, diff, _FP 240 _FP_FRAC_SRS_##wc(Y, diff, _FP_WFRACBITS_##fs); \ 253 else if (!_FP_FRAC_ZEROP_##wc(Y) 241 else if (!_FP_FRAC_ZEROP_##wc(Y)) \ 254 _FP_FRAC_SET_##wc(Y, _FP_MINFR 242 _FP_FRAC_SET_##wc(Y, _FP_MINFRAC_##wc); \ 255 } 243 } \ 256 R##_e = X##_e; 244 R##_e = X##_e; \ 257 } 245 } \ 258 246 \ 259 R##_c = FP_CLS_NORMAL; 247 R##_c = FP_CLS_NORMAL; \ 260 248 \ 261 if (X##_s == Y##_s) 249 if (X##_s == Y##_s) \ 262 { 250 { \ 263 R##_s = X##_s; 251 R##_s = X##_s; \ 264 _FP_FRAC_ADD_##wc(R, X, Y); 252 _FP_FRAC_ADD_##wc(R, X, Y); \ 265 if (_FP_FRAC_OVERP_##wc(fs, R)) 253 if (_FP_FRAC_OVERP_##wc(fs, R)) \ 266 { 254 { \ 267 _FP_FRAC_SRS_##wc(R, 1, _FP_WFRA 255 _FP_FRAC_SRS_##wc(R, 1, _FP_WFRACBITS_##fs); \ 268 R##_e++; 256 R##_e++; \ 269 } 257 } \ 270 } 258 } \ 271 else 259 else \ 272 { 260 { \ 273 R##_s = X##_s; 261 R##_s = X##_s; \ 274 _FP_FRAC_SUB_##wc(R, X, Y); 262 _FP_FRAC_SUB_##wc(R, X, Y); \ 275 if (_FP_FRAC_ZEROP_##wc(R)) 263 if (_FP_FRAC_ZEROP_##wc(R)) \ 276 { 264 { \ 277 /* return an exact zero */ 265 /* return an exact zero */ \ 278 if (FP_ROUNDMODE == FP_RND_MINF) 266 if (FP_ROUNDMODE == FP_RND_MINF) \ 279 R##_s |= Y##_s; 267 R##_s |= Y##_s; \ 280 else 268 else \ 281 R##_s &= Y##_s; 269 R##_s &= Y##_s; \ 282 R##_c = FP_CLS_ZERO; 270 R##_c = FP_CLS_ZERO; \ 283 } 271 } \ 284 else 272 else \ 285 { 273 { \ 286 if (_FP_FRAC_NEGP_##wc(R)) 274 if (_FP_FRAC_NEGP_##wc(R)) \ 287 { 275 { \ 288 _FP_FRAC_SUB_##wc(R, Y, X); 276 _FP_FRAC_SUB_##wc(R, Y, X); \ 289 R##_s = Y##_s; 277 R##_s = Y##_s; \ 290 } 278 } \ 291 279 \ 292 /* renormalize after subtraction 280 /* renormalize after subtraction */ \ 293 _FP_FRAC_CLZ_##wc(diff, R); 281 _FP_FRAC_CLZ_##wc(diff, R); \ 294 diff -= _FP_WFRACXBITS_##fs; 282 diff -= _FP_WFRACXBITS_##fs; \ 295 if (diff) 283 if (diff) \ 296 { 284 { \ 297 R##_e -= diff; 285 R##_e -= diff; \ 298 _FP_FRAC_SLL_##wc(R, diff); 286 _FP_FRAC_SLL_##wc(R, diff); \ 299 } 287 } \ 300 } 288 } \ 301 } 289 } \ 302 break; 290 break; \ 303 } 291 } \ 304 292 \ 305 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN): 293 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN): \ 306 _FP_CHOOSENAN(fs, wc, R, X, Y, OP); 294 _FP_CHOOSENAN(fs, wc, R, X, Y, OP); \ 307 break; 295 break; \ 308 296 \ 309 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZE 297 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO): \ 310 R##_e = X##_e; 298 R##_e = X##_e; \ 311 fallthrough; << 312 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMA 299 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL): \ 313 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF): 300 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF): \ 314 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO) 301 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO): \ 315 _FP_FRAC_COPY_##wc(R, X); 302 _FP_FRAC_COPY_##wc(R, X); \ 316 R##_s = X##_s; 303 R##_s = X##_s; \ 317 R##_c = X##_c; 304 R##_c = X##_c; \ 318 break; 305 break; \ 319 306 \ 320 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORM 307 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL): \ 321 R##_e = Y##_e; 308 R##_e = Y##_e; \ 322 fallthrough; << 323 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NA 309 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN): \ 324 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN): 310 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN): \ 325 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN) 311 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN): \ 326 _FP_FRAC_COPY_##wc(R, Y); 312 _FP_FRAC_COPY_##wc(R, Y); \ 327 R##_s = Y##_s; 313 R##_s = Y##_s; \ 328 R##_c = Y##_c; 314 R##_c = Y##_c; \ 329 break; 315 break; \ 330 316 \ 331 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF): 317 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF): \ 332 if (X##_s != Y##_s) 318 if (X##_s != Y##_s) \ 333 { 319 { \ 334 /* +INF + -INF => NAN */ 320 /* +INF + -INF => NAN */ \ 335 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs) 321 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \ 336 R##_s = _FP_NANSIGN_##fs; 322 R##_s = _FP_NANSIGN_##fs; \ 337 R##_c = FP_CLS_NAN; 323 R##_c = FP_CLS_NAN; \ 338 FP_SET_EXCEPTION(FP_EX_INVALID | FP_EX !! 324 FP_SET_EXCEPTION(FP_EX_INVALID); \ 339 break; 325 break; \ 340 } 326 } \ 341 fallthrough; !! 327 /* FALLTHRU */ \ 342 328 \ 343 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMA 329 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL): \ 344 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO) 330 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO): \ 345 R##_s = X##_s; 331 R##_s = X##_s; \ 346 R##_c = FP_CLS_INF; 332 R##_c = FP_CLS_INF; \ 347 break; 333 break; \ 348 334 \ 349 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_IN 335 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF): \ 350 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF) 336 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF): \ 351 R##_s = Y##_s; 337 R##_s = Y##_s; \ 352 R##_c = FP_CLS_INF; 338 R##_c = FP_CLS_INF; \ 353 break; 339 break; \ 354 340 \ 355 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO 341 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO): \ 356 /* make sure the sign is correct */ 342 /* make sure the sign is correct */ \ 357 if (FP_ROUNDMODE == FP_RND_MINF) 343 if (FP_ROUNDMODE == FP_RND_MINF) \ 358 R##_s = X##_s | Y##_s; 344 R##_s = X##_s | Y##_s; \ 359 else 345 else \ 360 R##_s = X##_s & Y##_s; 346 R##_s = X##_s & Y##_s; \ 361 R##_c = FP_CLS_ZERO; 347 R##_c = FP_CLS_ZERO; \ 362 break; 348 break; \ 363 349 \ 364 default: 350 default: \ 365 abort(); 351 abort(); \ 366 } 352 } \ 367 } while (0) 353 } while (0) 368 354 369 #define _FP_ADD(fs, wc, R, X, Y) _FP_ADD_INTER 355 #define _FP_ADD(fs, wc, R, X, Y) _FP_ADD_INTERNAL(fs, wc, R, X, Y, '+') 370 #define _FP_SUB(fs, wc, R, X, Y) 356 #define _FP_SUB(fs, wc, R, X, Y) \ 371 do { 357 do { \ 372 if (Y##_c != FP_CLS_NAN) Y##_s ^= 1; 358 if (Y##_c != FP_CLS_NAN) Y##_s ^= 1; \ 373 _FP_ADD_INTERNAL(fs, wc, R, X, Y, '-'); 359 _FP_ADD_INTERNAL(fs, wc, R, X, Y, '-'); \ 374 } while (0) 360 } while (0) 375 361 376 362 377 /* 363 /* 378 * Main negation routine. FIXME -- when we ca 364 * Main negation routine. FIXME -- when we care about setting exception 379 * bits reliably, this will not do. We should 365 * bits reliably, this will not do. We should examine all of the fp classes. 380 */ 366 */ 381 367 382 #define _FP_NEG(fs, wc, R, X) \ 368 #define _FP_NEG(fs, wc, R, X) \ 383 do { \ 369 do { \ 384 _FP_FRAC_COPY_##wc(R, X); \ 370 _FP_FRAC_COPY_##wc(R, X); \ 385 R##_c = X##_c; \ 371 R##_c = X##_c; \ 386 R##_e = X##_e; \ 372 R##_e = X##_e; \ 387 R##_s = 1 ^ X##_s; \ 373 R##_s = 1 ^ X##_s; \ 388 } while (0) 374 } while (0) 389 375 390 376 391 /* 377 /* 392 * Main multiplication routine. The input val 378 * Main multiplication routine. The input values should be cooked. 393 */ 379 */ 394 380 395 #define _FP_MUL(fs, wc, R, X, Y) 381 #define _FP_MUL(fs, wc, R, X, Y) \ 396 do { 382 do { \ 397 R##_s = X##_s ^ Y##_s; 383 R##_s = X##_s ^ Y##_s; \ 398 switch (_FP_CLS_COMBINE(X##_c, Y##_c)) 384 switch (_FP_CLS_COMBINE(X##_c, Y##_c)) \ 399 { 385 { \ 400 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NO 386 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL): \ 401 R##_c = FP_CLS_NORMAL; 387 R##_c = FP_CLS_NORMAL; \ 402 R##_e = X##_e + Y##_e + 1; 388 R##_e = X##_e + Y##_e + 1; \ 403 389 \ 404 _FP_MUL_MEAT_##fs(R,X,Y); 390 _FP_MUL_MEAT_##fs(R,X,Y); \ 405 391 \ 406 if (_FP_FRAC_OVERP_##wc(fs, R)) 392 if (_FP_FRAC_OVERP_##wc(fs, R)) \ 407 _FP_FRAC_SRS_##wc(R, 1, _FP_WFRACBITS_## 393 _FP_FRAC_SRS_##wc(R, 1, _FP_WFRACBITS_##fs); \ 408 else 394 else \ 409 R##_e--; 395 R##_e--; \ 410 break; 396 break; \ 411 397 \ 412 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN): 398 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN): \ 413 _FP_CHOOSENAN(fs, wc, R, X, Y, '*'); 399 _FP_CHOOSENAN(fs, wc, R, X, Y, '*'); \ 414 break; 400 break; \ 415 401 \ 416 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMA 402 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL): \ 417 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF): 403 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF): \ 418 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO) 404 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO): \ 419 R##_s = X##_s; 405 R##_s = X##_s; \ 420 fallthrough; << 421 406 \ 422 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF): 407 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF): \ 423 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMA 408 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL): \ 424 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORM 409 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL): \ 425 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO 410 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO): \ 426 _FP_FRAC_COPY_##wc(R, X); 411 _FP_FRAC_COPY_##wc(R, X); \ 427 R##_c = X##_c; 412 R##_c = X##_c; \ 428 break; 413 break; \ 429 414 \ 430 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NA 415 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN): \ 431 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN): 416 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN): \ 432 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN) 417 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN): \ 433 R##_s = Y##_s; 418 R##_s = Y##_s; \ 434 fallthrough; << 435 419 \ 436 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_IN 420 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF): \ 437 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZE 421 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO): \ 438 _FP_FRAC_COPY_##wc(R, Y); 422 _FP_FRAC_COPY_##wc(R, Y); \ 439 R##_c = Y##_c; 423 R##_c = Y##_c; \ 440 break; 424 break; \ 441 425 \ 442 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO) 426 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO): \ 443 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF) 427 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF): \ 444 R##_s = _FP_NANSIGN_##fs; 428 R##_s = _FP_NANSIGN_##fs; \ 445 R##_c = FP_CLS_NAN; 429 R##_c = FP_CLS_NAN; \ 446 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); 430 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \ 447 FP_SET_EXCEPTION(FP_EX_INVALID | FP_EX_INV !! 431 FP_SET_EXCEPTION(FP_EX_INVALID); \ 448 break; 432 break; \ 449 433 \ 450 default: 434 default: \ 451 abort(); 435 abort(); \ 452 } 436 } \ 453 } while (0) 437 } while (0) 454 438 455 439 456 /* 440 /* 457 * Main division routine. The input values sh 441 * Main division routine. The input values should be cooked. 458 */ 442 */ 459 443 460 #define _FP_DIV(fs, wc, R, X, Y) 444 #define _FP_DIV(fs, wc, R, X, Y) \ 461 do { 445 do { \ 462 R##_s = X##_s ^ Y##_s; 446 R##_s = X##_s ^ Y##_s; \ 463 switch (_FP_CLS_COMBINE(X##_c, Y##_c)) 447 switch (_FP_CLS_COMBINE(X##_c, Y##_c)) \ 464 { 448 { \ 465 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NO 449 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NORMAL): \ 466 R##_c = FP_CLS_NORMAL; 450 R##_c = FP_CLS_NORMAL; \ 467 R##_e = X##_e - Y##_e; 451 R##_e = X##_e - Y##_e; \ 468 452 \ 469 _FP_DIV_MEAT_##fs(R,X,Y); 453 _FP_DIV_MEAT_##fs(R,X,Y); \ 470 break; 454 break; \ 471 455 \ 472 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN): 456 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NAN): \ 473 _FP_CHOOSENAN(fs, wc, R, X, Y, '/'); 457 _FP_CHOOSENAN(fs, wc, R, X, Y, '/'); \ 474 break; 458 break; \ 475 459 \ 476 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMA 460 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_NORMAL): \ 477 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF): 461 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_INF): \ 478 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO) 462 case _FP_CLS_COMBINE(FP_CLS_NAN,FP_CLS_ZERO): \ 479 R##_s = X##_s; 463 R##_s = X##_s; \ 480 _FP_FRAC_COPY_##wc(R, X); 464 _FP_FRAC_COPY_##wc(R, X); \ 481 R##_c = X##_c; 465 R##_c = X##_c; \ 482 break; 466 break; \ 483 467 \ 484 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NA 468 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_NAN): \ 485 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN): 469 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NAN): \ 486 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN) 470 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NAN): \ 487 R##_s = Y##_s; 471 R##_s = Y##_s; \ 488 _FP_FRAC_COPY_##wc(R, Y); 472 _FP_FRAC_COPY_##wc(R, Y); \ 489 R##_c = Y##_c; 473 R##_c = Y##_c; \ 490 break; 474 break; \ 491 475 \ 492 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_IN 476 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_INF): \ 493 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF) 477 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_INF): \ 494 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORM 478 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_NORMAL): \ 495 R##_c = FP_CLS_ZERO; 479 R##_c = FP_CLS_ZERO; \ 496 break; 480 break; \ 497 481 \ 498 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZE 482 case _FP_CLS_COMBINE(FP_CLS_NORMAL,FP_CLS_ZERO): \ 499 FP_SET_EXCEPTION(FP_EX_DIVZERO); 483 FP_SET_EXCEPTION(FP_EX_DIVZERO); \ 500 fallthrough; << 501 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO) 484 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_ZERO): \ 502 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMA 485 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_NORMAL): \ 503 R##_c = FP_CLS_INF; 486 R##_c = FP_CLS_INF; \ 504 break; 487 break; \ 505 488 \ 506 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF): 489 case _FP_CLS_COMBINE(FP_CLS_INF,FP_CLS_INF): \ 507 R##_s = _FP_NANSIGN_##fs; << 508 R##_c = FP_CLS_NAN; << 509 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); << 510 FP_SET_EXCEPTION(FP_EX_INVALID | FP_EX_INV << 511 break; << 512 << 513 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO 490 case _FP_CLS_COMBINE(FP_CLS_ZERO,FP_CLS_ZERO): \ 514 R##_s = _FP_NANSIGN_##fs; 491 R##_s = _FP_NANSIGN_##fs; \ 515 R##_c = FP_CLS_NAN; 492 R##_c = FP_CLS_NAN; \ 516 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); 493 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \ 517 FP_SET_EXCEPTION(FP_EX_INVALID | FP_EX_INV !! 494 FP_SET_EXCEPTION(FP_EX_INVALID); \ 518 break; 495 break; \ 519 496 \ 520 default: 497 default: \ 521 abort(); 498 abort(); \ 522 } 499 } \ 523 } while (0) 500 } while (0) 524 501 525 502 526 /* 503 /* 527 * Main differential comparison routine. The 504 * Main differential comparison routine. The inputs should be raw not 528 * cooked. The return is -1,0,1 for normal va 505 * cooked. The return is -1,0,1 for normal values, 2 otherwise. 529 */ 506 */ 530 507 531 #define _FP_CMP(fs, wc, ret, X, Y, un) 508 #define _FP_CMP(fs, wc, ret, X, Y, un) \ 532 do { 509 do { \ 533 /* NANs are unordered */ 510 /* NANs are unordered */ \ 534 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC 511 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(X)) \ 535 || (Y##_e == _FP_EXPMAX_##fs && !_FP_F 512 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(Y))) \ 536 { 513 { \ 537 ret = un; 514 ret = un; \ 538 } 515 } \ 539 else 516 else \ 540 { 517 { \ 541 int __is_zero_x; 518 int __is_zero_x; \ 542 int __is_zero_y; 519 int __is_zero_y; \ 543 520 \ 544 __is_zero_x = (!X##_e && _FP_FRAC_ZERO 521 __is_zero_x = (!X##_e && _FP_FRAC_ZEROP_##wc(X)) ? 1 : 0; \ 545 __is_zero_y = (!Y##_e && _FP_FRAC_ZERO 522 __is_zero_y = (!Y##_e && _FP_FRAC_ZEROP_##wc(Y)) ? 1 : 0; \ 546 523 \ 547 if (__is_zero_x && __is_zero_y) 524 if (__is_zero_x && __is_zero_y) \ 548 ret = 0; 525 ret = 0; \ 549 else if (__is_zero_x) 526 else if (__is_zero_x) \ 550 ret = Y##_s ? 1 : -1; 527 ret = Y##_s ? 1 : -1; \ 551 else if (__is_zero_y) 528 else if (__is_zero_y) \ 552 ret = X##_s ? -1 : 1; 529 ret = X##_s ? -1 : 1; \ 553 else if (X##_s != Y##_s) 530 else if (X##_s != Y##_s) \ 554 ret = X##_s ? -1 : 1; 531 ret = X##_s ? -1 : 1; \ 555 else if (X##_e > Y##_e) 532 else if (X##_e > Y##_e) \ 556 ret = X##_s ? -1 : 1; 533 ret = X##_s ? -1 : 1; \ 557 else if (X##_e < Y##_e) 534 else if (X##_e < Y##_e) \ 558 ret = X##_s ? 1 : -1; 535 ret = X##_s ? 1 : -1; \ 559 else if (_FP_FRAC_GT_##wc(X, Y)) 536 else if (_FP_FRAC_GT_##wc(X, Y)) \ 560 ret = X##_s ? -1 : 1; 537 ret = X##_s ? -1 : 1; \ 561 else if (_FP_FRAC_GT_##wc(Y, X)) 538 else if (_FP_FRAC_GT_##wc(Y, X)) \ 562 ret = X##_s ? 1 : -1; 539 ret = X##_s ? 1 : -1; \ 563 else 540 else \ 564 ret = 0; 541 ret = 0; \ 565 } 542 } \ 566 } while (0) 543 } while (0) 567 544 568 545 569 /* Simplification for strict equality. */ 546 /* Simplification for strict equality. */ 570 547 571 #define _FP_CMP_EQ(fs, wc, ret, X, Y) 548 #define _FP_CMP_EQ(fs, wc, ret, X, Y) \ 572 do { 549 do { \ 573 /* NANs are unordered */ 550 /* NANs are unordered */ \ 574 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC 551 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(X)) \ 575 || (Y##_e == _FP_EXPMAX_##fs && !_FP_F 552 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc(Y))) \ 576 { 553 { \ 577 ret = 1; 554 ret = 1; \ 578 } 555 } \ 579 else 556 else \ 580 { 557 { \ 581 ret = !(X##_e == Y##_e 558 ret = !(X##_e == Y##_e \ 582 && _FP_FRAC_EQ_##wc(X, Y) 559 && _FP_FRAC_EQ_##wc(X, Y) \ 583 && (X##_s == Y##_s || !X##_e & 560 && (X##_s == Y##_s || !X##_e && _FP_FRAC_ZEROP_##wc(X))); \ 584 } 561 } \ 585 } while (0) 562 } while (0) 586 563 587 /* 564 /* 588 * Main square root routine. The input value 565 * Main square root routine. The input value should be cooked. 589 */ 566 */ 590 567 591 #define _FP_SQRT(fs, wc, R, X) 568 #define _FP_SQRT(fs, wc, R, X) \ 592 do { 569 do { \ 593 _FP_FRAC_DECL_##wc(T); _FP_FRAC_DECL_##wc( 570 _FP_FRAC_DECL_##wc(T); _FP_FRAC_DECL_##wc(S); \ 594 _FP_W_TYPE q; 571 _FP_W_TYPE q; \ 595 switch (X##_c) 572 switch (X##_c) \ 596 { 573 { \ 597 case FP_CLS_NAN: 574 case FP_CLS_NAN: \ 598 _FP_FRAC_COPY_##wc(R, X); 575 _FP_FRAC_COPY_##wc(R, X); \ 599 R##_s = X##_s; 576 R##_s = X##_s; \ 600 R##_c = FP_CLS_NAN; 577 R##_c = FP_CLS_NAN; \ 601 break; 578 break; \ 602 case FP_CLS_INF: 579 case FP_CLS_INF: \ 603 if (X##_s) 580 if (X##_s) \ 604 { 581 { \ 605 R##_s = _FP_NANSIGN_##fs; 582 R##_s = _FP_NANSIGN_##fs; \ 606 R##_c = FP_CLS_NAN; /* NAN */ 583 R##_c = FP_CLS_NAN; /* NAN */ \ 607 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_# 584 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \ 608 FP_SET_EXCEPTION(FP_EX_INVALID); 585 FP_SET_EXCEPTION(FP_EX_INVALID); \ 609 } 586 } \ 610 else 587 else \ 611 { 588 { \ 612 R##_s = 0; 589 R##_s = 0; \ 613 R##_c = FP_CLS_INF; /* sqrt(+inf) 590 R##_c = FP_CLS_INF; /* sqrt(+inf) = +inf */ \ 614 } 591 } \ 615 break; 592 break; \ 616 case FP_CLS_ZERO: 593 case FP_CLS_ZERO: \ 617 R##_s = X##_s; 594 R##_s = X##_s; \ 618 R##_c = FP_CLS_ZERO; /* sqrt(+-0) = +- 595 R##_c = FP_CLS_ZERO; /* sqrt(+-0) = +-0 */ \ 619 break; 596 break; \ 620 case FP_CLS_NORMAL: 597 case FP_CLS_NORMAL: \ 621 R##_s = 0; 598 R##_s = 0; \ 622 if (X##_s) 599 if (X##_s) \ 623 { 600 { \ 624 R##_c = FP_CLS_NAN; /* sNAN */ 601 R##_c = FP_CLS_NAN; /* sNAN */ \ 625 R##_s = _FP_NANSIGN_##fs; 602 R##_s = _FP_NANSIGN_##fs; \ 626 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_# 603 _FP_FRAC_SET_##wc(R, _FP_NANFRAC_##fs); \ 627 FP_SET_EXCEPTION(FP_EX_INVALID); 604 FP_SET_EXCEPTION(FP_EX_INVALID); \ 628 break; 605 break; \ 629 } 606 } \ 630 R##_c = FP_CLS_NORMAL; 607 R##_c = FP_CLS_NORMAL; \ 631 if (X##_e & 1) 608 if (X##_e & 1) \ 632 _FP_FRAC_SLL_##wc(X, 1); 609 _FP_FRAC_SLL_##wc(X, 1); \ 633 R##_e = X##_e >> 1; 610 R##_e = X##_e >> 1; \ 634 _FP_FRAC_SET_##wc(S, _FP_ZEROFRAC_##wc 611 _FP_FRAC_SET_##wc(S, _FP_ZEROFRAC_##wc); \ 635 _FP_FRAC_SET_##wc(R, _FP_ZEROFRAC_##wc 612 _FP_FRAC_SET_##wc(R, _FP_ZEROFRAC_##wc); \ 636 q = _FP_OVERFLOW_##fs >> 1; 613 q = _FP_OVERFLOW_##fs >> 1; \ 637 _FP_SQRT_MEAT_##wc(R, S, T, X, q); 614 _FP_SQRT_MEAT_##wc(R, S, T, X, q); \ 638 } 615 } \ 639 } while (0) 616 } while (0) 640 617 641 /* 618 /* 642 * Convert from FP to integer 619 * Convert from FP to integer 643 */ 620 */ 644 621 645 /* RSIGNED can have following values: 622 /* RSIGNED can have following values: 646 * 0: the number is required to be 0..(2^rsiz 623 * 0: the number is required to be 0..(2^rsize)-1, if not, NV is set plus 647 * the result is either 0 or (2^rsize)-1 d 624 * the result is either 0 or (2^rsize)-1 depending on the sign in such case. 648 * 1: the number is required to be -(2^(rsize 625 * 1: the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not, NV is 649 * set plus the result is either -(2^(rsiz 626 * set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1 depending 650 * on the sign in such case. 627 * on the sign in such case. 651 * 2: the number is required to be -(2^(rsize 628 * 2: the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not, NV is 652 * set plus the result is truncated to fit 629 * set plus the result is truncated to fit into destination. 653 * -1: the number is required to be -(2^(rsize 630 * -1: the number is required to be -(2^(rsize-1))..(2^rsize)-1, if not, NV is 654 * set plus the result is either -(2^(rsiz 631 * set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1 depending 655 * on the sign in such case. 632 * on the sign in such case. 656 */ 633 */ 657 #define _FP_TO_INT(fs, wc, r, X, rsize, rsigne 634 #define _FP_TO_INT(fs, wc, r, X, rsize, rsigned) \ 658 do { 635 do { \ 659 switch (X##_c) 636 switch (X##_c) \ 660 { 637 { \ 661 case FP_CLS_NORMAL: 638 case FP_CLS_NORMAL: \ 662 if (X##_e < 0) 639 if (X##_e < 0) \ 663 { 640 { \ 664 FP_SET_EXCEPTION(FP_EX_INEXACT); 641 FP_SET_EXCEPTION(FP_EX_INEXACT); \ 665 fallthrough; << 666 case FP_CLS_ZERO: 642 case FP_CLS_ZERO: \ 667 r = 0; 643 r = 0; \ 668 } 644 } \ 669 else if (X##_e >= rsize - (rsigned > 0 645 else if (X##_e >= rsize - (rsigned > 0 || X##_s) \ 670 || (!rsigned && X##_s)) 646 || (!rsigned && X##_s)) \ 671 { /* overflow */ 647 { /* overflow */ \ 672 fallthrough; << 673 case FP_CLS_NAN: 648 case FP_CLS_NAN: \ 674 case FP_CLS_INF: 649 case FP_CLS_INF: \ 675 if (rsigned == 2) 650 if (rsigned == 2) \ 676 { 651 { \ 677 if (X##_c != FP_CLS_NORMAL 652 if (X##_c != FP_CLS_NORMAL \ 678 || X##_e >= rsize - 1 + _F 653 || X##_e >= rsize - 1 + _FP_WFRACBITS_##fs) \ 679 r = 0; 654 r = 0; \ 680 else 655 else \ 681 { 656 { \ 682 _FP_FRAC_SLL_##wc(X, (X##_ 657 _FP_FRAC_SLL_##wc(X, (X##_e - _FP_WFRACBITS_##fs + 1)); \ 683 _FP_FRAC_ASSEMBLE_##wc(r, 658 _FP_FRAC_ASSEMBLE_##wc(r, X, rsize); \ 684 } 659 } \ 685 } 660 } \ 686 else if (rsigned) 661 else if (rsigned) \ 687 { 662 { \ 688 r = 1; 663 r = 1; \ 689 r <<= rsize - 1; 664 r <<= rsize - 1; \ 690 r -= 1 - X##_s; 665 r -= 1 - X##_s; \ 691 } 666 } \ 692 else 667 else \ 693 { 668 { \ 694 r = 0; 669 r = 0; \ 695 if (!X##_s) !! 670 if (X##_s) \ 696 r = ~r; 671 r = ~r; \ 697 } 672 } \ 698 FP_SET_EXCEPTION(FP_EX_INVALID); 673 FP_SET_EXCEPTION(FP_EX_INVALID); \ 699 } 674 } \ 700 else 675 else \ 701 { 676 { \ 702 if (_FP_W_TYPE_SIZE*wc < rsize) 677 if (_FP_W_TYPE_SIZE*wc < rsize) \ 703 { 678 { \ 704 _FP_FRAC_ASSEMBLE_##wc(r, X, r 679 _FP_FRAC_ASSEMBLE_##wc(r, X, rsize); \ 705 r <<= X##_e - _FP_WFRACBITS_## 680 r <<= X##_e - _FP_WFRACBITS_##fs; \ 706 } 681 } \ 707 else 682 else \ 708 { 683 { \ 709 if (X##_e >= _FP_WFRACBITS_##f 684 if (X##_e >= _FP_WFRACBITS_##fs) \ 710 _FP_FRAC_SLL_##wc(X, (X##_e 685 _FP_FRAC_SLL_##wc(X, (X##_e - _FP_WFRACBITS_##fs + 1)); \ 711 else if (X##_e < _FP_WFRACBITS 686 else if (X##_e < _FP_WFRACBITS_##fs - 1) \ 712 { 687 { \ 713 _FP_FRAC_SRS_##wc(X, (_FP_ 688 _FP_FRAC_SRS_##wc(X, (_FP_WFRACBITS_##fs - X##_e - 2), \ 714 _FP_WFRA 689 _FP_WFRACBITS_##fs); \ 715 if (_FP_FRAC_LOW_##wc(X) & 690 if (_FP_FRAC_LOW_##wc(X) & 1) \ 716 FP_SET_EXCEPTION(FP_EX_I 691 FP_SET_EXCEPTION(FP_EX_INEXACT); \ 717 _FP_FRAC_SRL_##wc(X, 1); 692 _FP_FRAC_SRL_##wc(X, 1); \ 718 } 693 } \ 719 _FP_FRAC_ASSEMBLE_##wc(r, X, r 694 _FP_FRAC_ASSEMBLE_##wc(r, X, rsize); \ 720 } 695 } \ 721 if (rsigned && X##_s) 696 if (rsigned && X##_s) \ 722 r = -r; 697 r = -r; \ 723 } 698 } \ 724 break; 699 break; \ 725 } 700 } \ 726 } while (0) 701 } while (0) 727 702 728 #define _FP_TO_INT_ROUND(fs, wc, r, X, rsize, 703 #define _FP_TO_INT_ROUND(fs, wc, r, X, rsize, rsigned) \ 729 do { 704 do { \ 730 r = 0; 705 r = 0; \ 731 switch (X##_c) 706 switch (X##_c) \ 732 { 707 { \ 733 case FP_CLS_NORMAL: 708 case FP_CLS_NORMAL: \ 734 if (X##_e >= _FP_FRACBITS_##fs - 1) 709 if (X##_e >= _FP_FRACBITS_##fs - 1) \ 735 { 710 { \ 736 if (X##_e < rsize - 1 + _FP_WFRACB 711 if (X##_e < rsize - 1 + _FP_WFRACBITS_##fs) \ 737 { 712 { \ 738 if (X##_e >= _FP_WFRACBITS_##f 713 if (X##_e >= _FP_WFRACBITS_##fs - 1) \ 739 { 714 { \ 740 _FP_FRAC_ASSEMBLE_##wc(r, 715 _FP_FRAC_ASSEMBLE_##wc(r, X, rsize); \ 741 r <<= X##_e - _FP_WFRACBIT 716 r <<= X##_e - _FP_WFRACBITS_##fs + 1; \ 742 } 717 } \ 743 else 718 else \ 744 { 719 { \ 745 _FP_FRAC_SRL_##wc(X, _FP_W 720 _FP_FRAC_SRL_##wc(X, _FP_WORKBITS - X##_e \ 746 + _FP_FR 721 + _FP_FRACBITS_##fs - 1); \ 747 _FP_FRAC_ASSEMBLE_##wc(r, 722 _FP_FRAC_ASSEMBLE_##wc(r, X, rsize); \ 748 } 723 } \ 749 } 724 } \ 750 } 725 } \ 751 else 726 else \ 752 { 727 { \ 753 int _lz0, _lz1; << 754 if (X##_e <= -_FP_WORKBITS - 1) 728 if (X##_e <= -_FP_WORKBITS - 1) \ 755 _FP_FRAC_SET_##wc(X, _FP_MINFRAC 729 _FP_FRAC_SET_##wc(X, _FP_MINFRAC_##wc); \ 756 else 730 else \ 757 _FP_FRAC_SRS_##wc(X, _FP_FRACBIT 731 _FP_FRAC_SRS_##wc(X, _FP_FRACBITS_##fs - 1 - X##_e, \ 758 _FP_WFRACBITS_ 732 _FP_WFRACBITS_##fs); \ 759 _FP_FRAC_CLZ_##wc(_lz0, X); << 760 _FP_ROUND(wc, X); 733 _FP_ROUND(wc, X); \ 761 _FP_FRAC_CLZ_##wc(_lz1, X); << 762 if (_lz1 < _lz0) << 763 X##_e++; /* For overflow detecti << 764 _FP_FRAC_SRL_##wc(X, _FP_WORKBITS) 734 _FP_FRAC_SRL_##wc(X, _FP_WORKBITS); \ 765 _FP_FRAC_ASSEMBLE_##wc(r, X, rsize 735 _FP_FRAC_ASSEMBLE_##wc(r, X, rsize); \ 766 } 736 } \ 767 if (rsigned && X##_s) 737 if (rsigned && X##_s) \ 768 r = -r; 738 r = -r; \ 769 if (X##_e >= rsize - (rsigned > 0 || X 739 if (X##_e >= rsize - (rsigned > 0 || X##_s) \ 770 || (!rsigned && X##_s)) 740 || (!rsigned && X##_s)) \ 771 { /* overflow */ 741 { /* overflow */ \ 772 fallthrough; << 773 case FP_CLS_NAN: 742 case FP_CLS_NAN: \ 774 case FP_CLS_INF: 743 case FP_CLS_INF: \ 775 if (!rsigned) 744 if (!rsigned) \ 776 { 745 { \ 777 r = 0; 746 r = 0; \ 778 if (!X##_s) !! 747 if (X##_s) \ 779 r = ~r; 748 r = ~r; \ 780 } 749 } \ 781 else if (rsigned != 2) 750 else if (rsigned != 2) \ 782 { 751 { \ 783 r = 1; 752 r = 1; \ 784 r <<= rsize - 1; 753 r <<= rsize - 1; \ 785 r -= 1 - X##_s; 754 r -= 1 - X##_s; \ 786 } 755 } \ 787 FP_SET_EXCEPTION(FP_EX_INVALID); 756 FP_SET_EXCEPTION(FP_EX_INVALID); \ 788 } 757 } \ 789 break; 758 break; \ 790 case FP_CLS_ZERO: 759 case FP_CLS_ZERO: \ 791 break; 760 break; \ 792 } 761 } \ 793 } while (0) 762 } while (0) 794 763 795 #define _FP_FROM_INT(fs, wc, X, r, rsize, rtyp 764 #define _FP_FROM_INT(fs, wc, X, r, rsize, rtype) \ 796 do { 765 do { \ 797 if (r) 766 if (r) \ 798 { 767 { \ 799 unsigned rtype ur_; 768 unsigned rtype ur_; \ 800 X##_c = FP_CLS_NORMAL; 769 X##_c = FP_CLS_NORMAL; \ 801 770 \ 802 if ((X##_s = (r < 0))) 771 if ((X##_s = (r < 0))) \ 803 ur_ = (unsigned rtype) -r; !! 772 r = -r; \ >> 773 \ >> 774 ur_ = (unsigned rtype) r; \ >> 775 if (rsize <= _FP_W_TYPE_SIZE) \ >> 776 __FP_CLZ(X##_e, ur_); \ 804 else 777 else \ 805 ur_ = (unsigned rtype) r; !! 778 __FP_CLZ_2(X##_e, (_FP_W_TYPE)(ur_ >> _FP_W_TYPE_SIZE), \ 806 (void) (((rsize) <= _FP_W_TYPE_SIZE) !! 779 (_FP_W_TYPE)ur_); \ 807 ? ({ __FP_CLZ(X##_e, ur_); }) << 808 : ({ << 809 __FP_CLZ_2(X##_e, (_FP_W_ << 810 << 811 })); << 812 if (rsize < _FP_W_TYPE_SIZE) 780 if (rsize < _FP_W_TYPE_SIZE) \ 813 X##_e -= (_FP_W_TYPE_SIZE - rs 781 X##_e -= (_FP_W_TYPE_SIZE - rsize); \ 814 X##_e = rsize - X##_e - 1; 782 X##_e = rsize - X##_e - 1; \ 815 783 \ 816 if (_FP_FRACBITS_##fs < rsize && _FP_W !! 784 if (_FP_FRACBITS_##fs < rsize && _FP_WFRACBITS_##fs < X##_e) \ 817 __FP_FRAC_SRS_1(ur_, (X##_e - _FP_WF 785 __FP_FRAC_SRS_1(ur_, (X##_e - _FP_WFRACBITS_##fs + 1), rsize);\ 818 _FP_FRAC_DISASSEMBLE_##wc(X, ur_, rsiz 786 _FP_FRAC_DISASSEMBLE_##wc(X, ur_, rsize); \ 819 if ((_FP_WFRACBITS_##fs - X##_e - 1) > 787 if ((_FP_WFRACBITS_##fs - X##_e - 1) > 0) \ 820 _FP_FRAC_SLL_##wc(X, (_FP_WFRACBITS_ 788 _FP_FRAC_SLL_##wc(X, (_FP_WFRACBITS_##fs - X##_e - 1)); \ 821 } 789 } \ 822 else 790 else \ 823 { 791 { \ 824 X##_c = FP_CLS_ZERO, X##_s = 0; 792 X##_c = FP_CLS_ZERO, X##_s = 0; \ 825 } 793 } \ 826 } while (0) 794 } while (0) 827 795 828 796 829 #define FP_CONV(dfs,sfs,dwc,swc,D,S) 797 #define FP_CONV(dfs,sfs,dwc,swc,D,S) \ 830 do { 798 do { \ 831 _FP_FRAC_CONV_##dwc##_##swc(dfs, sfs, D, S 799 _FP_FRAC_CONV_##dwc##_##swc(dfs, sfs, D, S); \ 832 D##_e = S##_e; 800 D##_e = S##_e; \ 833 D##_c = S##_c; 801 D##_c = S##_c; \ 834 D##_s = S##_s; 802 D##_s = S##_s; \ 835 } while (0) 803 } while (0) 836 804 837 /* 805 /* 838 * Helper primitives. 806 * Helper primitives. 839 */ 807 */ 840 808 841 /* Count leading zeros in a word. */ 809 /* Count leading zeros in a word. */ 842 810 843 #ifndef __FP_CLZ 811 #ifndef __FP_CLZ 844 #if _FP_W_TYPE_SIZE < 64 812 #if _FP_W_TYPE_SIZE < 64 845 /* this is just to shut the compiler up about 813 /* this is just to shut the compiler up about shifts > word length -- PMM 02/1998 */ 846 #define __FP_CLZ(r, x) 814 #define __FP_CLZ(r, x) \ 847 do { 815 do { \ 848 _FP_W_TYPE _t = (x); 816 _FP_W_TYPE _t = (x); \ 849 r = _FP_W_TYPE_SIZE - 1; 817 r = _FP_W_TYPE_SIZE - 1; \ 850 if (_t > 0xffff) r -= 16; 818 if (_t > 0xffff) r -= 16; \ 851 if (_t > 0xffff) _t >>= 16; 819 if (_t > 0xffff) _t >>= 16; \ 852 if (_t > 0xff) r -= 8; 820 if (_t > 0xff) r -= 8; \ 853 if (_t > 0xff) _t >>= 8; 821 if (_t > 0xff) _t >>= 8; \ 854 if (_t & 0xf0) r -= 4; 822 if (_t & 0xf0) r -= 4; \ 855 if (_t & 0xf0) _t >>= 4; 823 if (_t & 0xf0) _t >>= 4; \ 856 if (_t & 0xc) r -= 2; 824 if (_t & 0xc) r -= 2; \ 857 if (_t & 0xc) _t >>= 2; 825 if (_t & 0xc) _t >>= 2; \ 858 if (_t & 0x2) r -= 1; 826 if (_t & 0x2) r -= 1; \ 859 } while (0) 827 } while (0) 860 #else /* not _FP_W_TYPE_SIZE < 64 */ 828 #else /* not _FP_W_TYPE_SIZE < 64 */ 861 #define __FP_CLZ(r, x) 829 #define __FP_CLZ(r, x) \ 862 do { 830 do { \ 863 _FP_W_TYPE _t = (x); 831 _FP_W_TYPE _t = (x); \ 864 r = _FP_W_TYPE_SIZE - 1; 832 r = _FP_W_TYPE_SIZE - 1; \ 865 if (_t > 0xffffffff) r -= 32; 833 if (_t > 0xffffffff) r -= 32; \ 866 if (_t > 0xffffffff) _t >>= 32; 834 if (_t > 0xffffffff) _t >>= 32; \ 867 if (_t > 0xffff) r -= 16; 835 if (_t > 0xffff) r -= 16; \ 868 if (_t > 0xffff) _t >>= 16; 836 if (_t > 0xffff) _t >>= 16; \ 869 if (_t > 0xff) r -= 8; 837 if (_t > 0xff) r -= 8; \ 870 if (_t > 0xff) _t >>= 8; 838 if (_t > 0xff) _t >>= 8; \ 871 if (_t & 0xf0) r -= 4; 839 if (_t & 0xf0) r -= 4; \ 872 if (_t & 0xf0) _t >>= 4; 840 if (_t & 0xf0) _t >>= 4; \ 873 if (_t & 0xc) r -= 2; 841 if (_t & 0xc) r -= 2; \ 874 if (_t & 0xc) _t >>= 2; 842 if (_t & 0xc) _t >>= 2; \ 875 if (_t & 0x2) r -= 1; 843 if (_t & 0x2) r -= 1; \ 876 } while (0) 844 } while (0) 877 #endif /* not _FP_W_TYPE_SIZE < 64 */ 845 #endif /* not _FP_W_TYPE_SIZE < 64 */ 878 #endif /* ndef __FP_CLZ */ 846 #endif /* ndef __FP_CLZ */ 879 847 880 #define _FP_DIV_HELP_imm(q, r, n, d) 848 #define _FP_DIV_HELP_imm(q, r, n, d) \ 881 do { 849 do { \ 882 q = n / d, r = n % d; 850 q = n / d, r = n % d; \ 883 } while (0) 851 } while (0) 884 852 885 #endif /* __MATH_EMU_OP_COMMON_H__ */ 853 #endif /* __MATH_EMU_OP_COMMON_H__ */ 886 854
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.