1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
|
/* Basic functions for Long-Floats */
/* error-message for too long Long-FLoat */
nonreturning_function(local, fehler_LF_toolong, (void)) {
fehler(arithmetic_error,GETTEXT("long float too long"));
}
/* Decoding a Long-Float:
LF_decode(obj, zero_statement, sign=,exp=,mantMSDptr=,mantlen=,mantLSDptr=);
decodes a Long-Float obj.
If obj=0.0, zero_statement is executed.
Else: signean sign = sign (0 = +, -1 = -),
sintL exp = exponent (with sign),
UDS mantMSDptr/mantlen/mantLSDptr = mantissa
(>= 2^(intDsize*mantlen-1), < 2^(intDsize*mantlen)),
with mantlen>=LF_minlen. */
#define LF_decode(obj,zero_statement,sign_zuweisung,exp_zuweisung,mantMSDptr_zuweisung,mantlen_zuweisung,mantLSDptr_zuweisung) do { \
var object _obj = (obj); \
var Lfloat _x = TheLfloat(_obj); \
var uintL uexp = _x->expo; \
if (uexp==0) { \
unused (mantlen_zuweisung lfloat_length(_x)); \
zero_statement; /* e=0 -> number 0.0 */ \
} else { \
exp_zuweisung (sintL)(uexp - LF_exp_mid); /* exponent */ \
sign_zuweisung LF_sign(_obj); /* sign */ \
unused (mantMSDptr_zuweisung &(_x->data[0])); /* mantissa-UDS */ \
unused (mantLSDptr_zuweisung &(_x->data[(uintP)( mantlen_zuweisung lfloat_length(_x) )])); \
} \
} while(0)
/* encoding a Long-Float:
encode_LF0(len,erg_zuweisung) returns a Long-Float 0.0 with len digits.
> uintC len: number of digits
< object erg: new Long-Float 0.0 with len digits
can trigger GC */
#define encode_LF0(len,erg_zuweisung) do { \
var uintC _len = (len); \
var object _erg = allocate_lfloat(_len,0,0); /* exponent 0, sign + */ \
clear_loop_up(&TheLfloat(_erg)->data[0],_len); /* mantissa := 0 */ \
erg_zuweisung _erg; \
} while(0)
/* encoding a Long-Float:
encode_LF1s(sign,len,erg_zuweisung) returns a Long-Float +-1.0 with len digits.
> signean sign: sign
> uintC len: number of digits
< object erg: new Long-Float +1.0 or -1.0 with len digits
can trigger GC */
#define encode_LF1s(sign,len,erg_zuweisung) do { \
var uintC _len = (len); \
var object _erg = allocate_lfloat(_len,LF_exp_mid+1,(sign)); /* exponent 1 */ \
TheLfloat(_erg)->data[0] = bit(intDsize-1); /* mantissa := 2^(intDsize*len-1) */ \
clear_loop_up(&TheLfloat(_erg)->data[1],_len-1); \
erg_zuweisung _erg; \
} while(0)
/* encoding a Long-Float:
encode_LF1(len,erg_zuweisung) returns a Long-Float 1.0 with len digits.
> uintC len: number of digits
< object erg: new Long-Float 1.0 with len digits
can trigger GC */
#define encode_LF1(len,erg_zuweisung) encode_LF1s(0,len,erg_zuweisung)
/* encoding a Long-Float:
encode_LFu(sign,uexp,mantMSDptr,mantlen, erg_zuweisung) returns a Long-Float
> signean sign: sign
> uintL exp: exponent + LF_exp_mid
> uintD* mantMSDptr: pointer to a NUDS with set highest bit
> uintC mantlen: number of digits, >= LF_minlen
< object erg: new Long-Float with the UDS mantMSDptr/mantlen/.. as mantissa
the exponent is not tested for overflow/underflow.
can trigger GC */
#define encode_LFu(sign,uexp,mantMSDptr,mantlen,erg_zuweisung) do { \
var uintC _len = (mantlen); \
var object _erg = allocate_lfloat(_len,uexp,(sign)); /* exponent */ \
copy_loop_up((mantMSDptr),&TheLfloat(_erg)->data[0],_len); /* mantissa copied */ \
erg_zuweisung _erg; \
} while(0)
/* encoding a Long-Float:
encode_LF(sign,exp,mantMSDptr,mantlen, erg_zuweisung) returns a Long-Float
> signean sign: sign
> sintL exp: exponent
> uintD* mantMSDptr: pointer to a NUDS with set highest bit
> uintC mantlen: number of digits, >= LF_minlen
< object erg: new Long-Float with the UDS mantMSDptr/mantlen/.. as mantissa
the exponent is not tested for overflow/underflow.
can trigger GC */
#define encode_LF(sign,exp,mantMSDptr,mantlen,erg_zuweisung) \
encode_LFu(sign,LF_exp_mid+(uintL)(exp),mantMSDptr,mantlen,_EMA_ erg_zuweisung)
/* hash-code of a Long-Float: mixture of exponent, length, first 32 bits */
global uint32 hashcode_lfloat (object obj) {
return TheLfloat(obj)->expo + Lfloat_length(obj)
+ get_32_Dptr(&TheLfloat(obj)->data[0]);
}
/* LF_zerop(x) determines, if a Long-Float x is = 0.0 . */
#define LF_zerop(x) (TheLfloat(x)->expo == 0)
/* returns for a Long-Float x : (ftruncate x), a LF.
LF_ftruncate_LF(x)
x is rounded towards 0 to the next whole number.
can trigger GC */
local maygc object LF_ftruncate_LF (object x);
/* method:
x = 0.0 or e<=0 -> result 0.0
1<=e<=16n -> set the last (16n-e) bits of the mantissa to 0,
keep exponent and sign
e>=16n -> result x */
#if 0
local maygc object LF_ftruncate_LF (object x)
{
var signean sign;
var sintL exp;
var uintD* mantMSDptr;
var uintC mantlen;
LF_decode(x, { return x; }, sign=,exp=,mantMSDptr=,mantlen=,);
if (exp<=0) { encode_LF0(mantlen, return); } /* e<=0 -> result 0.0 */
if ((uintL)exp >= intDsize*(uintL)mantlen) { /* e>=16n -> x as result */
return x;
} else { /* 0 < e < 16n */
/* create new NUDS wit e bits from mant and 16n-e nullbits: */
SAVE_NUM_STACK /* save num_stack */
var uintD* MSDptr;
num_stack_need(mantlen, MSDptr=,);
{
var uintC count = floor((uintL)exp,intDsize); /* digits to copy, < mantlen */
var uintC bitcount = ((uintL)exp) % intDsize; /* bits to copy behind it, >=0, <intDsize */
var uintD* ptr = /* copy count complete digits */
copy_loop_up(mantMSDptr,MSDptr,count);
*ptr++ = mantMSDptr[count] & minus_bitm(intDsize-bitcount); /* then copy bitcount bits */
clear_loop_up(ptr,mantlen-count-1); /* fill rest with Nulls */
}
var object result;
encode_LF(sign,exp,MSDptr,mantlen, result =);
RESTORE_NUM_STACK /* restore num_stack */
return result;
}
}
#else
local maygc object LF_ftruncate_LF (object x)
{
var uintC len = Lfloat_length(x);
var uintL uexp = TheLfloat(x)->expo;
if (uexp <= LF_exp_mid) {
if (uexp == 0)
return x; /* x=0.0 -> result 0.0 */
encode_LF0(len, return); /* e<=0 -> result 0.0 */
}
var uintL exp = uexp - LF_exp_mid;
if (exp >= intDsize*(uintL)len) /* e>=16n -> x as result */
return x;
/* 0 < e < 16n */
pushSTACK(x);
var object y = allocate_lfloat(len,uexp,LF_sign(x)); /* new Long-Float */
x = popSTACK();
/* y_mant := NUDS with e bits from x_mant and 16n-e nullbits: */
var uintC count = floor(exp,intDsize); /* digits to copy, < mantlen */
var uintC bitcount = exp % intDsize; /* bits to copy behind it, >=0, <intDsize */
var uintD* x_mantMSDptr = &TheLfloat(x)->data[0];
var uintD* ptr = /* copy count complete digits */
copy_loop_up(x_mantMSDptr,&TheLfloat(y)->data[0],count);
*ptr++ = x_mantMSDptr[count] & minus_bitm(intDsize-bitcount); /* then copy bitcount bits */
clear_loop_up(ptr,len-count-1); /* fill rest with Nulls */
return y;
}
#endif
/* Returns for a Long-Float x : (futruncate x), a LF.
LF_futruncate_LF(x)
x is rounded away from the 0 to the next whole number.
can trigger GC */
local maygc object LF_futruncate_LF (object x);
/* method:
x = 0.0 -> result 0.0
e<=0 -> result 1.0 or -1.0, according to sign of x.
1<=e<16n -> extract the last (16n-e) bits from x.
If they are all =0 -> result x.
Else set them all to 0 and then increase the front e bits
by 1.
No Overflow -> done.
Else (result, a power of two): mantissa := .1000...000,
e:=e+1. (test for overflow superfluous because of e<=16n)
e>=16n -> result x. */
#if 0
local maygc object LF_futruncate_LF (object x)
{
var signean sign;
var sintL exp;
var uintD* mantMSDptr;
var uintC mantlen;
LF_decode(x, { return x; }, sign=,exp=,mantMSDptr=,mantlen=,);
if (exp<=0) { encode_LF1s(sign,mantlen, return); } /* e<=0 -> result +-1.0 */
if ((uintL)exp >= intDsize*(uintL)mantlen) { /* e>=16n -> x as result */
return x;
} else { /* 0 < e < 16n */
/* test, if all rear 16n-e bits are =0: */
var uintC count = floor((uintL)exp,intDsize); /* digits to copy, < mantlen */
var uintC bitcount = ((uintL)exp) % intDsize; /* bits to copy behind it, >=0, <intDsize */
var uintD mask = minus_bitm(intDsize-bitcount); /* mask with bitcount bits */
var uintD* mantptr = &mantMSDptr[count];
if (((mantptr[0] & ~mask) ==0)
&& !test_loop_up(&mantptr[1],mantlen-count-1))
return x;
/* create new NUDS with e bits from mant with increment
and 16n-e nullbits: */
{
SAVE_NUM_STACK /* save num_stack */
var uintD* MSDptr;
num_stack_need(mantlen, MSDptr=,);
{
var uintD* ptr = /* copy count complete digits */
copy_loop_up(mantMSDptr,MSDptr,count);
if ((ptr[0] = ((mantptr[0] & mask) - mask)) == 0) /* then copy bitcount bits and increment */
if (inc_loop_down(ptr,count)!=0) { /* poss. continue to increment */
MSDptr[0] = bit(intDsize-1); exp = exp+1; /* poss. increase exponent */
}
clear_loop_up(&ptr[1],mantlen-count-1); /* fill restrest with Nulls */
}
var object result;
encode_LF(sign,exp,MSDptr,mantlen, result =);
RESTORE_NUM_STACK /* restore num_stack */
return result;
}
}
}
#else
local maygc object LF_futruncate_LF (object x)
{
var uintC len = Lfloat_length(x);
var uintL uexp = TheLfloat(x)->expo;
if (uexp <= LF_exp_mid) {
if (uexp == 0)
return x; /* x=0.0 -> result 0.0 */
encode_LF1s(LF_sign(x),len, return); /* e<=0 -> result +-1.0 */
}
var uintL exp = uexp - LF_exp_mid;
if (exp >= intDsize*(uintL)len) /* e>=16n -> x as result */
return x;
/* 0 < e < 16n */
/* test, if all rear 16n-e bits are =0 : */
var uintC count = floor(exp,intDsize); /* digits to copy, < mantlen */
var uintC bitcount = exp % intDsize; /* bits to copy behind it, >=0, <intDsize */
var uintD mask = minus_bitm(intDsize-bitcount); /* mask with bitcount bits */
{
var uintD* mantptr = &TheLfloat(x)->data[count];
if (((mantptr[0] & ~mask) ==0)
&& !test_loop_up(&mantptr[1],len-count-1))
return x;
}
/* no -> produce new Long-Float: */
pushSTACK(x);
var object y = allocate_lfloat(len,uexp,LF_sign(x)); /* new Long-Float */
x = popSTACK();
/* y_mant := NUDS with e bits from x_mant with increment and 16n-e nullbits: */
var uintD* x_mantMSDptr = &TheLfloat(x)->data[0];
var uintD* y_mantMSDptr = &TheLfloat(y)->data[0];
var uintD* ptr = /* copy count complete digits */
copy_loop_up(x_mantMSDptr,y_mantMSDptr,count);
if ((ptr[0] = ((x_mantMSDptr[count] & mask) - mask)) == 0) /* then copy bitcount bits and increment */
if (inc_loop_down(ptr,count)!=0) { /* poss. continue to increment */
y_mantMSDptr[0] = bit(intDsize-1); (TheLfloat(y)->expo)++; /* poss. increase exponent */
}
clear_loop_up(&ptr[1],len-count-1); /* fill rest with Nulls */
return y;
}
#endif
/* returns for a Long-Float x : (fround x), a LF.
LF_fround_LF(x)
x is rounded to the next whole number.
can trigger GC */
local maygc object LF_fround_LF (object x);
/* method:
x = 0.0 or e<0 -> result 0.0
0<=e<16n -> round away the last (16n-e) bits of the mantissa,
keep exponent and sign.
e>=16n -> result x */
#if 0
local maygc object LF_fround_LF (object x)
{
var signean sign;
var sintL exp;
var uintD* mantMSDptr;
var uintC mantlen;
LF_decode(x, { return x; }, sign=,exp=,mantMSDptr=,mantlen=,);
if (exp<0) { encode_LF0(mantlen, return); } /* e<0 -> result 0.0 */
if ((uintL)exp >= intDsize*(uintL)mantlen) { /* e>=16n -> x as result */
return x;
} else { /* 0 <= e < 16n */
/* round away all rear 16n-e bits : */
var uintC count = floor((uintL)exp,intDsize); /* digits to copy, < mantlen */
var uintC bitcount = ((uintL)exp) % intDsize; /* bits to copy behind it, >=0, <intDsize */
var uintD mask = minus_bit(intDsize-bitcount-1); /* mask with bitcount+1 bits */
var uintD* mantptr = &mantMSDptr[count];
if ((mantptr[0] & -mask) ==0) /* bit 16n-e-1 =0 -> round off */
goto ab;
if (!((mantptr[0] & ~mask) ==0)) /* bit 16n-e-1 =1 and bits 16n-e-2..0 >0 -> round up */
goto auf;
if (test_loop_up(&mantptr[1],mantlen-count-1))
goto auf;
/* round-to-even, according to bit 16n-e : */
if (bitcount>0) {
if ((mantptr[0] & (-2*mask)) ==0)
goto ab;
else
goto auf;
} else if (count>0) {
if ((mantptr[-1] & bit(0)) ==0)
goto ab;
else
goto auf;
} else {
/* bitcount=0, count=0, so exp=0: round off from +-0.5 to 0.0 */
encode_LF0(mantlen, return);
}
ab: { /* round off */
SAVE_NUM_STACK /* save num_stack */
var uintD* MSDptr;
num_stack_need(mantlen, MSDptr=,);
var uintD* ptr = /* copy count complete digits */
copy_loop_up(mantMSDptr,MSDptr,count);
*ptr++ = mantMSDptr[count] & mask; /* then copy bitcount bits */
clear_loop_up(ptr,mantlen-count-1); /* fill rest with Nulls */
var object result;
encode_LF(sign,exp,MSDptr,mantlen, result =);
RESTORE_NUM_STACK /* restore num_stack */
return result;
}
auf: { /* round up */
SAVE_NUM_STACK /* save num_stack */
var uintD* MSDptr;
num_stack_need(mantlen, MSDptr=,);
var uintD* ptr = /* copy count complete digits */
copy_loop_up(mantMSDptr,MSDptr,count);
if ((ptr[0] = ((mantptr[0] & mask) - mask)) == 0) /* then copy bitcount bits and increment */
if (inc_loop_down(ptr,count)!=0) { /* poss. continue to increment */
MSDptr[0] = bit(intDsize-1); exp = exp+1; /* poss. increase exponent */
}
clear_loop_up(&ptr[1],mantlen-count-1); /* fill rest with Nulls */
var object result;
encode_LF(sign,exp,MSDptr,mantlen, result =);
RESTORE_NUM_STACK /* restore num_stack */
return result;
}
}
}
#else
local maygc object LF_fround_LF (object x)
{
var uintC len = Lfloat_length(x);
var uintL uexp = TheLfloat(x)->expo;
if (uexp < LF_exp_mid) {
if (uexp == 0)
return x; /* x=0.0 -> result 0.0 */
encode_LF0(len, return); /* e<0 -> result 0.0 */
}
var uintL exp = uexp - LF_exp_mid;
if (exp >= intDsize*(uintL)len) /* e>=16n -> x as result */
return x;
/* 0 <= e < 16n */
/* round away all rear 16n-e bits: */
var uintC count = floor(exp,intDsize); /* digits to copy, < mantlen */
var uintC bitcount = exp % intDsize; /* bits to copy behind it, >=0, <intDsize */
var uintD mask = minus_bit(intDsize-bitcount-1); /* mask with bitcount+1 bits */
{
var uintD* mantptr = &TheLfloat(x)->data[count];
#if !(defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 7))
if ((mantptr[0] & -mask) ==0) /* Bit 16n-e-1 =0 -> round off */
#else
/* Work around gcc-2.7.x bug on i386/ELF */
if ((mantptr[0] & ((~mask)+1)) ==0) /* Bit 16n-e-1 =0 -> round off */
#endif
goto ab;
if ((mantptr[0] & ~mask)!=0) /* Bit 16n-e-1 =1 and Bits 16n-e-2..0 >0 -> round up */
goto auf;
if (test_loop_up(&mantptr[1],len-count-1))
goto auf;
/* round-to-even, according to bit 16n-e : */
if (bitcount>0) {
if ((mantptr[0] & (-2*mask)) ==0)
goto ab;
else
goto auf;
} else if (count>0) {
if ((mantptr[-1] & bit(0)) ==0)
goto ab;
else
goto auf;
} else {
/* bitcount=0, count=0, so exp=0: rounding off from +-0.5 to 0.0 */
encode_LF0(len, return);
}
}
ab: { /* round off */
pushSTACK(x);
var object y = allocate_lfloat(len,uexp,LF_sign(x)); /* new Long-Float */
x = popSTACK();
/* y_mant := NUDS with e bits from x_mant and 16n-e Nullbits: */
var uintD* x_mantMSDptr = &TheLfloat(x)->data[0];
var uintD* ptr =
copy_loop_up(x_mantMSDptr,&TheLfloat(y)->data[0],count); /* copy count complete digits */
*ptr++ = x_mantMSDptr[count] & mask; /* then copy bitcount bits */
clear_loop_up(ptr,len-count-1); /* fill rest with Nulls */
return y;
}
auf: { /* round up */
pushSTACK(x);
var object y = allocate_lfloat(len,uexp,LF_sign(x)); /* new Long-Float */
x = popSTACK();
/* y_mant := NUDS with e bits from x_mant with increment and 16n-e Nullbits: */
var uintD* x_mantMSDptr = &TheLfloat(x)->data[0];
var uintD* y_mantMSDptr = &TheLfloat(y)->data[0];
var uintD* ptr = /* copy count complete digits */
copy_loop_up(x_mantMSDptr,y_mantMSDptr,count);
if ((ptr[0] = ((x_mantMSDptr[count] & mask) - mask)) == 0) /* then copy bitcount bits and increment */
if (inc_loop_down(ptr,count)!=0) { /* poss. continue to increment */
y_mantMSDptr[0] = bit(intDsize-1); (TheLfloat(y)->expo)++; /* poss. increase exponent */
}
clear_loop_up(&ptr[1],len-count-1); /* fill rest with Nulls */
return y;
}
}
#endif
/* returns for a Long-Float x : (- x), a LF.
LF_minus_LF(x)
can trigger GC
method:
if x=0.0, done. Else flip sign bit and keep pointer. */
local maygc object LF_minus_LF (object x)
{
if (TheLfloat(x)->expo == 0) {
return x;
} else {
#if defined(SPVW_MIXED) && defined(TYPECODES)
return as_object(as_oint(x) ^ wbit(vorz_bit_o));
#else
var uintC len = Lfloat_length(x);
pushSTACK(x);
var object mx = allocate_lfloat(len,TheLfloat(x)->expo,~LF_sign(x));
x = popSTACK();
copy_loop_up(&TheLfloat(x)->data[0],&TheLfloat(mx)->data[0],len);
return mx;
#endif
}
}
/* LF_LF_comp(x,y) compares two Long-Floats x and y.
result: 0 if x=y, +1 if x>y, -1 if x<y.
method:
x and y have different signs ->
x < 0 -> x < y
x >= 0 -> x > y
x and y have equal signs ->
x >=0 -> compare x and y (the right 24 bits)
x <0 -> compare y and x (the right 24 bits) */
local signean LF_LF_comp (object x, object y)
{
if (!R_minusp(y)) { /* y>=0 */
if (!R_minusp(x)) { /* y>=0, x>=0 */
{ /* compare exponents and mantissas: */
var uintL x_uexp = TheLfloat(x)->expo;
var uintL y_uexp = TheLfloat(y)->expo;
if (x_uexp < y_uexp)
return signean_minus; /* x<y */
if (x_uexp > y_uexp)
return signean_plus; /* x>y */
}
{
var uintC x_len = Lfloat_length(x);
var uintC y_len = Lfloat_length(y);
var uintC len = (x_len<y_len ? x_len : y_len); /* min(x_len,y_len) */
/* compare len digits: */
var signean erg =
compare_loop_up(&TheLfloat(x)->data[0],&TheLfloat(y)->data[0],len);
if (erg!=0)
return erg; /* different -> done */
/* commen sub part was equal */
if (x_len == y_len)
return signean_null; /* equal length -> done */
if (x_len > y_len) { /* x longer than y */
if (test_loop_up(&TheLfloat(x)->data[y_len],x_len-y_len))
return signean_plus; /* x>y */
else
return signean_null;
} else { /* y longer than x */
if (test_loop_up(&TheLfloat(y)->data[x_len],y_len-x_len))
return signean_minus; /* x<y */
else
return signean_null;
}
}
} else { /* y>=0, x<0 */
return signean_minus; /* x<y */
}
} else {
if (!R_minusp(x)) { /* y<0, x>=0 */
return signean_plus; /* x>y */
} else { /* y<0, x<0 */
{ /* compare exponents and mantissas: */
var uintL x_uexp = TheLfloat(x)->expo;
var uintL y_uexp = TheLfloat(y)->expo;
if (x_uexp < y_uexp)
return signean_plus; /* |x|<|y| -> x>y */
if (x_uexp > y_uexp)
return signean_minus; /* |x|>|y| -> x<y */
}
{
var uintC x_len = Lfloat_length(x);
var uintC y_len = Lfloat_length(y);
var uintC len = (x_len<y_len ? x_len : y_len); /* min(x_len,y_len) */
/* compare len digits: */
var signean erg =
compare_loop_up(&TheLfloat(y)->data[0],&TheLfloat(x)->data[0],len);
if (erg!=0)
return erg; /* different -> done */
/* common sub part was equal */
if (x_len == y_len)
return signean_null; /* equal length -> done */
if (x_len > y_len) { /* x longer than y */
if (test_loop_up(&TheLfloat(x)->data[y_len],x_len-y_len))
return signean_minus; /* |x|>|y| -> x<y */
else
return signean_null;
} else { /* y longer than x */
if (test_loop_up(&TheLfloat(y)->data[x_len],y_len-x_len))
return signean_plus; /* |x|<|y| -> x>y */
else
return signean_null;
}
}
}
}
}
/* LF_shorten_LF(x,len) shortens a Long-Float x to given length len
and rounds.
> object x: a Long-FLoat
> uintC len: wished length (>= LF_minlen, < Lfloat_length(x))
< object result: shortened Long-Float
can trigger GC */
local maygc object LF_shorten_LF (object x, uintC len)
{
/* x = 0.0 does not need to be caught, because when mantissa is 0
we round of anyway, so the mantissa remains 0. */
pushSTACK(x);
var object y = allocate_lfloat(len,TheLfloat(x)->expo,LF_sign(x)); /* new LF */
x = popSTACK();
var uintC oldlen = Lfloat_length(x); /* old length, > len */
/* copy mantissa from x to y: */
copy_loop_up(&TheLfloat(x)->data[0],&TheLfloat(y)->data[0],len);
/* decide, if to round up or off: */
var uintD* ptr = &TheLfloat(x)->data[len];
if (((sintD)ptr[0] >= 0) /* next bit a 0 -> round off */
|| (((ptr[0] & ((uintD)bit(intDsize-1)-1)) ==0) /* a 1 and all further Nulls? */
&& !test_loop_up(&ptr[1],oldlen-len-1)
/* round-to-even */
&& ((ptr[-1] & bit(0)) ==0))) {
/* round off */
} else { /* round up */
if ( inc_loop_down(&TheLfloat(y)->data[len],len) ) {
/* carry by rounding up */
TheLfloat(y)->data[0] = bit(intDsize-1); /* mantissa := 10...0 */
/* increase exponent: */
if (++(TheLfloat(y)->expo) == (uint32)(LF_exp_high+1))
fehler_overflow();
}
}
return y;
}
/* LF_extend_LF(x,len) extends a Long-Float x to a given length len.
> object x: a Long-FLoat
> uintC len: wished length (> Lfloat_length(x))
< object result: extended Long-Float
can trigger GC */
local maygc object LF_extend_LF (object x, uintC len)
{
pushSTACK(x);
var object y = allocate_lfloat(len,TheLfloat(x)->expo,LF_sign(x)); /* new LF */
x = popSTACK();
var uintC oldlen = Lfloat_length(x); /* old length, < len */
/* copy mantissa from x to y: */
var uintD* ptr =
copy_loop_up(&TheLfloat(x)->data[0],&TheLfloat(y)->data[0],oldlen);
/* and complete with Null digits: */
clear_loop_up(ptr,len-oldlen);
return y;
}
/* LF_to_LF(x,len) converts a Long-Float x into a Long-Float of given length
len and rounds if necessary.
> object x: a Long-FLoat
> uintC len: wished length (>= LF_minlen)
< object result: Long-Float of given length
can trigger GC */
local maygc object LF_to_LF (object x, uintC len)
{
var uintC oldlen = Lfloat_length(x);
if (len < oldlen)
return LF_shorten_LF(x,len);
if (len > oldlen)
return LF_extend_LF(x,len);
/* len = oldlen */
return x;
}
/* returns for two equal-length Long-Float x and y : (+ x y), a LF.
LF_LF_plus_LF(x,y)
can trigger GC
method (according to [Knuth, II, Seminumerical Algorithms, section 4.2.1., S.200]):
if e1<e2, swap x1 and x2.
So e1 >= e2.
if e2=0, so x2=0.0, result x1.
if e1 - e2 >= 16n+2, result x1.
Extend the mantissas right by 3 bits (bit -1 as protection bit, bits -2,-3
as rounding bits: 00 exact, 01 1. half, 10 exact middle, 11 2. half.)
Shift the mantissa of x2 by e0-e1 bits to the right. (Execute the
rounding: bit -3 is the logical Or of the bits -3,-4,-5,...)
if x1,x2 have equal sign: Add it to the mantissa of x1.
if x1,x2 have different sign: Subtract it from the
mantissa of x1. <0 -> (e1=e2) swap the signs, negate.
=0 -> result 0.0
exponent is e1.
normalize, done. */
local maygc object LF_LF_plus_LF (object x1, object x2)
{
var uintL uexp1 = TheLfloat(x1)->expo;
var uintL uexp2 = TheLfloat(x2)->expo;
if (uexp1 < uexp2) { /* swap x1 and x2 */
swap(object, x1,x2); swap(uintL, uexp1,uexp2);
}
/* uexp1 >= uexp2 */
if (uexp2==0)
return x1; /* x2=0.0 -> x1 as result */
var uintC len = Lfloat_length(x1); /* length n of x1 and x2 */
var uintL expdiff = uexp1-uexp2; /* e1-e2 */
#if !(defined(SPVW_MIXED) && defined(TYPECODES))
if ((expdiff == 0) && !same_sign_p(x1,x2)) {
/* different signs, but same exponent */
/* determine sign of the result: */
var signean erg = /* compare mantissas (len digits at a time) */
compare_loop_up(&TheLfloat(x1)->data[0],&TheLfloat(x2)->data[0],len);
if (erg==0) { /* mantissas equal */
encode_LF0(len, return); /* result 0.0 */
}
if (erg<0) { /* |x1| < |x2| */
/* swap x1 and x2, expdiff remains =0 */
swap(object, x1,x2); swap(uintL, uexp1,uexp2);
}
}
#endif
if (expdiff >= intDsize * (uintL)len + 2) /* e1-e2 >= 16n+2 ? */
return x1; /* yes -> x1 as result */
/* allocate new Long-Float: */
pushSTACK(x1); pushSTACK(x2);
var object y = allocate_lfloat(len,uexp1,LF_sign(x1));
x2 = popSTACK(); x1 = popSTACK();
var uintL i = floor(expdiff,intDsize); /* e1-e2 div 16 (>=0, <=n) */
var uintL j = expdiff % intDsize; /* e1-e2 mod 16 (>=0, <16) */
/* Mantissa of x2 must be shifted by intDsize*i+j bits to the right. */
var uintC x2_len = len - i; /* n-i digits of x2 used */
/* shift x2_len digits by j bits to the right and copy them: */
SAVE_NUM_STACK /* save num_stack */
var uintD* x2_MSDptr;
var uintD* x2_LSDptr;
var uintD rounding_bits;
num_stack_need(x2_len, x2_MSDptr=,x2_LSDptr=); /* x2_len digits room */
begin_arith_call();
if (j==0) {
copy_loop_up(&TheLfloat(x2)->data[0],x2_MSDptr,x2_len); rounding_bits = 0;
} else {
rounding_bits = shiftrightcopy_loop_up(&TheLfloat(x2)->data[0],x2_MSDptr,x2_len,j,0);
}
/* x2_MSDptr/x2_len/x2_LSDptr are the essential digits of x2.
rounding_bits contains the last j shifted out bits.
build the 3 rounding bits from rounding_bits and the next i digits
(as bits intDsize-1..intDsize-3 from rounding_bits) : */
if (j>=2) { /* j>=2 -> bits -1,-2 are OK, determine bit -3: */
if ((rounding_bits & (bit(intDsize-3)-1)) ==0) {
if (test_loop_up(&TheLfloat(x2)->data[x2_len],i))
rounding_bits |= bit(intDsize-3); /* set rounding bit -3 */
} else {
rounding_bits |= bit(intDsize-3); /* set rounding bit -3 */
rounding_bits &= bitm(intDsize)-bit(intDsize-3); /* delete other bits */
}
} else {
/* j<=3 -> bits intDsize-4..0 from rounding_bits are already Null.
pull up next and further i-1 digits: */
if (i > 0) { /* i=0 -> bits -1,-2,-3 are OK. */
var uintD* ptr = &TheLfloat(x2)->data[x2_len];
rounding_bits |= (ptr[0] >> j); /* add further relevant bits of the next digit */
if ((rounding_bits & (bit(intDsize-3)-1)) ==0) { /* all bits -3,-4,... =0 ? */
if (((ptr[0] & (bit(3)-1))!=0) /* j (<=3) lower bits of ptr[0] all =0 ? */
|| test_loop_up(&ptr[1],i-1))
rounding_bits |= bit(intDsize-3); /* set rounding bit -3 */
} else {
rounding_bits |= bit(intDsize-3); /* set rounding bit -3 */
rounding_bits &= bitm(intDsize)-bit(intDsize-3); /* delete other bits */
}
}
}
/* x2 is there in shifted form in the UDS x2_MSDptr/x2_len/x2_LSDptr ,
with rounding bits in bit intDsize-1..intDsize-3 from rounding_bits. */
{
var uintD* y_mantMSDptr = &TheLfloat(y)->data[0];
var uintD* y_mantLSDptr = &y_mantMSDptr[(uintP)len];
if (same_sign_p(x1,x2)) { /* equal sign -> add mantissas */
/* first the right mantissa part (x2_len digits) by addition: */
var uintD carry =
add_loop_down(&TheLfloat(x1)->data[(uintP)len],x2_LSDptr,
y_mantLSDptr, x2_len);
/* then copy the left mantissa part (i digits) directly: */
var uintD* ptr =
copy_loop_up(&TheLfloat(x1)->data[0],y_mantMSDptr,i);
/* then add carry from the right part to the left mantissa part: */
if (carry!=0)
if ( inc_loop_down(ptr,i) ) { /* carry beyond the first digit */
/* increment exponent of y : */
if (++(TheLfloat(y)->expo) == (uint32)(LF_exp_high+1))
fehler_overflow();
/* normalize by shifting by 1 bit to the right: */
var uintD carry_rechts =
shift1right_loop_up(y_mantMSDptr,len,(uintD)(-1));
rounding_bits = rounding_bits>>1; /* also shift rounding bits */
if (!(carry_rechts==0))
rounding_bits |= bit(intDsize-1);
}
} else { /* different signs -> subtract mantissas */
/* first the right mantissa part (x2_len digits) by subtraction: */
rounding_bits = -rounding_bits;
var uintD carry =
subx_loop_down(&TheLfloat(x1)->data[(uintP)len],x2_LSDptr,
y_mantLSDptr, x2_len,
(rounding_bits==0 ? 0 : -1L));
/* then copy the left mantissa part (i digits) directly: */
var uintD* ptr =
copy_loop_up(&TheLfloat(x1)->data[0],y_mantMSDptr,i);
/* then subtract carry from the right part from the left mantissa part: */
if (carry!=0)
if ( dec_loop_down(ptr,i) ) {
/* carry beyond the first digit, so e1=e2 */
#if !(defined(SPVW_MIXED) && defined(TYPECODES))
NOTREACHED; /* we have already treated this case */
#else
/* negate: */
y = as_object(as_oint(y) ^ wbit(vorz_bit_o));
rounding_bits = -rounding_bits;
if (rounding_bits==0) {
/* negate without carry */
neg_loop_down(y_mantLSDptr,len);
} else { /* negate with carry from the right */
/* not_loop_down(y_mantLSDptr,len); // or */
not_loop_up(y_mantMSDptr,len);
}
#endif
}
}
/* normalize UDS y_mantMSDptr/len/y_mantLSDptr/rounding_bits: */
{
var uintD* ptr = y_mantMSDptr;
var uintL k = 0;
var uintC count;
dotimesC(count,len, {
if (!(ptr[0]==0))
goto nonzero_found;
ptr++; k++;
});
if (!(rounding_bits==0))
goto nonzero_found;
/* the UDS is completely Null. So e1=e2, no rounding bits. */
end_arith_call();
RESTORE_NUM_STACK /* restore num_stack */
#if !(defined(SPVW_MIXED) && defined(TYPECODES))
NOTREACHED; /* we have already treated this case */
#else
TheLfloat(y)->expo = 0; /* 0.0 as result */
return as_object(as_oint(y) & ~wbit(vorz_bit_o));
#endif
nonzero_found: /* digit /=0 found */
/* copy UDS from ptr to y_mantMSDptr by k digits downwards: */
if (k>0) {
/* at least one leading Nulldigit. So, e1-e2 = 0 or 1. */
ptr = copy_loop_up(ptr,y_mantMSDptr,len-k); /* shift len-k digits */
*ptr++ = rounding_bits; /* rounding bits as further digit */
clear_loop_up(ptr,k-1); /* then k-1 nulldigits */
rounding_bits = 0; /* and no further rounding bits */
/* decrease exponent by intDsize*k : */
k = intDsize*k;
var uintL uexp = TheLfloat(y)->expo;
#if !(LF_exp_low==1)
if (uexp < k+LF_exp_low)
#else
if (uexp <= k)
#endif
{
end_arith_call();
RESTORE_NUM_STACK /* restore num_stack */
if (underflow_allowed()) {
fehler_underflow();
} else {
encode_LF0(len, return); /* result 0.0 */
}
}
TheLfloat(y)->expo = uexp - k;
}
}
/* normalize NUDS y_mantMSDptr/len/y_mantLSDptr/rounding_bits : */
{
var uintL s;
integerlengthD(y_mantMSDptr[0], s = intDsize - );
/* s = number of leading nullbits in first word (>=0, <intDsize) */
if (s > 0) {
/* Shift the NUDS y_mantMSDptr/len/y_mantLSDptr/rounding_bits
by s bits to the left.
(e1-e2>1 enforces s=1.) */
if (s==1) {
shift1left_loop_down(y_mantLSDptr,len);
if (rounding_bits & bit(intDsize-1))
y_mantLSDptr[-1] |= bit(0);
rounding_bits = rounding_bits << 1;
} else { /* s>1, so e1-e2 <= 1 <= s. */
shiftleft_loop_down(y_mantLSDptr,len,s,rounding_bits>>(intDsize-s));
rounding_bits = 0; /* = rounding_bits << s; */
}
/* decrease exponent by s : */
var uintL uexp = TheLfloat(y)->expo;
#if !(LF_exp_low==1)
if (uexp < s+LF_exp_low)
#else
if (uexp <= s)
#endif
{
end_arith_call();
RESTORE_NUM_STACK /* restore num_stack */
if (underflow_allowed()) {
fehler_underflow();
} else {
encode_LF0(len, return); /* result 0.0 */
}
}
TheLfloat(y)->expo = uexp - s;
}
}
/* here, rounding_bits contains bit -1 as Bit intDsize-1, bit -2 as
bit intDsize-2, Bit -3 as Or(Bits intDsize-3..0) !
Round. Inspect rounding_bits: */
if ((rounding_bits & bit(intDsize-1)) ==0) /* bit -1 deleted -> round off */
goto ab;
rounding_bits = rounding_bits<<1; /* bits -2,-3 */
if (!(rounding_bits==0)) /* bit -2 or bit -3 set -> round up */
goto auf;
/* round-to-even: */
if ((y_mantLSDptr[-1] & bit(0)) ==0)
goto ab;
auf: /* round up */
if ( inc_loop_down(y_mantLSDptr,len) ) {
/* carry by rounding up */
y_mantMSDptr[0] = bit(intDsize-1); /* mantissa := 10...0 */
/* increase exponent: */
if (++(TheLfloat(y)->expo) == (uint32)(LF_exp_high+1)) {
end_arith_call(); RESTORE_NUM_STACK; fehler_overflow();
}
}
ab: /* round off */
;
}
end_arith_call();
RESTORE_NUM_STACK /* restore num_stack */
/* y done. */
return y;
}
/* returns for two equal-length Long-Float x and y : (- x y), a LF.
LF_LF_minus_LF(x,y)
can trigger GC
method:
(- x1 x2) = (+ x1 (- x2)) */
local maygc object LF_LF_minus_LF (object x1, object x2)
{
if (TheLfloat(x2)->expo == 0) {
return x1;
} else {
#if defined(SPVW_MIXED) && defined(TYPECODES)
return LF_LF_plus_LF(x1, as_object(as_oint(x2) ^ wbit(vorz_bit_o)) );
#else
var uintC len2 = Lfloat_length(x2);
pushSTACK(x1); pushSTACK(x2);
var object mx2 = allocate_lfloat(len2,TheLfloat(x2)->expo,~LF_sign(x2));
x2 = popSTACK();
copy_loop_up(&TheLfloat(x2)->data[0],&TheLfloat(mx2)->data[0],len2);
return LF_LF_plus_LF(popSTACK(),mx2);
#endif
}
}
/* returns for a Long-Float x : (* x x), a LF.
LF_square_LF(x)
can trigger GC
method:
If x=0.0 -> result 0.0
Else: result-sign = positive.
result-exponent = 2 * exponent of x.
calcualte square of the mantissas (2n digits).
If the leading bit is =0 : shift mantissa product by 1 bit to
the left (the front n+1 digits are enough)
and decrement exponent.
rounding to n digits yields the result-mantissa. */
local maygc object LF_square_LF (object x)
{
var uintL uexp = TheLfloat(x)->expo;
if (uexp==0)
return x; /* x=0.0 -> result 0.0 */
/* add exponents:
(uexp-LF_exp_mid) + (uexp-LF_exp_mid) = (2*uexp-LF_exp_mid)-LF_exp_mid */
if ((sintL)uexp >= 0) { /* no Carry */
uexp = 2*uexp;
if (uexp < LF_exp_mid+LF_exp_low) {
if (underflow_allowed()) {
fehler_underflow();
} else {
encode_LF0(Lfloat_length(x), return); /* result 0.0 */
}
}
} else { /* Carry */
uexp = 2*uexp;
if (uexp > (uintL)(LF_exp_mid+LF_exp_high+1))
fehler_overflow();
}
uexp = uexp - LF_exp_mid;
/* now, LF_exp_low <= uexp <= LF_exp_high+1.
allocate new Long-Float: */
pushSTACK(x);
var uintC len = Lfloat_length(x); /* length n of x */
var object y = allocate_lfloat(len,uexp,0);
SAVE_NUM_STACK /* save num_stack */
x = popSTACK();
{ /* form product: */
var uintD* MSDptr;
begin_arith_call();
UDS_square_UDS(len,&TheLfloat(x)->data[(uintP)len],
MSDptr=,_EMA_,);
var uintD* midptr = &MSDptr[(uintP)len]; /* pointer into the middle of the 2n digits */
if ((sintD)MSDptr[0] >= 0) { /* test leading bit */
/* shift the first n+1 digits by 1 bit to the left: */
shift1left_loop_down(&midptr[1],len+1);
/* decrement exponent: */
if ((TheLfloat(y)->expo)-- == LF_exp_low-1) {
end_arith_call();
RESTORE_NUM_STACK /* restore num_stack */
if (underflow_allowed()) {
fehler_underflow();
} else {
encode_LF0(len, return); /* result 0.0 */
}
}
}
end_arith_call();
/* carry forward the first half of the mantissa product: */
var uintD* y_mantMSDptr = &TheLfloat(y)->data[0];
var uintD* y_mantLSDptr =
copy_loop_up(MSDptr,y_mantMSDptr,len);
/* round: */
if (((sintD)midptr[0] >= 0) /* next bit =0 -> round off */
|| (((midptr[0] & ((uintD)bit(intDsize-1)-1)) ==0) /* bit =1, further bits >0 -> round up */
&& !test_loop_up(&midptr[1],len-1)
/* round-to-even */
&& ((midptr[-1] & bit(0)) ==0))) {
/* round off */
} else {
/* round up */
if ( inc_loop_down(y_mantLSDptr,len) ) {
/* carry due to rounding up (can only happen,
if a shift by 1 bit to the left took place, beforehand) */
y_mantMSDptr[0] = bit(intDsize-1); /* mantissa := 10...0 */
(TheLfloat(y)->expo)++; /* re-increment exponent again */
}
}
/* assure LF_exp_low <= exp <= LF_exp_high : */
if (TheLfloat(y)->expo == (uint32)(LF_exp_high+1)) {
RESTORE_NUM_STACK; fehler_overflow();
}
}
RESTORE_NUM_STACK /* restore num_stack */
return y;
}
/* returns for two equal-length Long-Float x and y : (* x y), a LF.
LF_LF_mal_LF(x,y)
can trigger GC
method:
If x1=0.0 or x2=0.0 -> result 0.0
Else: result-sign = sign of x1 xor sign of x2.
result-exponent = sum of exponents of x1 and x2.
form product of mantissas (2n Digits).
If the leading bit is =0 : shift mantissa product by 1 bit to
the left (the front n+1 digits are enough)
and decrement exponent.
Rounding to n digits yields the result-mantissa. */
local maygc object LF_LF_mal_LF (object x1, object x2)
{
var uintL uexp1 = TheLfloat(x1)->expo;
if (uexp1==0)
return x1; /* x1=0.0 -> result 0.0 */
var uintL uexp2 = TheLfloat(x2)->expo;
if (uexp2==0)
return x2; /* x2=0.0 -> result 0.0 */
/* add exponents:
(uexp1-LF_exp_mid) + (uexp2-LF_exp_mid) =
(uexp1+uexp2-LF_exp_mid)-LF_exp_mid */
uexp1 = uexp1 + uexp2;
if (uexp1 >= uexp2) { /* no Carry */
if (uexp1 < LF_exp_mid+LF_exp_low) {
if (underflow_allowed()) {
fehler_underflow();
} else {
encode_LF0(Lfloat_length(x1), return); /* result 0.0 */
}
}
} else { /* Carry */
if (uexp1 > (uintL)(LF_exp_mid+LF_exp_high+1))
fehler_overflow();
}
uexp1 = uexp1 - LF_exp_mid;
/* now, LF_exp_low <= uexp1 <= LF_exp_high+1. */
/* allocate new Long-Float: */
pushSTACK(x1); pushSTACK(x2);
var uintC len = Lfloat_length(x1); /* length n of x1 and x2 */
#ifdef TYPECODES
var signean sign = R_sign(as_object(as_oint(x1) ^ as_oint(x2))); /* combine signs */
#else
var signean sign = LF_sign(x1) ^ LF_sign(x2);
#endif
var object y = allocate_lfloat(len,uexp1,sign);
SAVE_NUM_STACK /* save num_stack */
x2 = popSTACK(); x1 = popSTACK();
{ /* form product: */
var uintD* MSDptr;
begin_arith_call();
UDS_UDS_mal_UDS(len,&TheLfloat(x1)->data[(uintP)len],
len,&TheLfloat(x2)->data[(uintP)len],
MSDptr=,_EMA_,);
var uintD* midptr = &MSDptr[(uintP)len]; /* pointer into the middle of the 2n digits */
if ((sintD)MSDptr[0] >= 0) { /* test leading bit */
/* shift the first n+1 Digits by 1 bit to the left: */
shift1left_loop_down(&midptr[1],len+1);
/* decrement exponent: */
if (--(TheLfloat(y)->expo) == LF_exp_low-1) {
end_arith_call();
RESTORE_NUM_STACK /* restore num_stack */
if (underflow_allowed()) {
fehler_underflow();
} else {
encode_LF0(len, return); /* result 0.0 */
}
}
}
end_arith_call();
/* carry first half of the mantissa product : */
var uintD* y_mantMSDptr = &TheLfloat(y)->data[0];
var uintD* y_mantLSDptr = copy_loop_up(MSDptr,y_mantMSDptr,len);
/* round: */
if (((sintD)midptr[0] >= 0) /* next bit =0 -> round off */
|| (((midptr[0] & ((uintD)bit(intDsize-1)-1)) ==0) /* bit =1, further bits >0 -> round up */
&& !test_loop_up(&midptr[1],len-1)
/* round-to-even */
&& ((midptr[-1] & bit(0)) ==0))) {
/* round off */
} else { /* round up */
if ( inc_loop_down(y_mantLSDptr,len) ) {
/* carry due to rounding up (can only happen,
if shift by 1 bit to the left took place, beforehand) */
y_mantMSDptr[0] = bit(intDsize-1); /* mantissa := 10...0 */
(TheLfloat(y)->expo)++; /* re-increment exponent again */
}
}
/* assure LF_exp_low <= exp <= LF_exp_high : */
if (TheLfloat(y)->expo == (uint32)(LF_exp_high+1)) {
RESTORE_NUM_STACK; fehler_overflow();
}
}
RESTORE_NUM_STACK /* restore num_stack */
return y;
}
/* returns for two equal-length Long-Float x and y : (/ x y), a LF.
LF_LF_durch_LF(x,y)
can trigger GC
method:
x2 = 0.0 -> Error
x1 = 0.0 -> result 0.0
Else:
result-sign = xor of the two sings of x1 and x2
result-exponent = difference of the two exponents of x1 and x2
result-mantissa = mantissa mant1 / mantissa mant2, rounded.
mant1/mant2 > 1/2, mant1/mant2 < 2;
after rounding mant1/mant2 >=1/2, <=2*mant1<2.
When mant1/mant2 >=1 we need 16n-1 bits behind the dot,
when mant1/mant2 <1 we need 16n bits behind the dot.
For Rounding: We need one rounding bit (rest specifies, if exact).
Hence, we need 16n+1 bits behind the dot from mant1/mant2, altogether.
Divide daher (as Unsigned Integers)
2^16(n+1)*(2^16n*m0) by (2^16n*m1).
If the quotient is >=2^16(n+1) , shift it by 1 bit to the right,
increase the exponent by 1 and round the last digit away.
If the quotient is <2^16(n+1) , round the last digit away. When rounding
overflow occurs, shift by 1 bit to the right and increase exponent by 1. */
/* work around a gcc-2.7.0 bug on i386. */
#if defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ == 7)
#define workaround_gcc270_bug() *&uexp1 = *&uexp1;
#else
#define workaround_gcc270_bug()
#endif
local maygc object LF_LF_durch_LF (object x1, object x2)
{
var uintL uexp2 = TheLfloat(x2)->expo;
if (uexp2==0)
divide_0(); /* x2=0.0 -> Error */
var uintL uexp1 = TheLfloat(x1)->expo;
if (uexp1==0)
return x1; /* x1=0.0 -> result 0.0 */
/* suvtract exponents:
(uexp1-LF_exp_mid) - (uexp2-LF_exp_mid) =
(uexp1-uexp2+LF_exp_mid)-LF_exp_mid */
if (uexp1 >= uexp2) {
uexp1 = uexp1 - uexp2; /* no carry */
workaround_gcc270_bug();
if (uexp1 > LF_exp_high-LF_exp_mid)
fehler_overflow();
uexp1 = uexp1 + LF_exp_mid;
} else {
uexp1 = uexp1 - uexp2; /* carry */
workaround_gcc270_bug();
if (uexp1 < (uintL)(LF_exp_low-1-LF_exp_mid)) {
if (underflow_allowed()) {
fehler_underflow();
} else {
encode_LF0(Lfloat_length(x1), return); /* result 0.0 */
}
}
uexp1 = uexp1 + LF_exp_mid;
}
/* LF_exp_low-1 <= uexp1 <= LF_exp_high. */
/* allocate new Long-Float: */
pushSTACK(x1); pushSTACK(x2);
var uintC len = Lfloat_length(x1); /* length n of x1 and x2 */
#ifdef TYPECODES
var signean sign = R_sign(as_object(as_oint(x1) ^ as_oint(x2))); /* combine sign */
#else
var signean sign = LF_sign(x1) ^ LF_sign(x2);
#endif
var object y = allocate_lfloat(len,uexp1,sign);
x2 = popSTACK(); x1 = popSTACK();
{ /* form counter: */
var uintD* z_MSDptr;
var uintL z_len;
var uintD* z_LSDptr;
z_len = 2*(uintL)len + 1;
if ((intWCsize < 32) && (z_len > (uintL)(bitc(intWCsize)-1)))
fehler_LF_toolong();
{
SAVE_NUM_STACK /* save num_stack */
num_stack_need(z_len, z_MSDptr=,z_LSDptr=);
{
var uintD* ptr =
copy_loop_up(&TheLfloat(x1)->data[0],z_MSDptr,len); /* copy n digits */
clear_loop_up(ptr,len+1); /* and n+1 Null-Digits */
}
/* form quotient: divide 2n+1-digit-number by n-digit-number */
begin_arith_call();
var DS q;
var DS r;
{
var uintD* x2_mantMSDptr = &TheLfloat(x2)->data[0];
UDS_divide(z_MSDptr,z_len,z_LSDptr,
x2_mantMSDptr,len,&x2_mantMSDptr[(uintP)len],
&q, &r);
}
/* q is the quotient with n+1 or n+2 digits, r the rest. */
if (q.len > len+1) {
/* quotient has n+2 digits -> shift by 1 bit to the right: */
var uintD* y_mantMSDptr = &TheLfloat(y)->data[0];
var uintD carry_rechts =
shiftrightcopy_loop_up(&q.MSDptr[1],y_mantMSDptr,len,1,
/* carry left = q.MSDptr[0] = 1 */ 1 );
/* increment exponent: */
if (++(TheLfloat(y)->expo) == (uint32)(LF_exp_high+1))
fehler_overflow();
/* round: */
if ((carry_rechts == 0) /* shifted out bit =0 -> round off */
|| ((q.LSDptr[-1]==0) /* =1 and further bits >0 or rest >0 -> round up */
&& (r.len==0)
/* round-to-even */
&& ((q.LSDptr[-2] & bit(1)) ==0))) {
/* round off */
} else {
/* round up */
inc_loop_down(&y_mantMSDptr[(uintP)len],len);
}
} else {
/* quotient has n+1 digits -> justs copy: */
var uintD* y_mantMSDptr = &TheLfloat(y)->data[0];
copy_loop_up(q.MSDptr,y_mantMSDptr,len);
/* round: */
if (((sintD)(q.LSDptr[-1]) >= 0) /* next bit =0 -> round off */
|| (((q.LSDptr[-1] & ((uintD)bit(intDsize-1)-1)) ==0) /* =1 and further bits >0 or rest >0 -> round up */
&& (r.len==0)
/* round-to-even */
&& ((q.LSDptr[-2] & bit(0)) ==0))) {
/* round off */
} else { /* round up */
if ( inc_loop_down(&y_mantMSDptr[(uintP)len],len) ) {
/* carry created by rounding up */
y_mantMSDptr[0] = bit(intDsize-1); /* mantissa := 10...0 */
/* increment exponents: */
if (++(TheLfloat(y)->expo) == (uint32)(LF_exp_high+1))
fehler_overflow();
}
}
}
end_arith_call();
RESTORE_NUM_STACK /* restore num_stack */
}
/* assure LF_exp_low <= exp <= LF_exp_high : */
if (TheLfloat(y)->expo == LF_exp_low-1) {
if (underflow_allowed()) {
fehler_underflow();
} else {
encode_LF0(len, return); /* result 0.0 */
}
}
}
return y;
}
/* returns for a Long-Float x>=0 : (sqrt x), a LF.
LF_sqrt_LF(x)
can trigger GC
method:
x = 0.0 -> result 0.0
result-sign := positive,
result-exponent := ceiling(e/2),
result-mantissa:
Extend the mantissa (n digits) by n+2 nulldigits behind.
if e is odd, shift it (or only the first n+1 digits)
by 1 bit to the right.
calculate the integer-root, a n+1-digit-number with a
leading 1.
Round the last digit away:
bit 15 = 0 -> round off,
bit 15 = 1, rest =0 and root exact -> round-to-even,
else round up.
When rounding overflow occurs, shift mantissa by 1 bit to the right
and increment exponent. */
local maygc object LF_sqrt_LF (object x)
{
var uintL uexp = TheLfloat(x)->expo;
if (uexp==0)
return x; /* x=0.0 -> 0.0 as result */
var uintC len = Lfloat_length(x);
/* create radicand: */
var uintD* r_MSDptr;
var uintD* r_LSDptr;
var uintL r_len = 2*(uintL)len+2; /* length of the radicand */
if ((intWCsize < 32) && (r_len > (uintL)(bitc(intWCsize)-1)))
fehler_LF_toolong();
{
SAVE_NUM_STACK /* save num_stack */
num_stack_need(r_len, r_MSDptr=,r_LSDptr=);
if ((uexp & bit(0)) == (LF_exp_mid & bit(0))) { /* exponent even */
var uintD* ptr =
copy_loop_up(&TheLfloat(x)->data[0],r_MSDptr,len); /* copy n digits */
clear_loop_up(ptr,len+2); /* append n+2 Nulldigits */
} else { /* exponent odd */
begin_arith_call();
var uintD carry_rechts = /* copy n digits and shift by 1 bit to the right */
shiftrightcopy_loop_up(&TheLfloat(x)->data[0],r_MSDptr,len,1,0);
var uintD* ptr = &r_MSDptr[(uintP)len];
*ptr++ = carry_rechts; /* append carry and */
clear_loop_up(ptr,len+1); /* n+1 Nulldigits */
end_arith_call();
}
/* Compute ((uexp - LF_exp_mid + 1) >> 1) + LF_exp_mid without risking
uintL overflow. */
uexp = ((uexp - ((LF_exp_mid - 1) & 1)) >> 1) - ((LF_exp_mid - 1) >> 1)
+ LF_exp_mid;
/* allocate result: */
var object y = allocate_lfloat(len,uexp,0);
var uintD* y_mantMSDptr = &TheLfloat(y)->data[0];
/* calculate root: */
var DS w;
var bool exactp;
UDS_sqrt(r_MSDptr,r_len,r_LSDptr, &w, exactp=);
/* w is the integer-root, a n+1-digit-number. */
copy_loop_up(w.MSDptr,y_mantMSDptr,len); /* copy NUDS to y */
/* round: */
if (((sintD)(w.LSDptr[-1]) >= 0) /* next bit =0 -> round off */
|| (((w.LSDptr[-1] & ((uintD)bit(intDsize-1)-1)) ==0) /* =1 and further bits >0 or rest >0 -> round up */
&& exactp
/* round-to-even */
&& ((w.LSDptr[-2] & bit(0)) ==0))) {
/* round off */
} else { /* round up */
if ( inc_loop_down(&y_mantMSDptr[(uintP)len],len) ) {
/* carry by rounding up */
y_mantMSDptr[0] = bit(intDsize-1); /* mantissa := 10...0 */
(TheLfloat(y)->expo)++; /* increment exponent */
}
}
RESTORE_NUM_STACK /* restore num_stack */
return y;
}
}
/* LF_to_I(x) converts a Long-Float x, that represents an integer number,
into an Integer.
can trigger GC
method:
If x=0.0, result 0.
Else (ASH sign*mantissa (e-16n)). */
local maygc object LF_to_I (object x)
{
var uintL uexp = TheLfloat(x)->expo;
if (uexp==0)
return Fixnum_0; /* x=0.0 -> result 0 */
/* turn mantissa into an Integer: */
var uintD* MSDptr;
var uintD* LSDptr;
var uintC len = Lfloat_length(x);
var uintC len1 = len+1; /* need 1 Digit more */
if (uintWCoverflow(len1))
fehler_LF_toolong();
{
SAVE_NUM_STACK /* save num_stack */
num_stack_need(len1, MSDptr=,LSDptr=);
copy_loop_up(&TheLfloat(x)->data[0],&MSDptr[1],len); /* copy mantissa */
MSDptr[0] = 0; /* and additional nulldigit */
/* mantissa is the UDS MSDptr/len1/LSDptr. */
if (R_minusp(x))
/* x<0 -> negate mantissa: */
neg_loop_down(LSDptr,len1);
/* sign*mantissa is the DS MSDptr/len1/LSDptr. */
pushSTACK(DS_to_I(MSDptr,len1)); /* sign*mantissa as Integer */
RESTORE_NUM_STACK /* restore num_stack */
}
/* form e-16n = uexp-LF_exp_mid-16n as Integer: */
var uintL sub = LF_exp_mid + intDsize*(uintL)len;
var object shiftcount = UL_UL_minus_I(uexp,sub);
/* execute (ASH Vorzeichen*Mantisse (- e 16n)) : */
return I_I_ash_I(popSTACK(),shiftcount);
}
/* I_to_LF(x,len,signal_overflow) converts an integer x to a long-float with
len digits, and rounds thereby.
can trigger GC
method:
x=0 -> result 0.0
Memorize sign of x.
x:=(abs x)
exponent:=(integer-length x)
let mantissa contain the most significant 16n bits of the Integer x
(with the leading 16-(e mod 16) nullbits to be discarded).
Round the further bits away:
There are no more -> round off,
next bit = 0 -> round off,
next bit bit = 1 and rest =0 -> round-to-even,
next bit bit = 1 and rest >0 -> round up.
When rounding up: rounding overflow -> shift mantissa by 1 bit to the right
and increment exponent. */
local maygc object I_to_LF (object x, uintC len, bool signal_overflow)
{
if (eq(x,Fixnum_0)) {
encode_LF0(len, return); /* x=0 -> result 0.0 */
}
var signean sign = R_sign(x); /* sign of x */
if (!(sign==0))
x = I_minus_I(x); /* take absolut value of x */
var uintL exp = I_integer_length(x); /* (integer-length x) < intDsize*2^intWCsize */
/* test, if exp <= LF_exp_high-LF_exp_mid : */
if ((log2_intDsize+intWCsize < 32)
&& ((uintL)(intDsize*bitc(intWCsize)-1) <=
(uintL)(LF_exp_high-LF_exp_mid))) {
/* guarantees exp <= intDsize*2^intWCsize-1 <= LF_exp_high-LF_exp_mid */
} else {
if (!(exp <= (uintL)(LF_exp_high-LF_exp_mid))) {
if (signal_overflow) fehler_overflow(); else return nullobj;
}
}
/* build Long-Float: */
pushSTACK(x);
var object y = allocate_lfloat(len,exp+LF_exp_mid,sign);
var uintD* y_mantMSDptr = &TheLfloat(y)->data[0];
var uintD* x_MSDptr;
var uintC x_len;
I_to_NDS_nocopy(popSTACK(), x_MSDptr=,x_len=,); /* form NDS for x, x_len>0 */
/* shift x_MSDptr/x_len/.. by (exp mod 16) bits to the right and fill
into y (particularly: only at most len digits): */
var uintL shiftcount = exp % intDsize;
/* the NDS begins with intDsize-shiftcount nullbits, then comes a 1. */
begin_arith_call();
if (x_len > len) {
x_len -= 1+len;
if (shiftcount>0) {
var uintD carry_rechts =
shiftrightcopy_loop_up(&x_MSDptr[1],y_mantMSDptr,len,shiftcount,x_MSDptr[0]);
/* mantissa is filled. Round: */
if (((sintD)carry_rechts >= 0) /* next bit =0 -> round off */
|| (((carry_rechts & ((uintD)bit(intDsize-1)-1)) ==0) /* =1, rest >0 -> round up */
&& !test_loop_up(&x_MSDptr[1+(uintP)len],x_len)
/* round-to-even */
&& ((y_mantMSDptr[(uintP)len-1] & bit(0)) ==0)))
goto ab; /* round off */
else
goto auf; /* round up */
} else {
copy_loop_up(&x_MSDptr[1],y_mantMSDptr,len);
/* mantissa is filled. Round: */
var uintD* ptr = &x_MSDptr[1+(uintP)len];
if ((x_len==0) /* no more bits -> round off */
|| ((sintD)ptr[0] >= 0) /* next bit =0 -> round off */
|| (((ptr[0] & ((uintD)bit(intDsize-1)-1)) ==0) /* =1, rest >0 -> round up */
&& !test_loop_up(&ptr[1],x_len-1)
/* round-to-even */
&& ((ptr[-1] & bit(0)) ==0)))
goto ab; /* round off */
else
goto auf; /* round up */
}
auf: /* round up */
if ( inc_loop_down(&y_mantMSDptr[(uintP)len],len) ) {
/* carry due to rounding up */
y_mantMSDptr[0] = bit(intDsize-1); /* mantissa := 10...0 */
/* increment exponent: */
if ((log2_intDsize+intWCsize < 32)
&& ((uintL)(intDsize*bitc(intWCsize)-1) <
(uintL)(LF_exp_high-LF_exp_mid))) {
/* guarantees exp < intDsize*2^intWCsize-1 <= LF_exp_high-LF_exp_mid */
(TheLfloat(y)->expo)++; /* now, exp <= LF_exp_high-LF_exp_mid */
} else {
if (++(TheLfloat(y)->expo) == (uint32)(LF_exp_high+1)) {
if (signal_overflow) fehler_overflow(); else return nullobj;
}
}
}
ab: /* round off */
;
} else { /* x_len <= len */
var uintD carry_rechts;
len -= x_len;
x_len -= 1;
if (shiftcount>0) {
carry_rechts = shiftrightcopy_loop_up(&x_MSDptr[1],y_mantMSDptr,x_len,shiftcount,x_MSDptr[0]);
} else {
copy_loop_up(&x_MSDptr[1],y_mantMSDptr,x_len); carry_rechts = 0;
}
var uintD* y_ptr = &y_mantMSDptr[x_len];
*y_ptr++ = carry_rechts; /* carry as next digit */
clear_loop_up(y_ptr,len); /* then len-x_len nulldigits */
}
end_arith_call();
return y;
}
/* RA_to_LF(x,len,signal_overflow) converts a rational number x to a long-float
with len digits and rounds thereby.
can trigger GC
method:
x integer -> obvious.
x = +/- a/b with Integers a,b>0:
let k,m be chosen, so that
2^(k-1) <= a < 2^k, 2^(m-1) <= b < 2^m.
Then, 2^(k-m-1) < a/b < 2^(k-m+1).
result-sign := sign of x.
Calculate k=(integer-length a) and m=(integer-length b).
result-exponent := k-m.
result-mantissa:
calculate floor(2^(-k+m+16n+1)*a/b) :
When k-m>=16n+1, divide a by (ash b (k-m-16n-1)),
when k-m<16n+1 divide (ash a (-k+m+16n+1)) by b.
The first value is >=2^16n, <2^(16n+2).
If it is >=2^(16n+1) , increase exponent by 1,
round 2 bits away and shift by 2 bits to the right;
if it is <2^(16n+1) ,
round 1 bit away and shift by 1 bit to the right. */
local maygc object RA_to_LF (object x, uintC len, bool signal_overflow)
{
if (RA_integerp(x))
return I_to_LF(x,len,signal_overflow);
/* x Ratio */
pushSTACK(TheRatio(x)->rt_den); /* b */
var signean sign = RT_sign(x); /* sign */
x = TheRatio(x)->rt_num; /* +/- a */
if (!(sign==0))
x = I_minus_I(x); /* take absolute value, return a */
pushSTACK(x);
/* stack layout: b, a. */
var sintL lendiff = I_integer_length(x) /* (integer-length a) */
- I_integer_length(STACK_1); /* (integer-length b) */
/* |lendiff| < intDsize*2^intWCsize. For LF-Exponenten there is a sintL
available, so we need no test for Overflow or Underflow. */
{
var uintL difflimit = intDsize*(uintL)len + 1; /* 16n+1 */
var object zaehler;
var object nenner;
if (lendiff > (sintL)difflimit) {
/* 0 <= k-m-16n-1 < k < intDsize*2^intWCsize */
nenner = I_I_ash_I(STACK_1,(log2_intDsize+intWCsize<=oint_data_len /* intDsize*2^intWCsize <= 2^oint_data_len ? */
? fixnum( (uintL)(lendiff - difflimit))
: UL_to_I((uintL)(lendiff - difflimit))));
zaehler = popSTACK(); /* a */
skipSTACK(1);
} else { /* 0 < -k+m+16n+1 <= m+1 + 16n < intDsize*2^intWCsize + intDsize*2^intWCsize */
var object shiftcount = /* -k+m+16n+1 */
(log2_intDsize+intWCsize+1<=oint_data_len /* 2*intDsize*2^intWCsize <= 2^oint_data_len ? */
? fixnum( (uintL)(difflimit - lendiff))
: UL_to_I((uintL)(difflimit - lendiff)));
zaehler = I_I_ash_I(popSTACK(),shiftcount); /* (ash a -k+m+16n+1) */
nenner = popSTACK(); /* b */
}
/* execute division zaehler/nenner (engl. numerator/denominator): */
I_I_divide_I_I(zaehler,nenner);
}
/* stack layout: q, r. */
/* 2^16n <= q < 2^(16n+2), so q is Bignum with n+1 Digits. */
var object y = allocate_lfloat(len,lendiff+LF_exp_mid,sign); /* new Long-Float */
var uintD* y_mantMSDptr = &TheLfloat(y)->data[0];
begin_arith_call();
{
var uintD* q_MSDptr = &TheBignum(STACK_1)->data[0];
if (q_MSDptr[0] == 1) { /* first digit =1 or =2,3 ? */
/* 2^16n <= q < 2^(16n+1), so 2^(k-m-1) < a/b < 2^(k-m). */
/* fill mantissa with a shift loop by 1 bit to the right: */
var uintD rounding_bit =
shiftrightcopy_loop_up(&q_MSDptr[1],y_mantMSDptr,len,1,1);
if ((rounding_bit == 0) /* shifted out bit =0 -> round off */
|| (eq(STACK_0,Fixnum_0) /* =1 and rest r > 0 -> round up */
/* round-to-even */
&& ((y_mantMSDptr[(uintP)len-1] & bit(0)) ==0)))
goto ab; /* round off */
else
goto auf; /* round up */
} else {
/* 2^(16n+1) <= q < 2^(16n+2), also 2^(k-m) < a/b < 2^(k-m+1). */
/* fill mantissa with a shift loop by 2 bits to the right: */
var uintD rounding_bits =
shiftrightcopy_loop_up(&q_MSDptr[1],y_mantMSDptr,len,2,q_MSDptr[0]);
(TheLfloat(y)->expo)++; /* increment exponent to k-m+1 */
if (((sintD)rounding_bits >= 0) /* shifted out bit =0 -> round off */
|| (((rounding_bits & bit(intDsize-2)) ==0) /* =1 and next bit =1 or rest r > 0 -> round up */
&& eq(STACK_0,Fixnum_0)
/* round-to-even */
&& ((y_mantMSDptr[(uintP)len-1] & bit(0)) ==0)))
goto ab; /* round off */
else
goto auf; /* round up */
}
}
auf: /* round up */
if ( inc_loop_down(&y_mantMSDptr[(uintP)len],len) ) {
/* carry due to rounding up */
y_mantMSDptr[0] = bit(intDsize-1); /* mantissa := 10...0 */
(TheLfloat(y)->expo)++; /* increment exponent */
}
ab: /* round off */
end_arith_call();
skipSTACK(2);
return y;
}
|