Changes in uspace/lib/softfloat/generic/conversion.c [88d5c1e:9d58539] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/softfloat/generic/conversion.c
r88d5c1e r9d58539 39 39 #include <common.h> 40 40 41 float64 float32_to_float64(float32 a)41 float64 convertFloat32ToFloat64(float32 a) 42 42 { 43 43 float64 result; … … 48 48 result.parts.fraction <<= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE); 49 49 50 if ((is _float32_infinity(a)) || (is_float32_nan(a))) {50 if ((isFloat32Infinity(a)) || (isFloat32NaN(a))) { 51 51 result.parts.exp = FLOAT64_MAX_EXPONENT; 52 / / TODO; check if its correct for SigNaNs52 /* TODO; check if its correct for SigNaNs*/ 53 53 return result; 54 54 } … … 57 57 if (a.parts.exp == 0) { 58 58 /* normalize denormalized numbers */ 59 59 60 60 if (result.parts.fraction == 0) { /* fix zero */ 61 61 result.parts.exp = 0; … … 77 77 } 78 78 79 float128 float32_to_float128(float32 a)79 float128 convertFloat32ToFloat128(float32 a) 80 80 { 81 81 float128 result; 82 82 uint64_t frac_hi, frac_lo; 83 83 uint64_t tmp_hi, tmp_lo; 84 84 85 85 result.parts.sign = a.parts.sign; 86 86 result.parts.frac_hi = 0; … … 91 91 result.parts.frac_hi = frac_hi; 92 92 result.parts.frac_lo = frac_lo; 93 94 if ((is _float32_infinity(a)) || (is_float32_nan(a))) {93 94 if ((isFloat32Infinity(a)) || (isFloat32NaN(a))) { 95 95 result.parts.exp = FLOAT128_MAX_EXPONENT; 96 / / TODO; check if its correct for SigNaNs97 return result; 98 } 99 96 /* TODO; check if its correct for SigNaNs*/ 97 return result; 98 } 99 100 100 result.parts.exp = a.parts.exp + ((int) FLOAT128_BIAS - FLOAT32_BIAS); 101 101 if (a.parts.exp == 0) { 102 102 /* normalize denormalized numbers */ 103 103 104 104 if (eq128(result.parts.frac_hi, 105 105 result.parts.frac_lo, 0x0ll, 0x0ll)) { /* fix zero */ … … 107 107 return result; 108 108 } 109 109 110 110 frac_hi = result.parts.frac_hi; 111 111 frac_lo = result.parts.frac_lo; 112 112 113 113 and128(frac_hi, frac_lo, 114 114 FLOAT128_HIDDEN_BIT_MASK_HI, FLOAT128_HIDDEN_BIT_MASK_LO, … … 118 118 --result.parts.exp; 119 119 } 120 120 121 121 ++result.parts.exp; 122 122 result.parts.frac_hi = frac_hi; 123 123 result.parts.frac_lo = frac_lo; 124 124 } 125 126 return result; 127 } 128 129 float128 float64_to_float128(float64 a)125 126 return result; 127 } 128 129 float128 convertFloat64ToFloat128(float64 a) 130 130 { 131 131 float128 result; 132 132 uint64_t frac_hi, frac_lo; 133 133 uint64_t tmp_hi, tmp_lo; 134 134 135 135 result.parts.sign = a.parts.sign; 136 136 result.parts.frac_hi = 0; … … 141 141 result.parts.frac_hi = frac_hi; 142 142 result.parts.frac_lo = frac_lo; 143 144 if ((is _float64_infinity(a)) || (is_float64_nan(a))) {143 144 if ((isFloat64Infinity(a)) || (isFloat64NaN(a))) { 145 145 result.parts.exp = FLOAT128_MAX_EXPONENT; 146 / / TODO; check if its correct for SigNaNs147 return result; 148 } 149 146 /* TODO; check if its correct for SigNaNs*/ 147 return result; 148 } 149 150 150 result.parts.exp = a.parts.exp + ((int) FLOAT128_BIAS - FLOAT64_BIAS); 151 151 if (a.parts.exp == 0) { 152 152 /* normalize denormalized numbers */ 153 153 154 154 if (eq128(result.parts.frac_hi, 155 155 result.parts.frac_lo, 0x0ll, 0x0ll)) { /* fix zero */ … … 157 157 return result; 158 158 } 159 159 160 160 frac_hi = result.parts.frac_hi; 161 161 frac_lo = result.parts.frac_lo; 162 162 163 163 and128(frac_hi, frac_lo, 164 164 FLOAT128_HIDDEN_BIT_MASK_HI, FLOAT128_HIDDEN_BIT_MASK_LO, … … 168 168 --result.parts.exp; 169 169 } 170 170 171 171 ++result.parts.exp; 172 172 result.parts.frac_hi = frac_hi; 173 173 result.parts.frac_lo = frac_lo; 174 174 } 175 176 return result; 177 } 178 179 float32 float64_to_float32(float64 a)175 176 return result; 177 } 178 179 float32 convertFloat64ToFloat32(float64 a) 180 180 { 181 181 float32 result; … … 185 185 result.parts.sign = a.parts.sign; 186 186 187 if (is _float64_nan(a)) {187 if (isFloat64NaN(a)) { 188 188 result.parts.exp = FLOAT32_MAX_EXPONENT; 189 189 190 if (is _float64_signan(a)) {190 if (isFloat64SigNaN(a)) { 191 191 /* set first bit of fraction nonzero */ 192 192 result.parts.fraction = FLOAT32_HIDDEN_BIT_MASK >> 1; 193 193 return result; 194 194 } 195 195 196 196 /* fraction nonzero but its first bit is zero */ 197 197 result.parts.fraction = 0x1; 198 198 return result; 199 199 } 200 201 if (is _float64_infinity(a)) {200 201 if (isFloat64Infinity(a)) { 202 202 result.parts.fraction = 0; 203 203 result.parts.exp = FLOAT32_MAX_EXPONENT; 204 204 return result; 205 205 } 206 206 207 207 exp = (int) a.parts.exp - FLOAT64_BIAS + FLOAT32_BIAS; 208 208 … … 239 239 return result; 240 240 } 241 241 242 242 result.parts.exp = exp; 243 243 result.parts.fraction = … … 246 246 } 247 247 248 float32 float128_to_float32(float128 a)248 float32 convertFloat128ToFloat32(float128 a) 249 249 { 250 250 float32 result; 251 251 int32_t exp; 252 252 uint64_t frac_hi, frac_lo; 253 253 254 254 result.parts.sign = a.parts.sign; 255 256 if (is _float128_nan(a)) {255 256 if (isFloat128NaN(a)) { 257 257 result.parts.exp = FLOAT32_MAX_EXPONENT; 258 259 if (is _float128_signan(a)) {258 259 if (isFloat128SigNaN(a)) { 260 260 /* set first bit of fraction nonzero */ 261 261 result.parts.fraction = FLOAT32_HIDDEN_BIT_MASK >> 1; 262 262 return result; 263 263 } 264 264 265 265 /* fraction nonzero but its first bit is zero */ 266 266 result.parts.fraction = 0x1; 267 267 return result; 268 268 } 269 270 if (is _float128_infinity(a)) {269 270 if (isFloat128Infinity(a)) { 271 271 result.parts.fraction = 0; 272 272 result.parts.exp = FLOAT32_MAX_EXPONENT; 273 273 return result; 274 274 } 275 275 276 276 exp = (int) a.parts.exp - FLOAT128_BIAS + FLOAT32_BIAS; 277 277 278 278 if (exp >= FLOAT32_MAX_EXPONENT) { 279 279 /* FIXME: overflow */ … … 283 283 } else if (exp <= 0) { 284 284 /* underflow or denormalized */ 285 285 286 286 result.parts.exp = 0; 287 287 288 288 exp *= -1; 289 289 if (exp > FLOAT32_FRACTION_SIZE) { … … 292 292 return result; 293 293 } 294 294 295 295 /* denormalized */ 296 296 297 297 frac_hi = a.parts.frac_hi; 298 298 frac_lo = a.parts.frac_lo; 299 299 300 300 /* denormalize and set hidden bit */ 301 301 frac_hi |= FLOAT128_HIDDEN_BIT_MASK_HI; 302 302 303 303 rshift128(frac_hi, frac_lo, 304 304 (FLOAT128_FRACTION_SIZE - FLOAT32_FRACTION_SIZE + 1), 305 305 &frac_hi, &frac_lo); 306 306 307 307 while (exp > 0) { 308 308 --exp; … … 310 310 } 311 311 result.parts.fraction = frac_lo; 312 313 return result; 314 } 315 312 313 return result; 314 } 315 316 316 result.parts.exp = exp; 317 317 frac_hi = a.parts.frac_hi; … … 324 324 } 325 325 326 float64 float128_to_float64(float128 a)326 float64 convertFloat128ToFloat64(float128 a) 327 327 { 328 328 float64 result; 329 329 int32_t exp; 330 330 uint64_t frac_hi, frac_lo; 331 331 332 332 result.parts.sign = a.parts.sign; 333 334 if (is _float128_nan(a)) {333 334 if (isFloat128NaN(a)) { 335 335 result.parts.exp = FLOAT64_MAX_EXPONENT; 336 337 if (is _float128_signan(a)) {336 337 if (isFloat128SigNaN(a)) { 338 338 /* set first bit of fraction nonzero */ 339 339 result.parts.fraction = FLOAT64_HIDDEN_BIT_MASK >> 1; 340 340 return result; 341 341 } 342 342 343 343 /* fraction nonzero but its first bit is zero */ 344 344 result.parts.fraction = 0x1; 345 345 return result; 346 346 } 347 348 if (is _float128_infinity(a)) {347 348 if (isFloat128Infinity(a)) { 349 349 result.parts.fraction = 0; 350 350 result.parts.exp = FLOAT64_MAX_EXPONENT; 351 351 return result; 352 352 } 353 353 354 354 exp = (int) a.parts.exp - FLOAT128_BIAS + FLOAT64_BIAS; 355 355 356 356 if (exp >= FLOAT64_MAX_EXPONENT) { 357 357 /* FIXME: overflow */ … … 361 361 } else if (exp <= 0) { 362 362 /* underflow or denormalized */ 363 363 364 364 result.parts.exp = 0; 365 365 366 366 exp *= -1; 367 367 if (exp > FLOAT64_FRACTION_SIZE) { … … 370 370 return result; 371 371 } 372 372 373 373 /* denormalized */ 374 374 375 375 frac_hi = a.parts.frac_hi; 376 376 frac_lo = a.parts.frac_lo; 377 377 378 378 /* denormalize and set hidden bit */ 379 379 frac_hi |= FLOAT128_HIDDEN_BIT_MASK_HI; 380 380 381 381 rshift128(frac_hi, frac_lo, 382 382 (FLOAT128_FRACTION_SIZE - FLOAT64_FRACTION_SIZE + 1), 383 383 &frac_hi, &frac_lo); 384 384 385 385 while (exp > 0) { 386 386 --exp; … … 388 388 } 389 389 result.parts.fraction = frac_lo; 390 391 return result; 392 } 393 390 391 return result; 392 } 393 394 394 result.parts.exp = exp; 395 395 frac_hi = a.parts.frac_hi; … … 402 402 } 403 403 404 /** Helper procedure for converting float32 to uint32. 404 405 /** 406 * Helping procedure for converting float32 to uint32. 405 407 * 406 408 * @param a Floating point number in normalized form … … 422 424 /* shift fraction to left so hidden bit will be the most significant bit */ 423 425 frac <<= 32 - FLOAT32_FRACTION_SIZE - 1; 424 426 425 427 frac >>= 32 - (a.parts.exp - FLOAT32_BIAS) - 1; 426 428 if ((a.parts.sign == 1) && (frac != 0)) { … … 432 434 } 433 435 434 /* 435 * FIXME: Im not sure what to return if overflow/underflow happens 436 * 437 */ 436 /* 437 * FIXME: Im not sure what to return if overflow/underflow happens 438 * - now its the biggest or the smallest int 439 */ 438 440 uint32_t float32_to_uint32(float32 a) 439 441 { 440 if (is _float32_nan(a))442 if (isFloat32NaN(a)) 441 443 return UINT32_MAX; 442 444 443 if (is _float32_infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS))) {445 if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS))) { 444 446 if (a.parts.sign) 445 447 return UINT32_MIN; … … 451 453 } 452 454 453 /* 454 * FIXME: Im not sure what to return if overflow/underflow happens 455 * 456 */ 455 /* 456 * FIXME: Im not sure what to return if overflow/underflow happens 457 * - now its the biggest or the smallest int 458 */ 457 459 int32_t float32_to_int32(float32 a) 458 460 { 459 if (is _float32_nan(a))461 if (isFloat32NaN(a)) 460 462 return INT32_MAX; 461 463 462 if (is _float32_infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS))) {464 if (isFloat32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS))) { 463 465 if (a.parts.sign) 464 466 return INT32_MIN; … … 470 472 } 471 473 472 /** Helper procedure for converting float32 to uint64. 474 475 /** 476 * Helping procedure for converting float32 to uint64. 473 477 * 474 478 * @param a Floating point number in normalized form … … 479 483 { 480 484 uint64_t frac; 481 485 482 486 if (a.parts.exp < FLOAT32_BIAS) { 483 / / TODO: rounding487 /*TODO: rounding*/ 484 488 return 0; 485 489 } 486 490 487 491 frac = a.parts.fraction; 488 492 489 493 frac |= FLOAT32_HIDDEN_BIT_MASK; 490 494 /* shift fraction to left so hidden bit will be the most significant bit */ 491 495 frac <<= 64 - FLOAT32_FRACTION_SIZE - 1; 492 496 493 497 frac >>= 64 - (a.parts.exp - FLOAT32_BIAS) - 1; 494 498 if ((a.parts.sign == 1) && (frac != 0)) { … … 496 500 ++frac; 497 501 } 498 502 499 503 return frac; 500 504 } 501 505 502 /* 506 /* 503 507 * FIXME: Im not sure what to return if overflow/underflow happens 504 * 508 * - now its the biggest or the smallest int 505 509 */ 506 510 uint64_t float32_to_uint64(float32 a) 507 511 { 508 if (is _float32_nan(a))512 if (isFloat32NaN(a)) 509 513 return UINT64_MAX; 510 511 if (is_float32_infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS))) { 514 515 516 if (isFloat32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS))) { 512 517 if (a.parts.sign) 513 518 return UINT64_MIN; 514 519 515 520 return UINT64_MAX; 516 521 } 517 522 518 523 return _float32_to_uint64_helper(a); 519 524 } 520 525 521 /* 526 /* 522 527 * FIXME: Im not sure what to return if overflow/underflow happens 523 * 528 * - now its the biggest or the smallest int 524 529 */ 525 530 int64_t float32_to_int64(float32 a) 526 531 { 527 if (is _float32_nan(a))532 if (isFloat32NaN(a)) 528 533 return INT64_MAX; 529 530 if (is _float32_infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS))) {534 535 if (isFloat32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS))) { 531 536 if (a.parts.sign) 532 537 return INT64_MIN; 533 538 534 539 return INT64_MAX; 535 540 } 536 541 537 542 return _float32_to_uint64_helper(a); 538 543 } 539 544 540 /** Helper procedure for converting float64 to uint64. 545 546 /** 547 * Helping procedure for converting float64 to uint64. 541 548 * 542 549 * @param a Floating point number in normalized form … … 547 554 { 548 555 uint64_t frac; 549 556 550 557 if (a.parts.exp < FLOAT64_BIAS) { 551 / / TODO: rounding558 /*TODO: rounding*/ 552 559 return 0; 553 560 } 554 561 555 562 frac = a.parts.fraction; 556 563 557 564 frac |= FLOAT64_HIDDEN_BIT_MASK; 558 565 /* shift fraction to left so hidden bit will be the most significant bit */ 559 566 frac <<= 64 - FLOAT64_FRACTION_SIZE - 1; 560 567 561 568 frac >>= 64 - (a.parts.exp - FLOAT64_BIAS) - 1; 562 569 if ((a.parts.sign == 1) && (frac != 0)) { … … 564 571 ++frac; 565 572 } 566 573 567 574 return frac; 568 575 } … … 570 577 /* 571 578 * FIXME: Im not sure what to return if overflow/underflow happens 572 * 579 * - now its the biggest or the smallest int 573 580 */ 574 581 uint32_t float64_to_uint32(float64 a) 575 582 { 576 if (is _float64_nan(a))583 if (isFloat64NaN(a)) 577 584 return UINT32_MAX; 578 579 if (is _float64_infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS))) {585 586 if (isFloat64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS))) { 580 587 if (a.parts.sign) 581 588 return UINT32_MIN; 582 589 583 590 return UINT32_MAX; 584 591 } 585 592 586 593 return (uint32_t) _float64_to_uint64_helper(a); 587 594 } … … 589 596 /* 590 597 * FIXME: Im not sure what to return if overflow/underflow happens 591 * 598 * - now its the biggest or the smallest int 592 599 */ 593 600 int32_t float64_to_int32(float64 a) 594 601 { 595 if (is _float64_nan(a))602 if (isFloat64NaN(a)) 596 603 return INT32_MAX; 597 598 if (is _float64_infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS))) {604 605 if (isFloat64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS))) { 599 606 if (a.parts.sign) 600 607 return INT32_MIN; 601 608 602 609 return INT32_MAX; 603 610 } 604 611 605 612 return (int32_t) _float64_to_uint64_helper(a); 606 613 } 607 614 608 /* 615 616 /* 609 617 * FIXME: Im not sure what to return if overflow/underflow happens 610 * 611 */ 618 * - now its the biggest or the smallest int 619 */ 612 620 uint64_t float64_to_uint64(float64 a) 613 621 { 614 if (is _float64_nan(a))622 if (isFloat64NaN(a)) 615 623 return UINT64_MAX; 616 624 617 if (is _float64_infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) {625 if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) { 618 626 if (a.parts.sign) 619 627 return UINT64_MIN; … … 625 633 } 626 634 627 /* 635 /* 628 636 * FIXME: Im not sure what to return if overflow/underflow happens 629 * 630 */ 637 * - now its the biggest or the smallest int 638 */ 631 639 int64_t float64_to_int64(float64 a) 632 640 { 633 if (is _float64_nan(a))641 if (isFloat64NaN(a)) 634 642 return INT64_MAX; 635 643 636 if (is _float64_infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) {644 if (isFloat64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) { 637 645 if (a.parts.sign) 638 646 return INT64_MIN; … … 644 652 } 645 653 646 /** Helper procedure for converting float128 to uint64. 654 655 /** 656 * Helping procedure for converting float128 to uint64. 647 657 * 648 658 * @param a Floating point number in normalized form … … 653 663 { 654 664 uint64_t frac_hi, frac_lo; 655 665 656 666 if (a.parts.exp < FLOAT128_BIAS) { 657 / / TODO: rounding667 /*TODO: rounding*/ 658 668 return 0; 659 669 } 660 670 661 671 frac_hi = a.parts.frac_hi; 662 672 frac_lo = a.parts.frac_lo; 663 673 664 674 frac_hi |= FLOAT128_HIDDEN_BIT_MASK_HI; 665 675 /* shift fraction to left so hidden bit will be the most significant bit */ 666 676 lshift128(frac_hi, frac_lo, 667 677 (128 - FLOAT128_FRACTION_SIZE - 1), &frac_hi, &frac_lo); 668 678 669 679 rshift128(frac_hi, frac_lo, 670 680 (128 - (a.parts.exp - FLOAT128_BIAS) - 1), &frac_hi, &frac_lo); … … 673 683 add128(frac_hi, frac_lo, 0x0ll, 0x1ll, &frac_hi, &frac_lo); 674 684 } 675 685 676 686 return frac_lo; 677 687 } … … 679 689 /* 680 690 * FIXME: Im not sure what to return if overflow/underflow happens 681 * 691 * - now its the biggest or the smallest int 682 692 */ 683 693 uint32_t float128_to_uint32(float128 a) 684 694 { 685 if (is _float128_nan(a))695 if (isFloat128NaN(a)) 686 696 return UINT32_MAX; 687 688 if (is _float128_infinity(a) || (a.parts.exp >= (32 + FLOAT128_BIAS))) {697 698 if (isFloat128Infinity(a) || (a.parts.exp >= (32 + FLOAT128_BIAS))) { 689 699 if (a.parts.sign) 690 700 return UINT32_MIN; 691 701 692 702 return UINT32_MAX; 693 703 } 694 704 695 705 return (uint32_t) _float128_to_uint64_helper(a); 696 706 } … … 698 708 /* 699 709 * FIXME: Im not sure what to return if overflow/underflow happens 700 * 710 * - now its the biggest or the smallest int 701 711 */ 702 712 int32_t float128_to_int32(float128 a) 703 713 { 704 if (is _float128_nan(a))714 if (isFloat128NaN(a)) 705 715 return INT32_MAX; 706 707 if (is _float128_infinity(a) || (a.parts.exp >= (32 + FLOAT128_BIAS))) {716 717 if (isFloat128Infinity(a) || (a.parts.exp >= (32 + FLOAT128_BIAS))) { 708 718 if (a.parts.sign) 709 719 return INT32_MIN; 710 720 711 721 return INT32_MAX; 712 722 } 713 723 714 724 return (int32_t) _float128_to_uint64_helper(a); 715 725 } 726 716 727 717 728 /* 718 729 * FIXME: Im not sure what to return if overflow/underflow happens 719 * 730 * - now its the biggest or the smallest int 720 731 */ 721 732 uint64_t float128_to_uint64(float128 a) 722 733 { 723 if (is _float128_nan(a))734 if (isFloat128NaN(a)) 724 735 return UINT64_MAX; 725 726 if (is _float128_infinity(a) || (a.parts.exp >= (64 + FLOAT128_BIAS))) {736 737 if (isFloat128Infinity(a) || (a.parts.exp >= (64 + FLOAT128_BIAS))) { 727 738 if (a.parts.sign) 728 739 return UINT64_MIN; 729 740 730 741 return UINT64_MAX; 731 742 } 732 743 733 744 return _float128_to_uint64_helper(a); 734 745 } … … 736 747 /* 737 748 * FIXME: Im not sure what to return if overflow/underflow happens 738 * 749 * - now its the biggest or the smallest int 739 750 */ 740 751 int64_t float128_to_int64(float128 a) 741 752 { 742 if (is _float128_nan(a))753 if (isFloat128NaN(a)) 743 754 return INT64_MAX; 744 745 if (is _float128_infinity(a) || (a.parts.exp >= (64 + FLOAT128_BIAS))) {755 756 if (isFloat128Infinity(a) || (a.parts.exp >= (64 + FLOAT128_BIAS))) { 746 757 if (a.parts.sign) 747 758 return INT64_MIN; 748 759 749 760 return INT64_MAX; 750 761 } 751 762 752 763 return _float128_to_uint64_helper(a); 753 764 } 765 754 766 755 767 float32 uint32_to_float32(uint32_t i) … … 761 773 result.parts.sign = 0; 762 774 result.parts.fraction = 0; 763 764 counter = count _zeroes32(i);765 775 776 counter = countZeroes32(i); 777 766 778 exp = FLOAT32_BIAS + 32 - counter - 1; 767 779 768 780 if (counter == 32) { 769 result.bin = 0;781 result.binary = 0; 770 782 return result; 771 783 } … … 776 788 i >>= 1; 777 789 } 778 779 round _float32(&exp, &i);780 790 791 roundFloat32(&exp, &i); 792 781 793 result.parts.fraction = i >> (32 - FLOAT32_FRACTION_SIZE - 2); 782 794 result.parts.exp = exp; 783 784 return result; 785 } 786 787 float32 int32_to_float32(int32_t i) 795 796 return result; 797 } 798 799 float32 int32_to_float32(int32_t i) 788 800 { 789 801 float32 result; 790 791 if (i < 0) 802 803 if (i < 0) { 792 804 result = uint32_to_float32((uint32_t) (-i)); 793 else805 } else { 794 806 result = uint32_to_float32((uint32_t) i); 807 } 795 808 796 809 result.parts.sign = i < 0; 797 798 return result; 799 } 800 801 float32 uint64_to_float32(uint64_t i) 810 811 return result; 812 } 813 814 815 float32 uint64_to_float32(uint64_t i) 802 816 { 803 817 int counter; … … 808 822 result.parts.sign = 0; 809 823 result.parts.fraction = 0; 810 811 counter = count _zeroes64(i);812 824 825 counter = countZeroes64(i); 826 813 827 exp = FLOAT32_BIAS + 64 - counter - 1; 814 828 815 829 if (counter == 64) { 816 result.bin = 0;830 result.binary = 0; 817 831 return result; 818 832 } … … 826 840 827 841 j = (uint32_t) i; 828 round _float32(&exp, &j);829 842 roundFloat32(&exp, &j); 843 830 844 result.parts.fraction = j >> (32 - FLOAT32_FRACTION_SIZE - 2); 831 845 result.parts.exp = exp; … … 833 847 } 834 848 835 float32 int64_to_float32(int64_t i) 849 float32 int64_to_float32(int64_t i) 836 850 { 837 851 float32 result; 838 839 if (i < 0) 852 853 if (i < 0) { 840 854 result = uint64_to_float32((uint64_t) (-i)); 841 else855 } else { 842 856 result = uint64_to_float32((uint64_t) i); 857 } 843 858 844 859 result.parts.sign = i < 0; 845 846 return result;860 861 return result; 847 862 } 848 863 … … 856 871 result.parts.sign = 0; 857 872 result.parts.fraction = 0; 858 859 counter = count _zeroes32(i);860 873 874 counter = countZeroes32(i); 875 861 876 exp = FLOAT64_BIAS + 32 - counter - 1; 862 877 863 878 if (counter == 32) { 864 result.bin = 0;879 result.binary = 0; 865 880 return result; 866 881 } 867 882 868 883 frac = i; 869 frac <<= counter + 32 - 1; 870 871 round _float64(&exp, &frac);872 884 frac <<= counter + 32 - 1; 885 886 roundFloat64(&exp, &frac); 887 873 888 result.parts.fraction = frac >> (64 - FLOAT64_FRACTION_SIZE - 2); 874 889 result.parts.exp = exp; 875 876 return result; 877 } 878 879 float64 int32_to_float64(int32_t i) 890 891 return result; 892 } 893 894 float64 int32_to_float64(int32_t i) 880 895 { 881 896 float64 result; 882 883 if (i < 0) 897 898 if (i < 0) { 884 899 result = uint32_to_float64((uint32_t) (-i)); 885 else900 } else { 886 901 result = uint32_to_float64((uint32_t) i); 902 } 887 903 888 904 result.parts.sign = i < 0; 889 890 return result;891 } 892 893 894 float64 uint64_to_float64(uint64_t i) 905 906 return result; 907 } 908 909 910 float64 uint64_to_float64(uint64_t i) 895 911 { 896 912 int counter; … … 900 916 result.parts.sign = 0; 901 917 result.parts.fraction = 0; 902 903 counter = count _zeroes64(i);904 918 919 counter = countZeroes64(i); 920 905 921 exp = FLOAT64_BIAS + 64 - counter - 1; 906 922 907 923 if (counter == 64) { 908 result.bin = 0;924 result.binary = 0; 909 925 return result; 910 926 } … … 915 931 i >>= 1; 916 932 } 917 918 round _float64(&exp, &i);919 933 934 roundFloat64(&exp, &i); 935 920 936 result.parts.fraction = i >> (64 - FLOAT64_FRACTION_SIZE - 2); 921 937 result.parts.exp = exp; … … 923 939 } 924 940 925 float64 int64_to_float64(int64_t i) 941 float64 int64_to_float64(int64_t i) 926 942 { 927 943 float64 result; 928 929 if (i < 0) 944 945 if (i < 0) { 930 946 result = uint64_to_float64((uint64_t) (-i)); 931 else947 } else { 932 948 result = uint64_to_float64((uint64_t) i); 949 } 933 950 934 951 result.parts.sign = i < 0; 935 936 return result; 937 } 952 953 return result; 954 } 955 938 956 939 957 float128 uint32_to_float128(uint32_t i) … … 943 961 float128 result; 944 962 uint64_t frac_hi, frac_lo; 945 963 946 964 result.parts.sign = 0; 947 965 result.parts.frac_hi = 0; 948 966 result.parts.frac_lo = 0; 949 950 counter = count _zeroes32(i);951 967 968 counter = countZeroes32(i); 969 952 970 exp = FLOAT128_BIAS + 32 - counter - 1; 953 971 954 972 if (counter == 32) { 955 result.bin .hi = 0;956 result.bin .lo = 0;957 return result; 958 } 959 973 result.binary.hi = 0; 974 result.binary.lo = 0; 975 return result; 976 } 977 960 978 frac_hi = 0; 961 979 frac_lo = i; 962 980 lshift128(frac_hi, frac_lo, (counter + 96 - 1), &frac_hi, &frac_lo); 963 964 round _float128(&exp, &frac_hi, &frac_lo);965 981 982 roundFloat128(&exp, &frac_hi, &frac_lo); 983 966 984 rshift128(frac_hi, frac_lo, 967 985 (128 - FLOAT128_FRACTION_SIZE - 2), &frac_hi, &frac_lo); … … 969 987 result.parts.frac_lo = frac_lo; 970 988 result.parts.exp = exp; 971 989 972 990 return result; 973 991 } … … 976 994 { 977 995 float128 result; 978 979 if (i < 0) 996 997 if (i < 0) { 980 998 result = uint32_to_float128((uint32_t) (-i)); 981 else999 } else { 982 1000 result = uint32_to_float128((uint32_t) i); 983 1001 } 1002 984 1003 result.parts.sign = i < 0; 985 986 return result;1004 1005 return result; 987 1006 } 988 1007 … … 994 1013 float128 result; 995 1014 uint64_t frac_hi, frac_lo; 996 1015 997 1016 result.parts.sign = 0; 998 1017 result.parts.frac_hi = 0; 999 1018 result.parts.frac_lo = 0; 1000 1001 counter = count _zeroes64(i);1002 1019 1020 counter = countZeroes64(i); 1021 1003 1022 exp = FLOAT128_BIAS + 64 - counter - 1; 1004 1023 1005 1024 if (counter == 64) { 1006 result.bin .hi = 0;1007 result.bin .lo = 0;1008 return result; 1009 } 1010 1025 result.binary.hi = 0; 1026 result.binary.lo = 0; 1027 return result; 1028 } 1029 1011 1030 frac_hi = 0; 1012 1031 frac_lo = i; 1013 1032 lshift128(frac_hi, frac_lo, (counter + 64 - 1), &frac_hi, &frac_lo); 1014 1015 round _float128(&exp, &frac_hi, &frac_lo);1016 1033 1034 roundFloat128(&exp, &frac_hi, &frac_lo); 1035 1017 1036 rshift128(frac_hi, frac_lo, 1018 1037 (128 - FLOAT128_FRACTION_SIZE - 2), &frac_hi, &frac_lo); … … 1020 1039 result.parts.frac_lo = frac_lo; 1021 1040 result.parts.exp = exp; 1022 1041 1023 1042 return result; 1024 1043 } … … 1027 1046 { 1028 1047 float128 result; 1029 1030 if (i < 0) 1048 1049 if (i < 0) { 1031 1050 result = uint64_to_float128((uint64_t) (-i)); 1032 else1051 } else { 1033 1052 result = uint64_to_float128((uint64_t) i); 1034 1053 } 1054 1035 1055 result.parts.sign = i < 0; 1036 1037 return result;1056 1057 return result; 1038 1058 } 1039 1059
Note:
See TracChangeset
for help on using the changeset viewer.