| 1 | |
| 2 | /*============================================================================ |
| 3 | |
| 4 | This C source file is part of the SoftFloat IEC/IEEE Floating-point Arithmetic |
| 5 | Package, Release 2b. |
| 6 | |
| 7 | Written by John R. Hauser. This work was made possible in part by the |
| 8 | International Computer Science Institute, located at Suite 600, 1947 Center |
| 9 | Street, Berkeley, California 94704. Funding was partially provided by the |
| 10 | National Science Foundation under grant MIP-9311980. The original version |
| 11 | of this code was written as part of a project to build a fixed-point vector |
| 12 | processor in collaboration with the University of California at Berkeley, |
| 13 | overseen by Profs. Nelson Morgan and John Wawrzynek. More information |
| 14 | is available through the Web page `http://www.cs.berkeley.edu/~jhauser/ |
| 15 | arithmetic/SoftFloat.html'. |
| 16 | |
| 17 | THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort has |
| 18 | been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT TIMES |
| 19 | RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO PERSONS |
| 20 | AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ALL LOSSES, |
| 21 | COSTS, OR OTHER PROBLEMS THEY INCUR DUE TO THE SOFTWARE, AND WHO FURTHERMORE |
| 22 | EFFECTIVELY INDEMNIFY JOHN HAUSER AND THE INTERNATIONAL COMPUTER SCIENCE |
| 23 | INSTITUTE (possibly via similar legal warning) AGAINST ALL LOSSES, COSTS, OR |
| 24 | OTHER PROBLEMS INCURRED BY THEIR CUSTOMERS AND CLIENTS DUE TO THE SOFTWARE. |
| 25 | |
| 26 | Derivative works are acceptable, even for commercial purposes, so long as |
| 27 | (1) the source code for the derivative work includes prominent notice that |
| 28 | the work is derivative, and (2) the source code includes prominent notice with |
| 29 | these four paragraphs for those parts of this code that are retained. |
| 30 | |
| 31 | =============================================================================*/ |
| 32 | |
| 33 | /* |
| 34 | WARNING: Modified for usage in Mednafen's PC-FX V810 emulation, specifically, to "wrap" the exponent(by subtracting 192 |
| 35 | from it) in the case of an overflow condition(only modified in the 32-bit float code, the 64-bit float code is unused in Mednafen), |
| 36 | rather than returning an infinity. |
| 37 | |
| 38 | TODO: Make this configurable, and add it to float64 too(subtract 1536?) |
| 39 | */ |
| 40 | |
| 41 | |
| 42 | #include "milieu.h" |
| 43 | #include "softfloat.h" |
| 44 | |
| 45 | /*---------------------------------------------------------------------------- |
| 46 | | Floating-point rounding mode and exception flags. |
| 47 | *----------------------------------------------------------------------------*/ |
| 48 | int8 float_rounding_mode = float_round_nearest_even; |
| 49 | int8 float_exception_flags = 0; |
| 50 | |
| 51 | /*---------------------------------------------------------------------------- |
| 52 | | Primitive arithmetic functions, including multi-word arithmetic, and |
| 53 | | division and square root approximations. (Can be specialized to target if |
| 54 | | desired.) |
| 55 | *----------------------------------------------------------------------------*/ |
| 56 | #include "softfloat-macros" |
| 57 | |
| 58 | /*---------------------------------------------------------------------------- |
| 59 | | Functions and definitions to determine: (1) whether tininess for underflow |
| 60 | | is detected before or after rounding by default, (2) what (if anything) |
| 61 | | happens when exceptions are raised, (3) how signaling NaNs are distinguished |
| 62 | | from quiet NaNs, (4) the default generated quiet NaNs, and (4) how NaNs |
| 63 | | are propagated from function inputs to output. These details are target- |
| 64 | | specific. |
| 65 | *----------------------------------------------------------------------------*/ |
| 66 | #include "softfloat-specialize" |
| 67 | |
| 68 | /*---------------------------------------------------------------------------- |
| 69 | | Returns the fraction bits of the single-precision floating-point value `a'. |
| 70 | *----------------------------------------------------------------------------*/ |
| 71 | |
| 72 | INLINE bits32 extractFloat32Frac( float32 a ) |
| 73 | { |
| 74 | |
| 75 | return a & 0x007FFFFF; |
| 76 | |
| 77 | } |
| 78 | |
| 79 | /*---------------------------------------------------------------------------- |
| 80 | | Returns the exponent bits of the single-precision floating-point value `a'. |
| 81 | *----------------------------------------------------------------------------*/ |
| 82 | |
| 83 | INLINE int16 extractFloat32Exp( float32 a ) |
| 84 | { |
| 85 | |
| 86 | return ( a>>23 ) & 0xFF; |
| 87 | |
| 88 | } |
| 89 | |
| 90 | /*---------------------------------------------------------------------------- |
| 91 | | Returns the sign bit of the single-precision floating-point value `a'. |
| 92 | *----------------------------------------------------------------------------*/ |
| 93 | |
| 94 | INLINE flag extractFloat32Sign( float32 a ) |
| 95 | { |
| 96 | |
| 97 | return a>>31; |
| 98 | |
| 99 | } |
| 100 | |
| 101 | /*---------------------------------------------------------------------------- |
| 102 | | Normalizes the subnormal single-precision floating-point value represented |
| 103 | | by the denormalized significand `aSig'. The normalized exponent and |
| 104 | | significand are stored at the locations pointed to by `zExpPtr' and |
| 105 | | `zSigPtr', respectively. |
| 106 | *----------------------------------------------------------------------------*/ |
| 107 | |
| 108 | static void |
| 109 | normalizeFloat32Subnormal( bits32 aSig, int16 *zExpPtr, bits32 *zSigPtr ) |
| 110 | { |
| 111 | int8 shiftCount; |
| 112 | |
| 113 | shiftCount = countLeadingZeros32( aSig ) - 8; |
| 114 | *zSigPtr = aSig<<shiftCount; |
| 115 | *zExpPtr = 1 - shiftCount; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | /*---------------------------------------------------------------------------- |
| 120 | | Packs the sign `zSign', exponent `zExp', and significand `zSig' into a |
| 121 | | single-precision floating-point value, returning the result. After being |
| 122 | | shifted into the proper positions, the three fields are simply added |
| 123 | | together to form the result. This means that any integer portion of `zSig' |
| 124 | | will be added into the exponent. Since a properly normalized significand |
| 125 | | will have an integer portion equal to 1, the `zExp' input should be 1 less |
| 126 | | than the desired result exponent whenever `zSig' is a complete, normalized |
| 127 | | significand. |
| 128 | *----------------------------------------------------------------------------*/ |
| 129 | |
| 130 | INLINE float32 packFloat32( flag zSign, int16 zExp, bits32 zSig ) |
| 131 | { |
| 132 | |
| 133 | return ( ( (bits32) zSign )<<31 ) + ( ( (bits32) zExp )<<23 ) + zSig; |
| 134 | |
| 135 | } |
| 136 | |
| 137 | /*---------------------------------------------------------------------------- |
| 138 | | Takes an abstract floating-point value having sign `zSign', exponent `zExp', |
| 139 | | and significand `zSig', and returns the proper single-precision floating- |
| 140 | | point value corresponding to the abstract input. Ordinarily, the abstract |
| 141 | | value is simply rounded and packed into the single-precision format, with |
| 142 | | the inexact exception raised if the abstract input cannot be represented |
| 143 | | exactly. However, if the abstract value is too large, the overflow and |
| 144 | | inexact exceptions are raised and an infinity or maximal finite value is |
| 145 | | returned. If the abstract value is too small, the input value is rounded to |
| 146 | | a subnormal number, and the underflow and inexact exceptions are raised if |
| 147 | | the abstract input cannot be represented exactly as a subnormal single- |
| 148 | | precision floating-point number. |
| 149 | | The input significand `zSig' has its binary point between bits 30 |
| 150 | | and 29, which is 7 bits to the left of the usual location. This shifted |
| 151 | | significand must be normalized or smaller. If `zSig' is not normalized, |
| 152 | | `zExp' must be 0; in that case, the result returned is a subnormal number, |
| 153 | | and it must not require rounding. In the usual case that `zSig' is |
| 154 | | normalized, `zExp' must be 1 less than the ``true'' floating-point exponent. |
| 155 | | The handling of underflow and overflow follows the IEC/IEEE Standard for |
| 156 | | Binary Floating-Point Arithmetic. |
| 157 | *----------------------------------------------------------------------------*/ |
| 158 | |
| 159 | static float32 roundAndPackFloat32( flag zSign, int16 zExp, bits32 zSig ) |
| 160 | { |
| 161 | int8 roundingMode; |
| 162 | flag roundNearestEven; |
| 163 | int8 roundIncrement, roundBits; |
| 164 | flag isTiny; |
| 165 | |
| 166 | roundingMode = float_rounding_mode; |
| 167 | roundNearestEven = roundingMode == float_round_nearest_even; |
| 168 | roundIncrement = 0x40; |
| 169 | if ( ! roundNearestEven ) { |
| 170 | if ( roundingMode == float_round_to_zero ) { |
| 171 | roundIncrement = 0; |
| 172 | } |
| 173 | else { |
| 174 | roundIncrement = 0x7F; |
| 175 | if ( zSign ) { |
| 176 | if ( roundingMode == float_round_up ) roundIncrement = 0; |
| 177 | } |
| 178 | else { |
| 179 | if ( roundingMode == float_round_down ) roundIncrement = 0; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | roundBits = zSig & 0x7F; |
| 184 | if ( 0xFD <= (bits16) zExp ) { |
| 185 | if ( ( 0xFD < zExp ) |
| 186 | || ( ( zExp == 0xFD ) |
| 187 | && ( (sbits32) ( zSig + roundIncrement ) < 0 ) ) |
| 188 | ) { |
| 189 | // Mednafen hack |
| 190 | //float_raise( float_flag_overflow | float_flag_inexact ); |
| 191 | float_raise( float_flag_overflow ); |
| 192 | |
| 193 | // Mednafen hack |
| 194 | zExp -= 192; |
| 195 | //return packFloat32( zSign, 0xFF, 0 ) - ( roundIncrement == 0 ); |
| 196 | } |
| 197 | if ( zExp < 0 ) { |
| 198 | isTiny = |
| 199 | ( float_detect_tininess == float_tininess_before_rounding ) |
| 200 | || ( zExp < -1 ) |
| 201 | || ( zSig + roundIncrement < 0x80000000 ); |
| 202 | shift32RightJamming( zSig, - zExp, &zSig ); |
| 203 | zExp = 0; |
| 204 | roundBits = zSig & 0x7F; |
| 205 | if ( isTiny && roundBits ) float_raise( float_flag_underflow ); |
| 206 | } |
| 207 | } |
| 208 | if ( roundBits ) float_exception_flags |= float_flag_inexact; |
| 209 | zSig = ( zSig + roundIncrement )>>7; |
| 210 | zSig &= ~ ( ( ( roundBits ^ 0x40 ) == 0 ) & roundNearestEven ); |
| 211 | if ( zSig == 0 ) zExp = 0; |
| 212 | return packFloat32( zSign, zExp, zSig ); |
| 213 | |
| 214 | } |
| 215 | |
| 216 | /*---------------------------------------------------------------------------- |
| 217 | | Takes an abstract floating-point value having sign `zSign', exponent `zExp', |
| 218 | | and significand `zSig', and returns the proper single-precision floating- |
| 219 | | point value corresponding to the abstract input. This routine is just like |
| 220 | | `roundAndPackFloat32' except that `zSig' does not have to be normalized. |
| 221 | | Bit 31 of `zSig' must be zero, and `zExp' must be 1 less than the ``true'' |
| 222 | | floating-point exponent. |
| 223 | *----------------------------------------------------------------------------*/ |
| 224 | |
| 225 | static float32 |
| 226 | normalizeRoundAndPackFloat32( flag zSign, int16 zExp, bits32 zSig ) |
| 227 | { |
| 228 | int8 shiftCount; |
| 229 | |
| 230 | shiftCount = countLeadingZeros32( zSig ) - 1; |
| 231 | return roundAndPackFloat32( zSign, zExp - shiftCount, zSig<<shiftCount ); |
| 232 | |
| 233 | } |
| 234 | |
| 235 | /*---------------------------------------------------------------------------- |
| 236 | | Returns the least-significant 32 fraction bits of the double-precision |
| 237 | | floating-point value `a'. |
| 238 | *----------------------------------------------------------------------------*/ |
| 239 | |
| 240 | INLINE bits32 extractFloat64Frac1( float64 a ) |
| 241 | { |
| 242 | |
| 243 | return a.low; |
| 244 | |
| 245 | } |
| 246 | |
| 247 | /*---------------------------------------------------------------------------- |
| 248 | | Returns the most-significant 20 fraction bits of the double-precision |
| 249 | | floating-point value `a'. |
| 250 | *----------------------------------------------------------------------------*/ |
| 251 | |
| 252 | INLINE bits32 extractFloat64Frac0( float64 a ) |
| 253 | { |
| 254 | |
| 255 | return a.high & 0x000FFFFF; |
| 256 | |
| 257 | } |
| 258 | |
| 259 | /*---------------------------------------------------------------------------- |
| 260 | | Returns the exponent bits of the double-precision floating-point value `a'. |
| 261 | *----------------------------------------------------------------------------*/ |
| 262 | |
| 263 | INLINE int16 extractFloat64Exp( float64 a ) |
| 264 | { |
| 265 | |
| 266 | return ( a.high>>20 ) & 0x7FF; |
| 267 | |
| 268 | } |
| 269 | |
| 270 | /*---------------------------------------------------------------------------- |
| 271 | | Returns the sign bit of the double-precision floating-point value `a'. |
| 272 | *----------------------------------------------------------------------------*/ |
| 273 | |
| 274 | INLINE flag extractFloat64Sign( float64 a ) |
| 275 | { |
| 276 | |
| 277 | return a.high>>31; |
| 278 | |
| 279 | } |
| 280 | |
| 281 | /*---------------------------------------------------------------------------- |
| 282 | | Normalizes the subnormal double-precision floating-point value represented |
| 283 | | by the denormalized significand formed by the concatenation of `aSig0' and |
| 284 | | `aSig1'. The normalized exponent is stored at the location pointed to by |
| 285 | | `zExpPtr'. The most significant 21 bits of the normalized significand are |
| 286 | | stored at the location pointed to by `zSig0Ptr', and the least significant |
| 287 | | 32 bits of the normalized significand are stored at the location pointed to |
| 288 | | by `zSig1Ptr'. |
| 289 | *----------------------------------------------------------------------------*/ |
| 290 | |
| 291 | static void |
| 292 | normalizeFloat64Subnormal( |
| 293 | bits32 aSig0, |
| 294 | bits32 aSig1, |
| 295 | int16 *zExpPtr, |
| 296 | bits32 *zSig0Ptr, |
| 297 | bits32 *zSig1Ptr |
| 298 | ) |
| 299 | { |
| 300 | int8 shiftCount; |
| 301 | |
| 302 | if ( aSig0 == 0 ) { |
| 303 | shiftCount = countLeadingZeros32( aSig1 ) - 11; |
| 304 | if ( shiftCount < 0 ) { |
| 305 | *zSig0Ptr = aSig1>>( - shiftCount ); |
| 306 | *zSig1Ptr = aSig1<<( shiftCount & 31 ); |
| 307 | } |
| 308 | else { |
| 309 | *zSig0Ptr = aSig1<<shiftCount; |
| 310 | *zSig1Ptr = 0; |
| 311 | } |
| 312 | *zExpPtr = - shiftCount - 31; |
| 313 | } |
| 314 | else { |
| 315 | shiftCount = countLeadingZeros32( aSig0 ) - 11; |
| 316 | shortShift64Left( aSig0, aSig1, shiftCount, zSig0Ptr, zSig1Ptr ); |
| 317 | *zExpPtr = 1 - shiftCount; |
| 318 | } |
| 319 | |
| 320 | } |
| 321 | |
| 322 | /*---------------------------------------------------------------------------- |
| 323 | | Packs the sign `zSign', the exponent `zExp', and the significand formed by |
| 324 | | the concatenation of `zSig0' and `zSig1' into a double-precision floating- |
| 325 | | point value, returning the result. After being shifted into the proper |
| 326 | | positions, the three fields `zSign', `zExp', and `zSig0' are simply added |
| 327 | | together to form the most significant 32 bits of the result. This means |
| 328 | | that any integer portion of `zSig0' will be added into the exponent. Since |
| 329 | | a properly normalized significand will have an integer portion equal to 1, |
| 330 | | the `zExp' input should be 1 less than the desired result exponent whenever |
| 331 | | `zSig0' and `zSig1' concatenated form a complete, normalized significand. |
| 332 | *----------------------------------------------------------------------------*/ |
| 333 | |
| 334 | INLINE float64 |
| 335 | packFloat64( flag zSign, int16 zExp, bits32 zSig0, bits32 zSig1 ) |
| 336 | { |
| 337 | float64 z; |
| 338 | |
| 339 | z.low = zSig1; |
| 340 | z.high = ( ( (bits32) zSign )<<31 ) + ( ( (bits32) zExp )<<20 ) + zSig0; |
| 341 | return z; |
| 342 | |
| 343 | } |
| 344 | |
| 345 | /*---------------------------------------------------------------------------- |
| 346 | | Takes an abstract floating-point value having sign `zSign', exponent `zExp', |
| 347 | | and extended significand formed by the concatenation of `zSig0', `zSig1', |
| 348 | | and `zSig2', and returns the proper double-precision floating-point value |
| 349 | | corresponding to the abstract input. Ordinarily, the abstract value is |
| 350 | | simply rounded and packed into the double-precision format, with the inexact |
| 351 | | exception raised if the abstract input cannot be represented exactly. |
| 352 | | However, if the abstract value is too large, the overflow and inexact |
| 353 | | exceptions are raised and an infinity or maximal finite value is returned. |
| 354 | | If the abstract value is too small, the input value is rounded to a |
| 355 | | subnormal number, and the underflow and inexact exceptions are raised if the |
| 356 | | abstract input cannot be represented exactly as a subnormal double-precision |
| 357 | | floating-point number. |
| 358 | | The input significand must be normalized or smaller. If the input |
| 359 | | significand is not normalized, `zExp' must be 0; in that case, the result |
| 360 | | returned is a subnormal number, and it must not require rounding. In the |
| 361 | | usual case that the input significand is normalized, `zExp' must be 1 less |
| 362 | | than the ``true'' floating-point exponent. The handling of underflow and |
| 363 | | overflow follows the IEC/IEEE Standard for Binary Floating-Point Arithmetic. |
| 364 | *----------------------------------------------------------------------------*/ |
| 365 | |
| 366 | static float64 |
| 367 | roundAndPackFloat64( |
| 368 | flag zSign, int16 zExp, bits32 zSig0, bits32 zSig1, bits32 zSig2 ) |
| 369 | { |
| 370 | int8 roundingMode; |
| 371 | flag roundNearestEven, increment, isTiny; |
| 372 | |
| 373 | roundingMode = float_rounding_mode; |
| 374 | roundNearestEven = ( roundingMode == float_round_nearest_even ); |
| 375 | increment = ( (sbits32) zSig2 < 0 ); |
| 376 | if ( ! roundNearestEven ) { |
| 377 | if ( roundingMode == float_round_to_zero ) { |
| 378 | increment = 0; |
| 379 | } |
| 380 | else { |
| 381 | if ( zSign ) { |
| 382 | increment = ( roundingMode == float_round_down ) && zSig2; |
| 383 | } |
| 384 | else { |
| 385 | increment = ( roundingMode == float_round_up ) && zSig2; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | if ( 0x7FD <= (bits16) zExp ) { |
| 390 | if ( ( 0x7FD < zExp ) |
| 391 | || ( ( zExp == 0x7FD ) |
| 392 | && eq64( 0x001FFFFF, 0xFFFFFFFF, zSig0, zSig1 ) |
| 393 | && increment |
| 394 | ) |
| 395 | ) { |
| 396 | float_raise( float_flag_overflow | float_flag_inexact ); |
| 397 | if ( ( roundingMode == float_round_to_zero ) |
| 398 | || ( zSign && ( roundingMode == float_round_up ) ) |
| 399 | || ( ! zSign && ( roundingMode == float_round_down ) ) |
| 400 | ) { |
| 401 | return packFloat64( zSign, 0x7FE, 0x000FFFFF, 0xFFFFFFFF ); |
| 402 | } |
| 403 | return packFloat64( zSign, 0x7FF, 0, 0 ); |
| 404 | } |
| 405 | if ( zExp < 0 ) { |
| 406 | isTiny = |
| 407 | ( float_detect_tininess == float_tininess_before_rounding ) |
| 408 | || ( zExp < -1 ) |
| 409 | || ! increment |
| 410 | || lt64( zSig0, zSig1, 0x001FFFFF, 0xFFFFFFFF ); |
| 411 | shift64ExtraRightJamming( |
| 412 | zSig0, zSig1, zSig2, - zExp, &zSig0, &zSig1, &zSig2 ); |
| 413 | zExp = 0; |
| 414 | if ( isTiny && zSig2 ) float_raise( float_flag_underflow ); |
| 415 | if ( roundNearestEven ) { |
| 416 | increment = ( (sbits32) zSig2 < 0 ); |
| 417 | } |
| 418 | else { |
| 419 | if ( zSign ) { |
| 420 | increment = ( roundingMode == float_round_down ) && zSig2; |
| 421 | } |
| 422 | else { |
| 423 | increment = ( roundingMode == float_round_up ) && zSig2; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | if ( zSig2 ) float_exception_flags |= float_flag_inexact; |
| 429 | if ( increment ) { |
| 430 | add64( zSig0, zSig1, 0, 1, &zSig0, &zSig1 ); |
| 431 | zSig1 &= ~ ( ( zSig2 + zSig2 == 0 ) & roundNearestEven ); |
| 432 | } |
| 433 | else { |
| 434 | if ( ( zSig0 | zSig1 ) == 0 ) zExp = 0; |
| 435 | } |
| 436 | return packFloat64( zSign, zExp, zSig0, zSig1 ); |
| 437 | |
| 438 | } |
| 439 | |
| 440 | /*---------------------------------------------------------------------------- |
| 441 | | Takes an abstract floating-point value having sign `zSign', exponent `zExp', |
| 442 | | and significand formed by the concatenation of `zSig0' and `zSig1', and |
| 443 | | returns the proper double-precision floating-point value corresponding |
| 444 | | to the abstract input. This routine is just like `roundAndPackFloat64' |
| 445 | | except that the input significand has fewer bits and does not have to be |
| 446 | | normalized. In all cases, `zExp' must be 1 less than the ``true'' floating- |
| 447 | | point exponent. |
| 448 | *----------------------------------------------------------------------------*/ |
| 449 | |
| 450 | static float64 |
| 451 | normalizeRoundAndPackFloat64( |
| 452 | flag zSign, int16 zExp, bits32 zSig0, bits32 zSig1 ) |
| 453 | { |
| 454 | int8 shiftCount; |
| 455 | bits32 zSig2; |
| 456 | |
| 457 | if ( zSig0 == 0 ) { |
| 458 | zSig0 = zSig1; |
| 459 | zSig1 = 0; |
| 460 | zExp -= 32; |
| 461 | } |
| 462 | shiftCount = countLeadingZeros32( zSig0 ) - 11; |
| 463 | if ( 0 <= shiftCount ) { |
| 464 | zSig2 = 0; |
| 465 | shortShift64Left( zSig0, zSig1, shiftCount, &zSig0, &zSig1 ); |
| 466 | } |
| 467 | else { |
| 468 | shift64ExtraRightJamming( |
| 469 | zSig0, zSig1, 0, - shiftCount, &zSig0, &zSig1, &zSig2 ); |
| 470 | } |
| 471 | zExp -= shiftCount; |
| 472 | return roundAndPackFloat64( zSign, zExp, zSig0, zSig1, zSig2 ); |
| 473 | |
| 474 | } |
| 475 | |
| 476 | /*---------------------------------------------------------------------------- |
| 477 | | Returns the result of converting the 32-bit two's complement integer `a' to |
| 478 | | the single-precision floating-point format. The conversion is performed |
| 479 | | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic. |
| 480 | *----------------------------------------------------------------------------*/ |
| 481 | |
| 482 | float32 int32_to_float32( int32 a ) |
| 483 | { |
| 484 | flag zSign; |
| 485 | |
| 486 | if ( a == 0 ) return 0; |
| 487 | if ( a == (sbits32) 0x80000000 ) return packFloat32( 1, 0x9E, 0 ); |
| 488 | zSign = ( a < 0 ); |
| 489 | return normalizeRoundAndPackFloat32( zSign, 0x9C, zSign ? - a : a ); |
| 490 | |
| 491 | } |
| 492 | |
| 493 | /*---------------------------------------------------------------------------- |
| 494 | | Returns the result of converting the 32-bit two's complement integer `a' to |
| 495 | | the double-precision floating-point format. The conversion is performed |
| 496 | | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic. |
| 497 | *----------------------------------------------------------------------------*/ |
| 498 | |
| 499 | float64 int32_to_float64( int32 a ) |
| 500 | { |
| 501 | flag zSign; |
| 502 | bits32 absA; |
| 503 | int8 shiftCount; |
| 504 | bits32 zSig0, zSig1; |
| 505 | |
| 506 | if ( a == 0 ) return packFloat64( 0, 0, 0, 0 ); |
| 507 | zSign = ( a < 0 ); |
| 508 | absA = zSign ? - a : a; |
| 509 | shiftCount = countLeadingZeros32( absA ) - 11; |
| 510 | if ( 0 <= shiftCount ) { |
| 511 | zSig0 = absA<<shiftCount; |
| 512 | zSig1 = 0; |
| 513 | } |
| 514 | else { |
| 515 | shift64Right( absA, 0, - shiftCount, &zSig0, &zSig1 ); |
| 516 | } |
| 517 | return packFloat64( zSign, 0x412 - shiftCount, zSig0, zSig1 ); |
| 518 | |
| 519 | } |
| 520 | |
| 521 | /*---------------------------------------------------------------------------- |
| 522 | | Returns the result of converting the single-precision floating-point value |
| 523 | | `a' to the 32-bit two's complement integer format. The conversion is |
| 524 | | performed according to the IEC/IEEE Standard for Binary Floating-Point |
| 525 | | Arithmetic---which means in particular that the conversion is rounded |
| 526 | | according to the current rounding mode. If `a' is a NaN, the largest |
| 527 | | positive integer is returned. Otherwise, if the conversion overflows, the |
| 528 | | largest integer with the same sign as `a' is returned. |
| 529 | *----------------------------------------------------------------------------*/ |
| 530 | |
| 531 | int32 float32_to_int32( float32 a ) |
| 532 | { |
| 533 | flag aSign; |
| 534 | int16 aExp, shiftCount; |
| 535 | bits32 aSig, aSigExtra; |
| 536 | int32 z; |
| 537 | int8 roundingMode; |
| 538 | |
| 539 | aSig = extractFloat32Frac( a ); |
| 540 | aExp = extractFloat32Exp( a ); |
| 541 | aSign = extractFloat32Sign( a ); |
| 542 | shiftCount = aExp - 0x96; |
| 543 | if ( 0 <= shiftCount ) { |
| 544 | if ( 0x9E <= aExp ) { |
| 545 | if ( a != 0xCF000000 ) { |
| 546 | float_raise( float_flag_invalid ); |
| 547 | if ( ! aSign || ( ( aExp == 0xFF ) && aSig ) ) { |
| 548 | return 0x7FFFFFFF; |
| 549 | } |
| 550 | } |
| 551 | return (sbits32) 0x80000000; |
| 552 | } |
| 553 | z = ( aSig | 0x00800000 )<<shiftCount; |
| 554 | if ( aSign ) z = - z; |
| 555 | } |
| 556 | else { |
| 557 | if ( aExp < 0x7E ) { |
| 558 | aSigExtra = aExp | aSig; |
| 559 | z = 0; |
| 560 | } |
| 561 | else { |
| 562 | aSig |= 0x00800000; |
| 563 | aSigExtra = aSig<<( shiftCount & 31 ); |
| 564 | z = aSig>>( - shiftCount ); |
| 565 | } |
| 566 | if ( aSigExtra ) float_exception_flags |= float_flag_inexact; |
| 567 | roundingMode = float_rounding_mode; |
| 568 | if ( roundingMode == float_round_nearest_even ) { |
| 569 | if ( (sbits32) aSigExtra < 0 ) { |
| 570 | ++z; |
| 571 | if ( (bits32) ( aSigExtra<<1 ) == 0 ) z &= ~1; |
| 572 | } |
| 573 | if ( aSign ) z = - z; |
| 574 | } |
| 575 | else { |
| 576 | aSigExtra = ( aSigExtra != 0 ); |
| 577 | if ( aSign ) { |
| 578 | z += ( roundingMode == float_round_down ) & aSigExtra; |
| 579 | z = - z; |
| 580 | } |
| 581 | else { |
| 582 | z += ( roundingMode == float_round_up ) & aSigExtra; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | return z; |
| 587 | |
| 588 | } |
| 589 | |
| 590 | /*---------------------------------------------------------------------------- |
| 591 | | Returns the result of converting the single-precision floating-point value |
| 592 | | `a' to the 32-bit two's complement integer format. The conversion is |
| 593 | | performed according to the IEC/IEEE Standard for Binary Floating-Point |
| 594 | | Arithmetic, except that the conversion is always rounded toward zero. |
| 595 | | If `a' is a NaN, the largest positive integer is returned. Otherwise, if |
| 596 | | the conversion overflows, the largest integer with the same sign as `a' is |
| 597 | | returned. |
| 598 | *----------------------------------------------------------------------------*/ |
| 599 | |
| 600 | int32 float32_to_int32_round_to_zero( float32 a ) |
| 601 | { |
| 602 | flag aSign; |
| 603 | int16 aExp, shiftCount; |
| 604 | bits32 aSig; |
| 605 | int32 z; |
| 606 | |
| 607 | aSig = extractFloat32Frac( a ); |
| 608 | aExp = extractFloat32Exp( a ); |
| 609 | aSign = extractFloat32Sign( a ); |
| 610 | shiftCount = aExp - 0x9E; |
| 611 | if ( 0 <= shiftCount ) { |
| 612 | if ( a != 0xCF000000 ) { |
| 613 | float_raise( float_flag_invalid ); |
| 614 | if ( ! aSign || ( ( aExp == 0xFF ) && aSig ) ) return 0x7FFFFFFF; |
| 615 | } |
| 616 | return (sbits32) 0x80000000; |
| 617 | } |
| 618 | else if ( aExp <= 0x7E ) { |
| 619 | if ( aExp | aSig ) float_exception_flags |= float_flag_inexact; |
| 620 | return 0; |
| 621 | } |
| 622 | aSig = ( aSig | 0x00800000 )<<8; |
| 623 | z = aSig>>( - shiftCount ); |
| 624 | if ( (bits32) ( aSig<<( shiftCount & 31 ) ) ) { |
| 625 | float_exception_flags |= float_flag_inexact; |
| 626 | } |
| 627 | if ( aSign ) z = - z; |
| 628 | return z; |
| 629 | |
| 630 | } |
| 631 | |
| 632 | /*---------------------------------------------------------------------------- |
| 633 | | Returns the result of converting the single-precision floating-point value |
| 634 | | `a' to the double-precision floating-point format. The conversion is |
| 635 | | performed according to the IEC/IEEE Standard for Binary Floating-Point |
| 636 | | Arithmetic. |
| 637 | *----------------------------------------------------------------------------*/ |
| 638 | |
| 639 | float64 float32_to_float64( float32 a ) |
| 640 | { |
| 641 | flag aSign; |
| 642 | int16 aExp; |
| 643 | bits32 aSig, zSig0, zSig1; |
| 644 | |
| 645 | aSig = extractFloat32Frac( a ); |
| 646 | aExp = extractFloat32Exp( a ); |
| 647 | aSign = extractFloat32Sign( a ); |
| 648 | if ( aExp == 0xFF ) { |
| 649 | if ( aSig ) return commonNaNToFloat64( float32ToCommonNaN( a ) ); |
| 650 | return packFloat64( aSign, 0x7FF, 0, 0 ); |
| 651 | } |
| 652 | if ( aExp == 0 ) { |
| 653 | if ( aSig == 0 ) return packFloat64( aSign, 0, 0, 0 ); |
| 654 | normalizeFloat32Subnormal( aSig, &aExp, &aSig ); |
| 655 | --aExp; |
| 656 | } |
| 657 | shift64Right( aSig, 0, 3, &zSig0, &zSig1 ); |
| 658 | return packFloat64( aSign, aExp + 0x380, zSig0, zSig1 ); |
| 659 | |
| 660 | } |
| 661 | |
| 662 | /*---------------------------------------------------------------------------- |
| 663 | | Rounds the single-precision floating-point value `a' to an integer, |
| 664 | | and returns the result as a single-precision floating-point value. The |
| 665 | | operation is performed according to the IEC/IEEE Standard for Binary |
| 666 | | Floating-Point Arithmetic. |
| 667 | *----------------------------------------------------------------------------*/ |
| 668 | |
| 669 | float32 float32_round_to_int( float32 a ) |
| 670 | { |
| 671 | flag aSign; |
| 672 | int16 aExp; |
| 673 | bits32 lastBitMask, roundBitsMask; |
| 674 | int8 roundingMode; |
| 675 | float32 z; |
| 676 | |
| 677 | aExp = extractFloat32Exp( a ); |
| 678 | if ( 0x96 <= aExp ) { |
| 679 | if ( ( aExp == 0xFF ) && extractFloat32Frac( a ) ) { |
| 680 | return propagateFloat32NaN( a, a ); |
| 681 | } |
| 682 | return a; |
| 683 | } |
| 684 | if ( aExp <= 0x7E ) { |
| 685 | if ( (bits32) ( a<<1 ) == 0 ) return a; |
| 686 | float_exception_flags |= float_flag_inexact; |
| 687 | aSign = extractFloat32Sign( a ); |
| 688 | switch ( float_rounding_mode ) { |
| 689 | case float_round_nearest_even: |
| 690 | if ( ( aExp == 0x7E ) && extractFloat32Frac( a ) ) { |
| 691 | return packFloat32( aSign, 0x7F, 0 ); |
| 692 | } |
| 693 | break; |
| 694 | case float_round_down: |
| 695 | return aSign ? 0xBF800000 : 0; |
| 696 | case float_round_up: |
| 697 | return aSign ? 0x80000000 : 0x3F800000; |
| 698 | } |
| 699 | return packFloat32( aSign, 0, 0 ); |
| 700 | } |
| 701 | lastBitMask = 1; |
| 702 | lastBitMask <<= 0x96 - aExp; |
| 703 | roundBitsMask = lastBitMask - 1; |
| 704 | z = a; |
| 705 | roundingMode = float_rounding_mode; |
| 706 | if ( roundingMode == float_round_nearest_even ) { |
| 707 | z += lastBitMask>>1; |
| 708 | if ( ( z & roundBitsMask ) == 0 ) z &= ~ lastBitMask; |
| 709 | } |
| 710 | else if ( roundingMode != float_round_to_zero ) { |
| 711 | if ( extractFloat32Sign( z ) ^ ( roundingMode == float_round_up ) ) { |
| 712 | z += roundBitsMask; |
| 713 | } |
| 714 | } |
| 715 | z &= ~ roundBitsMask; |
| 716 | if ( z != a ) float_exception_flags |= float_flag_inexact; |
| 717 | return z; |
| 718 | |
| 719 | } |
| 720 | |
| 721 | /*---------------------------------------------------------------------------- |
| 722 | | Returns the result of adding the absolute values of the single-precision |
| 723 | | floating-point values `a' and `b'. If `zSign' is 1, the sum is negated |
| 724 | | before being returned. `zSign' is ignored if the result is a NaN. |
| 725 | | The addition is performed according to the IEC/IEEE Standard for Binary |
| 726 | | Floating-Point Arithmetic. |
| 727 | *----------------------------------------------------------------------------*/ |
| 728 | |
| 729 | static float32 addFloat32Sigs( float32 a, float32 b, flag zSign ) |
| 730 | { |
| 731 | int16 aExp, bExp, zExp; |
| 732 | bits32 aSig, bSig, zSig; |
| 733 | int16 expDiff; |
| 734 | |
| 735 | aSig = extractFloat32Frac( a ); |
| 736 | aExp = extractFloat32Exp( a ); |
| 737 | bSig = extractFloat32Frac( b ); |
| 738 | bExp = extractFloat32Exp( b ); |
| 739 | expDiff = aExp - bExp; |
| 740 | aSig <<= 6; |
| 741 | bSig <<= 6; |
| 742 | if ( 0 < expDiff ) { |
| 743 | if ( aExp == 0xFF ) { |
| 744 | if ( aSig ) return propagateFloat32NaN( a, b ); |
| 745 | return a; |
| 746 | } |
| 747 | if ( bExp == 0 ) { |
| 748 | --expDiff; |
| 749 | } |
| 750 | else { |
| 751 | bSig |= 0x20000000; |
| 752 | } |
| 753 | shift32RightJamming( bSig, expDiff, &bSig ); |
| 754 | zExp = aExp; |
| 755 | } |
| 756 | else if ( expDiff < 0 ) { |
| 757 | if ( bExp == 0xFF ) { |
| 758 | if ( bSig ) return propagateFloat32NaN( a, b ); |
| 759 | return packFloat32( zSign, 0xFF, 0 ); |
| 760 | } |
| 761 | if ( aExp == 0 ) { |
| 762 | ++expDiff; |
| 763 | } |
| 764 | else { |
| 765 | aSig |= 0x20000000; |
| 766 | } |
| 767 | shift32RightJamming( aSig, - expDiff, &aSig ); |
| 768 | zExp = bExp; |
| 769 | } |
| 770 | else { |
| 771 | if ( aExp == 0xFF ) { |
| 772 | if ( aSig | bSig ) return propagateFloat32NaN( a, b ); |
| 773 | return a; |
| 774 | } |
| 775 | if ( aExp == 0 ) return packFloat32( zSign, 0, ( aSig + bSig )>>6 ); |
| 776 | zSig = 0x40000000 + aSig + bSig; |
| 777 | zExp = aExp; |
| 778 | goto roundAndPack; |
| 779 | } |
| 780 | aSig |= 0x20000000; |
| 781 | zSig = ( aSig + bSig )<<1; |
| 782 | --zExp; |
| 783 | if ( (sbits32) zSig < 0 ) { |
| 784 | zSig = aSig + bSig; |
| 785 | ++zExp; |
| 786 | } |
| 787 | roundAndPack: |
| 788 | return roundAndPackFloat32( zSign, zExp, zSig ); |
| 789 | |
| 790 | } |
| 791 | |
| 792 | /*---------------------------------------------------------------------------- |
| 793 | | Returns the result of subtracting the absolute values of the single- |
| 794 | | precision floating-point values `a' and `b'. If `zSign' is 1, the |
| 795 | | difference is negated before being returned. `zSign' is ignored if the |
| 796 | | result is a NaN. The subtraction is performed according to the IEC/IEEE |
| 797 | | Standard for Binary Floating-Point Arithmetic. |
| 798 | *----------------------------------------------------------------------------*/ |
| 799 | |
| 800 | static float32 subFloat32Sigs( float32 a, float32 b, flag zSign ) |
| 801 | { |
| 802 | int16 aExp, bExp, zExp; |
| 803 | bits32 aSig, bSig, zSig; |
| 804 | int16 expDiff; |
| 805 | |
| 806 | aSig = extractFloat32Frac( a ); |
| 807 | aExp = extractFloat32Exp( a ); |
| 808 | bSig = extractFloat32Frac( b ); |
| 809 | bExp = extractFloat32Exp( b ); |
| 810 | expDiff = aExp - bExp; |
| 811 | aSig <<= 7; |
| 812 | bSig <<= 7; |
| 813 | if ( 0 < expDiff ) goto aExpBigger; |
| 814 | if ( expDiff < 0 ) goto bExpBigger; |
| 815 | if ( aExp == 0xFF ) { |
| 816 | if ( aSig | bSig ) return propagateFloat32NaN( a, b ); |
| 817 | float_raise( float_flag_invalid ); |
| 818 | return float32_default_nan; |
| 819 | } |
| 820 | if ( aExp == 0 ) { |
| 821 | aExp = 1; |
| 822 | bExp = 1; |
| 823 | } |
| 824 | if ( bSig < aSig ) goto aBigger; |
| 825 | if ( aSig < bSig ) goto bBigger; |
| 826 | return packFloat32( float_rounding_mode == float_round_down, 0, 0 ); |
| 827 | bExpBigger: |
| 828 | if ( bExp == 0xFF ) { |
| 829 | if ( bSig ) return propagateFloat32NaN( a, b ); |
| 830 | return packFloat32( zSign ^ 1, 0xFF, 0 ); |
| 831 | } |
| 832 | if ( aExp == 0 ) { |
| 833 | ++expDiff; |
| 834 | } |
| 835 | else { |
| 836 | aSig |= 0x40000000; |
| 837 | } |
| 838 | shift32RightJamming( aSig, - expDiff, &aSig ); |
| 839 | bSig |= 0x40000000; |
| 840 | bBigger: |
| 841 | zSig = bSig - aSig; |
| 842 | zExp = bExp; |
| 843 | zSign ^= 1; |
| 844 | goto normalizeRoundAndPack; |
| 845 | aExpBigger: |
| 846 | if ( aExp == 0xFF ) { |
| 847 | if ( aSig ) return propagateFloat32NaN( a, b ); |
| 848 | return a; |
| 849 | } |
| 850 | if ( bExp == 0 ) { |
| 851 | --expDiff; |
| 852 | } |
| 853 | else { |
| 854 | bSig |= 0x40000000; |
| 855 | } |
| 856 | shift32RightJamming( bSig, expDiff, &bSig ); |
| 857 | aSig |= 0x40000000; |
| 858 | aBigger: |
| 859 | zSig = aSig - bSig; |
| 860 | zExp = aExp; |
| 861 | normalizeRoundAndPack: |
| 862 | --zExp; |
| 863 | return normalizeRoundAndPackFloat32( zSign, zExp, zSig ); |
| 864 | |
| 865 | } |
| 866 | |
| 867 | /*---------------------------------------------------------------------------- |
| 868 | | Returns the result of adding the single-precision floating-point values `a' |
| 869 | | and `b'. The operation is performed according to the IEC/IEEE Standard for |
| 870 | | Binary Floating-Point Arithmetic. |
| 871 | *----------------------------------------------------------------------------*/ |
| 872 | |
| 873 | float32 float32_add( float32 a, float32 b ) |
| 874 | { |
| 875 | flag aSign, bSign; |
| 876 | |
| 877 | aSign = extractFloat32Sign( a ); |
| 878 | bSign = extractFloat32Sign( b ); |
| 879 | if ( aSign == bSign ) { |
| 880 | return addFloat32Sigs( a, b, aSign ); |
| 881 | } |
| 882 | else { |
| 883 | return subFloat32Sigs( a, b, aSign ); |
| 884 | } |
| 885 | |
| 886 | } |
| 887 | |
| 888 | /*---------------------------------------------------------------------------- |
| 889 | | Returns the result of subtracting the single-precision floating-point values |
| 890 | | `a' and `b'. The operation is performed according to the IEC/IEEE Standard |
| 891 | | for Binary Floating-Point Arithmetic. |
| 892 | *----------------------------------------------------------------------------*/ |
| 893 | |
| 894 | float32 float32_sub( float32 a, float32 b ) |
| 895 | { |
| 896 | flag aSign, bSign; |
| 897 | |
| 898 | aSign = extractFloat32Sign( a ); |
| 899 | bSign = extractFloat32Sign( b ); |
| 900 | if ( aSign == bSign ) { |
| 901 | return subFloat32Sigs( a, b, aSign ); |
| 902 | } |
| 903 | else { |
| 904 | return addFloat32Sigs( a, b, aSign ); |
| 905 | } |
| 906 | |
| 907 | } |
| 908 | |
| 909 | /*---------------------------------------------------------------------------- |
| 910 | | Returns the result of multiplying the single-precision floating-point values |
| 911 | | `a' and `b'. The operation is performed according to the IEC/IEEE Standard |
| 912 | | for Binary Floating-Point Arithmetic. |
| 913 | *----------------------------------------------------------------------------*/ |
| 914 | |
| 915 | float32 float32_mul( float32 a, float32 b ) |
| 916 | { |
| 917 | flag aSign, bSign, zSign; |
| 918 | int16 aExp, bExp, zExp; |
| 919 | bits32 aSig, bSig, zSig0, zSig1; |
| 920 | |
| 921 | aSig = extractFloat32Frac( a ); |
| 922 | aExp = extractFloat32Exp( a ); |
| 923 | aSign = extractFloat32Sign( a ); |
| 924 | bSig = extractFloat32Frac( b ); |
| 925 | bExp = extractFloat32Exp( b ); |
| 926 | bSign = extractFloat32Sign( b ); |
| 927 | zSign = aSign ^ bSign; |
| 928 | if ( aExp == 0xFF ) { |
| 929 | if ( aSig || ( ( bExp == 0xFF ) && bSig ) ) { |
| 930 | return propagateFloat32NaN( a, b ); |
| 931 | } |
| 932 | if ( ( bExp | bSig ) == 0 ) { |
| 933 | float_raise( float_flag_invalid ); |
| 934 | return float32_default_nan; |
| 935 | } |
| 936 | return packFloat32( zSign, 0xFF, 0 ); |
| 937 | } |
| 938 | if ( bExp == 0xFF ) { |
| 939 | if ( bSig ) return propagateFloat32NaN( a, b ); |
| 940 | if ( ( aExp | aSig ) == 0 ) { |
| 941 | float_raise( float_flag_invalid ); |
| 942 | return float32_default_nan; |
| 943 | } |
| 944 | return packFloat32( zSign, 0xFF, 0 ); |
| 945 | } |
| 946 | if ( aExp == 0 ) { |
| 947 | if ( aSig == 0 ) return packFloat32( zSign, 0, 0 ); |
| 948 | normalizeFloat32Subnormal( aSig, &aExp, &aSig ); |
| 949 | } |
| 950 | if ( bExp == 0 ) { |
| 951 | if ( bSig == 0 ) return packFloat32( zSign, 0, 0 ); |
| 952 | normalizeFloat32Subnormal( bSig, &bExp, &bSig ); |
| 953 | } |
| 954 | zExp = aExp + bExp - 0x7F; |
| 955 | aSig = ( aSig | 0x00800000 )<<7; |
| 956 | bSig = ( bSig | 0x00800000 )<<8; |
| 957 | mul32To64( aSig, bSig, &zSig0, &zSig1 ); |
| 958 | zSig0 |= ( zSig1 != 0 ); |
| 959 | if ( 0 <= (sbits32) ( zSig0<<1 ) ) { |
| 960 | zSig0 <<= 1; |
| 961 | --zExp; |
| 962 | } |
| 963 | return roundAndPackFloat32( zSign, zExp, zSig0 ); |
| 964 | |
| 965 | } |
| 966 | |
| 967 | /*---------------------------------------------------------------------------- |
| 968 | | Returns the result of dividing the single-precision floating-point value `a' |
| 969 | | by the corresponding value `b'. The operation is performed according to the |
| 970 | | IEC/IEEE Standard for Binary Floating-Point Arithmetic. |
| 971 | *----------------------------------------------------------------------------*/ |
| 972 | |
| 973 | float32 float32_div( float32 a, float32 b ) |
| 974 | { |
| 975 | flag aSign, bSign, zSign; |
| 976 | int16 aExp, bExp, zExp; |
| 977 | bits32 aSig, bSig, zSig, rem0, rem1, term0, term1; |
| 978 | |
| 979 | aSig = extractFloat32Frac( a ); |
| 980 | aExp = extractFloat32Exp( a ); |
| 981 | aSign = extractFloat32Sign( a ); |
| 982 | bSig = extractFloat32Frac( b ); |
| 983 | bExp = extractFloat32Exp( b ); |
| 984 | bSign = extractFloat32Sign( b ); |
| 985 | zSign = aSign ^ bSign; |
| 986 | if ( aExp == 0xFF ) { |
| 987 | if ( aSig ) return propagateFloat32NaN( a, b ); |
| 988 | if ( bExp == 0xFF ) { |
| 989 | if ( bSig ) return propagateFloat32NaN( a, b ); |
| 990 | float_raise( float_flag_invalid ); |
| 991 | return float32_default_nan; |
| 992 | } |
| 993 | return packFloat32( zSign, 0xFF, 0 ); |
| 994 | } |
| 995 | if ( bExp == 0xFF ) { |
| 996 | if ( bSig ) return propagateFloat32NaN( a, b ); |
| 997 | return packFloat32( zSign, 0, 0 ); |
| 998 | } |
| 999 | if ( bExp == 0 ) { |
| 1000 | if ( bSig == 0 ) { |
| 1001 | if ( ( aExp | aSig ) == 0 ) { |
| 1002 | float_raise( float_flag_invalid ); |
| 1003 | return float32_default_nan; |
| 1004 | } |
| 1005 | float_raise( float_flag_divbyzero ); |
| 1006 | return packFloat32( zSign, 0xFF, 0 ); |
| 1007 | } |
| 1008 | normalizeFloat32Subnormal( bSig, &bExp, &bSig ); |
| 1009 | } |
| 1010 | if ( aExp == 0 ) { |
| 1011 | if ( aSig == 0 ) return packFloat32( zSign, 0, 0 ); |
| 1012 | normalizeFloat32Subnormal( aSig, &aExp, &aSig ); |
| 1013 | } |
| 1014 | zExp = aExp - bExp + 0x7D; |
| 1015 | aSig = ( aSig | 0x00800000 )<<7; |
| 1016 | bSig = ( bSig | 0x00800000 )<<8; |
| 1017 | if ( bSig <= ( aSig + aSig ) ) { |
| 1018 | aSig >>= 1; |
| 1019 | ++zExp; |
| 1020 | } |
| 1021 | zSig = estimateDiv64To32( aSig, 0, bSig ); |
| 1022 | if ( ( zSig & 0x3F ) <= 2 ) { |
| 1023 | mul32To64( bSig, zSig, &term0, &term1 ); |
| 1024 | sub64( aSig, 0, term0, term1, &rem0, &rem1 ); |
| 1025 | while ( (sbits32) rem0 < 0 ) { |
| 1026 | --zSig; |
| 1027 | add64( rem0, rem1, 0, bSig, &rem0, &rem1 ); |
| 1028 | } |
| 1029 | zSig |= ( rem1 != 0 ); |
| 1030 | } |
| 1031 | return roundAndPackFloat32( zSign, zExp, zSig ); |
| 1032 | |
| 1033 | } |
| 1034 | |
| 1035 | /*---------------------------------------------------------------------------- |
| 1036 | | Returns the remainder of the single-precision floating-point value `a' |
| 1037 | | with respect to the corresponding value `b'. The operation is performed |
| 1038 | | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic. |
| 1039 | *----------------------------------------------------------------------------*/ |
| 1040 | |
| 1041 | float32 float32_rem( float32 a, float32 b ) |
| 1042 | { |
| 1043 | flag aSign, bSign, zSign; |
| 1044 | int16 aExp, bExp, expDiff; |
| 1045 | bits32 aSig, bSig, q, allZero, alternateASig; |
| 1046 | sbits32 sigMean; |
| 1047 | |
| 1048 | aSig = extractFloat32Frac( a ); |
| 1049 | aExp = extractFloat32Exp( a ); |
| 1050 | aSign = extractFloat32Sign( a ); |
| 1051 | bSig = extractFloat32Frac( b ); |
| 1052 | bExp = extractFloat32Exp( b ); |
| 1053 | bSign = extractFloat32Sign( b ); |
| 1054 | if ( aExp == 0xFF ) { |
| 1055 | if ( aSig || ( ( bExp == 0xFF ) && bSig ) ) { |
| 1056 | return propagateFloat32NaN( a, b ); |
| 1057 | } |
| 1058 | float_raise( float_flag_invalid ); |
| 1059 | return float32_default_nan; |
| 1060 | } |
| 1061 | if ( bExp == 0xFF ) { |
| 1062 | if ( bSig ) return propagateFloat32NaN( a, b ); |
| 1063 | return a; |
| 1064 | } |
| 1065 | if ( bExp == 0 ) { |
| 1066 | if ( bSig == 0 ) { |
| 1067 | float_raise( float_flag_invalid ); |
| 1068 | return float32_default_nan; |
| 1069 | } |
| 1070 | normalizeFloat32Subnormal( bSig, &bExp, &bSig ); |
| 1071 | } |
| 1072 | if ( aExp == 0 ) { |
| 1073 | if ( aSig == 0 ) return a; |
| 1074 | normalizeFloat32Subnormal( aSig, &aExp, &aSig ); |
| 1075 | } |
| 1076 | expDiff = aExp - bExp; |
| 1077 | aSig = ( aSig | 0x00800000 )<<8; |
| 1078 | bSig = ( bSig | 0x00800000 )<<8; |
| 1079 | if ( expDiff < 0 ) { |
| 1080 | if ( expDiff < -1 ) return a; |
| 1081 | aSig >>= 1; |
| 1082 | } |
| 1083 | q = ( bSig <= aSig ); |
| 1084 | if ( q ) aSig -= bSig; |
| 1085 | expDiff -= 32; |
| 1086 | while ( 0 < expDiff ) { |
| 1087 | q = estimateDiv64To32( aSig, 0, bSig ); |
| 1088 | q = ( 2 < q ) ? q - 2 : 0; |
| 1089 | aSig = - ( ( bSig>>2 ) * q ); |
| 1090 | expDiff -= 30; |
| 1091 | } |
| 1092 | expDiff += 32; |
| 1093 | if ( 0 < expDiff ) { |
| 1094 | q = estimateDiv64To32( aSig, 0, bSig ); |
| 1095 | q = ( 2 < q ) ? q - 2 : 0; |
| 1096 | q >>= 32 - expDiff; |
| 1097 | bSig >>= 2; |
| 1098 | aSig = ( ( aSig>>1 )<<( expDiff - 1 ) ) - bSig * q; |
| 1099 | } |
| 1100 | else { |
| 1101 | aSig >>= 2; |
| 1102 | bSig >>= 2; |
| 1103 | } |
| 1104 | do { |
| 1105 | alternateASig = aSig; |
| 1106 | ++q; |
| 1107 | aSig -= bSig; |
| 1108 | } while ( 0 <= (sbits32) aSig ); |
| 1109 | sigMean = aSig + alternateASig; |
| 1110 | if ( ( sigMean < 0 ) || ( ( sigMean == 0 ) && ( q & 1 ) ) ) { |
| 1111 | aSig = alternateASig; |
| 1112 | } |
| 1113 | zSign = ( (sbits32) aSig < 0 ); |
| 1114 | if ( zSign ) aSig = - aSig; |
| 1115 | return normalizeRoundAndPackFloat32( aSign ^ zSign, bExp, aSig ); |
| 1116 | |
| 1117 | } |
| 1118 | |
| 1119 | /*---------------------------------------------------------------------------- |
| 1120 | | Returns the square root of the single-precision floating-point value `a'. |
| 1121 | | The operation is performed according to the IEC/IEEE Standard for Binary |
| 1122 | | Floating-Point Arithmetic. |
| 1123 | *----------------------------------------------------------------------------*/ |
| 1124 | |
| 1125 | float32 float32_sqrt( float32 a ) |
| 1126 | { |
| 1127 | flag aSign; |
| 1128 | int16 aExp, zExp; |
| 1129 | bits32 aSig, zSig, rem0, rem1, term0, term1; |
| 1130 | |
| 1131 | aSig = extractFloat32Frac( a ); |
| 1132 | aExp = extractFloat32Exp( a ); |
| 1133 | aSign = extractFloat32Sign( a ); |
| 1134 | if ( aExp == 0xFF ) { |
| 1135 | if ( aSig ) return propagateFloat32NaN( a, 0 ); |
| 1136 | if ( ! aSign ) return a; |
| 1137 | float_raise( float_flag_invalid ); |
| 1138 | return float32_default_nan; |
| 1139 | } |
| 1140 | if ( aSign ) { |
| 1141 | if ( ( aExp | aSig ) == 0 ) return a; |
| 1142 | float_raise( float_flag_invalid ); |
| 1143 | return float32_default_nan; |
| 1144 | } |
| 1145 | if ( aExp == 0 ) { |
| 1146 | if ( aSig == 0 ) return 0; |
| 1147 | normalizeFloat32Subnormal( aSig, &aExp, &aSig ); |
| 1148 | } |
| 1149 | zExp = ( ( aExp - 0x7F )>>1 ) + 0x7E; |
| 1150 | aSig = ( aSig | 0x00800000 )<<8; |
| 1151 | zSig = estimateSqrt32( aExp, aSig ) + 2; |
| 1152 | if ( ( zSig & 0x7F ) <= 5 ) { |
| 1153 | if ( zSig < 2 ) { |
| 1154 | zSig = 0x7FFFFFFF; |
| 1155 | goto roundAndPack; |
| 1156 | } |
| 1157 | else { |
| 1158 | aSig >>= aExp & 1; |
| 1159 | mul32To64( zSig, zSig, &term0, &term1 ); |
| 1160 | sub64( aSig, 0, term0, term1, &rem0, &rem1 ); |
| 1161 | while ( (sbits32) rem0 < 0 ) { |
| 1162 | --zSig; |
| 1163 | shortShift64Left( 0, zSig, 1, &term0, &term1 ); |
| 1164 | term1 |= 1; |
| 1165 | add64( rem0, rem1, term0, term1, &rem0, &rem1 ); |
| 1166 | } |
| 1167 | zSig |= ( ( rem0 | rem1 ) != 0 ); |
| 1168 | } |
| 1169 | } |
| 1170 | shift32RightJamming( zSig, 1, &zSig ); |
| 1171 | roundAndPack: |
| 1172 | return roundAndPackFloat32( 0, zExp, zSig ); |
| 1173 | |
| 1174 | } |
| 1175 | |
| 1176 | /*---------------------------------------------------------------------------- |
| 1177 | | Returns 1 if the single-precision floating-point value `a' is equal to |
| 1178 | | the corresponding value `b', and 0 otherwise. The comparison is performed |
| 1179 | | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic. |
| 1180 | *----------------------------------------------------------------------------*/ |
| 1181 | |
| 1182 | flag float32_eq( float32 a, float32 b ) |
| 1183 | { |
| 1184 | |
| 1185 | if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) ) |
| 1186 | || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) ) |
| 1187 | ) { |
| 1188 | if ( float32_is_signaling_nan( a ) || float32_is_signaling_nan( b ) ) { |
| 1189 | float_raise( float_flag_invalid ); |
| 1190 | } |
| 1191 | return 0; |
| 1192 | } |
| 1193 | return ( a == b ) || ( (bits32) ( ( a | b )<<1 ) == 0 ); |
| 1194 | |
| 1195 | } |
| 1196 | |
| 1197 | /*---------------------------------------------------------------------------- |
| 1198 | | Returns 1 if the single-precision floating-point value `a' is less than |
| 1199 | | or equal to the corresponding value `b', and 0 otherwise. The comparison |
| 1200 | | is performed according to the IEC/IEEE Standard for Binary Floating-Point |
| 1201 | | Arithmetic. |
| 1202 | *----------------------------------------------------------------------------*/ |
| 1203 | |
| 1204 | flag float32_le( float32 a, float32 b ) |
| 1205 | { |
| 1206 | flag aSign, bSign; |
| 1207 | |
| 1208 | if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) ) |
| 1209 | || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) ) |
| 1210 | ) { |
| 1211 | float_raise( float_flag_invalid ); |
| 1212 | return 0; |
| 1213 | } |
| 1214 | aSign = extractFloat32Sign( a ); |
| 1215 | bSign = extractFloat32Sign( b ); |
| 1216 | if ( aSign != bSign ) return aSign || ( (bits32) ( ( a | b )<<1 ) == 0 ); |
| 1217 | return ( a == b ) || ( aSign ^ ( a < b ) ); |
| 1218 | |
| 1219 | } |
| 1220 | |
| 1221 | /*---------------------------------------------------------------------------- |
| 1222 | | Returns 1 if the single-precision floating-point value `a' is less than |
| 1223 | | the corresponding value `b', and 0 otherwise. The comparison is performed |
| 1224 | | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic. |
| 1225 | *----------------------------------------------------------------------------*/ |
| 1226 | |
| 1227 | flag float32_lt( float32 a, float32 b ) |
| 1228 | { |
| 1229 | flag aSign, bSign; |
| 1230 | |
| 1231 | if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) ) |
| 1232 | || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) ) |
| 1233 | ) { |
| 1234 | float_raise( float_flag_invalid ); |
| 1235 | return 0; |
| 1236 | } |
| 1237 | aSign = extractFloat32Sign( a ); |
| 1238 | bSign = extractFloat32Sign( b ); |
| 1239 | if ( aSign != bSign ) return aSign && ( (bits32) ( ( a | b )<<1 ) != 0 ); |
| 1240 | return ( a != b ) && ( aSign ^ ( a < b ) ); |
| 1241 | |
| 1242 | } |
| 1243 | |
| 1244 | /*---------------------------------------------------------------------------- |
| 1245 | | Returns 1 if the single-precision floating-point value `a' is equal to |
| 1246 | | the corresponding value `b', and 0 otherwise. The invalid exception is |
| 1247 | | raised if either operand is a NaN. Otherwise, the comparison is performed |
| 1248 | | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic. |
| 1249 | *----------------------------------------------------------------------------*/ |
| 1250 | |
| 1251 | flag float32_eq_signaling( float32 a, float32 b ) |
| 1252 | { |
| 1253 | |
| 1254 | if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) ) |
| 1255 | || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) ) |
| 1256 | ) { |
| 1257 | float_raise( float_flag_invalid ); |
| 1258 | return 0; |
| 1259 | } |
| 1260 | return ( a == b ) || ( (bits32) ( ( a | b )<<1 ) == 0 ); |
| 1261 | |
| 1262 | } |
| 1263 | |
| 1264 | /*---------------------------------------------------------------------------- |
| 1265 | | Returns 1 if the single-precision floating-point value `a' is less than or |
| 1266 | | equal to the corresponding value `b', and 0 otherwise. Quiet NaNs do not |
| 1267 | | cause an exception. Otherwise, the comparison is performed according to the |
| 1268 | | IEC/IEEE Standard for Binary Floating-Point Arithmetic. |
| 1269 | *----------------------------------------------------------------------------*/ |
| 1270 | |
| 1271 | flag float32_le_quiet( float32 a, float32 b ) |
| 1272 | { |
| 1273 | flag aSign, bSign; |
| 1274 | int16 aExp, bExp; |
| 1275 | |
| 1276 | if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) ) |
| 1277 | || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) ) |
| 1278 | ) { |
| 1279 | if ( float32_is_signaling_nan( a ) || float32_is_signaling_nan( b ) ) { |
| 1280 | float_raise( float_flag_invalid ); |
| 1281 | } |
| 1282 | return 0; |
| 1283 | } |
| 1284 | aSign = extractFloat32Sign( a ); |
| 1285 | bSign = extractFloat32Sign( b ); |
| 1286 | if ( aSign != bSign ) return aSign || ( (bits32) ( ( a | b )<<1 ) == 0 ); |
| 1287 | return ( a == b ) || ( aSign ^ ( a < b ) ); |
| 1288 | |
| 1289 | } |
| 1290 | |
| 1291 | /*---------------------------------------------------------------------------- |
| 1292 | | Returns 1 if the single-precision floating-point value `a' is less than |
| 1293 | | the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause an |
| 1294 | | exception. Otherwise, the comparison is performed according to the IEC/IEEE |
| 1295 | | Standard for Binary Floating-Point Arithmetic. |
| 1296 | *----------------------------------------------------------------------------*/ |
| 1297 | |
| 1298 | flag float32_lt_quiet( float32 a, float32 b ) |
| 1299 | { |
| 1300 | flag aSign, bSign; |
| 1301 | |
| 1302 | if ( ( ( extractFloat32Exp( a ) == 0xFF ) && extractFloat32Frac( a ) ) |
| 1303 | || ( ( extractFloat32Exp( b ) == 0xFF ) && extractFloat32Frac( b ) ) |
| 1304 | ) { |
| 1305 | if ( float32_is_signaling_nan( a ) || float32_is_signaling_nan( b ) ) { |
| 1306 | float_raise( float_flag_invalid ); |
| 1307 | } |
| 1308 | return 0; |
| 1309 | } |
| 1310 | aSign = extractFloat32Sign( a ); |
| 1311 | bSign = extractFloat32Sign( b ); |
| 1312 | if ( aSign != bSign ) return aSign && ( (bits32) ( ( a | b )<<1 ) != 0 ); |
| 1313 | return ( a != b ) && ( aSign ^ ( a < b ) ); |
| 1314 | |
| 1315 | } |
| 1316 | |
| 1317 | /*---------------------------------------------------------------------------- |
| 1318 | | Returns the result of converting the double-precision floating-point value |
| 1319 | | `a' to the 32-bit two's complement integer format. The conversion is |
| 1320 | | performed according to the IEC/IEEE Standard for Binary Floating-Point |
| 1321 | | Arithmetic---which means in particular that the conversion is rounded |
| 1322 | | according to the current rounding mode. If `a' is a NaN, the largest |
| 1323 | | positive integer is returned. Otherwise, if the conversion overflows, the |
| 1324 | | largest integer with the same sign as `a' is returned. |
| 1325 | *----------------------------------------------------------------------------*/ |
| 1326 | |
| 1327 | int32 float64_to_int32( float64 a ) |
| 1328 | { |
| 1329 | flag aSign; |
| 1330 | int16 aExp, shiftCount; |
| 1331 | bits32 aSig0, aSig1, absZ, aSigExtra; |
| 1332 | int32 z; |
| 1333 | int8 roundingMode; |
| 1334 | |
| 1335 | aSig1 = extractFloat64Frac1( a ); |
| 1336 | aSig0 = extractFloat64Frac0( a ); |
| 1337 | aExp = extractFloat64Exp( a ); |
| 1338 | aSign = extractFloat64Sign( a ); |
| 1339 | shiftCount = aExp - 0x413; |
| 1340 | if ( 0 <= shiftCount ) { |
| 1341 | if ( 0x41E < aExp ) { |
| 1342 | if ( ( aExp == 0x7FF ) && ( aSig0 | aSig1 ) ) aSign = 0; |
| 1343 | goto invalid; |
| 1344 | } |
| 1345 | shortShift64Left( |
| 1346 | aSig0 | 0x00100000, aSig1, shiftCount, &absZ, &aSigExtra ); |
| 1347 | if ( 0x80000000 < absZ ) goto invalid; |
| 1348 | } |
| 1349 | else { |
| 1350 | aSig1 = ( aSig1 != 0 ); |
| 1351 | if ( aExp < 0x3FE ) { |
| 1352 | aSigExtra = aExp | aSig0 | aSig1; |
| 1353 | absZ = 0; |
| 1354 | } |
| 1355 | else { |
| 1356 | aSig0 |= 0x00100000; |
| 1357 | aSigExtra = ( aSig0<<( shiftCount & 31 ) ) | aSig1; |
| 1358 | absZ = aSig0>>( - shiftCount ); |
| 1359 | } |
| 1360 | } |
| 1361 | roundingMode = float_rounding_mode; |
| 1362 | if ( roundingMode == float_round_nearest_even ) { |
| 1363 | if ( (sbits32) aSigExtra < 0 ) { |
| 1364 | ++absZ; |
| 1365 | if ( (bits32) ( aSigExtra<<1 ) == 0 ) absZ &= ~1; |
| 1366 | } |
| 1367 | z = aSign ? - absZ : absZ; |
| 1368 | } |
| 1369 | else { |
| 1370 | aSigExtra = ( aSigExtra != 0 ); |
| 1371 | if ( aSign ) { |
| 1372 | z = - ( absZ |
| 1373 | + ( ( roundingMode == float_round_down ) & aSigExtra ) ); |
| 1374 | } |
| 1375 | else { |
| 1376 | z = absZ + ( ( roundingMode == float_round_up ) & aSigExtra ); |
| 1377 | } |
| 1378 | } |
| 1379 | if ( ( aSign ^ ( z < 0 ) ) && z ) { |
| 1380 | invalid: |
| 1381 | float_raise( float_flag_invalid ); |
| 1382 | return aSign ? (sbits32) 0x80000000 : 0x7FFFFFFF; |
| 1383 | } |
| 1384 | if ( aSigExtra ) float_exception_flags |= float_flag_inexact; |
| 1385 | return z; |
| 1386 | |
| 1387 | } |
| 1388 | |
| 1389 | /*---------------------------------------------------------------------------- |
| 1390 | | Returns the result of converting the double-precision floating-point value |
| 1391 | | `a' to the 32-bit two's complement integer format. The conversion is |
| 1392 | | performed according to the IEC/IEEE Standard for Binary Floating-Point |
| 1393 | | Arithmetic, except that the conversion is always rounded toward zero. |
| 1394 | | If `a' is a NaN, the largest positive integer is returned. Otherwise, if |
| 1395 | | the conversion overflows, the largest integer with the same sign as `a' is |
| 1396 | | returned. |
| 1397 | *----------------------------------------------------------------------------*/ |
| 1398 | |
| 1399 | int32 float64_to_int32_round_to_zero( float64 a ) |
| 1400 | { |
| 1401 | flag aSign; |
| 1402 | int16 aExp, shiftCount; |
| 1403 | bits32 aSig0, aSig1, absZ, aSigExtra; |
| 1404 | int32 z; |
| 1405 | |
| 1406 | aSig1 = extractFloat64Frac1( a ); |
| 1407 | aSig0 = extractFloat64Frac0( a ); |
| 1408 | aExp = extractFloat64Exp( a ); |
| 1409 | aSign = extractFloat64Sign( a ); |
| 1410 | shiftCount = aExp - 0x413; |
| 1411 | if ( 0 <= shiftCount ) { |
| 1412 | if ( 0x41E < aExp ) { |
| 1413 | if ( ( aExp == 0x7FF ) && ( aSig0 | aSig1 ) ) aSign = 0; |
| 1414 | goto invalid; |
| 1415 | } |
| 1416 | shortShift64Left( |
| 1417 | aSig0 | 0x00100000, aSig1, shiftCount, &absZ, &aSigExtra ); |
| 1418 | } |
| 1419 | else { |
| 1420 | if ( aExp < 0x3FF ) { |
| 1421 | if ( aExp | aSig0 | aSig1 ) { |
| 1422 | float_exception_flags |= float_flag_inexact; |
| 1423 | } |
| 1424 | return 0; |
| 1425 | } |
| 1426 | aSig0 |= 0x00100000; |
| 1427 | aSigExtra = ( aSig0<<( shiftCount & 31 ) ) | aSig1; |
| 1428 | absZ = aSig0>>( - shiftCount ); |
| 1429 | } |
| 1430 | z = aSign ? - absZ : absZ; |
| 1431 | if ( ( aSign ^ ( z < 0 ) ) && z ) { |
| 1432 | invalid: |
| 1433 | float_raise( float_flag_invalid ); |
| 1434 | return aSign ? (sbits32) 0x80000000 : 0x7FFFFFFF; |
| 1435 | } |
| 1436 | if ( aSigExtra ) float_exception_flags |= float_flag_inexact; |
| 1437 | return z; |
| 1438 | |
| 1439 | } |
| 1440 | |
| 1441 | /*---------------------------------------------------------------------------- |
| 1442 | | Returns the result of converting the double-precision floating-point value |
| 1443 | | `a' to the single-precision floating-point format. The conversion is |
| 1444 | | performed according to the IEC/IEEE Standard for Binary Floating-Point |
| 1445 | | Arithmetic. |
| 1446 | *----------------------------------------------------------------------------*/ |
| 1447 | |
| 1448 | float32 float64_to_float32( float64 a ) |
| 1449 | { |
| 1450 | flag aSign; |
| 1451 | int16 aExp; |
| 1452 | bits32 aSig0, aSig1, zSig; |
| 1453 | bits32 allZero; |
| 1454 | |
| 1455 | aSig1 = extractFloat64Frac1( a ); |
| 1456 | aSig0 = extractFloat64Frac0( a ); |
| 1457 | aExp = extractFloat64Exp( a ); |
| 1458 | aSign = extractFloat64Sign( a ); |
| 1459 | if ( aExp == 0x7FF ) { |
| 1460 | if ( aSig0 | aSig1 ) { |
| 1461 | return commonNaNToFloat32( float64ToCommonNaN( a ) ); |
| 1462 | } |
| 1463 | return packFloat32( aSign, 0xFF, 0 ); |
| 1464 | } |
| 1465 | shift64RightJamming( aSig0, aSig1, 22, &allZero, &zSig ); |
| 1466 | if ( aExp ) zSig |= 0x40000000; |
| 1467 | return roundAndPackFloat32( aSign, aExp - 0x381, zSig ); |
| 1468 | |
| 1469 | } |
| 1470 | |
| 1471 | /*---------------------------------------------------------------------------- |
| 1472 | | Rounds the double-precision floating-point value `a' to an integer, |
| 1473 | | and returns the result as a double-precision floating-point value. The |
| 1474 | | operation is performed according to the IEC/IEEE Standard for Binary |
| 1475 | | Floating-Point Arithmetic. |
| 1476 | *----------------------------------------------------------------------------*/ |
| 1477 | |
| 1478 | float64 float64_round_to_int( float64 a ) |
| 1479 | { |
| 1480 | flag aSign; |
| 1481 | int16 aExp; |
| 1482 | bits32 lastBitMask, roundBitsMask; |
| 1483 | int8 roundingMode; |
| 1484 | float64 z; |
| 1485 | |
| 1486 | aExp = extractFloat64Exp( a ); |
| 1487 | if ( 0x413 <= aExp ) { |
| 1488 | if ( 0x433 <= aExp ) { |
| 1489 | if ( ( aExp == 0x7FF ) |
| 1490 | && ( extractFloat64Frac0( a ) | extractFloat64Frac1( a ) ) ) { |
| 1491 | return propagateFloat64NaN( a, a ); |
| 1492 | } |
| 1493 | return a; |
| 1494 | } |
| 1495 | lastBitMask = 1; |
| 1496 | lastBitMask = ( lastBitMask<<( 0x432 - aExp ) )<<1; |
| 1497 | roundBitsMask = lastBitMask - 1; |
| 1498 | z = a; |
| 1499 | roundingMode = float_rounding_mode; |
| 1500 | if ( roundingMode == float_round_nearest_even ) { |
| 1501 | if ( lastBitMask ) { |
| 1502 | add64( z.high, z.low, 0, lastBitMask>>1, &z.high, &z.low ); |
| 1503 | if ( ( z.low & roundBitsMask ) == 0 ) z.low &= ~ lastBitMask; |
| 1504 | } |
| 1505 | else { |
| 1506 | if ( (sbits32) z.low < 0 ) { |
| 1507 | ++z.high; |
| 1508 | if ( (bits32) ( z.low<<1 ) == 0 ) z.high &= ~1; |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | else if ( roundingMode != float_round_to_zero ) { |
| 1513 | if ( extractFloat64Sign( z ) |
| 1514 | ^ ( roundingMode == float_round_up ) ) { |
| 1515 | add64( z.high, z.low, 0, roundBitsMask, &z.high, &z.low ); |
| 1516 | } |
| 1517 | } |
| 1518 | z.low &= ~ roundBitsMask; |
| 1519 | } |
| 1520 | else { |
| 1521 | if ( aExp <= 0x3FE ) { |
| 1522 | if ( ( ( (bits32) ( a.high<<1 ) ) | a.low ) == 0 ) return a; |
| 1523 | float_exception_flags |= float_flag_inexact; |
| 1524 | aSign = extractFloat64Sign( a ); |
| 1525 | switch ( float_rounding_mode ) { |
| 1526 | case float_round_nearest_even: |
| 1527 | if ( ( aExp == 0x3FE ) |
| 1528 | && ( extractFloat64Frac0( a ) | extractFloat64Frac1( a ) ) |
| 1529 | ) { |
| 1530 | return packFloat64( aSign, 0x3FF, 0, 0 ); |
| 1531 | } |
| 1532 | break; |
| 1533 | case float_round_down: |
| 1534 | return |
| 1535 | aSign ? packFloat64( 1, 0x3FF, 0, 0 ) |
| 1536 | : packFloat64( 0, 0, 0, 0 ); |
| 1537 | case float_round_up: |
| 1538 | return |
| 1539 | aSign ? packFloat64( 1, 0, 0, 0 ) |
| 1540 | : packFloat64( 0, 0x3FF, 0, 0 ); |
| 1541 | } |
| 1542 | return packFloat64( aSign, 0, 0, 0 ); |
| 1543 | } |
| 1544 | lastBitMask = 1; |
| 1545 | lastBitMask <<= 0x413 - aExp; |
| 1546 | roundBitsMask = lastBitMask - 1; |
| 1547 | z.low = 0; |
| 1548 | z.high = a.high; |
| 1549 | roundingMode = float_rounding_mode; |
| 1550 | if ( roundingMode == float_round_nearest_even ) { |
| 1551 | z.high += lastBitMask>>1; |
| 1552 | if ( ( ( z.high & roundBitsMask ) | a.low ) == 0 ) { |
| 1553 | z.high &= ~ lastBitMask; |
| 1554 | } |
| 1555 | } |
| 1556 | else if ( roundingMode != float_round_to_zero ) { |
| 1557 | if ( extractFloat64Sign( z ) |
| 1558 | ^ ( roundingMode == float_round_up ) ) { |
| 1559 | z.high |= ( a.low != 0 ); |
| 1560 | z.high += roundBitsMask; |
| 1561 | } |
| 1562 | } |
| 1563 | z.high &= ~ roundBitsMask; |
| 1564 | } |
| 1565 | if ( ( z.low != a.low ) || ( z.high != a.high ) ) { |
| 1566 | float_exception_flags |= float_flag_inexact; |
| 1567 | } |
| 1568 | return z; |
| 1569 | |
| 1570 | } |
| 1571 | |
| 1572 | /*---------------------------------------------------------------------------- |
| 1573 | | Returns the result of adding the absolute values of the double-precision |
| 1574 | | floating-point values `a' and `b'. If `zSign' is 1, the sum is negated |
| 1575 | | before being returned. `zSign' is ignored if the result is a NaN. |
| 1576 | | The addition is performed according to the IEC/IEEE Standard for Binary |
| 1577 | | Floating-Point Arithmetic. |
| 1578 | *----------------------------------------------------------------------------*/ |
| 1579 | |
| 1580 | static float64 addFloat64Sigs( float64 a, float64 b, flag zSign ) |
| 1581 | { |
| 1582 | int16 aExp, bExp, zExp; |
| 1583 | bits32 aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2; |
| 1584 | int16 expDiff; |
| 1585 | |
| 1586 | aSig1 = extractFloat64Frac1( a ); |
| 1587 | aSig0 = extractFloat64Frac0( a ); |
| 1588 | aExp = extractFloat64Exp( a ); |
| 1589 | bSig1 = extractFloat64Frac1( b ); |
| 1590 | bSig0 = extractFloat64Frac0( b ); |
| 1591 | bExp = extractFloat64Exp( b ); |
| 1592 | expDiff = aExp - bExp; |
| 1593 | if ( 0 < expDiff ) { |
| 1594 | if ( aExp == 0x7FF ) { |
| 1595 | if ( aSig0 | aSig1 ) return propagateFloat64NaN( a, b ); |
| 1596 | return a; |
| 1597 | } |
| 1598 | if ( bExp == 0 ) { |
| 1599 | --expDiff; |
| 1600 | } |
| 1601 | else { |
| 1602 | bSig0 |= 0x00100000; |
| 1603 | } |
| 1604 | shift64ExtraRightJamming( |
| 1605 | bSig0, bSig1, 0, expDiff, &bSig0, &bSig1, &zSig2 ); |
| 1606 | zExp = aExp; |
| 1607 | } |
| 1608 | else if ( expDiff < 0 ) { |
| 1609 | if ( bExp == 0x7FF ) { |
| 1610 | if ( bSig0 | bSig1 ) return propagateFloat64NaN( a, b ); |
| 1611 | return packFloat64( zSign, 0x7FF, 0, 0 ); |
| 1612 | } |
| 1613 | if ( aExp == 0 ) { |
| 1614 | ++expDiff; |
| 1615 | } |
| 1616 | else { |
| 1617 | aSig0 |= 0x00100000; |
| 1618 | } |
| 1619 | shift64ExtraRightJamming( |
| 1620 | aSig0, aSig1, 0, - expDiff, &aSig0, &aSig1, &zSig2 ); |
| 1621 | zExp = bExp; |
| 1622 | } |
| 1623 | else { |
| 1624 | if ( aExp == 0x7FF ) { |
| 1625 | if ( aSig0 | aSig1 | bSig0 | bSig1 ) { |
| 1626 | return propagateFloat64NaN( a, b ); |
| 1627 | } |
| 1628 | return a; |
| 1629 | } |
| 1630 | add64( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1 ); |
| 1631 | if ( aExp == 0 ) return packFloat64( zSign, 0, zSig0, zSig1 ); |
| 1632 | zSig2 = 0; |
| 1633 | zSig0 |= 0x00200000; |
| 1634 | zExp = aExp; |
| 1635 | goto shiftRight1; |
| 1636 | } |
| 1637 | aSig0 |= 0x00100000; |
| 1638 | add64( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1 ); |
| 1639 | --zExp; |
| 1640 | if ( zSig0 < 0x00200000 ) goto roundAndPack; |
| 1641 | ++zExp; |
| 1642 | shiftRight1: |
| 1643 | shift64ExtraRightJamming( zSig0, zSig1, zSig2, 1, &zSig0, &zSig1, &zSig2 ); |
| 1644 | roundAndPack: |
| 1645 | return roundAndPackFloat64( zSign, zExp, zSig0, zSig1, zSig2 ); |
| 1646 | |
| 1647 | } |
| 1648 | |
| 1649 | /*---------------------------------------------------------------------------- |
| 1650 | | Returns the result of subtracting the absolute values of the double- |
| 1651 | | precision floating-point values `a' and `b'. If `zSign' is 1, the |
| 1652 | | difference is negated before being returned. `zSign' is ignored if the |
| 1653 | | result is a NaN. The subtraction is performed according to the IEC/IEEE |
| 1654 | | Standard for Binary Floating-Point Arithmetic. |
| 1655 | *----------------------------------------------------------------------------*/ |
| 1656 | |
| 1657 | static float64 subFloat64Sigs( float64 a, float64 b, flag zSign ) |
| 1658 | { |
| 1659 | int16 aExp, bExp, zExp; |
| 1660 | bits32 aSig0, aSig1, bSig0, bSig1, zSig0, zSig1; |
| 1661 | int16 expDiff; |
| 1662 | float64 z; |
| 1663 | |
| 1664 | aSig1 = extractFloat64Frac1( a ); |
| 1665 | aSig0 = extractFloat64Frac0( a ); |
| 1666 | aExp = extractFloat64Exp( a ); |
| 1667 | bSig1 = extractFloat64Frac1( b ); |
| 1668 | bSig0 = extractFloat64Frac0( b ); |
| 1669 | bExp = extractFloat64Exp( b ); |
| 1670 | expDiff = aExp - bExp; |
| 1671 | shortShift64Left( aSig0, aSig1, 10, &aSig0, &aSig1 ); |
| 1672 | shortShift64Left( bSig0, bSig1, 10, &bSig0, &bSig1 ); |
| 1673 | if ( 0 < expDiff ) goto aExpBigger; |
| 1674 | if ( expDiff < 0 ) goto bExpBigger; |
| 1675 | if ( aExp == 0x7FF ) { |
| 1676 | if ( aSig0 | aSig1 | bSig0 | bSig1 ) { |
| 1677 | return propagateFloat64NaN( a, b ); |
| 1678 | } |
| 1679 | float_raise( float_flag_invalid ); |
| 1680 | z.low = float64_default_nan_low; |
| 1681 | z.high = float64_default_nan_high; |
| 1682 | return z; |
| 1683 | } |
| 1684 | if ( aExp == 0 ) { |
| 1685 | aExp = 1; |
| 1686 | bExp = 1; |
| 1687 | } |
| 1688 | if ( bSig0 < aSig0 ) goto aBigger; |
| 1689 | if ( aSig0 < bSig0 ) goto bBigger; |
| 1690 | if ( bSig1 < aSig1 ) goto aBigger; |
| 1691 | if ( aSig1 < bSig1 ) goto bBigger; |
| 1692 | return packFloat64( float_rounding_mode == float_round_down, 0, 0, 0 ); |
| 1693 | bExpBigger: |
| 1694 | if ( bExp == 0x7FF ) { |
| 1695 | if ( bSig0 | bSig1 ) return propagateFloat64NaN( a, b ); |
| 1696 | return packFloat64( zSign ^ 1, 0x7FF, 0, 0 ); |
| 1697 | } |
| 1698 | if ( aExp == 0 ) { |
| 1699 | ++expDiff; |
| 1700 | } |
| 1701 | else { |
| 1702 | aSig0 |= 0x40000000; |
| 1703 | } |
| 1704 | shift64RightJamming( aSig0, aSig1, - expDiff, &aSig0, &aSig1 ); |
| 1705 | bSig0 |= 0x40000000; |
| 1706 | bBigger: |
| 1707 | sub64( bSig0, bSig1, aSig0, aSig1, &zSig0, &zSig1 ); |
| 1708 | zExp = bExp; |
| 1709 | zSign ^= 1; |
| 1710 | goto normalizeRoundAndPack; |
| 1711 | aExpBigger: |
| 1712 | if ( aExp == 0x7FF ) { |
| 1713 | if ( aSig0 | aSig1 ) return propagateFloat64NaN( a, b ); |
| 1714 | return a; |
| 1715 | } |
| 1716 | if ( bExp == 0 ) { |
| 1717 | --expDiff; |
| 1718 | } |
| 1719 | else { |
| 1720 | bSig0 |= 0x40000000; |
| 1721 | } |
| 1722 | shift64RightJamming( bSig0, bSig1, expDiff, &bSig0, &bSig1 ); |
| 1723 | aSig0 |= 0x40000000; |
| 1724 | aBigger: |
| 1725 | sub64( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1 ); |
| 1726 | zExp = aExp; |
| 1727 | normalizeRoundAndPack: |
| 1728 | --zExp; |
| 1729 | return normalizeRoundAndPackFloat64( zSign, zExp - 10, zSig0, zSig1 ); |
| 1730 | |
| 1731 | } |
| 1732 | |
| 1733 | /*---------------------------------------------------------------------------- |
| 1734 | | Returns the result of adding the double-precision floating-point values `a' |
| 1735 | | and `b'. The operation is performed according to the IEC/IEEE Standard for |
| 1736 | | Binary Floating-Point Arithmetic. |
| 1737 | *----------------------------------------------------------------------------*/ |
| 1738 | |
| 1739 | float64 float64_add( float64 a, float64 b ) |
| 1740 | { |
| 1741 | flag aSign, bSign; |
| 1742 | |
| 1743 | aSign = extractFloat64Sign( a ); |
| 1744 | bSign = extractFloat64Sign( b ); |
| 1745 | if ( aSign == bSign ) { |
| 1746 | return addFloat64Sigs( a, b, aSign ); |
| 1747 | } |
| 1748 | else { |
| 1749 | return subFloat64Sigs( a, b, aSign ); |
| 1750 | } |
| 1751 | |
| 1752 | } |
| 1753 | |
| 1754 | /*---------------------------------------------------------------------------- |
| 1755 | | Returns the result of subtracting the double-precision floating-point values |
| 1756 | | `a' and `b'. The operation is performed according to the IEC/IEEE Standard |
| 1757 | | for Binary Floating-Point Arithmetic. |
| 1758 | *----------------------------------------------------------------------------*/ |
| 1759 | |
| 1760 | float64 float64_sub( float64 a, float64 b ) |
| 1761 | { |
| 1762 | flag aSign, bSign; |
| 1763 | |
| 1764 | aSign = extractFloat64Sign( a ); |
| 1765 | bSign = extractFloat64Sign( b ); |
| 1766 | if ( aSign == bSign ) { |
| 1767 | return subFloat64Sigs( a, b, aSign ); |
| 1768 | } |
| 1769 | else { |
| 1770 | return addFloat64Sigs( a, b, aSign ); |
| 1771 | } |
| 1772 | |
| 1773 | } |
| 1774 | |
| 1775 | /*---------------------------------------------------------------------------- |
| 1776 | | Returns the result of multiplying the double-precision floating-point values |
| 1777 | | `a' and `b'. The operation is performed according to the IEC/IEEE Standard |
| 1778 | | for Binary Floating-Point Arithmetic. |
| 1779 | *----------------------------------------------------------------------------*/ |
| 1780 | |
| 1781 | float64 float64_mul( float64 a, float64 b ) |
| 1782 | { |
| 1783 | flag aSign, bSign, zSign; |
| 1784 | int16 aExp, bExp, zExp; |
| 1785 | bits32 aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2, zSig3; |
| 1786 | float64 z; |
| 1787 | |
| 1788 | aSig1 = extractFloat64Frac1( a ); |
| 1789 | aSig0 = extractFloat64Frac0( a ); |
| 1790 | aExp = extractFloat64Exp( a ); |
| 1791 | aSign = extractFloat64Sign( a ); |
| 1792 | bSig1 = extractFloat64Frac1( b ); |
| 1793 | bSig0 = extractFloat64Frac0( b ); |
| 1794 | bExp = extractFloat64Exp( b ); |
| 1795 | bSign = extractFloat64Sign( b ); |
| 1796 | zSign = aSign ^ bSign; |
| 1797 | if ( aExp == 0x7FF ) { |
| 1798 | if ( ( aSig0 | aSig1 ) |
| 1799 | || ( ( bExp == 0x7FF ) && ( bSig0 | bSig1 ) ) ) { |
| 1800 | return propagateFloat64NaN( a, b ); |
| 1801 | } |
| 1802 | if ( ( bExp | bSig0 | bSig1 ) == 0 ) goto invalid; |
| 1803 | return packFloat64( zSign, 0x7FF, 0, 0 ); |
| 1804 | } |
| 1805 | if ( bExp == 0x7FF ) { |
| 1806 | if ( bSig0 | bSig1 ) return propagateFloat64NaN( a, b ); |
| 1807 | if ( ( aExp | aSig0 | aSig1 ) == 0 ) { |
| 1808 | invalid: |
| 1809 | float_raise( float_flag_invalid ); |
| 1810 | z.low = float64_default_nan_low; |
| 1811 | z.high = float64_default_nan_high; |
| 1812 | return z; |
| 1813 | } |
| 1814 | return packFloat64( zSign, 0x7FF, 0, 0 ); |
| 1815 | } |
| 1816 | if ( aExp == 0 ) { |
| 1817 | if ( ( aSig0 | aSig1 ) == 0 ) return packFloat64( zSign, 0, 0, 0 ); |
| 1818 | normalizeFloat64Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 ); |
| 1819 | } |
| 1820 | if ( bExp == 0 ) { |
| 1821 | if ( ( bSig0 | bSig1 ) == 0 ) return packFloat64( zSign, 0, 0, 0 ); |
| 1822 | normalizeFloat64Subnormal( bSig0, bSig1, &bExp, &bSig0, &bSig1 ); |
| 1823 | } |
| 1824 | zExp = aExp + bExp - 0x400; |
| 1825 | aSig0 |= 0x00100000; |
| 1826 | shortShift64Left( bSig0, bSig1, 12, &bSig0, &bSig1 ); |
| 1827 | mul64To128( aSig0, aSig1, bSig0, bSig1, &zSig0, &zSig1, &zSig2, &zSig3 ); |
| 1828 | add64( zSig0, zSig1, aSig0, aSig1, &zSig0, &zSig1 ); |
| 1829 | zSig2 |= ( zSig3 != 0 ); |
| 1830 | if ( 0x00200000 <= zSig0 ) { |
| 1831 | shift64ExtraRightJamming( |
| 1832 | zSig0, zSig1, zSig2, 1, &zSig0, &zSig1, &zSig2 ); |
| 1833 | ++zExp; |
| 1834 | } |
| 1835 | return roundAndPackFloat64( zSign, zExp, zSig0, zSig1, zSig2 ); |
| 1836 | |
| 1837 | } |
| 1838 | |
| 1839 | /*---------------------------------------------------------------------------- |
| 1840 | | Returns the result of dividing the double-precision floating-point value `a' |
| 1841 | | by the corresponding value `b'. The operation is performed according to the |
| 1842 | | IEC/IEEE Standard for Binary Floating-Point Arithmetic. |
| 1843 | *----------------------------------------------------------------------------*/ |
| 1844 | |
| 1845 | float64 float64_div( float64 a, float64 b ) |
| 1846 | { |
| 1847 | flag aSign, bSign, zSign; |
| 1848 | int16 aExp, bExp, zExp; |
| 1849 | bits32 aSig0, aSig1, bSig0, bSig1, zSig0, zSig1, zSig2; |
| 1850 | bits32 rem0, rem1, rem2, rem3, term0, term1, term2, term3; |
| 1851 | float64 z; |
| 1852 | |
| 1853 | aSig1 = extractFloat64Frac1( a ); |
| 1854 | aSig0 = extractFloat64Frac0( a ); |
| 1855 | aExp = extractFloat64Exp( a ); |
| 1856 | aSign = extractFloat64Sign( a ); |
| 1857 | bSig1 = extractFloat64Frac1( b ); |
| 1858 | bSig0 = extractFloat64Frac0( b ); |
| 1859 | bExp = extractFloat64Exp( b ); |
| 1860 | bSign = extractFloat64Sign( b ); |
| 1861 | zSign = aSign ^ bSign; |
| 1862 | if ( aExp == 0x7FF ) { |
| 1863 | if ( aSig0 | aSig1 ) return propagateFloat64NaN( a, b ); |
| 1864 | if ( bExp == 0x7FF ) { |
| 1865 | if ( bSig0 | bSig1 ) return propagateFloat64NaN( a, b ); |
| 1866 | goto invalid; |
| 1867 | } |
| 1868 | return packFloat64( zSign, 0x7FF, 0, 0 ); |
| 1869 | } |
| 1870 | if ( bExp == 0x7FF ) { |
| 1871 | if ( bSig0 | bSig1 ) return propagateFloat64NaN( a, b ); |
| 1872 | return packFloat64( zSign, 0, 0, 0 ); |
| 1873 | } |
| 1874 | if ( bExp == 0 ) { |
| 1875 | if ( ( bSig0 | bSig1 ) == 0 ) { |
| 1876 | if ( ( aExp | aSig0 | aSig1 ) == 0 ) { |
| 1877 | invalid: |
| 1878 | float_raise( float_flag_invalid ); |
| 1879 | z.low = float64_default_nan_low; |
| 1880 | z.high = float64_default_nan_high; |
| 1881 | return z; |
| 1882 | } |
| 1883 | float_raise( float_flag_divbyzero ); |
| 1884 | return packFloat64( zSign, 0x7FF, 0, 0 ); |
| 1885 | } |
| 1886 | normalizeFloat64Subnormal( bSig0, bSig1, &bExp, &bSig0, &bSig1 ); |
| 1887 | } |
| 1888 | if ( aExp == 0 ) { |
| 1889 | if ( ( aSig0 | aSig1 ) == 0 ) return packFloat64( zSign, 0, 0, 0 ); |
| 1890 | normalizeFloat64Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 ); |
| 1891 | } |
| 1892 | zExp = aExp - bExp + 0x3FD; |
| 1893 | shortShift64Left( aSig0 | 0x00100000, aSig1, 11, &aSig0, &aSig1 ); |
| 1894 | shortShift64Left( bSig0 | 0x00100000, bSig1, 11, &bSig0, &bSig1 ); |
| 1895 | if ( le64( bSig0, bSig1, aSig0, aSig1 ) ) { |
| 1896 | shift64Right( aSig0, aSig1, 1, &aSig0, &aSig1 ); |
| 1897 | ++zExp; |
| 1898 | } |
| 1899 | zSig0 = estimateDiv64To32( aSig0, aSig1, bSig0 ); |
| 1900 | mul64By32To96( bSig0, bSig1, zSig0, &term0, &term1, &term2 ); |
| 1901 | sub96( aSig0, aSig1, 0, term0, term1, term2, &rem0, &rem1, &rem2 ); |
| 1902 | while ( (sbits32) rem0 < 0 ) { |
| 1903 | --zSig0; |
| 1904 | add96( rem0, rem1, rem2, 0, bSig0, bSig1, &rem0, &rem1, &rem2 ); |
| 1905 | } |
| 1906 | zSig1 = estimateDiv64To32( rem1, rem2, bSig0 ); |
| 1907 | if ( ( zSig1 & 0x3FF ) <= 4 ) { |
| 1908 | mul64By32To96( bSig0, bSig1, zSig1, &term1, &term2, &term3 ); |
| 1909 | sub96( rem1, rem2, 0, term1, term2, term3, &rem1, &rem2, &rem3 ); |
| 1910 | while ( (sbits32) rem1 < 0 ) { |
| 1911 | --zSig1; |
| 1912 | add96( rem1, rem2, rem3, 0, bSig0, bSig1, &rem1, &rem2, &rem3 ); |
| 1913 | } |
| 1914 | zSig1 |= ( ( rem1 | rem2 | rem3 ) != 0 ); |
| 1915 | } |
| 1916 | shift64ExtraRightJamming( zSig0, zSig1, 0, 11, &zSig0, &zSig1, &zSig2 ); |
| 1917 | return roundAndPackFloat64( zSign, zExp, zSig0, zSig1, zSig2 ); |
| 1918 | |
| 1919 | } |
| 1920 | |
| 1921 | /*---------------------------------------------------------------------------- |
| 1922 | | Returns the remainder of the double-precision floating-point value `a' |
| 1923 | | with respect to the corresponding value `b'. The operation is performed |
| 1924 | | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic. |
| 1925 | *----------------------------------------------------------------------------*/ |
| 1926 | |
| 1927 | float64 float64_rem( float64 a, float64 b ) |
| 1928 | { |
| 1929 | flag aSign, bSign, zSign; |
| 1930 | int16 aExp, bExp, expDiff; |
| 1931 | bits32 aSig0, aSig1, bSig0, bSig1, q, term0, term1, term2; |
| 1932 | bits32 allZero, alternateASig0, alternateASig1, sigMean1; |
| 1933 | sbits32 sigMean0; |
| 1934 | float64 z; |
| 1935 | |
| 1936 | aSig1 = extractFloat64Frac1( a ); |
| 1937 | aSig0 = extractFloat64Frac0( a ); |
| 1938 | aExp = extractFloat64Exp( a ); |
| 1939 | aSign = extractFloat64Sign( a ); |
| 1940 | bSig1 = extractFloat64Frac1( b ); |
| 1941 | bSig0 = extractFloat64Frac0( b ); |
| 1942 | bExp = extractFloat64Exp( b ); |
| 1943 | bSign = extractFloat64Sign( b ); |
| 1944 | if ( aExp == 0x7FF ) { |
| 1945 | if ( ( aSig0 | aSig1 ) |
| 1946 | || ( ( bExp == 0x7FF ) && ( bSig0 | bSig1 ) ) ) { |
| 1947 | return propagateFloat64NaN( a, b ); |
| 1948 | } |
| 1949 | goto invalid; |
| 1950 | } |
| 1951 | if ( bExp == 0x7FF ) { |
| 1952 | if ( bSig0 | bSig1 ) return propagateFloat64NaN( a, b ); |
| 1953 | return a; |
| 1954 | } |
| 1955 | if ( bExp == 0 ) { |
| 1956 | if ( ( bSig0 | bSig1 ) == 0 ) { |
| 1957 | invalid: |
| 1958 | float_raise( float_flag_invalid ); |
| 1959 | z.low = float64_default_nan_low; |
| 1960 | z.high = float64_default_nan_high; |
| 1961 | return z; |
| 1962 | } |
| 1963 | normalizeFloat64Subnormal( bSig0, bSig1, &bExp, &bSig0, &bSig1 ); |
| 1964 | } |
| 1965 | if ( aExp == 0 ) { |
| 1966 | if ( ( aSig0 | aSig1 ) == 0 ) return a; |
| 1967 | normalizeFloat64Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 ); |
| 1968 | } |
| 1969 | expDiff = aExp - bExp; |
| 1970 | if ( expDiff < -1 ) return a; |
| 1971 | shortShift64Left( |
| 1972 | aSig0 | 0x00100000, aSig1, 11 - ( expDiff < 0 ), &aSig0, &aSig1 ); |
| 1973 | shortShift64Left( bSig0 | 0x00100000, bSig1, 11, &bSig0, &bSig1 ); |
| 1974 | q = le64( bSig0, bSig1, aSig0, aSig1 ); |
| 1975 | if ( q ) sub64( aSig0, aSig1, bSig0, bSig1, &aSig0, &aSig1 ); |
| 1976 | expDiff -= 32; |
| 1977 | while ( 0 < expDiff ) { |
| 1978 | q = estimateDiv64To32( aSig0, aSig1, bSig0 ); |
| 1979 | q = ( 4 < q ) ? q - 4 : 0; |
| 1980 | mul64By32To96( bSig0, bSig1, q, &term0, &term1, &term2 ); |
| 1981 | shortShift96Left( term0, term1, term2, 29, &term1, &term2, &allZero ); |
| 1982 | shortShift64Left( aSig0, aSig1, 29, &aSig0, &allZero ); |
| 1983 | sub64( aSig0, 0, term1, term2, &aSig0, &aSig1 ); |
| 1984 | expDiff -= 29; |
| 1985 | } |
| 1986 | if ( -32 < expDiff ) { |
| 1987 | q = estimateDiv64To32( aSig0, aSig1, bSig0 ); |
| 1988 | q = ( 4 < q ) ? q - 4 : 0; |
| 1989 | q >>= - expDiff; |
| 1990 | shift64Right( bSig0, bSig1, 8, &bSig0, &bSig1 ); |
| 1991 | expDiff += 24; |
| 1992 | if ( expDiff < 0 ) { |
| 1993 | shift64Right( aSig0, aSig1, - expDiff, &aSig0, &aSig1 ); |
| 1994 | } |
| 1995 | else { |
| 1996 | shortShift64Left( aSig0, aSig1, expDiff, &aSig0, &aSig1 ); |
| 1997 | } |
| 1998 | mul64By32To96( bSig0, bSig1, q, &term0, &term1, &term2 ); |
| 1999 | sub64( aSig0, aSig1, term1, term2, &aSig0, &aSig1 ); |
| 2000 | } |
| 2001 | else { |
| 2002 | shift64Right( aSig0, aSig1, 8, &aSig0, &aSig1 ); |
| 2003 | shift64Right( bSig0, bSig1, 8, &bSig0, &bSig1 ); |
| 2004 | } |
| 2005 | do { |
| 2006 | alternateASig0 = aSig0; |
| 2007 | alternateASig1 = aSig1; |
| 2008 | ++q; |
| 2009 | sub64( aSig0, aSig1, bSig0, bSig1, &aSig0, &aSig1 ); |
| 2010 | } while ( 0 <= (sbits32) aSig0 ); |
| 2011 | add64( |
| 2012 | aSig0, aSig1, alternateASig0, alternateASig1, (bits32*)&sigMean0, &sigMean1 ); |
| 2013 | if ( ( sigMean0 < 0 ) |
| 2014 | || ( ( ( sigMean0 | sigMean1 ) == 0 ) && ( q & 1 ) ) ) { |
| 2015 | aSig0 = alternateASig0; |
| 2016 | aSig1 = alternateASig1; |
| 2017 | } |
| 2018 | zSign = ( (sbits32) aSig0 < 0 ); |
| 2019 | if ( zSign ) sub64( 0, 0, aSig0, aSig1, &aSig0, &aSig1 ); |
| 2020 | return |
| 2021 | normalizeRoundAndPackFloat64( aSign ^ zSign, bExp - 4, aSig0, aSig1 ); |
| 2022 | |
| 2023 | } |
| 2024 | |
| 2025 | /*---------------------------------------------------------------------------- |
| 2026 | | Returns the square root of the double-precision floating-point value `a'. |
| 2027 | | The operation is performed according to the IEC/IEEE Standard for Binary |
| 2028 | | Floating-Point Arithmetic. |
| 2029 | *----------------------------------------------------------------------------*/ |
| 2030 | |
| 2031 | float64 float64_sqrt( float64 a ) |
| 2032 | { |
| 2033 | flag aSign; |
| 2034 | int16 aExp, zExp; |
| 2035 | bits32 aSig0, aSig1, zSig0, zSig1, zSig2, doubleZSig0; |
| 2036 | bits32 rem0, rem1, rem2, rem3, term0, term1, term2, term3; |
| 2037 | float64 z; |
| 2038 | |
| 2039 | aSig1 = extractFloat64Frac1( a ); |
| 2040 | aSig0 = extractFloat64Frac0( a ); |
| 2041 | aExp = extractFloat64Exp( a ); |
| 2042 | aSign = extractFloat64Sign( a ); |
| 2043 | if ( aExp == 0x7FF ) { |
| 2044 | if ( aSig0 | aSig1 ) return propagateFloat64NaN( a, a ); |
| 2045 | if ( ! aSign ) return a; |
| 2046 | goto invalid; |
| 2047 | } |
| 2048 | if ( aSign ) { |
| 2049 | if ( ( aExp | aSig0 | aSig1 ) == 0 ) return a; |
| 2050 | invalid: |
| 2051 | float_raise( float_flag_invalid ); |
| 2052 | z.low = float64_default_nan_low; |
| 2053 | z.high = float64_default_nan_high; |
| 2054 | return z; |
| 2055 | } |
| 2056 | if ( aExp == 0 ) { |
| 2057 | if ( ( aSig0 | aSig1 ) == 0 ) return packFloat64( 0, 0, 0, 0 ); |
| 2058 | normalizeFloat64Subnormal( aSig0, aSig1, &aExp, &aSig0, &aSig1 ); |
| 2059 | } |
| 2060 | zExp = ( ( aExp - 0x3FF )>>1 ) + 0x3FE; |
| 2061 | aSig0 |= 0x00100000; |
| 2062 | shortShift64Left( aSig0, aSig1, 11, &term0, &term1 ); |
| 2063 | zSig0 = ( estimateSqrt32( aExp, term0 )>>1 ) + 1; |
| 2064 | if ( zSig0 == 0 ) zSig0 = 0x7FFFFFFF; |
| 2065 | doubleZSig0 = zSig0 + zSig0; |
| 2066 | shortShift64Left( aSig0, aSig1, 9 - ( aExp & 1 ), &aSig0, &aSig1 ); |
| 2067 | mul32To64( zSig0, zSig0, &term0, &term1 ); |
| 2068 | sub64( aSig0, aSig1, term0, term1, &rem0, &rem1 ); |
| 2069 | while ( (sbits32) rem0 < 0 ) { |
| 2070 | --zSig0; |
| 2071 | doubleZSig0 -= 2; |
| 2072 | add64( rem0, rem1, 0, doubleZSig0 | 1, &rem0, &rem1 ); |
| 2073 | } |
| 2074 | zSig1 = estimateDiv64To32( rem1, 0, doubleZSig0 ); |
| 2075 | if ( ( zSig1 & 0x1FF ) <= 5 ) { |
| 2076 | if ( zSig1 == 0 ) zSig1 = 1; |
| 2077 | mul32To64( doubleZSig0, zSig1, &term1, &term2 ); |
| 2078 | sub64( rem1, 0, term1, term2, &rem1, &rem2 ); |
| 2079 | mul32To64( zSig1, zSig1, &term2, &term3 ); |
| 2080 | sub96( rem1, rem2, 0, 0, term2, term3, &rem1, &rem2, &rem3 ); |
| 2081 | while ( (sbits32) rem1 < 0 ) { |
| 2082 | --zSig1; |
| 2083 | shortShift64Left( 0, zSig1, 1, &term2, &term3 ); |
| 2084 | term3 |= 1; |
| 2085 | term2 |= doubleZSig0; |
| 2086 | add96( rem1, rem2, rem3, 0, term2, term3, &rem1, &rem2, &rem3 ); |
| 2087 | } |
| 2088 | zSig1 |= ( ( rem1 | rem2 | rem3 ) != 0 ); |
| 2089 | } |
| 2090 | shift64ExtraRightJamming( zSig0, zSig1, 0, 10, &zSig0, &zSig1, &zSig2 ); |
| 2091 | return roundAndPackFloat64( 0, zExp, zSig0, zSig1, zSig2 ); |
| 2092 | |
| 2093 | } |
| 2094 | |
| 2095 | /*---------------------------------------------------------------------------- |
| 2096 | | Returns 1 if the double-precision floating-point value `a' is equal to |
| 2097 | | the corresponding value `b', and 0 otherwise. The comparison is performed |
| 2098 | | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic. |
| 2099 | *----------------------------------------------------------------------------*/ |
| 2100 | |
| 2101 | flag float64_eq( float64 a, float64 b ) |
| 2102 | { |
| 2103 | |
| 2104 | if ( ( ( extractFloat64Exp( a ) == 0x7FF ) |
| 2105 | && ( extractFloat64Frac0( a ) | extractFloat64Frac1( a ) ) ) |
| 2106 | || ( ( extractFloat64Exp( b ) == 0x7FF ) |
| 2107 | && ( extractFloat64Frac0( b ) | extractFloat64Frac1( b ) ) ) |
| 2108 | ) { |
| 2109 | if ( float64_is_signaling_nan( a ) || float64_is_signaling_nan( b ) ) { |
| 2110 | float_raise( float_flag_invalid ); |
| 2111 | } |
| 2112 | return 0; |
| 2113 | } |
| 2114 | return |
| 2115 | ( a.low == b.low ) |
| 2116 | && ( ( a.high == b.high ) |
| 2117 | || ( ( a.low == 0 ) |
| 2118 | && ( (bits32) ( ( a.high | b.high )<<1 ) == 0 ) ) |
| 2119 | ); |
| 2120 | |
| 2121 | } |
| 2122 | |
| 2123 | /*---------------------------------------------------------------------------- |
| 2124 | | Returns 1 if the double-precision floating-point value `a' is less than |
| 2125 | | or equal to the corresponding value `b', and 0 otherwise. The comparison |
| 2126 | | is performed according to the IEC/IEEE Standard for Binary Floating-Point |
| 2127 | | Arithmetic. |
| 2128 | *----------------------------------------------------------------------------*/ |
| 2129 | |
| 2130 | flag float64_le( float64 a, float64 b ) |
| 2131 | { |
| 2132 | flag aSign, bSign; |
| 2133 | |
| 2134 | if ( ( ( extractFloat64Exp( a ) == 0x7FF ) |
| 2135 | && ( extractFloat64Frac0( a ) | extractFloat64Frac1( a ) ) ) |
| 2136 | || ( ( extractFloat64Exp( b ) == 0x7FF ) |
| 2137 | && ( extractFloat64Frac0( b ) | extractFloat64Frac1( b ) ) ) |
| 2138 | ) { |
| 2139 | float_raise( float_flag_invalid ); |
| 2140 | return 0; |
| 2141 | } |
| 2142 | aSign = extractFloat64Sign( a ); |
| 2143 | bSign = extractFloat64Sign( b ); |
| 2144 | if ( aSign != bSign ) { |
| 2145 | return |
| 2146 | aSign |
| 2147 | || ( ( ( (bits32) ( ( a.high | b.high )<<1 ) ) | a.low | b.low ) |
| 2148 | == 0 ); |
| 2149 | } |
| 2150 | return |
| 2151 | aSign ? le64( b.high, b.low, a.high, a.low ) |
| 2152 | : le64( a.high, a.low, b.high, b.low ); |
| 2153 | |
| 2154 | } |
| 2155 | |
| 2156 | /*---------------------------------------------------------------------------- |
| 2157 | | Returns 1 if the double-precision floating-point value `a' is less than |
| 2158 | | the corresponding value `b', and 0 otherwise. The comparison is performed |
| 2159 | | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic. |
| 2160 | *----------------------------------------------------------------------------*/ |
| 2161 | |
| 2162 | flag float64_lt( float64 a, float64 b ) |
| 2163 | { |
| 2164 | flag aSign, bSign; |
| 2165 | |
| 2166 | if ( ( ( extractFloat64Exp( a ) == 0x7FF ) |
| 2167 | && ( extractFloat64Frac0( a ) | extractFloat64Frac1( a ) ) ) |
| 2168 | || ( ( extractFloat64Exp( b ) == 0x7FF ) |
| 2169 | && ( extractFloat64Frac0( b ) | extractFloat64Frac1( b ) ) ) |
| 2170 | ) { |
| 2171 | float_raise( float_flag_invalid ); |
| 2172 | return 0; |
| 2173 | } |
| 2174 | aSign = extractFloat64Sign( a ); |
| 2175 | bSign = extractFloat64Sign( b ); |
| 2176 | if ( aSign != bSign ) { |
| 2177 | return |
| 2178 | aSign |
| 2179 | && ( ( ( (bits32) ( ( a.high | b.high )<<1 ) ) | a.low | b.low ) |
| 2180 | != 0 ); |
| 2181 | } |
| 2182 | return |
| 2183 | aSign ? lt64( b.high, b.low, a.high, a.low ) |
| 2184 | : lt64( a.high, a.low, b.high, b.low ); |
| 2185 | |
| 2186 | } |
| 2187 | |
| 2188 | /*---------------------------------------------------------------------------- |
| 2189 | | Returns 1 if the double-precision floating-point value `a' is equal to |
| 2190 | | the corresponding value `b', and 0 otherwise. The invalid exception is |
| 2191 | | raised if either operand is a NaN. Otherwise, the comparison is performed |
| 2192 | | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic. |
| 2193 | *----------------------------------------------------------------------------*/ |
| 2194 | |
| 2195 | flag float64_eq_signaling( float64 a, float64 b ) |
| 2196 | { |
| 2197 | |
| 2198 | if ( ( ( extractFloat64Exp( a ) == 0x7FF ) |
| 2199 | && ( extractFloat64Frac0( a ) | extractFloat64Frac1( a ) ) ) |
| 2200 | || ( ( extractFloat64Exp( b ) == 0x7FF ) |
| 2201 | && ( extractFloat64Frac0( b ) | extractFloat64Frac1( b ) ) ) |
| 2202 | ) { |
| 2203 | float_raise( float_flag_invalid ); |
| 2204 | return 0; |
| 2205 | } |
| 2206 | return |
| 2207 | ( a.low == b.low ) |
| 2208 | && ( ( a.high == b.high ) |
| 2209 | || ( ( a.low == 0 ) |
| 2210 | && ( (bits32) ( ( a.high | b.high )<<1 ) == 0 ) ) |
| 2211 | ); |
| 2212 | |
| 2213 | } |
| 2214 | |
| 2215 | /*---------------------------------------------------------------------------- |
| 2216 | | Returns 1 if the double-precision floating-point value `a' is less than or |
| 2217 | | equal to the corresponding value `b', and 0 otherwise. Quiet NaNs do not |
| 2218 | | cause an exception. Otherwise, the comparison is performed according to the |
| 2219 | | IEC/IEEE Standard for Binary Floating-Point Arithmetic. |
| 2220 | *----------------------------------------------------------------------------*/ |
| 2221 | |
| 2222 | flag float64_le_quiet( float64 a, float64 b ) |
| 2223 | { |
| 2224 | flag aSign, bSign; |
| 2225 | |
| 2226 | if ( ( ( extractFloat64Exp( a ) == 0x7FF ) |
| 2227 | && ( extractFloat64Frac0( a ) | extractFloat64Frac1( a ) ) ) |
| 2228 | || ( ( extractFloat64Exp( b ) == 0x7FF ) |
| 2229 | && ( extractFloat64Frac0( b ) | extractFloat64Frac1( b ) ) ) |
| 2230 | ) { |
| 2231 | if ( float64_is_signaling_nan( a ) || float64_is_signaling_nan( b ) ) { |
| 2232 | float_raise( float_flag_invalid ); |
| 2233 | } |
| 2234 | return 0; |
| 2235 | } |
| 2236 | aSign = extractFloat64Sign( a ); |
| 2237 | bSign = extractFloat64Sign( b ); |
| 2238 | if ( aSign != bSign ) { |
| 2239 | return |
| 2240 | aSign |
| 2241 | || ( ( ( (bits32) ( ( a.high | b.high )<<1 ) ) | a.low | b.low ) |
| 2242 | == 0 ); |
| 2243 | } |
| 2244 | return |
| 2245 | aSign ? le64( b.high, b.low, a.high, a.low ) |
| 2246 | : le64( a.high, a.low, b.high, b.low ); |
| 2247 | |
| 2248 | } |
| 2249 | |
| 2250 | /*---------------------------------------------------------------------------- |
| 2251 | | Returns 1 if the double-precision floating-point value `a' is less than |
| 2252 | | the corresponding value `b', and 0 otherwise. Quiet NaNs do not cause an |
| 2253 | | exception. Otherwise, the comparison is performed according to the IEC/IEEE |
| 2254 | | Standard for Binary Floating-Point Arithmetic. |
| 2255 | *----------------------------------------------------------------------------*/ |
| 2256 | |
| 2257 | flag float64_lt_quiet( float64 a, float64 b ) |
| 2258 | { |
| 2259 | flag aSign, bSign; |
| 2260 | |
| 2261 | if ( ( ( extractFloat64Exp( a ) == 0x7FF ) |
| 2262 | && ( extractFloat64Frac0( a ) | extractFloat64Frac1( a ) ) ) |
| 2263 | || ( ( extractFloat64Exp( b ) == 0x7FF ) |
| 2264 | && ( extractFloat64Frac0( b ) | extractFloat64Frac1( b ) ) ) |
| 2265 | ) { |
| 2266 | if ( float64_is_signaling_nan( a ) || float64_is_signaling_nan( b ) ) { |
| 2267 | float_raise( float_flag_invalid ); |
| 2268 | } |
| 2269 | return 0; |
| 2270 | } |
| 2271 | aSign = extractFloat64Sign( a ); |
| 2272 | bSign = extractFloat64Sign( b ); |
| 2273 | if ( aSign != bSign ) { |
| 2274 | return |
| 2275 | aSign |
| 2276 | && ( ( ( (bits32) ( ( a.high | b.high )<<1 ) ) | a.low | b.low ) |
| 2277 | != 0 ); |
| 2278 | } |
| 2279 | return |
| 2280 | aSign ? lt64( b.high, b.low, a.high, a.low ) |
| 2281 | : lt64( a.high, a.low, b.high, b.low ); |
| 2282 | |
| 2283 | } |
| 2284 |