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
|
/*
Title: Arbitrary Precision Package.
Author: Dave Matthews, Cambridge University Computer Laboratory
Copyright (c) 2000
Cambridge University Technical Services Limited
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
Arbitrary precision package in C.
Integers are held in two formats in this system, long-form and short-form.
The two are distinquished by the integer tag bit, short-form having the tag
bit set and pointers to long-form being untagged.
The long-form integers use the standard Poly format for multi-word
objects, with the length count and flags in the word just before the object
pointed to. The sign of long-form integers is coded in one of the flag bits.
Short integers are signed quantities, and can be directly
manipulated by the relevant instructions, but if overflow occurs then the full
long versions of the operations will need to be called.
Long-form integers are held as vectors of bytes (i.e. unsigned char)
low-order byte first. It is assumed that a ``byte'' will hold an 8-bit
quantity and a ``long'' at least two ``bytes''. It is essential that unsigned
values are used.
Integers are always stored in the least possible number of words, and
will be shortened to the short-form when possible.
Thanks are due to D. Knuth for the long division algorithm.
*/
/*
DCJM 13/5/06 - Begin changes to use the top bit of the last byte as a sign bit.
Actually, it will probably be simpler to always use a whole byte for the sign.
Start by adding an extra zero byte.
N.B. This actually breaks the rule that all immutable values must have a
canonical representation. That is required for structure equality to work.
*/
#ifdef WIN32
#include "winconfig.h"
#else
#include "config.h"
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_ASSERT_H
#include <assert.h>
#define ASSERT(x) assert(x)
#else
#define ASSERT(x)
#endif
#include "globals.h"
#include "sys.h"
#include "run_time.h"
#include "arb.h"
#include "save_vec.h"
#include "processes.h"
/******************************************************************************/
/* */
/* get_length - utility function */
/* */
/******************************************************************************/
/* Returns the length of the argument with trailing zeros removed.
Excludes the sign byte (when we've added it - not done yet). */
static POLYUNSIGNED get_length(PolyWord x)
{
byte *u = (byte *)x.AsObjPtr();
POLYUNSIGNED lu = OBJECT_LENGTH(x)*sizeof(PolyWord);
for( ; (lu > 0) && (u[lu-1] == 0); lu--)
{
/* do nothing */
}
return lu;
} /* get_length */
/******************************************************************************/
/* */
/* get_C_ulong - called by runtime system */
/* */
/******************************************************************************/
/* indirection removed 31/10/93 SPF */
POLYUNSIGNED get_C_ulong(TaskData *taskData, PolyWord number)
{
if ( IS_INT(number) )
{
POLYSIGNED i = UNTAGGED(number);
if ( i < 0 ) raise_exception0(taskData, EXC_size );
return i;
}
else
{
POLYUNSIGNED length = get_length(number);
POLYSIGNED c = 0;
byte *ptr = number.AsCodePtr();
if (OBJ_IS_NEGATIVE(GetLengthWord(number))) raise_exception0(taskData, EXC_size );
if ( length > sizeof(PolyWord) ) raise_exception0(taskData, EXC_size );
while ( length-- ) c = (c << 8) | ((byte *) ptr)[length];
return c;
}
}
/******************************************************************************/
/* */
/* get_C_long - called by runtime system */
/* */
/******************************************************************************/
/* indirection removed 31/10/93 SPF */
#define MAX_INT_PLUS1 ((POLYUNSIGNED)0x80 << ( (sizeof(PolyWord)-1) *8))
POLYSIGNED get_C_long(TaskData *taskData, PolyWord number)
{
if ( IS_INT(number) )
{
return UNTAGGED(number);
}
else
{
int sign = OBJ_IS_NEGATIVE(GetLengthWord(number)) ? -1 : 0;
POLYUNSIGNED length = get_length(number);
POLYUNSIGNED c = 0;
byte *ptr = number.AsCodePtr();
if ( length > sizeof(PolyWord) ) raise_exception0(taskData, EXC_size );
while ( length-- )
{
c = (c << 8) | ptr[length];
}
if ( sign == 0 && c < MAX_INT_PLUS1) return (POLYSIGNED)c;
if ( sign != 0 && c <= MAX_INT_PLUS1) return -((POLYSIGNED)c);
raise_exception0(taskData, EXC_size );
/*NOTREACHED*/
return 0;
}
}
/******************************************************************************/
/* */
/* get_C_short - called by runtime system */
/* */
/******************************************************************************/
/* indirection removed 31/10/93 SPF */
short get_C_short(TaskData *taskData, PolyWord number)
{
int i = (int)get_C_long(taskData, number);
if ( i <= 32767 && i >= -32768 ) return i;
raise_exception0(taskData, EXC_size );
/*NOTREACHED*/
return 0;
}
/******************************************************************************/
/* */
/* get_C_ushort - called by runtime system */
/* */
/******************************************************************************/
/* indirection removed 31/10/93 SPF */
unsigned short get_C_ushort(TaskData *taskData, PolyWord number)
{
POLYUNSIGNED u = get_C_ulong(taskData, number );
if ( u <= 65535 ) return (short)u;
raise_exception0(taskData, EXC_size );
/*NOTREACHED*/
return 0;
}
/* Get a arbitrary precision value as a pair of words.
At present this is used to extract a 64-bit quantity as two
words. Only used in Windows code. */
void get_C_pair(TaskData *taskData, PolyWord number, unsigned long *pHi, unsigned long *pLo)
{
if ( IS_INT(number) )
{
/* It will fit in the low-order word. */
*pLo = (unsigned long) UNTAGGED(number);
*pHi = 0;
return;
}
else
{
POLYUNSIGNED length = get_length(number);
unsigned i;
unsigned long c;
POLYOBJPTR ptr = number.AsObjPtr();
if (OBJ_IS_NEGATIVE(GetLengthWord(number))) raise_exception0(taskData, EXC_size);
if ( length > 2*sizeof(unsigned long) ) raise_exception0(taskData, EXC_size);
/* Low-order word. */
if (length > sizeof(unsigned long)) i = sizeof(unsigned long); else i = length;
c = 0;
while (i--) c = (c << 8) | ((byte *) ptr)[i];
*pLo = c;
/* High-order word. */
i = length;
c = 0;
while (i-- > sizeof(unsigned long)) c = (c << 8) | ((byte *) ptr)[i];
*pHi = c;
return;
}
}
/******************************************************************************/
/* */
/* get_long - utility function */
/* */
/******************************************************************************/
/* Gets the arguments into the long form. */
/* HACK - must NOT be used for results. */
static Handle get_long(Handle x, Handle extend, int *sign)
{
if (IS_INT(DEREFWORD(x)))
{ /* Short form - put it in the temporary in the task-data */
POLYSIGNED x_v = UNTAGGED(DEREFWORD(x));
byte *u = DEREFBYTEHANDLE(extend);
if (x_v >= 0)
{
*sign = 0;
}
else /* Negative */
{
*sign = -1;
x_v = -x_v;
}
/* Put into extend buffer, low order byte first. */
for (unsigned i = 0; i < sizeof(PolyWord); i++)
{
u[i] = x_v & 0xff;
x_v = x_v >> 8;
}
return extend;
}
else
{ /* Long-form - x is an address. */
*sign = OBJ_IS_NEGATIVE(GetLengthWord(DEREFWORD(x))) ? -1 : 0;
return x;
}
} /* get_long */
/******************************************************************************/
/* */
/* copy_long - utility function */
/* */
/******************************************************************************/
static Handle copy_long(TaskData *taskData, Handle x, POLYUNSIGNED lx)
{
Handle y = alloc_and_save(taskData, WORDS(lx+1), F_BYTE_OBJ|F_MUTABLE_BIT);
// copy the bytes
byte *u = DEREFBYTEHANDLE(x);
byte *v = DEREFBYTEHANDLE(y);
for (unsigned i = 0; i < lx; i++) v[i] = u[i];
return y;
}
/******************************************************************************/
/* */
/* make_canonical - utility function */
/* */
/******************************************************************************/
/* make_canonical is used to force a result into its shortest form,
in the style of get_length, but also may convert its argument
from long to short integer */
static Handle make_canonical(TaskData *taskData, Handle x, int sign)
{ /* get length in BYTES */
POLYUNSIGNED size = get_length(DEREFWORD(x));
PolyObject *tempX = DEREFWORDHANDLE(x);
ASSERT(size < (OBJ_OBJECT_LENGTH(tempX->LengthWord())*sizeof(PolyWord))); // Check we always have a sign byte.
/* We can use the short representation if it will fit in 4 bytes. */
if (size <= sizeof(PolyWord))
{
/* Convert the digits. */
byte *u = DEREFBYTEHANDLE(x);
POLYUNSIGNED r = 0;
for (unsigned i=0; i < sizeof(PolyWord); i++)
{
r |= ((POLYUNSIGNED)u[i]) << (8*i);
}
/* Check for MAXTAGGED+1 before subtraction
in case MAXTAGGED is 0x7fffffff */
if (r <= MAXTAGGED || (r == MAXTAGGED+1 && sign < 0))
{
if (sign < 0)
return taskData->saveVec.push(TAGGED(-(POLYSIGNED)r));
else
return taskData->saveVec.push(TAGGED(r));
}
}
/* The length word of the object is changed to reflect the new length.
This is safe because any words thrown away must be zero. */
DEREFWORDHANDLE(x)->SetLengthWord(WORDS(size+1), F_BYTE_OBJ | (sign < 0 ? F_NEGATIVE_BIT: 0));
return x;
} /* make_canonical */
/******************************************************************************/
/* */
/* Make_XXX functions (used in runtime system) */
/* */
/******************************************************************************/
Handle Make_arbitrary_precision(TaskData *taskData, POLYSIGNED val)
/* Called from routines in the run-time system to generate an arbitrary
precision integer from a word value. */
{
if (val <= MAXTAGGED && val >= -MAXTAGGED-1) /* No overflow */
return taskData->saveVec.push(TAGGED(val));
POLYUNSIGNED uval;
if (val < 0) uval = -val;
else uval = val;
int words = 1;
// If the high order byte is non-zero add an extra word for the sign.
if ((uval >> ((sizeof(long)-1)*8)) != 0) words++;
Handle y = alloc_and_save(taskData, words, ((val < 0) ? F_NEGATIVE_BIT : 0)| F_BYTE_OBJ);
byte *v = DEREFBYTEHANDLE(y);
for (POLYUNSIGNED i = 0; uval != 0; i++)
{
v[i] = (byte)(uval & 0xff);
uval >>= 8;
}
return y;
}
Handle Make_unsigned(TaskData *taskData, POLYUNSIGNED uval)
/* Called from routines in the run-time system to generate an arbitrary
precision integer from an unsigned value. */
{
if (uval <= MAXTAGGED) return taskData->saveVec.push(TAGGED(uval));
int words = 1;
// If the high order byte is non-zero add an extra word for the sign.
if ((uval >> ((sizeof(long)-1)*8)) != 0) words++;
Handle y = alloc_and_save(taskData, words, F_BYTE_OBJ);
byte *v = DEREFBYTEHANDLE(y);
for (POLYUNSIGNED i = 0; uval != 0; i++)
{
v[i] = (byte)(uval & 0xff);
uval >>= 8;
}
return y;
}
/* Creates an arbitrary precision number from two words.
At present this is used for 64-bit quantities. */
Handle Make_arb_from_pair(TaskData *taskData, unsigned hi, unsigned lo)
{
/* If the high word is zero we can use either the tagged short
form or a single word. */
if (hi == 0) return Make_unsigned(taskData, lo);
int words = 2;
// If the high order byte is non-zero add an extra word for the sign.
if ((hi >> ((sizeof(hi)-1)*8)) != 0) words++;
Handle y = alloc_and_save(taskData, words, F_BYTE_OBJ);
byte *v = DEREFBYTEHANDLE(y);
int i;
for (i = 0; i < 4/* Get a 32 bit value. */; i++)
{
v[i] = lo & 0xff;
lo >>= 8;
}
for (; hi != 0 && i < 8; i++)
{
v[i] = hi & 0xff;
hi >>= 8;
}
return y;
}
/* Returns hi*scale+lo as an arbitrary precision number. Currently used
for Unix time values where the time is returned as two words, a number
of seconds and a number of microseconds and we wish to return the
result as a number of microseconds. */
Handle Make_arb_from_pair_scaled(TaskData *taskData, unsigned hi, unsigned lo, unsigned scale)
{
/* We might be able to compute the number as a 64 bit quantity and
then convert it but this is probably more portable. It does risk
overflowing the save vector. */
Handle hHi = Make_unsigned(taskData, hi);
Handle hLo = Make_unsigned(taskData, lo);
Handle hScale = Make_unsigned(taskData, scale);
return add_longc(taskData, mult_longc(taskData, hHi, hScale), hLo);
}
/******************************************************************************/
/* */
/* neg_longc - called from assembly code */
/* */
/******************************************************************************/
Handle neg_longc(TaskData *taskData, Handle x)
{
Handle long_x, long_y;
int sign_x;
if (IS_INT(DEREFWORD(x)))
{
POLYSIGNED s = UNTAGGED(DEREFWORD(x));
if (s != -MAXTAGGED-1) // If it won't overflow
return taskData->saveVec.push(TAGGED(-s));
}
/* Either overflow or long argument - convert to long form */
long_x = get_long(x, taskData->x_ehandle, &sign_x);
/* Get length of arg. */
POLYUNSIGNED lx = get_length(DEREFWORD(long_x));
/* copy the argument (so we can return it) */
long_y = copy_long(taskData, long_x,lx);
/* Return the value with the sign changed. */
return make_canonical(taskData, long_y, sign_x ^ -1);
} /* neg_longc */
/******************************************************************************/
/* */
/* add_unsigned_long - utility function */
/* computes sign * (abs(x) + abs(y)) */
/* */
/******************************************************************************/
static Handle add_unsigned_long(TaskData *taskData, Handle x, Handle y, int sign)
{
byte *u; /* byte-pointer for longer number */
byte *v; /* byte-pointer for shorter number */
Handle z;
int lu; /* length of u in bytes */
int lv; /* length of v in bytes */
/* find the longer number */
POLYUNSIGNED lx = get_length(DEREFWORD(x));
POLYUNSIGNED ly = get_length(DEREFWORD(y));
/* Make ``u'' the longer. */
if (lx < ly)
{
// Get result vector. It must be 1 byte longer than u
// to have space for any carry, plus one byte for the sign.
z = alloc_and_save(taskData, WORDS(ly+2), F_MUTABLE_BIT|F_BYTE_OBJ);
/* now safe to dereference pointers */
u = DEREFBYTEHANDLE(y); lu = ly;
v = DEREFBYTEHANDLE(x); lv = lx;
}
else
{
// Get result vector. It must be 1 byte longer than u
// to have space for any carry, plus one byte for the sign.
z = alloc_and_save(taskData, WORDS(lx+2), F_MUTABLE_BIT|F_BYTE_OBJ);
/* now safe to dereference pointers */
u = DEREFBYTEHANDLE(x); lu = lx;
v = DEREFBYTEHANDLE(y); lv = ly;
}
/* do the actual addition */
byte *w = DEREFBYTEHANDLE(z);
int carry = 0;
int i = 0;
/* Do the additions */
for( ; i < lv; i++)
{
carry += u[i] + v[i];
w[i] = carry & 0xff;
carry >>= 8;
}
/* Add the carry to the rest of ``u''. */
for( ; i < lu; i++)
{
carry += u[i];
w[i] = carry & 0xff;
carry >>= 8;
}
/* Finally put the carry into the last byte */
w[i] = (byte)carry;
return make_canonical(taskData, z, sign);
} /* add_unsigned_long */
/******************************************************************************/
/* */
/* sub_unsigned_long - utility function */
/* computes sign * (abs(x) - abs(y)) */
/* */
/******************************************************************************/
static Handle sub_unsigned_long(TaskData *taskData, Handle x, Handle y, int sign)
{
byte *u; /* byte-pointer alias for larger number */
byte *v; /* byte-pointer alias for smaller number */
int lu; /* length of u in bytes */
int lv; /* length of v in bytes */
Handle z;
/* get the larger argument into ``u'' */
/* This is necessary so that we can discard */
/* the borrow at the end of the subtraction */
POLYUNSIGNED lx = get_length(DEREFWORD(x));
POLYUNSIGNED ly = get_length(DEREFWORD(y));
if (lx < ly)
{
sign ^= -1; /* swap sign of result SPF 21/1/94 */
z = alloc_and_save(taskData, WORDS(ly+1), F_MUTABLE_BIT|F_BYTE_OBJ);
/* now safe to dereference pointers */
u = DEREFBYTEHANDLE(y); lu = ly;
v = DEREFBYTEHANDLE(x); lv = lx;
}
else if (ly < lx)
{
z = alloc_and_save(taskData, WORDS(lx+1), F_MUTABLE_BIT|F_BYTE_OBJ);
/* now safe to dereference pointers */
u = DEREFBYTEHANDLE(x); lu = lx;
v = DEREFBYTEHANDLE(y); lv = ly;
}
else /* lx == ly */
{ /* Must look at the numbers to decide which is bigger. */
int i = lx - 1;
for( ; i >= 0 && DEREFBYTEHANDLE(x)[i] == DEREFBYTEHANDLE(y)[i]; i--);
if (i < 0) return taskData->saveVec.push(TAGGED(0)); /* They are equal */
if (DEREFBYTEHANDLE(x)[i] < DEREFBYTEHANDLE(y)[i])
{
sign ^= -1; /* swap sign of result SPF 21/1/94 */
z = alloc_and_save(taskData, WORDS(ly+1), F_MUTABLE_BIT|F_BYTE_OBJ);
/* now safe to dereference pointers */
u = DEREFBYTEHANDLE(y); lu = ly;
v = DEREFBYTEHANDLE(x); lv = lx;
}
else
{
z = alloc_and_save(taskData, WORDS(lx+1), F_MUTABLE_BIT|F_BYTE_OBJ);
/* now safe to dereference pointers */
u = DEREFBYTEHANDLE(x); lu = lx;
v = DEREFBYTEHANDLE(y); lv = ly;
}
}
byte *w = DEREFBYTEHANDLE(z);
int borrow = 1; /* Becomes 0 if there is a borrow */
int i = 0;
/* Do the subtractions */
for( ; i < lv; i++)
{
borrow += 255 + u[i] - v[i];
w[i] = borrow & 0xff;
borrow >>= 8;
}
/* Add the borrow into the rest of ``u''. */
for( ; i < lu; i++)
{
borrow += 255 + u[i];
w[i] = borrow & 0xff;
borrow >>= 8;
}
return make_canonical(taskData, z, sign);
} /* sub_unsigned_long */
/******************************************************************************/
/* */
/* add_longc - called from sparc_dep.c */
/* */
/******************************************************************************/
Handle add_longc(TaskData *taskData, Handle y, Handle x)
{
Handle long_x, long_y;
int sign_x, sign_y;
if (IS_INT(DEREFWORD(x)) && IS_INT(DEREFWORD(y)))
{ /* Both short */
/* The easiest way to do the addition is simply *x-1+*y, but that
makes it more difficult to check for overflow. */
POLYSIGNED t = UNTAGGED(DEREFWORD(x)) + UNTAGGED(DEREFWORD(y));
if (t <= MAXTAGGED && t >= -MAXTAGGED-1) /* No overflow */
{
return taskData->saveVec.push(TAGGED(t));
}
}
/* Either overflow or long arguments - convert to long form */
long_x = get_long(x, taskData->x_ehandle , &sign_x);
long_y = get_long(y, taskData->y_ehandle , &sign_y);
/* Work out whether to add or subtract */
if ((sign_y ^ sign_x) >= 0) /* signs the same? */
{ /* sign(x) * (abs(x) + abs(y)) */
return add_unsigned_long(taskData, long_x, long_y, sign_x);
}
else
{ /* sign(x) * (abs(x) - abs(y)) */
return sub_unsigned_long(taskData, long_x, long_y, sign_x);
}
} /* add_longc */
/******************************************************************************/
/* */
/* sub_longc - called from sparc_dep.c */
/* */
/******************************************************************************/
Handle sub_longc(TaskData *taskData, Handle y, Handle x)
{
Handle long_x, long_y;
int sign_x, sign_y;
if (IS_INT(DEREFWORD(x)) &&
IS_INT(DEREFWORD(y))) /* Both short */
{
/* The easiest way to do the subtraction is simply *x-*y+1, but that
makes it more difficult to check for overflow. */
POLYSIGNED t = UNTAGGED(DEREFWORD(x)) - UNTAGGED(DEREFWORD(y));
if (t <= MAXTAGGED && t >= -MAXTAGGED-1) /* No overflow */
return taskData->saveVec.push(TAGGED(t));
}
/* Either overflow or long arguments. */
long_x = get_long(x, taskData->x_ehandle , &sign_x); /* Convert to long form */
long_y = get_long(y, taskData->y_ehandle , &sign_y);
/* If the signs are different add the two values. */
if ((sign_y ^ sign_x) < 0) /* signs differ */
{ /* sign(x) * (abs(x) + abs(y)) */
return add_unsigned_long(taskData, long_x, long_y, sign_x);
}
else
{ /* sign(x) * (abs(x) - abs(y)) */
return sub_unsigned_long(taskData, long_x, long_y, sign_x);
}
} /* sub_longc */
/******************************************************************************/
/* */
/* mult_longc - called from sparc_assembly.s */
/* */
/******************************************************************************/
Handle mult_longc(TaskData *taskData, Handle y, Handle x)
{
Handle long_x, long_y, long_z;
int sign_x, sign_y;
/* I'm not sure how to detect overflow here - so go through the full
routine even if both arguments are short. */
/* Convert to long form */
long_x = get_long(x, taskData->x_ehandle , &sign_x);
long_y = get_long(y, taskData->y_ehandle , &sign_y);
/* Get lengths of args. */
POLYUNSIGNED lx = get_length(DEREFWORD(long_x));
POLYUNSIGNED ly = get_length(DEREFWORD(long_y));
/* Check for zero args. */
if (lx == 0 || ly == 0)
{
return taskData->saveVec.push(TAGGED(0));
}
/* Get space for result */
long_z = alloc_and_save(taskData, WORDS(lx+ly+1), F_MUTABLE_BIT|F_BYTE_OBJ);
{
/* Can now load the actual addresses because they will not change now. */
byte *u = DEREFBYTEHANDLE(long_x);
byte *v = DEREFBYTEHANDLE(long_y);
byte *w = DEREFBYTEHANDLE(long_z);
for(POLYUNSIGNED i = 0; i < lx; i++)
{
POLYUNSIGNED j;
long r = 0; /* Set the carry to zero */
for(j = 0; j < ly; j++)
{
/* Compute the product. */
r += u[i] * v[j];
/* Now add in to the result. */
r += w[i+j];
w[i+j] = r & 0xff;
r >>= 8;
}
/* Put in any carry. */
w[i+j] = (byte)r;
}
return make_canonical(taskData, long_z, sign_x ^ sign_y);
}
} /* mult_long */
/******************************************************************************/
/* */
/* div_unsigned_long - utility function */
/* */
/******************************************************************************/
static void div_unsigned_long(byte *u, byte *v, byte *res, POLYUNSIGNED lu, POLYUNSIGNED lv)
/* Unsigned division. This is the main divide and remainder routine. The
result is packed into ``res'' and is separated out by div and rem */
{
POLYUNSIGNED i,j;
int bits;
long r;
/* Find out how far to shift v to get a 1 in the top bit. */
bits = 0;
for(r = v[lv-1]; r < 128; r <<= 1) bits++; /* 128 ??? */
/* Shift u that amount into res+2. We have allowed enough room for
overflow. */
r = 0;
for (i = 0; i < lu; i++)
{
r |= u[i] << bits; /*``Or in'' the new bits after shifting*/
res[i+2] = r & 0xff; /* Put into the destination. */
r >>= 8; /* and shift down the carry. */
}
res[i+2] = (byte)r; /* Put in the carry */
/* And v that amount. It has already been copied. */
if ( bits )
{
r = 0;
for (i = 0; i < lv; i++)
{ r |= v[i] << bits; v[i] = r & 0xff; r >>= 8; }
/* No carry */
}
for(j = lu+2; j >= lv+2; j--)
{
/* j iterates over the higher digits of the dividend until we are left
with a number which is less than the divisor. This is the remainder. */
long quotient, dividend, r;
dividend = res[j]*256 + res[j-1];
quotient = (res[j] == v[lv-1]) ? 255 : dividend/(long)v[lv-1];
if (lv != 1)
{
while ((long)v[lv-2]*quotient >
(dividend - quotient*(long)v[lv-1])*256 + (long)res[j-2])
{
quotient--;
}
}
/* The quotient is at most 1 too large */
/* Subtract the product of this with ``v'' from ``res''. */
r = 1; /* Initial borrow */
for(i = 0; i < lv; i++)
{
r += 255 + res[j-lv+i] - quotient * v[i];
res[j-lv+i] = r & 0xff;
r >>= 8;
}
r += res[j]; /* Borrow from leading digit. */
/* If we are left with a borrow when the subtraction is complete the
quotient must have been too big. We add ``v'' to the dividend and
subtract 1 from the quotient. */
if (r == 0 /* would be 1 if there were no borrow */)
{
quotient --;
r = 0;
for (i = 0; i < lv; i++)
{
r += v[i] + res[j-lv+i];
res[j-lv+i] = r & 0xff;
r >>= 8;
}
}
/* Place the next digit of quotient in result */
res[j] = (byte)quotient;
}
/* Likewise the remainder. */
if (bits)
{
r = 0;
j = lv;
j = lv;
while (j > 0)
{
j--;
r |= res[j+2];
res[j+2] = (r >> bits) & 0xff;
r = (r & 0xff) << 8;
}
}
} /* div_unsigned_long */
/******************************************************************************/
/* */
/* div_long_c - called from sparc_assembly.s */
/* */
/******************************************************************************/
Handle div_longc(TaskData *taskData, Handle y, Handle x)
{
Handle long_x, long_y;
int sign_x, sign_y;
if (IS_INT(DEREFWORD(x)) &&
IS_INT(DEREFWORD(y))) /* Both short */
{
POLYSIGNED xs = UNTAGGED(DEREFWORD(x));
POLYSIGNED ys = UNTAGGED(DEREFWORD(y));
/* Raise exceptions if dividing by zero. */
if (ys == 0)
raise_exception0(taskData, EXC_divide);
/* Only possible overflow is minint div -1 */
if (xs != -MAXTAGGED-1 || ys != -1)
return taskData->saveVec.push(TAGGED(xs / ys));
}
/* Long operands or overflow - convert to long form */
long_x = get_long(x, taskData->x_ehandle , &sign_x);
long_y = get_long(y, taskData->y_ehandle , &sign_y);
/* Get lengths of args. */
POLYUNSIGNED lx = get_length(DEREFWORD(long_x));
POLYUNSIGNED ly = get_length(DEREFWORD(long_y));
/* If length of v is zero raise divideerror */
if (ly == 0) raise_exception0(taskData, EXC_divide);
/* Get quotient */
if (lx < ly)
{
/* When x < y quotient must be zero. */
return taskData->saveVec.push(TAGGED(0));
}
else
{
Handle long_z;
long_y = copy_long(taskData, long_y,ly); /* copy in case it needs shifting */
/* vector for result - size of x + 3 */
long_z = alloc_and_save(taskData, WORDS(lx+3+1), F_MUTABLE_BIT|F_BYTE_OBJ);
div_unsigned_long
(DEREFBYTEHANDLE(long_x),
DEREFBYTEHANDLE(long_y),
DEREFBYTEHANDLE(long_z),
lx, ly);
/* Copy it down */
POLYUNSIGNED i;
for(i = 0; i <= lx-ly; i++)
{
DEREFBYTEHANDLE(long_z)[i] = DEREFBYTEHANDLE(long_z)[i+ly+2];
}
/* Clear the rest */
for(; i < lx+3; i++)
{
DEREFBYTEHANDLE(long_z)[i] = 0;
}
return make_canonical(taskData, long_z, sign_x ^ sign_y);
}
} /* div_longc */
/******************************************************************************/
/* */
/* rem_long_c - called from sparc_assembly.s */
/* */
/******************************************************************************/
Handle rem_longc(TaskData *taskData, Handle y, Handle x)
{
Handle long_x, long_y;
int sign_x, sign_y;
if (IS_INT(DEREFWORD(x)) &&
IS_INT(DEREFWORD(y))) /* Both short */
{
POLYSIGNED xs = UNTAGGED(DEREFWORD(x));
POLYSIGNED ys = UNTAGGED(DEREFWORD(y));
/* Raise exceptions if remaindering by zero. */
if (ys == 0)
raise_exception0(taskData, EXC_divide);
/* Only possible overflow is minint mod -1 */
if (xs != -MAXTAGGED-1 || ys != -1)
return taskData->saveVec.push(TAGGED(xs % ys));
}
/* Long operands or overflow - convert any shorts to long form */
long_x = get_long(x, taskData->x_ehandle , &sign_x);
long_y = get_long(y, taskData->y_ehandle , &sign_y);
/* Get lengths of args. */
POLYUNSIGNED lx = get_length(DEREFWORD(long_x));
POLYUNSIGNED ly = get_length(DEREFWORD(long_y));
/* If length of y is zero raise divideerror */
if (ly == 0) raise_exception0(taskData, EXC_divide);
/* Get remainder */
if (lx < ly)
{
/* When x < y remainder is x. */
return x; /* NOT long_x, since x may be a short. SPF 1/11/95 */
}
else
{
POLYUNSIGNED i;
Handle long_z;
/* copy in case it needs shifting */
long_y = copy_long(taskData, long_y,ly);
/* vector for result - size of x + 3 */
long_z = alloc_and_save(taskData, WORDS(lx+3+1), F_MUTABLE_BIT|F_BYTE_OBJ);
div_unsigned_long
(DEREFBYTEHANDLE(long_x),
DEREFBYTEHANDLE(long_y),
DEREFBYTEHANDLE(long_z),
lx, ly);
/* Copy it down */
for(i = 0; i < ly; i++)
{
DEREFBYTEHANDLE(long_z)[i] = DEREFBYTEHANDLE(long_z)[i+2];
}
/* Clear the rest */
for(; i < lx+3; i++)
{
DEREFBYTEHANDLE(long_z)[i] = 0;
}
return make_canonical(taskData, long_z, sign_x /* Same sign as dividend */ );
/* ML says it should have same as divisor. */
}
} /* rem_longc */
/******************************************************************************/
/* */
/* compare_unsigned - utility function */
/* */
/******************************************************************************/
/* compare_unsigned is passed LONG integers only */
static int compare_unsigned(Handle x, Handle y)
{
/* First look at the lengths */
POLYUNSIGNED lx = get_length(DEREFWORD(x));
POLYUNSIGNED ly = get_length(DEREFWORD(y));
if (lx != ly) /* u > v if u longer than v */
{
return (lx > ly ? 1 : -1);
}
{ /* Same length - look at the values. */
byte *u = DEREFBYTEHANDLE(x);
byte *v = DEREFBYTEHANDLE(y);
POLYUNSIGNED i = lx;
while (i > 0)
{
i--;
if (u[i] != v[i])
{
return u[i] > v[i] ? 1 : -1;
}
}
/* Must be equal */
return 0;
}
} /* compare_unsigned */
/******************************************************************************/
/* */
/* compareLong - called by run-time system */
/* */
/******************************************************************************/
int compareLong(TaskData *taskData, Handle y, Handle x)
{
Handle long_x, long_y;
int sign_x, sign_y;
if (IS_INT(DEREFWORD(x)) &&
IS_INT(DEREFWORD(y))) /* Both short */
{
POLYSIGNED t = UNTAGGED(DEREFWORD(x)) - UNTAGGED(DEREFWORD(y));
if (t == 0) return 0; /* equal */
else if (t < 0) return -1; /* less */
else return 1; /* greater */
}
/* Convert to long form */
long_x = get_long(x, taskData->x_ehandle, &sign_x);
long_y = get_long(y, taskData->y_ehandle, &sign_y);
if (sign_x >= 0) /* x is positive */
{
if (sign_y >= 0) /* y also positive */
{
return compare_unsigned(long_x, long_y);
}
else /* y negative so x > y */
{
return 1;
}
}
else
{ /* x is negative */
if (sign_y < 0) /* y also negative */
{
return compare_unsigned(long_y, long_x);
}
else /* y positive so x < y */
{
return -1;
}
}
} /* compareLong */
/* logical_long. General purpose function for binary logical operations. */
static Handle logical_long(TaskData *taskData, Handle x, Handle y, int signX, int signY,
unsigned(*op)(unsigned, unsigned))
{
byte *u; /* byte-pointer for longer number */
byte *v; /* byte-pointer for shorter number */
Handle z;
int sign, signU, signV;
POLYUNSIGNED lu; /* length of u in bytes */
POLYUNSIGNED lv; /* length of v in bytes */
{ /* find the longer number */
POLYUNSIGNED lx = get_length(DEREFWORD(x));
POLYUNSIGNED ly = get_length(DEREFWORD(y));
/* Make ``u'' the longer. */
if (lx < ly)
{
// Get result vector. There can't be any carry at the end so
// we just need to make this as large as the larger number
// plus sign byte
z = alloc_and_save(taskData, WORDS(ly+1), F_MUTABLE_BIT|F_BYTE_OBJ);
/* now safe to dereference pointers */
u = DEREFBYTEHANDLE(y); lu = ly;
v = DEREFBYTEHANDLE(x); lv = lx;
signU = signY; signV = signX;
}
else
{
/* Get result vector. */
z = alloc_and_save(taskData, WORDS(lx+1), F_MUTABLE_BIT|F_BYTE_OBJ);
/* now safe to dereference pointers */
u = DEREFBYTEHANDLE(x); lu = lx;
v = DEREFBYTEHANDLE(y); lv = ly;
signU = signX; signV = signY;
}
}
sign = (*op)(signU, signV); /* -1 if negative, 0 if positive. */
{ /* do the actual operations */
byte *w = DEREFBYTEHANDLE(z);
int borrowU = 1, borrowV = 1, borrowW = 1;
POLYUNSIGNED i = 0;
/* Do the operations. */
for( ; i < lv; i++)
{
int wI;
/* Have to convert negative values to twos complement. */
if (signU) borrowU += 255 - u[i];
else borrowU = u[i];
if (signV) borrowV += 255 - v[i];
else borrowV = v[i];
wI = (*op)(borrowU, borrowV) & 255;
if (sign)
{
/* Have to convert the result back to twos complement. */
borrowW += 255 - wI;
w[i] = borrowW & 255;
borrowW >>= 8;
}
else w[i] = wI;
borrowU >>= 8;
borrowV >>= 8;
}
/* At this point the borrow of V should be zero. */
ASSERT(signV == 0 || borrowV == 0);
/* Continue with ``u''. */
for( ; i < lu; i++)
{
int wI;
if (signU) borrowU += 255 - u[i];
else borrowU = u[i];
if (signV) borrowV = 255; else borrowV = 0;
wI = (*op)(borrowU, borrowV) & 255;
if (sign)
{
/* Have to convert the result back to twos complement. */
borrowW += 255 - wI;
w[i] = borrowW & 255;
borrowW >>= 8;
}
else w[i] = wI;
borrowU >>= 8;
borrowV >>= 8;
}
/* We should now no longer have any borrows. */
ASSERT(signU == 0 || borrowU == 0);
ASSERT(sign == 0 || borrowW == 0);
}
return make_canonical(taskData, z, sign);
} /* logical_long */
static unsigned doAnd(unsigned i, unsigned j)
{
return i & j;
}
static unsigned doOr(unsigned i, unsigned j)
{
return i | j;
}
static unsigned doXor(unsigned i, unsigned j)
{
return i ^ j;
}
/******************************************************************************/
/* */
/* and_longc - called by run-time system */
/* */
/******************************************************************************/
Handle and_longc(TaskData *taskData, Handle y, Handle x)
{
Handle long_x, long_y;
int sign_x, sign_y;
if (IS_INT(DEREFWORD(x)) &&
IS_INT(DEREFWORD(y))) /* Both short */
{
/* There's no problem with overflow so we can just AND together
the values. */
POLYSIGNED t = UNTAGGED(DEREFWORD(x)) & UNTAGGED(DEREFWORD(y));
return taskData->saveVec.push(TAGGED(t));
}
/* Long arguments. */
long_x = get_long(x, taskData->x_ehandle , &sign_x); /* Convert to long form */
long_y = get_long(y, taskData->y_ehandle , &sign_y);
return logical_long(taskData, long_x, long_y, sign_x, sign_y, doAnd);
}
/******************************************************************************/
/* */
/* or_longc - called by run-time system */
/* */
/******************************************************************************/
Handle or_longc(TaskData *taskData, Handle y, Handle x)
{
Handle long_x, long_y;
int sign_x, sign_y;
if (IS_INT(DEREFWORD(x)) &&
IS_INT(DEREFWORD(y))) /* Both short */
{
/* There's no problem with overflow so we can just OR together
the values. */
POLYSIGNED t = UNTAGGED(DEREFWORD(x)) | UNTAGGED(DEREFWORD(y));
return taskData->saveVec.push(TAGGED(t));
}
/* Long arguments. */
long_x = get_long(x, taskData->x_ehandle , &sign_x); /* Convert to long form */
long_y = get_long(y, taskData->y_ehandle , &sign_y);
return logical_long(taskData, long_x, long_y, sign_x, sign_y, doOr);
}
/******************************************************************************/
/* */
/* xor_longc - called by run-time system */
/* */
/******************************************************************************/
Handle xor_longc(TaskData *taskData, Handle y, Handle x)
{
Handle long_x, long_y;
int sign_x, sign_y;
if (IS_INT(DEREFWORD(x)) &&
IS_INT(DEREFWORD(y))) /* Both short */
{
/* There's no problem with overflow so we can just XOR together
the values. */
POLYSIGNED t = UNTAGGED(DEREFWORD(x)) ^ UNTAGGED(DEREFWORD(y));
return taskData->saveVec.push(TAGGED(t));
}
/* Long arguments. */
long_x = get_long(x, taskData->x_ehandle , &sign_x); /* Convert to long form */
long_y = get_long(y, taskData->y_ehandle , &sign_y);
return logical_long(taskData, long_x, long_y, sign_x, sign_y, doXor);
}
/*
These functions are used primarily during porting. They handle both
short and long forms of integers and are generally superseded by
hand coded assembly code versions and the code generator.
Some of them are retained in some code-generators to handle the
long forms of integers.
*/
Handle equal_longc(TaskData *taskData, Handle y, Handle x)
/* Returns 1 if the arguments are equal, otherwise 0. */
{
bool c = compareLong(taskData, y, x) == 0;
return taskData->saveVec.push(c ? TAGGED(1) : TAGGED(0));
}
Handle not_equal_longc(TaskData *taskData, Handle y, Handle x)
{
bool c = compareLong(taskData, y, x) != 0;
return taskData->saveVec.push(c ? TAGGED(1) : TAGGED(0));
}
Handle gt_longc(TaskData *taskData, Handle y, Handle x)
{
bool c = (compareLong(taskData, y, x) == 1);
return taskData->saveVec.push(c ? TAGGED(1) : TAGGED(0));
}
Handle ls_longc(TaskData *taskData, Handle y, Handle x)
{
bool c = (compareLong(taskData, y, x) == -1);
return taskData->saveVec.push(c ? TAGGED(1) : TAGGED(0));
}
Handle ge_longc(TaskData *taskData, Handle y, Handle x)
{
bool c = compareLong(taskData, y, x) != -1;
return taskData->saveVec.push(c ? TAGGED(1) : TAGGED(0));
}
Handle le_longc(TaskData *taskData, Handle y, Handle x)
{
bool c = compareLong(taskData, y, x) != 1;
return taskData->saveVec.push(c ? TAGGED(1) : TAGGED(0));
}
/* Return the low-order bits of an integer whether it is long or
short form. */
Handle int_to_word_c(TaskData *taskData, Handle x)
{
/* If it's already short it's easy. */
if (IS_INT(DEREFWORD(x)))
return x;
byte *u = DEREFBYTEHANDLE(x);
POLYUNSIGNED r = 0;
for (unsigned i=0; i < sizeof(PolyWord); i++)
{
r |= u[i] << (8*i);
}
if (OBJ_IS_NEGATIVE(x->Word().AsObjPtr()->LengthWord()))
r = 0-r; // Use 0-r rather than -r since it's an unsigned value.
return taskData->saveVec.push(TAGGED(r));
}
|