src/lib/softfloat/fyl2x.c
| r0 | r14870 | |
| 1 | /*============================================================================ |
| 2 | This source file is an extension to the SoftFloat IEC/IEEE Floating-point |
| 3 | Arithmetic Package, Release 2b, written for Bochs (x86 achitecture simulator) |
| 4 | floating point emulation. |
| 5 | float_raise(float_flag_invalid) |
| 6 | THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort has |
| 7 | been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT TIMES |
| 8 | RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO PERSONS |
| 9 | AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ALL LOSSES, |
| 10 | COSTS, OR OTHER PROBLEMS THEY INCUR DUE TO THE SOFTWARE, AND WHO FURTHERMORE |
| 11 | EFFECTIVELY INDEMNIFY JOHN HAUSER AND THE INTERNATIONAL COMPUTER SCIENCE |
| 12 | INSTITUTE (possibly via similar legal warning) AGAINST ALL LOSSES, COSTS, OR |
| 13 | OTHER PROBLEMS INCURRED BY THEIR CUSTOMERS AND CLIENTS DUE TO THE SOFTWARE. |
| 14 | |
| 15 | Derivative works are acceptable, even for commercial purposes, so long as |
| 16 | (1) the source code for the derivative work includes prominent notice that |
| 17 | the work is derivative, and (2) the source code includes prominent notice with |
| 18 | these four paragraphs for those parts of this code that are retained. |
| 19 | =============================================================================*/ |
| 20 | |
| 21 | /*============================================================================ |
| 22 | * Written for Bochs (x86 achitecture simulator) by |
| 23 | * Stanislav Shwartsman [sshwarts at sourceforge net] |
| 24 | * Adapted for lib/softfloat in MESS by Hans Ostermeyer (03/2012) |
| 25 | * ==========================================================================*/ |
| 26 | |
| 27 | #define FLOAT128 |
| 28 | |
| 29 | #define USE_estimateDiv128To64 |
| 30 | #include "mamesf.h" |
| 31 | #include "softfloat.h" |
| 32 | //#include "softfloat-specialize" |
| 33 | #include "fpu_constant.h" |
| 34 | |
| 35 | // FIXME: |
| 36 | #define float_flag_denormal float_flag_invalid |
| 37 | |
| 38 | static const floatx80 floatx80_log10_2 = packFloatx80(0, 0x3ffd, U64(0x9a209a84fbcff798)); |
| 39 | static const floatx80 floatx80_ln_2 = packFloatx80(0, 0x3ffe, U64(0xb17217f7d1cf79ac)); |
| 40 | static const floatx80 floatx80_one = packFloatx80(0, 0x3fff, U64(0x8000000000000000)); |
| 41 | static const floatx80 floatx80_default_nan = packFloatx80(0, 0xffff, U64(0xffffffffffffffff)); |
| 42 | |
| 43 | #define packFloat_128(zHi, zLo) {(zHi), (zLo)} |
| 44 | #define PACK_FLOAT_128(hi,lo) packFloat_128(LIT64(hi),LIT64(lo)) |
| 45 | |
| 46 | #define EXP_BIAS 0x3FFF |
| 47 | |
| 48 | /*---------------------------------------------------------------------------- |
| 49 | | Returns the fraction bits of the extended double-precision floating-point |
| 50 | | value `a'. |
| 51 | *----------------------------------------------------------------------------*/ |
| 52 | |
| 53 | INLINE bits64 extractFloatx80Frac( floatx80 a ) |
| 54 | { |
| 55 | |
| 56 | return a.low; |
| 57 | |
| 58 | } |
| 59 | |
| 60 | /*---------------------------------------------------------------------------- |
| 61 | | Returns the exponent bits of the extended double-precision floating-point |
| 62 | | value `a'. |
| 63 | *----------------------------------------------------------------------------*/ |
| 64 | |
| 65 | INLINE int32 extractFloatx80Exp( floatx80 a ) |
| 66 | { |
| 67 | |
| 68 | return a.high & 0x7FFF; |
| 69 | |
| 70 | } |
| 71 | |
| 72 | /*---------------------------------------------------------------------------- |
| 73 | | Returns the sign bit of the extended double-precision floating-point value |
| 74 | | `a'. |
| 75 | *----------------------------------------------------------------------------*/ |
| 76 | |
| 77 | INLINE flag extractFloatx80Sign( floatx80 a ) |
| 78 | { |
| 79 | |
| 80 | return a.high>>15; |
| 81 | |
| 82 | } |
| 83 | |
| 84 | /*---------------------------------------------------------------------------- |
| 85 | | Takes extended double-precision floating-point NaN `a' and returns the |
| 86 | | appropriate NaN result. If `a' is a signaling NaN, the invalid exception |
| 87 | | is raised. |
| 88 | *----------------------------------------------------------------------------*/ |
| 89 | |
| 90 | INLINE floatx80 propagateFloatx80NaNOneArg(floatx80 a) |
| 91 | { |
| 92 | if (floatx80_is_signaling_nan(a)) |
| 93 | float_raise(float_flag_invalid); |
| 94 | |
| 95 | a.low |= U64(0xC000000000000000); |
| 96 | |
| 97 | return a; |
| 98 | } |
| 99 | |
| 100 | /*---------------------------------------------------------------------------- |
| 101 | | Normalizes the subnormal extended double-precision floating-point value |
| 102 | | represented by the denormalized significand `aSig'. The normalized exponent |
| 103 | | and significand are stored at the locations pointed to by `zExpPtr' and |
| 104 | | `zSigPtr', respectively. |
| 105 | *----------------------------------------------------------------------------*/ |
| 106 | |
| 107 | INLINE void normalizeFloatx80Subnormal(UINT64 aSig, INT32 *zExpPtr, UINT64 *zSigPtr) |
| 108 | { |
| 109 | int shiftCount = countLeadingZeros64(aSig); |
| 110 | *zSigPtr = aSig<<shiftCount; |
| 111 | *zExpPtr = 1 - shiftCount; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /*---------------------------------------------------------------------------- |
| 116 | | Returns 1 if the extended double-precision floating-point value `a' is a |
| 117 | | NaN; otherwise returns 0. |
| 118 | *----------------------------------------------------------------------------*/ |
| 119 | |
| 120 | INLINE int floatx80_is_nan(floatx80 a) |
| 121 | { |
| 122 | return ((a.high & 0x7FFF) == 0x7FFF) && (INT64) (a.low<<1); |
| 123 | } |
| 124 | |
| 125 | /*---------------------------------------------------------------------------- |
| 126 | | Takes two extended double-precision floating-point values `a' and `b', one |
| 127 | | of which is a NaN, and returns the appropriate NaN result. If either `a' or |
| 128 | | `b' is a signaling NaN, the invalid exception is raised. |
| 129 | *----------------------------------------------------------------------------*/ |
| 130 | |
| 131 | static floatx80 propagateFloatx80NaN(floatx80 a, floatx80 b) |
| 132 | { |
| 133 | int aIsNaN = floatx80_is_nan(a); |
| 134 | int aIsSignalingNaN = floatx80_is_signaling_nan(a); |
| 135 | int bIsNaN = floatx80_is_nan(b); |
| 136 | int bIsSignalingNaN = floatx80_is_signaling_nan(b); |
| 137 | a.low |= U64(0xC000000000000000); |
| 138 | b.low |= U64(0xC000000000000000); |
| 139 | if (aIsSignalingNaN | bIsSignalingNaN) float_raise(float_flag_invalid); |
| 140 | if (aIsSignalingNaN) { |
| 141 | if (bIsSignalingNaN) goto returnLargerSignificand; |
| 142 | return bIsNaN ? b : a; |
| 143 | } |
| 144 | else if (aIsNaN) { |
| 145 | if (bIsSignalingNaN | ! bIsNaN) return a; |
| 146 | returnLargerSignificand: |
| 147 | if (a.low < b.low) return b; |
| 148 | if (b.low < a.low) return a; |
| 149 | return (a.high < b.high) ? a : b; |
| 150 | } |
| 151 | else { |
| 152 | return b; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static const float128 float128_one = |
| 157 | packFloat_128(U64(0x3fff000000000000), U64(0x0000000000000000)); |
| 158 | static const float128 float128_two = |
| 159 | packFloat_128(U64(0x4000000000000000), U64(0x0000000000000000)); |
| 160 | |
| 161 | static const float128 float128_ln2inv2 = |
| 162 | packFloat_128(U64(0x400071547652b82f), U64(0xe1777d0ffda0d23a)); |
| 163 | |
| 164 | #define SQRT2_HALF_SIG U64(0xb504f333f9de6484) |
| 165 | |
| 166 | extern float128 OddPoly(float128 x, float128 *arr, unsigned n); |
| 167 | |
| 168 | #define L2_ARR_SIZE 9 |
| 169 | |
| 170 | static float128 ln_arr[L2_ARR_SIZE] = |
| 171 | { |
| 172 | PACK_FLOAT_128(0x3fff000000000000, 0x0000000000000000), /* 1 */ |
| 173 | PACK_FLOAT_128(0x3ffd555555555555, 0x5555555555555555), /* 3 */ |
| 174 | PACK_FLOAT_128(0x3ffc999999999999, 0x999999999999999a), /* 5 */ |
| 175 | PACK_FLOAT_128(0x3ffc249249249249, 0x2492492492492492), /* 7 */ |
| 176 | PACK_FLOAT_128(0x3ffbc71c71c71c71, 0xc71c71c71c71c71c), /* 9 */ |
| 177 | PACK_FLOAT_128(0x3ffb745d1745d174, 0x5d1745d1745d1746), /* 11 */ |
| 178 | PACK_FLOAT_128(0x3ffb3b13b13b13b1, 0x3b13b13b13b13b14), /* 13 */ |
| 179 | PACK_FLOAT_128(0x3ffb111111111111, 0x1111111111111111), /* 15 */ |
| 180 | PACK_FLOAT_128(0x3ffae1e1e1e1e1e1, 0xe1e1e1e1e1e1e1e2) /* 17 */ |
| 181 | }; |
| 182 | |
| 183 | static float128 poly_ln(float128 x1) |
| 184 | { |
| 185 | /* |
| 186 | // |
| 187 | // 3 5 7 9 11 13 15 |
| 188 | // 1+u u u u u u u u |
| 189 | // 1/2 ln --- ~ u + --- + --- + --- + --- + ---- + ---- + ---- = |
| 190 | // 1-u 3 5 7 9 11 13 15 |
| 191 | // |
| 192 | // 2 4 6 8 10 12 14 |
| 193 | // u u u u u u u |
| 194 | // = u * [ 1 + --- + --- + --- + --- + ---- + ---- + ---- ] = |
| 195 | // 3 5 7 9 11 13 15 |
| 196 | // |
| 197 | // 3 3 |
| 198 | // -- 4k -- 4k+2 |
| 199 | // p(u) = > C * u q(u) = > C * u |
| 200 | // -- 2k -- 2k+1 |
| 201 | // k=0 k=0 |
| 202 | // |
| 203 | // 1+u 2 |
| 204 | // 1/2 ln --- ~ u * [ p(u) + u * q(u) ] |
| 205 | // 1-u |
| 206 | // |
| 207 | */ |
| 208 | return OddPoly(x1, ln_arr, L2_ARR_SIZE); |
| 209 | } |
| 210 | |
| 211 | /* required sqrt(2)/2 < x < sqrt(2) */ |
| 212 | static float128 poly_l2(float128 x) |
| 213 | { |
| 214 | /* using float128 for approximation */ |
| 215 | float128 x_p1 = float128_add(x, float128_one); |
| 216 | float128 x_m1 = float128_sub(x, float128_one); |
| 217 | x = float128_div(x_m1, x_p1); |
| 218 | x = poly_ln(x); |
| 219 | x = float128_mul(x, float128_ln2inv2); |
| 220 | return x; |
| 221 | } |
| 222 | |
| 223 | static float128 poly_l2p1(float128 x) |
| 224 | { |
| 225 | /* using float128 for approximation */ |
| 226 | float128 x_p2 = float128_add(x, float128_two); |
| 227 | x = float128_div(x, x_p2); |
| 228 | x = poly_ln(x); |
| 229 | x = float128_mul(x, float128_ln2inv2); |
| 230 | return x; |
| 231 | } |
| 232 | |
| 233 | // ================================================= |
| 234 | // FYL2X Compute y * log (x) |
| 235 | // 2 |
| 236 | // ================================================= |
| 237 | |
| 238 | // |
| 239 | // Uses the following identities: |
| 240 | // |
| 241 | // 1. ---------------------------------------------------------- |
| 242 | // ln(x) |
| 243 | // log (x) = -------, ln (x*y) = ln(x) + ln(y) |
| 244 | // 2 ln(2) |
| 245 | // |
| 246 | // 2. ---------------------------------------------------------- |
| 247 | // 1+u x-1 |
| 248 | // ln (x) = ln -----, when u = ----- |
| 249 | // 1-u x+1 |
| 250 | // |
| 251 | // 3. ---------------------------------------------------------- |
| 252 | // 3 5 7 2n+1 |
| 253 | // 1+u u u u u |
| 254 | // ln ----- = 2 [ u + --- + --- + --- + ... + ------ + ... ] |
| 255 | // 1-u 3 5 7 2n+1 |
| 256 | // |
| 257 | |
| 258 | static floatx80 fyl2x(floatx80 a, floatx80 b) |
| 259 | { |
| 260 | UINT64 aSig = extractFloatx80Frac(a); |
| 261 | INT32 aExp = extractFloatx80Exp(a); |
| 262 | int aSign = extractFloatx80Sign(a); |
| 263 | UINT64 bSig = extractFloatx80Frac(b); |
| 264 | INT32 bExp = extractFloatx80Exp(b); |
| 265 | int bSign = extractFloatx80Sign(b); |
| 266 | |
| 267 | int zSign = bSign ^ 1; |
| 268 | |
| 269 | if (aExp == 0x7FFF) { |
| 270 | if ((UINT64) (aSig<<1) |
| 271 | || ((bExp == 0x7FFF) && (UINT64) (bSig<<1))) |
| 272 | { |
| 273 | return propagateFloatx80NaN(a, b); |
| 274 | } |
| 275 | if (aSign) |
| 276 | { |
| 277 | invalid: |
| 278 | float_raise(float_flag_invalid); |
| 279 | return floatx80_default_nan; |
| 280 | } |
| 281 | else { |
| 282 | if (bExp == 0) { |
| 283 | if (bSig == 0) goto invalid; |
| 284 | float_raise(float_flag_denormal); |
| 285 | } |
| 286 | return packFloatx80(bSign, 0x7FFF, U64(0x8000000000000000)); |
| 287 | } |
| 288 | } |
| 289 | if (bExp == 0x7FFF) |
| 290 | { |
| 291 | if ((UINT64) (bSig<<1)) return propagateFloatx80NaN(a, b); |
| 292 | if (aSign && (UINT64)(aExp | aSig)) goto invalid; |
| 293 | if (aSig && (aExp == 0)) |
| 294 | float_raise(float_flag_denormal); |
| 295 | if (aExp < 0x3FFF) { |
| 296 | return packFloatx80(zSign, 0x7FFF, U64(0x8000000000000000)); |
| 297 | } |
| 298 | if (aExp == 0x3FFF && ((UINT64) (aSig<<1) == 0)) goto invalid; |
| 299 | return packFloatx80(bSign, 0x7FFF, U64(0x8000000000000000)); |
| 300 | } |
| 301 | if (aExp == 0) { |
| 302 | if (aSig == 0) { |
| 303 | if ((bExp | bSig) == 0) goto invalid; |
| 304 | float_raise(float_flag_divbyzero); |
| 305 | return packFloatx80(zSign, 0x7FFF, U64(0x8000000000000000)); |
| 306 | } |
| 307 | if (aSign) goto invalid; |
| 308 | float_raise(float_flag_denormal); |
| 309 | normalizeFloatx80Subnormal(aSig, &aExp, &aSig); |
| 310 | } |
| 311 | if (aSign) goto invalid; |
| 312 | if (bExp == 0) { |
| 313 | if (bSig == 0) { |
| 314 | if (aExp < 0x3FFF) return packFloatx80(zSign, 0, 0); |
| 315 | return packFloatx80(bSign, 0, 0); |
| 316 | } |
| 317 | float_raise(float_flag_denormal); |
| 318 | normalizeFloatx80Subnormal(bSig, &bExp, &bSig); |
| 319 | } |
| 320 | if (aExp == 0x3FFF && ((UINT64) (aSig<<1) == 0)) |
| 321 | return packFloatx80(bSign, 0, 0); |
| 322 | |
| 323 | float_raise(float_flag_inexact); |
| 324 | |
| 325 | int ExpDiff = aExp - 0x3FFF; |
| 326 | aExp = 0; |
| 327 | if (aSig >= SQRT2_HALF_SIG) { |
| 328 | ExpDiff++; |
| 329 | aExp--; |
| 330 | } |
| 331 | |
| 332 | /* ******************************** */ |
| 333 | /* using float128 for approximation */ |
| 334 | /* ******************************** */ |
| 335 | |
| 336 | UINT64 zSig0, zSig1; |
| 337 | shift128Right(aSig<<1, 0, 16, &zSig0, &zSig1); |
| 338 | float128 x = packFloat128(0, aExp+0x3FFF, zSig0, zSig1); |
| 339 | x = poly_l2(x); |
| 340 | x = float128_add(x, int64_to_float128((INT64) ExpDiff)); |
| 341 | return floatx80_mul(b, float128_to_floatx80(x)); |
| 342 | } |
| 343 | |
| 344 | // ================================================= |
| 345 | // FYL2XP1 Compute y * log (x + 1) |
| 346 | // 2 |
| 347 | // ================================================= |
| 348 | |
| 349 | // |
| 350 | // Uses the following identities: |
| 351 | // |
| 352 | // 1. ---------------------------------------------------------- |
| 353 | // ln(x) |
| 354 | // log (x) = ------- |
| 355 | // 2 ln(2) |
| 356 | // |
| 357 | // 2. ---------------------------------------------------------- |
| 358 | // 1+u x |
| 359 | // ln (x+1) = ln -----, when u = ----- |
| 360 | // 1-u x+2 |
| 361 | // |
| 362 | // 3. ---------------------------------------------------------- |
| 363 | // 3 5 7 2n+1 |
| 364 | // 1+u u u u u |
| 365 | // ln ----- = 2 [ u + --- + --- + --- + ... + ------ + ... ] |
| 366 | // 1-u 3 5 7 2n+1 |
| 367 | // |
| 368 | |
| 369 | floatx80 fyl2xp1(floatx80 a, floatx80 b) |
| 370 | { |
| 371 | INT32 aExp, bExp; |
| 372 | UINT64 aSig, bSig, zSig0, zSig1, zSig2; |
| 373 | int aSign, bSign; |
| 374 | |
| 375 | aSig = extractFloatx80Frac(a); |
| 376 | aExp = extractFloatx80Exp(a); |
| 377 | aSign = extractFloatx80Sign(a); |
| 378 | bSig = extractFloatx80Frac(b); |
| 379 | bExp = extractFloatx80Exp(b); |
| 380 | bSign = extractFloatx80Sign(b); |
| 381 | int zSign = aSign ^ bSign; |
| 382 | |
| 383 | if (aExp == 0x7FFF) { |
| 384 | if ((UINT64) (aSig<<1) |
| 385 | || ((bExp == 0x7FFF) && (UINT64) (bSig<<1))) |
| 386 | { |
| 387 | return propagateFloatx80NaN(a, b); |
| 388 | } |
| 389 | if (aSign) |
| 390 | { |
| 391 | invalid: |
| 392 | float_raise(float_flag_invalid); |
| 393 | return floatx80_default_nan; |
| 394 | } |
| 395 | else { |
| 396 | if (bExp == 0) { |
| 397 | if (bSig == 0) goto invalid; |
| 398 | float_raise(float_flag_denormal); |
| 399 | } |
| 400 | return packFloatx80(bSign, 0x7FFF, U64(0x8000000000000000)); |
| 401 | } |
| 402 | } |
| 403 | if (bExp == 0x7FFF) |
| 404 | { |
| 405 | if ((UINT64) (bSig<<1)) |
| 406 | return propagateFloatx80NaN(a, b); |
| 407 | |
| 408 | if (aExp == 0) { |
| 409 | if (aSig == 0) goto invalid; |
| 410 | float_raise(float_flag_denormal); |
| 411 | } |
| 412 | |
| 413 | return packFloatx80(zSign, 0x7FFF, U64(0x8000000000000000)); |
| 414 | } |
| 415 | if (aExp == 0) { |
| 416 | if (aSig == 0) { |
| 417 | if (bSig && (bExp == 0)) float_raise(float_flag_denormal); |
| 418 | return packFloatx80(zSign, 0, 0); |
| 419 | } |
| 420 | float_raise(float_flag_denormal); |
| 421 | normalizeFloatx80Subnormal(aSig, &aExp, &aSig); |
| 422 | } |
| 423 | if (bExp == 0) { |
| 424 | if (bSig == 0) return packFloatx80(zSign, 0, 0); |
| 425 | float_raise(float_flag_denormal); |
| 426 | normalizeFloatx80Subnormal(bSig, &bExp, &bSig); |
| 427 | } |
| 428 | |
| 429 | float_raise(float_flag_inexact); |
| 430 | |
| 431 | if (aSign && aExp >= 0x3FFF) |
| 432 | return a; |
| 433 | |
| 434 | if (aExp >= 0x3FFC) // big argument |
| 435 | { |
| 436 | return fyl2x(floatx80_add(a, floatx80_one), b); |
| 437 | } |
| 438 | |
| 439 | // handle tiny argument |
| 440 | if (aExp < EXP_BIAS-70) |
| 441 | { |
| 442 | // first order approximation, return (a*b)/ln(2) |
| 443 | INT32 zExp = aExp + FLOAT_LN2INV_EXP - 0x3FFE; |
| 444 | |
| 445 | mul128By64To192(FLOAT_LN2INV_HI, FLOAT_LN2INV_LO, aSig, &zSig0, &zSig1, &zSig2); |
| 446 | if (0 < (INT64) zSig0) { |
| 447 | shortShift128Left(zSig0, zSig1, 1, &zSig0, &zSig1); |
| 448 | --zExp; |
| 449 | } |
| 450 | |
| 451 | zExp = zExp + bExp - 0x3FFE; |
| 452 | mul128By64To192(zSig0, zSig1, bSig, &zSig0, &zSig1, &zSig2); |
| 453 | if (0 < (INT64) zSig0) { |
| 454 | shortShift128Left(zSig0, zSig1, 1, &zSig0, &zSig1); |
| 455 | --zExp; |
| 456 | } |
| 457 | |
| 458 | return |
| 459 | roundAndPackFloatx80(80, aSign ^ bSign, zExp, zSig0, zSig1); |
| 460 | } |
| 461 | |
| 462 | /* ******************************** */ |
| 463 | /* using float128 for approximation */ |
| 464 | /* ******************************** */ |
| 465 | |
| 466 | shift128Right(aSig<<1, 0, 16, &zSig0, &zSig1); |
| 467 | float128 x = packFloat128(aSign, aExp, zSig0, zSig1); |
| 468 | x = poly_l2p1(x); |
| 469 | return floatx80_mul(b, float128_to_floatx80(x)); |
| 470 | } |
| 471 | |
| 472 | floatx80 floatx80_flognp1(floatx80 a) |
| 473 | { |
| 474 | return fyl2xp1(a, floatx80_ln_2); |
| 475 | } |
| 476 | |
| 477 | floatx80 floatx80_flogn(floatx80 a) |
| 478 | { |
| 479 | return fyl2x(a, floatx80_ln_2); |
| 480 | } |
| 481 | |
| 482 | floatx80 floatx80_flog2(floatx80 a) |
| 483 | { |
| 484 | return fyl2x(a, floatx80_one); |
| 485 | } |
| 486 | |
| 487 | floatx80 floatx80_flog10(floatx80 a) |
| 488 | { |
| 489 | return fyl2x(a, floatx80_log10_2); |
| 490 | } |