Changes in uspace/lib/softfloat/generic/conversion.c [9d58539:88d5c1e] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/softfloat/generic/conversion.c
r9d58539 r88d5c1e 39 39 #include <common.h> 40 40 41 float64 convertFloat32ToFloat64(float32 a)41 float64 float32_to_float64(float32 a) 42 42 { 43 43 float64 result; … … 48 48 result.parts.fraction <<= (FLOAT64_FRACTION_SIZE - FLOAT32_FRACTION_SIZE); 49 49 50 if ((is Float32Infinity(a)) || (isFloat32NaN(a))) {50 if ((is_float32_infinity(a)) || (is_float32_nan(a))) { 51 51 result.parts.exp = FLOAT64_MAX_EXPONENT; 52 / * TODO; check if its correct for SigNaNs*/52 // 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 convertFloat32ToFloat128(float32 a)79 float128 float32_to_float128(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 Float32Infinity(a)) || (isFloat32NaN(a))) {93 94 if ((is_float32_infinity(a)) || (is_float32_nan(a))) { 95 95 result.parts.exp = FLOAT128_MAX_EXPONENT; 96 / * TODO; check if its correct for SigNaNs*/97 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 convertFloat64ToFloat128(float64 a)125 126 return result; 127 } 128 129 float128 float64_to_float128(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 Float64Infinity(a)) || (isFloat64NaN(a))) {143 144 if ((is_float64_infinity(a)) || (is_float64_nan(a))) { 145 145 result.parts.exp = FLOAT128_MAX_EXPONENT; 146 / * TODO; check if its correct for SigNaNs*/147 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 convertFloat64ToFloat32(float64 a)175 176 return result; 177 } 178 179 float32 float64_to_float32(float64 a) 180 180 { 181 181 float32 result; … … 185 185 result.parts.sign = a.parts.sign; 186 186 187 if (is Float64NaN(a)) {187 if (is_float64_nan(a)) { 188 188 result.parts.exp = FLOAT32_MAX_EXPONENT; 189 189 190 if (is Float64SigNaN(a)) {190 if (is_float64_signan(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 Float64Infinity(a)) {200 201 if (is_float64_infinity(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 convertFloat128ToFloat32(float128 a)248 float32 float128_to_float32(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 Float128NaN(a)) {255 256 if (is_float128_nan(a)) { 257 257 result.parts.exp = FLOAT32_MAX_EXPONENT; 258 259 if (is Float128SigNaN(a)) {258 259 if (is_float128_signan(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 Float128Infinity(a)) {269 270 if (is_float128_infinity(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 convertFloat128ToFloat64(float128 a)326 float64 float128_to_float64(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 Float128NaN(a)) {333 334 if (is_float128_nan(a)) { 335 335 result.parts.exp = FLOAT64_MAX_EXPONENT; 336 337 if (is Float128SigNaN(a)) {336 337 if (is_float128_signan(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 Float128Infinity(a)) {347 348 if (is_float128_infinity(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 405 /** 406 * Helping procedure for converting float32 to uint32. 404 /** Helper procedure for converting float32 to uint32. 407 405 * 408 406 * @param a Floating point number in normalized form … … 424 422 /* shift fraction to left so hidden bit will be the most significant bit */ 425 423 frac <<= 32 - FLOAT32_FRACTION_SIZE - 1; 426 424 427 425 frac >>= 32 - (a.parts.exp - FLOAT32_BIAS) - 1; 428 426 if ((a.parts.sign == 1) && (frac != 0)) { … … 434 432 } 435 433 436 /* 437 * FIXME: Im not sure what to return if overflow/underflow happens 438 * 439 */ 434 /* 435 * FIXME: Im not sure what to return if overflow/underflow happens 436 * - now its the biggest or the smallest int 437 */ 440 438 uint32_t float32_to_uint32(float32 a) 441 439 { 442 if (is Float32NaN(a))440 if (is_float32_nan(a)) 443 441 return UINT32_MAX; 444 442 445 if (is Float32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS))) {443 if (is_float32_infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS))) { 446 444 if (a.parts.sign) 447 445 return UINT32_MIN; … … 453 451 } 454 452 455 /* 456 * FIXME: Im not sure what to return if overflow/underflow happens 457 * 458 */ 453 /* 454 * FIXME: Im not sure what to return if overflow/underflow happens 455 * - now its the biggest or the smallest int 456 */ 459 457 int32_t float32_to_int32(float32 a) 460 458 { 461 if (is Float32NaN(a))459 if (is_float32_nan(a)) 462 460 return INT32_MAX; 463 461 464 if (is Float32Infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS))) {462 if (is_float32_infinity(a) || (a.parts.exp >= (32 + FLOAT32_BIAS))) { 465 463 if (a.parts.sign) 466 464 return INT32_MIN; … … 472 470 } 473 471 474 475 /** 476 * Helping procedure for converting float32 to uint64. 472 /** Helper procedure for converting float32 to uint64. 477 473 * 478 474 * @param a Floating point number in normalized form … … 483 479 { 484 480 uint64_t frac; 485 481 486 482 if (a.parts.exp < FLOAT32_BIAS) { 487 / *TODO: rounding*/483 // TODO: rounding 488 484 return 0; 489 485 } 490 486 491 487 frac = a.parts.fraction; 492 488 493 489 frac |= FLOAT32_HIDDEN_BIT_MASK; 494 490 /* shift fraction to left so hidden bit will be the most significant bit */ 495 491 frac <<= 64 - FLOAT32_FRACTION_SIZE - 1; 496 492 497 493 frac >>= 64 - (a.parts.exp - FLOAT32_BIAS) - 1; 498 494 if ((a.parts.sign == 1) && (frac != 0)) { … … 500 496 ++frac; 501 497 } 502 498 503 499 return frac; 504 500 } 505 501 506 /* 502 /* 507 503 * FIXME: Im not sure what to return if overflow/underflow happens 508 * 504 * - now its the biggest or the smallest int 509 505 */ 510 506 uint64_t float32_to_uint64(float32 a) 511 507 { 512 if (is Float32NaN(a))508 if (is_float32_nan(a)) 513 509 return UINT64_MAX; 514 515 516 if (isFloat32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS))) { 510 511 if (is_float32_infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS))) { 517 512 if (a.parts.sign) 518 513 return UINT64_MIN; 519 514 520 515 return UINT64_MAX; 521 516 } 522 517 523 518 return _float32_to_uint64_helper(a); 524 519 } 525 520 526 /* 521 /* 527 522 * FIXME: Im not sure what to return if overflow/underflow happens 528 * 523 * - now its the biggest or the smallest int 529 524 */ 530 525 int64_t float32_to_int64(float32 a) 531 526 { 532 if (is Float32NaN(a))527 if (is_float32_nan(a)) 533 528 return INT64_MAX; 534 535 if (is Float32Infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS))) {529 530 if (is_float32_infinity(a) || (a.parts.exp >= (64 + FLOAT32_BIAS))) { 536 531 if (a.parts.sign) 537 532 return INT64_MIN; 538 533 539 534 return INT64_MAX; 540 535 } 541 536 542 537 return _float32_to_uint64_helper(a); 543 538 } 544 539 545 546 /** 547 * Helping procedure for converting float64 to uint64. 540 /** Helper procedure for converting float64 to uint64. 548 541 * 549 542 * @param a Floating point number in normalized form … … 554 547 { 555 548 uint64_t frac; 556 549 557 550 if (a.parts.exp < FLOAT64_BIAS) { 558 / *TODO: rounding*/551 // TODO: rounding 559 552 return 0; 560 553 } 561 554 562 555 frac = a.parts.fraction; 563 556 564 557 frac |= FLOAT64_HIDDEN_BIT_MASK; 565 558 /* shift fraction to left so hidden bit will be the most significant bit */ 566 559 frac <<= 64 - FLOAT64_FRACTION_SIZE - 1; 567 560 568 561 frac >>= 64 - (a.parts.exp - FLOAT64_BIAS) - 1; 569 562 if ((a.parts.sign == 1) && (frac != 0)) { … … 571 564 ++frac; 572 565 } 573 566 574 567 return frac; 575 568 } … … 577 570 /* 578 571 * FIXME: Im not sure what to return if overflow/underflow happens 579 * 572 * - now its the biggest or the smallest int 580 573 */ 581 574 uint32_t float64_to_uint32(float64 a) 582 575 { 583 if (is Float64NaN(a))576 if (is_float64_nan(a)) 584 577 return UINT32_MAX; 585 586 if (is Float64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS))) {578 579 if (is_float64_infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS))) { 587 580 if (a.parts.sign) 588 581 return UINT32_MIN; 589 582 590 583 return UINT32_MAX; 591 584 } 592 585 593 586 return (uint32_t) _float64_to_uint64_helper(a); 594 587 } … … 596 589 /* 597 590 * FIXME: Im not sure what to return if overflow/underflow happens 598 * 591 * - now its the biggest or the smallest int 599 592 */ 600 593 int32_t float64_to_int32(float64 a) 601 594 { 602 if (is Float64NaN(a))595 if (is_float64_nan(a)) 603 596 return INT32_MAX; 604 605 if (is Float64Infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS))) {597 598 if (is_float64_infinity(a) || (a.parts.exp >= (32 + FLOAT64_BIAS))) { 606 599 if (a.parts.sign) 607 600 return INT32_MIN; 608 601 609 602 return INT32_MAX; 610 603 } 611 604 612 605 return (int32_t) _float64_to_uint64_helper(a); 613 606 } 614 607 615 616 /* 608 /* 617 609 * FIXME: Im not sure what to return if overflow/underflow happens 618 * 619 */ 610 * - now its the biggest or the smallest int 611 */ 620 612 uint64_t float64_to_uint64(float64 a) 621 613 { 622 if (is Float64NaN(a))614 if (is_float64_nan(a)) 623 615 return UINT64_MAX; 624 616 625 if (is Float64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) {617 if (is_float64_infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) { 626 618 if (a.parts.sign) 627 619 return UINT64_MIN; … … 633 625 } 634 626 635 /* 627 /* 636 628 * FIXME: Im not sure what to return if overflow/underflow happens 637 * 638 */ 629 * - now its the biggest or the smallest int 630 */ 639 631 int64_t float64_to_int64(float64 a) 640 632 { 641 if (is Float64NaN(a))633 if (is_float64_nan(a)) 642 634 return INT64_MAX; 643 635 644 if (is Float64Infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) {636 if (is_float64_infinity(a) || (a.parts.exp >= (64 + FLOAT64_BIAS))) { 645 637 if (a.parts.sign) 646 638 return INT64_MIN; … … 652 644 } 653 645 654 655 /** 656 * Helping procedure for converting float128 to uint64. 646 /** Helper procedure for converting float128 to uint64. 657 647 * 658 648 * @param a Floating point number in normalized form … … 663 653 { 664 654 uint64_t frac_hi, frac_lo; 665 655 666 656 if (a.parts.exp < FLOAT128_BIAS) { 667 / *TODO: rounding*/657 // TODO: rounding 668 658 return 0; 669 659 } 670 660 671 661 frac_hi = a.parts.frac_hi; 672 662 frac_lo = a.parts.frac_lo; 673 663 674 664 frac_hi |= FLOAT128_HIDDEN_BIT_MASK_HI; 675 665 /* shift fraction to left so hidden bit will be the most significant bit */ 676 666 lshift128(frac_hi, frac_lo, 677 667 (128 - FLOAT128_FRACTION_SIZE - 1), &frac_hi, &frac_lo); 678 668 679 669 rshift128(frac_hi, frac_lo, 680 670 (128 - (a.parts.exp - FLOAT128_BIAS) - 1), &frac_hi, &frac_lo); … … 683 673 add128(frac_hi, frac_lo, 0x0ll, 0x1ll, &frac_hi, &frac_lo); 684 674 } 685 675 686 676 return frac_lo; 687 677 } … … 689 679 /* 690 680 * FIXME: Im not sure what to return if overflow/underflow happens 691 * 681 * - now its the biggest or the smallest int 692 682 */ 693 683 uint32_t float128_to_uint32(float128 a) 694 684 { 695 if (is Float128NaN(a))685 if (is_float128_nan(a)) 696 686 return UINT32_MAX; 697 698 if (is Float128Infinity(a) || (a.parts.exp >= (32 + FLOAT128_BIAS))) {687 688 if (is_float128_infinity(a) || (a.parts.exp >= (32 + FLOAT128_BIAS))) { 699 689 if (a.parts.sign) 700 690 return UINT32_MIN; 701 691 702 692 return UINT32_MAX; 703 693 } 704 694 705 695 return (uint32_t) _float128_to_uint64_helper(a); 706 696 } … … 708 698 /* 709 699 * FIXME: Im not sure what to return if overflow/underflow happens 710 * 700 * - now its the biggest or the smallest int 711 701 */ 712 702 int32_t float128_to_int32(float128 a) 713 703 { 714 if (is Float128NaN(a))704 if (is_float128_nan(a)) 715 705 return INT32_MAX; 716 717 if (is Float128Infinity(a) || (a.parts.exp >= (32 + FLOAT128_BIAS))) {706 707 if (is_float128_infinity(a) || (a.parts.exp >= (32 + FLOAT128_BIAS))) { 718 708 if (a.parts.sign) 719 709 return INT32_MIN; 720 710 721 711 return INT32_MAX; 722 712 } 723 713 724 714 return (int32_t) _float128_to_uint64_helper(a); 725 715 } 726 716 727 728 717 /* 729 718 * FIXME: Im not sure what to return if overflow/underflow happens 730 * 719 * - now its the biggest or the smallest int 731 720 */ 732 721 uint64_t float128_to_uint64(float128 a) 733 722 { 734 if (is Float128NaN(a))723 if (is_float128_nan(a)) 735 724 return UINT64_MAX; 736 737 if (is Float128Infinity(a) || (a.parts.exp >= (64 + FLOAT128_BIAS))) {725 726 if (is_float128_infinity(a) || (a.parts.exp >= (64 + FLOAT128_BIAS))) { 738 727 if (a.parts.sign) 739 728 return UINT64_MIN; 740 729 741 730 return UINT64_MAX; 742 731 } 743 732 744 733 return _float128_to_uint64_helper(a); 745 734 } … … 747 736 /* 748 737 * FIXME: Im not sure what to return if overflow/underflow happens 749 * 738 * - now its the biggest or the smallest int 750 739 */ 751 740 int64_t float128_to_int64(float128 a) 752 741 { 753 if (is Float128NaN(a))742 if (is_float128_nan(a)) 754 743 return INT64_MAX; 755 756 if (is Float128Infinity(a) || (a.parts.exp >= (64 + FLOAT128_BIAS))) {744 745 if (is_float128_infinity(a) || (a.parts.exp >= (64 + FLOAT128_BIAS))) { 757 746 if (a.parts.sign) 758 747 return INT64_MIN; 759 748 760 749 return INT64_MAX; 761 750 } 762 751 763 752 return _float128_to_uint64_helper(a); 764 753 } 765 766 754 767 755 float32 uint32_to_float32(uint32_t i) … … 773 761 result.parts.sign = 0; 774 762 result.parts.fraction = 0; 775 776 counter = count Zeroes32(i);777 763 764 counter = count_zeroes32(i); 765 778 766 exp = FLOAT32_BIAS + 32 - counter - 1; 779 767 780 768 if (counter == 32) { 781 result.bin ary= 0;769 result.bin = 0; 782 770 return result; 783 771 } … … 788 776 i >>= 1; 789 777 } 790 791 round Float32(&exp, &i);792 778 779 round_float32(&exp, &i); 780 793 781 result.parts.fraction = i >> (32 - FLOAT32_FRACTION_SIZE - 2); 794 782 result.parts.exp = exp; 795 796 return result; 797 } 798 799 float32 int32_to_float32(int32_t i) 783 784 return result; 785 } 786 787 float32 int32_to_float32(int32_t i) 800 788 { 801 789 float32 result; 802 803 if (i < 0) {790 791 if (i < 0) 804 792 result = uint32_to_float32((uint32_t) (-i)); 805 } else {793 else 806 794 result = uint32_to_float32((uint32_t) i); 807 }808 795 809 796 result.parts.sign = i < 0; 810 811 return result; 812 } 813 814 815 float32 uint64_to_float32(uint64_t i) 797 798 return result; 799 } 800 801 float32 uint64_to_float32(uint64_t i) 816 802 { 817 803 int counter; … … 822 808 result.parts.sign = 0; 823 809 result.parts.fraction = 0; 824 825 counter = count Zeroes64(i);826 810 811 counter = count_zeroes64(i); 812 827 813 exp = FLOAT32_BIAS + 64 - counter - 1; 828 814 829 815 if (counter == 64) { 830 result.bin ary= 0;816 result.bin = 0; 831 817 return result; 832 818 } … … 840 826 841 827 j = (uint32_t) i; 842 round Float32(&exp, &j);843 828 round_float32(&exp, &j); 829 844 830 result.parts.fraction = j >> (32 - FLOAT32_FRACTION_SIZE - 2); 845 831 result.parts.exp = exp; … … 847 833 } 848 834 849 float32 int64_to_float32(int64_t i) 835 float32 int64_to_float32(int64_t i) 850 836 { 851 837 float32 result; 852 853 if (i < 0) {838 839 if (i < 0) 854 840 result = uint64_to_float32((uint64_t) (-i)); 855 } else {841 else 856 842 result = uint64_to_float32((uint64_t) i); 857 }858 843 859 844 result.parts.sign = i < 0; 860 861 845 846 return result; 862 847 } 863 848 … … 871 856 result.parts.sign = 0; 872 857 result.parts.fraction = 0; 873 874 counter = count Zeroes32(i);875 858 859 counter = count_zeroes32(i); 860 876 861 exp = FLOAT64_BIAS + 32 - counter - 1; 877 862 878 863 if (counter == 32) { 879 result.bin ary= 0;864 result.bin = 0; 880 865 return result; 881 866 } 882 867 883 868 frac = i; 884 frac <<= counter + 32 - 1; 885 886 round Float64(&exp, &frac);887 869 frac <<= counter + 32 - 1; 870 871 round_float64(&exp, &frac); 872 888 873 result.parts.fraction = frac >> (64 - FLOAT64_FRACTION_SIZE - 2); 889 874 result.parts.exp = exp; 890 891 return result; 892 } 893 894 float64 int32_to_float64(int32_t i) 875 876 return result; 877 } 878 879 float64 int32_to_float64(int32_t i) 895 880 { 896 881 float64 result; 897 898 if (i < 0) {882 883 if (i < 0) 899 884 result = uint32_to_float64((uint32_t) (-i)); 900 } else {885 else 901 886 result = uint32_to_float64((uint32_t) i); 902 }903 887 904 888 result.parts.sign = i < 0; 905 906 907 } 908 909 910 float64 uint64_to_float64(uint64_t i) 889 890 return result; 891 } 892 893 894 float64 uint64_to_float64(uint64_t i) 911 895 { 912 896 int counter; … … 916 900 result.parts.sign = 0; 917 901 result.parts.fraction = 0; 918 919 counter = count Zeroes64(i);920 902 903 counter = count_zeroes64(i); 904 921 905 exp = FLOAT64_BIAS + 64 - counter - 1; 922 906 923 907 if (counter == 64) { 924 result.bin ary= 0;908 result.bin = 0; 925 909 return result; 926 910 } … … 931 915 i >>= 1; 932 916 } 933 934 round Float64(&exp, &i);935 917 918 round_float64(&exp, &i); 919 936 920 result.parts.fraction = i >> (64 - FLOAT64_FRACTION_SIZE - 2); 937 921 result.parts.exp = exp; … … 939 923 } 940 924 941 float64 int64_to_float64(int64_t i) 925 float64 int64_to_float64(int64_t i) 942 926 { 943 927 float64 result; 944 945 if (i < 0) {928 929 if (i < 0) 946 930 result = uint64_to_float64((uint64_t) (-i)); 947 } else {931 else 948 932 result = uint64_to_float64((uint64_t) i); 949 }950 933 951 934 result.parts.sign = i < 0; 952 953 return result; 954 } 955 935 936 return result; 937 } 956 938 957 939 float128 uint32_to_float128(uint32_t i) … … 961 943 float128 result; 962 944 uint64_t frac_hi, frac_lo; 963 945 964 946 result.parts.sign = 0; 965 947 result.parts.frac_hi = 0; 966 948 result.parts.frac_lo = 0; 967 968 counter = count Zeroes32(i);969 949 950 counter = count_zeroes32(i); 951 970 952 exp = FLOAT128_BIAS + 32 - counter - 1; 971 953 972 954 if (counter == 32) { 973 result.bin ary.hi = 0;974 result.bin ary.lo = 0;975 return result; 976 } 977 955 result.bin.hi = 0; 956 result.bin.lo = 0; 957 return result; 958 } 959 978 960 frac_hi = 0; 979 961 frac_lo = i; 980 962 lshift128(frac_hi, frac_lo, (counter + 96 - 1), &frac_hi, &frac_lo); 981 982 round Float128(&exp, &frac_hi, &frac_lo);983 963 964 round_float128(&exp, &frac_hi, &frac_lo); 965 984 966 rshift128(frac_hi, frac_lo, 985 967 (128 - FLOAT128_FRACTION_SIZE - 2), &frac_hi, &frac_lo); … … 987 969 result.parts.frac_lo = frac_lo; 988 970 result.parts.exp = exp; 989 971 990 972 return result; 991 973 } … … 994 976 { 995 977 float128 result; 996 997 if (i < 0) {978 979 if (i < 0) 998 980 result = uint32_to_float128((uint32_t) (-i)); 999 } else {981 else 1000 982 result = uint32_to_float128((uint32_t) i); 1001 } 1002 983 1003 984 result.parts.sign = i < 0; 1004 1005 985 986 return result; 1006 987 } 1007 988 … … 1013 994 float128 result; 1014 995 uint64_t frac_hi, frac_lo; 1015 996 1016 997 result.parts.sign = 0; 1017 998 result.parts.frac_hi = 0; 1018 999 result.parts.frac_lo = 0; 1019 1020 counter = count Zeroes64(i);1021 1000 1001 counter = count_zeroes64(i); 1002 1022 1003 exp = FLOAT128_BIAS + 64 - counter - 1; 1023 1004 1024 1005 if (counter == 64) { 1025 result.bin ary.hi = 0;1026 result.bin ary.lo = 0;1027 return result; 1028 } 1029 1006 result.bin.hi = 0; 1007 result.bin.lo = 0; 1008 return result; 1009 } 1010 1030 1011 frac_hi = 0; 1031 1012 frac_lo = i; 1032 1013 lshift128(frac_hi, frac_lo, (counter + 64 - 1), &frac_hi, &frac_lo); 1033 1034 round Float128(&exp, &frac_hi, &frac_lo);1035 1014 1015 round_float128(&exp, &frac_hi, &frac_lo); 1016 1036 1017 rshift128(frac_hi, frac_lo, 1037 1018 (128 - FLOAT128_FRACTION_SIZE - 2), &frac_hi, &frac_lo); … … 1039 1020 result.parts.frac_lo = frac_lo; 1040 1021 result.parts.exp = exp; 1041 1022 1042 1023 return result; 1043 1024 } … … 1046 1027 { 1047 1028 float128 result; 1048 1049 if (i < 0) {1029 1030 if (i < 0) 1050 1031 result = uint64_to_float128((uint64_t) (-i)); 1051 } else {1032 else 1052 1033 result = uint64_to_float128((uint64_t) i); 1053 } 1054 1034 1055 1035 result.parts.sign = i < 0; 1056 1057 1036 1037 return result; 1058 1038 } 1059 1039
Note:
See TracChangeset
for help on using the changeset viewer.