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
|
/* DECIMAL.C (c) Copyright Roger Bowler, 1991-2003 */
/* ESA/390 Packed Decimal Routines */
/*-------------------------------------------------------------------*/
/* This module contains packed decimal subroutines for ESA/390. */
/* */
/* Acknowledgements: */
/* The lowest level string-math functions are modelled on */
/* algorithms in D.E.Knuth's 'The Art of Computer Programming */
/* Vol.2', and on C.E.Burton's algorithms in DDJ #89. */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* Complete rework for reworked instruction decode/execution code */
/* Jan Jaeger 01/07/00 */
/* Add trialrun to ED and EDMK Jan Jaeger 19/07/00 */
/* Fix random MP bug - Mario Bezzi */
/* Clear DXC on data exception - Peter Kuschnerus V209*/
/* z/Architecture support - (c) Copyright Jan Jaeger, 1999-2003 */
/* TP instruction - Roger Bowler 08/02/01 */
/*-------------------------------------------------------------------*/
#include "hercules.h"
#include "opcode.h"
#include "inline.h"
#if !defined(_DECIMAL_C)
#define _DECIMAL_C
/*-------------------------------------------------------------------*/
/* Internal macro definitions */
/*-------------------------------------------------------------------*/
#define MAX_DECIMAL_LENGTH 16
#define MAX_DECIMAL_DIGITS (((MAX_DECIMAL_LENGTH)*2)-1)
/*-------------------------------------------------------------------*/
/* Add two decimal byte strings as unsigned decimal numbers */
/* */
/* Input: */
/* dec1 A 31-byte area containing the decimal digits of */
/* the first operand. Each byte contains one decimal */
/* digit in the low-order 4 bits of the byte. */
/* dec2 A 31-byte area containing the decimal digits of */
/* the second operand. Each byte contains one decimal */
/* digit in the low-order 4 bits of the byte. */
/* Output: */
/* result Points to a 31-byte area to contain the result */
/* digits. One decimal digit is placed in the low-order */
/* 4 bits of each byte. */
/* count Points to an integer to receive the number of */
/* digits in the result excluding leading zeroes. */
/* This field is set to zero if the result is all zero, */
/* or to MAX_DECIMAL_DIGITS+1 if overflow occurred. */
/*-------------------------------------------------------------------*/
static void add_decimal (BYTE *dec1, BYTE *dec2,
BYTE *result, int *count)
{
int d; /* Decimal digit */
int i; /* Array subscript */
int n = 0; /* Significant digit counter */
int carry = 0; /* Carry indicator */
/* Add digits from right to left */
for (i = MAX_DECIMAL_DIGITS - 1; i >= 0; i--)
{
/* Add digits from first and second operands */
d = dec1[i] + dec2[i] + carry;
/* Check for carry into next digit */
if (d > 9) {
d -= 10;
carry = 1;
} else {
carry = 0;
}
/* Check for significant digit */
if (d != 0)
n = MAX_DECIMAL_DIGITS - i;
/* Store digit in result */
result[i] = d;
} /* end for */
/* Check for carry out of leftmost digit */
if (carry)
n = MAX_DECIMAL_DIGITS + 1;
/* Return significant digit counter */
*count = n;
} /* end function add_decimal */
/*-------------------------------------------------------------------*/
/* Subtract two decimal byte strings as unsigned decimal numbers */
/* */
/* Input: */
/* dec1 A 31-byte area containing the decimal digits of */
/* the first operand. Each byte contains one decimal */
/* digit in the low-order 4 bits of the byte. */
/* dec2 A 31-byte area containing the decimal digits of */
/* the second operand. Each byte contains one decimal */
/* digit in the low-order 4 bits of the byte. */
/* Output: */
/* result Points to a 31-byte area to contain the result */
/* digits. One decimal digit is placed in the low-order */
/* 4 bits of each byte. */
/* count Points to an integer to receive the number of */
/* digits in the result excluding leading zeroes. */
/* This field is set to zero if the result is all zero. */
/* sign -1 if the result is negative (operand2 > operand1) */
/* +1 if the result is positive (operand2 <= operand1) */
/*-------------------------------------------------------------------*/
static void subtract_decimal (BYTE *dec1, BYTE *dec2,
BYTE *result, int *count, int *sign)
{
int d; /* Decimal digit */
int i; /* Array subscript */
int n = 0; /* Significant digit counter */
int borrow = 0; /* Borrow indicator */
int rc; /* Return code */
BYTE *higher; /* -> Higher value operand */
BYTE *lower; /* -> Lower value operand */
/* Compare digits to find which operand has higher numeric value */
rc = memcmp (dec1, dec2, MAX_DECIMAL_DIGITS);
/* Return positive zero result if both operands are equal */
if (rc == 0) {
memset (result, 0, MAX_DECIMAL_DIGITS);
*count = 0;
*sign = +1;
return;
}
/* Point to higher and lower value operands and set sign */
if (rc > 0) {
higher = dec1;
lower = dec2;
*sign = +1;
} else {
lower = dec1;
higher = dec2;
*sign = -1;
}
/* Subtract digits from right to left */
for (i = MAX_DECIMAL_DIGITS - 1; i >= 0; i--)
{
/* Subtract lower operand digit from higher operand digit */
d = higher[i] - lower[i] - borrow;
/* Check for borrow from next digit */
if (d < 0) {
d += 10;
borrow = 1;
} else {
borrow = 0;
}
/* Check for significant digit */
if (d != 0)
n = MAX_DECIMAL_DIGITS - i;
/* Store digit in result */
result[i] = d;
} /* end for */
/* Return significant digit counter */
*count = n;
} /* end function subtract_decimal */
/*-------------------------------------------------------------------*/
/* Divide two decimal byte strings as unsigned decimal numbers */
/* */
/* Input: */
/* dec1 A 31-byte area containing the decimal digits of */
/* the dividend. Each byte contains one decimal */
/* digit in the low-order 4 bits of the byte. */
/* count1 The number of significant digits in the dividend. */
/* dec2 A 31-byte area containing the decimal digits of */
/* the divisor. Each byte contains one decimal */
/* digit in the low-order 4 bits of the byte. */
/* count2 The number of significant digits in the divisor. */
/* Output: */
/* quot Points to a 31-byte area to contain the quotient */
/* digits. One decimal digit is placed in the low-order */
/* 4 bits of each byte. */
/* rem Points to a 31-byte area to contain the remainder */
/* digits. One decimal digit is placed in the low-order */
/* 4 bits of each byte. */
/* Restrictions: */
/* It is assumed that the caller has already verified that */
/* divide overflow cannot occur, that the divisor is not zero, */
/* and that the dividend has at least one high order zero. */
/*-------------------------------------------------------------------*/
static void divide_decimal (BYTE *dec1, int count1, BYTE *dec2,
int count2, BYTE *quot, BYTE *rem)
{
BYTE *num1; /* -> dividend digits */
BYTE *num2; /* -> divisor digits */
int div, flag, scale; /* Work areas for algorithm */
int index, index1, index2; /* Work areas for algorithm */
int indexq, indexr, temp1, temp2; /* Work areas for algorithm */
int temp3, temp4, temp5, qtest; /* Work areas for algorithm */
/* Clear the result fields */
memset (quot, 0, MAX_DECIMAL_DIGITS);
memset (rem, 0, MAX_DECIMAL_DIGITS);
/* If dividend is zero then return zero quotient and remainder */
if (count1 == 0)
return;
/* If dividend is less than divisor then return zero quotient
and set remainder equal to dividend */
if (memcmp (dec1, dec2, MAX_DECIMAL_DIGITS) < 0)
{
memcpy (rem, dec1, MAX_DECIMAL_DIGITS);
return;
}
/* Adjust dividend digit count to give one leading zero */
count1++;
/* Point to significant digits of dividend with leading zero */
num1 = dec1 + MAX_DECIMAL_DIGITS - count1;
/* Point to significant digits of divisor */
num2 = dec2 + MAX_DECIMAL_DIGITS - count2;
scale = 10 / (num2[0] + 1);
if (scale > 1)
{
for (index1 = count1-1, flag = 0; index1 >= 0; index1--)
{
div = flag + scale*num1[index1];
num1[index1] = div % 10;
flag = div / 10;
} /* end for(index1) */
for (index2 = count2-1, flag = 0; index2 >= 0; index2--)
{
div = flag + scale*num2[index2];
num2[index2] = div % 10;
flag = div / 10;
} /* end for(index2) */
} /* end if(scale>1) */
for (index1 = 0; index1 < count1-count2; index1++)
{
if (num2[0] == num1[index1])
qtest = 9;
else
{
temp2 = (index1+1 < count1) ? num1[index1+1] : 0;
qtest = (10*num1[index1] + temp2) / num2[0];
}
temp2 = num1[index1];
temp4 = num2[0];
temp1 = (count2 >= 2) ? num2[1] : 0;
if (index1+1 < count1)
{
temp3 = num1[index1+1];
temp5 = (index1+2 < count1) ? num1[index1+2] : 0;
}
else
{
temp3 = 0;
temp5 = 0;
}
while (qtest*temp1 > (10*(10*temp2 + temp3
- qtest*temp4) + temp5))
--qtest;
for (index = index1+count2, index2 = count2-1, flag = 0;
index >= index1; index--, index2--)
{
if (index2 >= 0)
flag -= qtest*num2[index2];
div = flag + num1[index];
if (div < 0)
{
flag = div / 10;
div %= 10;
if (div < 0)
{
div += 10;
--flag;
}
}
else
flag = 0;
num1[index] = div;
} /* end for(index) */
indexq = MAX_DECIMAL_DIGITS - (count1-count2) + index1;
if (flag != 0)
{
quot[indexq] = qtest - 1;
for (index = index1+count2, index2 = count2-1, flag = 0;
index >= index1; index--, index2--)
{
if (index2 >= 0)
flag += num2[index2];
div = flag + num1[index];
if (div > 9)
{
div -= 10;
flag = 1;
}
else
flag = 0;
num1[index] = div;
} /* end for(index) */
}
else
quot[indexq] = qtest;
} /* end for(index1) */
for (index1 = count1-count2,
indexr = MAX_DECIMAL_DIGITS-count2, flag = 0;
index1 < count1; index1++, indexr++)
{
div = num1[index1] + 10*flag;
rem[indexr] = div / scale;
flag = div % scale;
} /* end for(index1) */
for (index2 = 0, flag = 0; index2 < count2; index2++)
{
div = num2[index2] + 10*flag;
num2[index2] = div / scale;
flag = div % scale;
} /* end for(index2) */
} /* end function divide_decimal */
#endif /*!defined(_DECIMAL_C)*/
/*-------------------------------------------------------------------*/
/* Load a packed decimal storage operand into a decimal byte string */
/* */
/* Input: */
/* addr Logical address of packed decimal storage operand */
/* len Length minus one of storage operand (range 0-15) */
/* arn Access register number associated with operand */
/* regs CPU register context */
/* Output: */
/* result Points to a 31-byte area into which the decimal */
/* digits are loaded. One decimal digit is loaded */
/* into the low-order 4 bits of each byte, and the */
/* result is padded to the left with high-order zeroes */
/* if the storage operand contains less than 31 digits. */
/* count Points to an integer to receive the number of */
/* digits in the result excluding leading zeroes. */
/* This field is set to zero if the result is all zero. */
/* sign Points to an integer which will be set to -1 if a */
/* negative sign was loaded from the operand, or +1 if */
/* a positive sign was loaded from the operand. */
/* */
/* A program check may be generated if the logical address */
/* causes an addressing, translation, or fetch protection */
/* exception, or if the operand causes a data exception */
/* because of invalid decimal digits or sign. */
/*-------------------------------------------------------------------*/
static void ARCH_DEP(load_decimal) (VADR addr, int len, int arn, REGS *regs,
BYTE *result, int *count, int *sign)
{
int h; /* Hexadecimal digit */
int i, j; /* Array subscripts */
int n; /* Significant digit counter */
BYTE pack[MAX_DECIMAL_LENGTH]; /* Packed decimal work area */
/* Fetch the packed decimal operand into work area */
memset (pack, 0, sizeof(pack));
ARCH_DEP(vfetchc) (pack+sizeof(pack)-len-1, len, addr, arn, regs);
/* Unpack digits into result */
for (i=0, j=0, n=0; i < MAX_DECIMAL_DIGITS; i++)
{
/* Load source digit */
if (i & 1)
h = pack[j++] & 0x0F;
else
h = pack[j] >> 4;
/* Check for valid numeric */
if (h > 9)
{
regs->dxc = DXC_DECIMAL;
ARCH_DEP(program_interrupt) (regs, PGM_DATA_EXCEPTION);
return;
}
/* Count significant digits */
if (n > 0 || h != 0)
n++;
/* Store decimal digit in result */
result[i] = h;
} /* end for */
/* Check for valid sign */
h = pack[MAX_DECIMAL_LENGTH-1] & 0x0F;
if (h < 0x0A)
{
regs->dxc = DXC_DECIMAL;
ARCH_DEP(program_interrupt) (regs, PGM_DATA_EXCEPTION);
return;
}
/* Set number of significant digits */
*count = n;
/* Set sign of operand */
*sign = (h == 0x0B || h == 0x0D) ? -1 : 1;
} /* end function ARCH_DEP(load_decimal) */
/*-------------------------------------------------------------------*/
/* Store decimal byte string into packed decimal storage operand */
/* */
/* Input: */
/* addr Logical address of packed decimal storage operand */
/* len Length minus one of storage operand (range 0-15) */
/* arn Access register number associated with operand */
/* regs CPU register context */
/* dec A 31-byte area containing the decimal digits to be */
/* stored. Each byte contains one decimal digit in */
/* the low-order 4 bits of the byte. */
/* sign -1 if a negative sign is to be stored, or +1 if a */
/* positive sign is to be stored. */
/* */
/* A program check may be generated if the logical address */
/* causes an addressing, translation, or protection exception. */
/*-------------------------------------------------------------------*/
static void ARCH_DEP(store_decimal) (VADR addr, int len, int arn, REGS *regs,
BYTE *dec, int sign)
{
int i, j; /* Array subscripts */
BYTE pack[MAX_DECIMAL_LENGTH]; /* Packed decimal work area */
/* if operand crosses page, make sure both pages are accessable */
if((addr & PAGEFRAME_PAGEMASK) !=
((addr + len) & PAGEFRAME_PAGEMASK))
ARCH_DEP(validate_operand) (addr, arn, len, ACCTYPE_WRITE_SKP, regs);
/* Pack digits into packed decimal work area */
for (i=0, j=0; i < MAX_DECIMAL_DIGITS; i++)
{
if (i & 1)
pack[j++] |= dec[i];
else
pack[j] = dec[i] << 4;
} /* end for */
/* Pack the sign into low-order digit of work area */
pack[MAX_DECIMAL_LENGTH-1] |= (sign < 0 ? 0x0D : 0x0C);
/* Store the result at the operand location */
ARCH_DEP(vstorec) (pack+sizeof(pack)-len-1, len, addr, arn, regs);
} /* end function ARCH_DEP(store_decimal) */
/*-------------------------------------------------------------------*/
/* FA AP - Add Decimal [SS] */
/*-------------------------------------------------------------------*/
DEF_INST(add_decimal)
{
int l1, l2; /* Length values */
int b1, b2; /* Base register numbers */
VADR effective_addr1,
effective_addr2; /* Effective addresses */
int cc; /* Condition code */
BYTE dec1[MAX_DECIMAL_DIGITS]; /* Work area for operand 1 */
BYTE dec2[MAX_DECIMAL_DIGITS]; /* Work area for operand 2 */
BYTE dec3[MAX_DECIMAL_DIGITS]; /* Work area for result */
int count1, count2, count3; /* Significant digit counters*/
int sign1, sign2, sign3; /* Sign of operands & result */
SS(inst, execflag, regs, l1, l2, b1, effective_addr1,
b2, effective_addr2);
/* Load operands into work areas */
ARCH_DEP(load_decimal) (effective_addr1, l1, b1, regs, dec1, &count1, &sign1);
ARCH_DEP(load_decimal) (effective_addr2, l2, b2, regs, dec2, &count2, &sign2);
/* Add or subtract operand values */
if (count2 == 0)
{
/* If second operand is zero then result is first operand */
memcpy (dec3, dec1, MAX_DECIMAL_DIGITS);
count3 = count1;
sign3 = sign1;
}
else if (count1 == 0)
{
/* If first operand is zero then result is second operand */
memcpy (dec3, dec2, MAX_DECIMAL_DIGITS);
count3 = count2;
sign3 = sign2;
}
else if (sign1 == sign2)
{
/* If signs are equal then add operands */
add_decimal (dec1, dec2, dec3, &count3);
sign3 = sign1;
}
else
{
/* If signs are opposite then subtract operands */
subtract_decimal (dec1, dec2, dec3, &count3, &sign3);
if (sign1 < 0) sign3 = -sign3;
}
/* Set condition code */
cc = (count3 == 0) ? 0 : (sign3 < 1) ? 1 : 2;
/* Overflow if result exceeds first operand length */
if (count3 > (l1+1) * 2 - 1)
cc = 3;
/* Set positive sign if result is zero */
if (count3 == 0)
sign3 = 1;
/* Store result into first operand location */
ARCH_DEP(store_decimal) (effective_addr1, l1, b1, regs, dec3, sign3);
/* Set condition code */
regs->psw.cc = cc;
/* Program check if overflow and PSW program mask is set */
if (cc == 3 && regs->psw.domask)
ARCH_DEP(program_interrupt) (regs, PGM_DECIMAL_OVERFLOW_EXCEPTION);
} /* end DEF_INST(add_decimal) */
/*-------------------------------------------------------------------*/
/* F9 CP - Compare Decimal [SS] */
/*-------------------------------------------------------------------*/
DEF_INST(compare_decimal)
{
int l1, l2; /* Length values */
int b1, b2; /* Base register numbers */
VADR effective_addr1,
effective_addr2; /* Effective addresses */
BYTE dec1[MAX_DECIMAL_DIGITS]; /* Work area for operand 1 */
BYTE dec2[MAX_DECIMAL_DIGITS]; /* Work area for operand 2 */
int count1, count2; /* Significant digit counters*/
int sign1, sign2; /* Sign of each operand */
int rc; /* Return code */
SS(inst, execflag, regs, l1, l2, b1, effective_addr1,
b2, effective_addr2);
/* Load operands into work areas */
ARCH_DEP(load_decimal) (effective_addr1, l1, b1, regs, dec1, &count1, &sign1);
ARCH_DEP(load_decimal) (effective_addr2, l2, b2, regs, dec2, &count2, &sign2);
/* Result is equal if both operands are zero */
if (count1 == 0 && count2 == 0)
{
regs->psw.cc = 0;
return;
}
/* Result is low if operand 1 is -ve and operand 2 is +ve */
if (sign1 < 0 && sign2 > 0)
{
regs->psw.cc = 1;
return;
}
/* Result is high if operand 1 is +ve and operand 2 is -ve */
if (sign1 > 0 && sign2 < 0)
{
regs->psw.cc = 2;
return;
}
/* If signs are equal then compare the digits */
rc = memcmp (dec1, dec2, MAX_DECIMAL_DIGITS);
/* Return low or high (depending on sign) if digits are unequal */
if (rc < 0)
regs->psw.cc = (sign1 > 0) ? 1 : 2;
else
if (rc > 0)
regs->psw.cc = (sign1 > 0) ? 2 : 1;
else
regs->psw.cc = 0;
} /* end DEF_INST(compare_decimal) */
/*-------------------------------------------------------------------*/
/* FD DP - Divide Decimal [SS] */
/*-------------------------------------------------------------------*/
DEF_INST(divide_decimal)
{
int l1, l2; /* Length values */
int b1, b2; /* Base register numbers */
VADR effective_addr1,
effective_addr2; /* Effective addresses */
BYTE dec1[MAX_DECIMAL_DIGITS]; /* Operand 1 (dividend) */
BYTE dec2[MAX_DECIMAL_DIGITS]; /* Operand 2 (divisor) */
BYTE quot[MAX_DECIMAL_DIGITS]; /* Quotient */
BYTE rem[MAX_DECIMAL_DIGITS]; /* Remainder */
int count1, count2; /* Significant digit counters*/
int sign1, sign2; /* Sign of operands */
int signq, signr; /* Sign of quotient/remainder*/
SS(inst, execflag, regs, l1, l2, b1, effective_addr1,
b2, effective_addr2);
/* Program check if the second operand length exceeds 15 digits
or is equal to or greater than the first operand length */
if (l2 > 7 || l2 >= l1)
ARCH_DEP(program_interrupt) (regs, PGM_SPECIFICATION_EXCEPTION);
/* Load operands into work areas */
ARCH_DEP(load_decimal) (effective_addr1, l1, b1, regs, dec1, &count1, &sign1);
ARCH_DEP(load_decimal) (effective_addr2, l2, b2, regs, dec2, &count2, &sign2);
/* Program check if second operand value is zero */
if (count2 == 0)
ARCH_DEP(program_interrupt) (regs, PGM_DECIMAL_DIVIDE_EXCEPTION);
/* Perform trial comparison to determine potential overflow.
The leftmost digit of the divisor is aligned one digit to
the right of the leftmost dividend digit. When the divisor,
so aligned, is less than or equal to the dividend, ignoring
signs, a divide exception is indicated. As a result of this
comparison, it is also certain that the leftmost digit of the
dividend must be zero, and that the divisor cannot be zero */
if (memcmp(dec2 + (MAX_DECIMAL_DIGITS - l2*2 - 2),
dec1 + (MAX_DECIMAL_DIGITS - l1*2 - 1),
l2*2 + 2) <= 0)
ARCH_DEP(program_interrupt) (regs, PGM_DECIMAL_DIVIDE_EXCEPTION);
/* Perform decimal division */
divide_decimal (dec1, count1, dec2, count2, quot, rem);
/* Quotient is positive if operand signs are equal, and negative
if operand signs are opposite, even if quotient is zero */
signq = (sign1 == sign2) ? 1 : -1;
/* Remainder sign is same as dividend, even if remainder is zero */
signr = sign1;
/* Store remainder into entire first operand location. The entire
field will be filled in order to check for store protection.
Subsequently the quotient will be stored in the leftmost bytes
of the first operand location, overwriting high order zeroes */
ARCH_DEP(store_decimal) (effective_addr1, l1, b1, regs, rem, signr);
/* Store quotient in leftmost bytes of first operand location */
ARCH_DEP(store_decimal) (effective_addr1, l1-l2-1, b1, regs, quot, signq);
} /* end DEF_INST(divide_decimal) */
/*-------------------------------------------------------------------*/
/* DE ED - Edit [SS] */
/* DF EDMK - Edit and Mark [SS] */
/*-------------------------------------------------------------------*/
DEF_INST(edit_x_edit_and_mark)
{
int l; /* Length value */
int b1, b2; /* Base register numbers */
VADR effective_addr1,
effective_addr2, /* Effective addresses */
addr1,
addr2;
int cc = 0; /* Condition code */
int sig = 0; /* Significance indicator */
int trial_run; /* 1=trial run */
int i; /* Loop counter */
int d; /* 1=Use right source digit */
int h; /* Hexadecimal digit */
BYTE sbyte; /* Source operand byte */
BYTE fbyte; /* Fill byte */
BYTE pbyte; /* Pattern byte */
BYTE rbyte; /* Result byte */
SS_L(inst, execflag, regs, l, b1, effective_addr1,
b2, effective_addr2);
/* If addr1 crosses page, make sure both pages are accessable */
if((effective_addr1 & PAGEFRAME_PAGEMASK) !=
((effective_addr1 + l) & PAGEFRAME_PAGEMASK))
ARCH_DEP(validate_operand) (effective_addr1, b1, l, ACCTYPE_WRITE_SKP, regs);
/* If addr2 might cross page, do a trial run to catch possible access rupts */
if((effective_addr2 & PAGEFRAME_PAGEMASK) !=
((effective_addr2 + l) & PAGEFRAME_PAGEMASK))
trial_run = 1;
else
trial_run = 0;
for(;trial_run >= 0; trial_run--)
{
/* Initialize variables */
addr1 = effective_addr1;
addr2 = effective_addr2;
cc = 0;
sig = 0;
sbyte = 0;
fbyte = 0;
/* Process first operand from left to right */
for (i = 0, d = 0; i < l+1; i++)
{
/* Fetch pattern byte from first operand */
pbyte = ARCH_DEP(vfetchb) ( addr1, b1, regs );
/* The first pattern byte is also the fill byte */
if (i == 0) fbyte = pbyte;
/* If pattern byte is digit selector (X'20') or
significance starter (X'21') then fetch next
hexadecimal digit from the second operand */
if (pbyte == 0x20 || pbyte == 0x21)
{
if (d == 0)
{
/* Fetch source byte and extract left digit */
sbyte = ARCH_DEP(vfetchb) ( addr2, b2, regs );
h = sbyte >> 4;
sbyte &= 0x0F;
d = 1;
/* Increment second operand address */
addr2++;
addr2 &= ADDRESS_MAXWRAP(regs);
/* Program check if left digit is not numeric */
if (h > 9)
{
regs->dxc = DXC_DECIMAL;
ARCH_DEP(program_interrupt) (regs, PGM_DATA_EXCEPTION);
}
}
else
{
/* Use right digit of source byte */
h = sbyte;
d = 0;
}
/* For the EDMK instruction only, insert address of
result byte into general register 1 if the digit
is non-zero and significance indicator was off */
if (!trial_run && (inst[0] == 0xDF) && h > 0 && sig == 0)
{
#if defined(FEATURE_ESAME)
if (regs->psw.amode64)
regs->GR_G(1) = addr1;
else
#endif
if ( regs->psw.amode )
regs->GR_L(1) = addr1;
else
regs->GR_LA24(1) = addr1;
}
/* Replace the pattern byte by the fill character
or by a zoned decimal digit */
rbyte = (sig == 0 && h == 0) ? fbyte : (0xF0 | h);
if(!trial_run)
ARCH_DEP(vstoreb) ( rbyte, addr1, b1, regs );
else
ARCH_DEP(validate_operand) (addr1, b1, 0, ACCTYPE_WRITE_SKP, regs);
/* Set condition code 2 if digit is non-zero */
if (h > 0) cc = 2;
/* Turn on significance indicator if pattern
byte is significance starter or if source
digit is non-zero */
if (pbyte == 0x21 || h > 0)
sig = 1;
/* Examine right digit for sign code */
if (d == 1 && sbyte > 9)
{
/* Turn off the significance indicator if
the right digit is a plus sign code */
if (sbyte != 0x0B && sbyte != 0x0D)
sig = 0;
/* Take next digit from next source byte */
d = 0;
}
}
/* If pattern byte is field separator (X'22') then
replace it by the fill character, turn off the
significance indicator, and zeroize conditon code */
else if (pbyte == 0x22)
{
if(!trial_run)
ARCH_DEP(vstoreb) ( fbyte, addr1, b1, regs );
else
ARCH_DEP(validate_operand) (addr1, b1, 0, ACCTYPE_WRITE_SKP, regs);
sig = 0;
cc = 0;
}
/* If pattern byte is a message byte (anything other
than X'20', X'21', or X'22') then replace it by
the fill byte if the significance indicator is off */
else
{
if (sig == 0)
{
if (!trial_run)
ARCH_DEP(vstoreb) ( fbyte, addr1, b1, regs );
else
ARCH_DEP(validate_operand) (addr1, b1, 0, ACCTYPE_WRITE_SKP, regs);
}
else /* store message byte */
{
if (!trial_run)
ARCH_DEP(vstoreb) ( pbyte, addr1, b1, regs );
else
ARCH_DEP(validate_operand) (addr1, b1, 0, ACCTYPE_WRITE_SKP, regs);
}
}
/* Increment first operand address */
addr1++;
addr1 &= ADDRESS_MAXWRAP(regs);
} /* end for(i) */
} /* end for(trial_run) */
/* Replace condition code 2 by condition code 1 if the
significance indicator is on at the end of editing */
if (sig && cc == 2) cc = 1;
/* Set condition code */
regs->psw.cc = cc;
} /* end DEF_INST(edit_x_edit_and_mark) */
/*-------------------------------------------------------------------*/
/* FC MP - Multiply Decimal [SS] */
/*-------------------------------------------------------------------*/
DEF_INST(multiply_decimal)
{
int l1, l2; /* Length values */
int b1, b2; /* Base register numbers */
VADR effective_addr1,
effective_addr2; /* Effective addresses */
BYTE dec1[MAX_DECIMAL_DIGITS]; /* Work area for operand 1 */
BYTE dec2[MAX_DECIMAL_DIGITS]; /* Work area for operand 2 */
BYTE dec3[MAX_DECIMAL_DIGITS]; /* Work area for result */
int count1, count2; /* Significant digit counters*/
int sign1, sign2, sign3; /* Sign of operands & result */
int d; /* Decimal digit */
int i1, i2, i3; /* Array subscripts */
int carry; /* Carry indicator */
SS(inst, execflag, regs, l1, l2, b1, effective_addr1,
b2, effective_addr2);
/* Program check if the second operand length exceeds 15 digits
or is equal to or greater than the first operand length */
if (l2 > 7 || l2 >= l1)
ARCH_DEP(program_interrupt) (regs, PGM_SPECIFICATION_EXCEPTION);
/* Load operands into work areas */
ARCH_DEP(load_decimal) (effective_addr1, l1, b1, regs, dec1, &count1, &sign1);
ARCH_DEP(load_decimal) (effective_addr2, l2, b2, regs, dec2, &count2, &sign2);
/* Program check if the number of bytes in the second operand
is less than the number of bytes of high-order zeroes in the
first operand; this ensures that overflow cannot occur */
if (l2 > l1 - (count1/2 + 1))
{
regs->dxc = DXC_DECIMAL;
ARCH_DEP(program_interrupt) (regs, PGM_DATA_EXCEPTION);
}
/* Clear the result field */
memset (dec3, 0, MAX_DECIMAL_DIGITS);
/* Perform decimal multiplication */
for (i2 = MAX_DECIMAL_DIGITS-1; i2 >= 0; i2--)
{
if (dec2[i2] != 0)
{
for (i1 = MAX_DECIMAL_DIGITS - 1, i3 = i2, carry = 0;
i3 >= 0; i1--, i3--)
{
d = carry + dec1[i1]*dec2[i2] + dec3[i3];
dec3[i3] = d % 10;
carry = d / 10;
}
}
} /* end for(i2) */
/* Result is positive if operand signs are equal, and negative
if operand signs are opposite, even if result is zero */
sign3 = (sign1 == sign2) ? 1 : -1;
/* Store result into first operand location */
ARCH_DEP(store_decimal) (effective_addr1, l1, b1, regs, dec3, sign3);
} /* end DEF_INST(multiply_decimal) */
/*-------------------------------------------------------------------*/
/* F0 SRP - Shift and Round Decimal [SS] */
/*-------------------------------------------------------------------*/
DEF_INST(shift_and_round_decimal)
{
int l1, i3; /* Length and rounding */
int b1, b2; /* Base register numbers */
VADR effective_addr1,
effective_addr2; /* Effective addresses */
int cc; /* Condition code */
BYTE dec[MAX_DECIMAL_DIGITS]; /* Work area for operand */
int count; /* Significant digit counter */
int sign; /* Sign of operand/result */
int i, j; /* Array subscripts */
int d; /* Decimal digit */
int carry; /* Carry indicator */
SS(inst, execflag, regs, l1, i3, b1, effective_addr1,
b2, effective_addr2);
/* Load operand into work area */
ARCH_DEP(load_decimal) (effective_addr1, l1, b1, regs, dec, &count, &sign);
/* Program check if rounding digit is invalid */
if (i3 > 9)
{
regs->dxc = DXC_DECIMAL;
ARCH_DEP(program_interrupt) (regs, PGM_DATA_EXCEPTION);
}
/* Isolate low-order six bits of shift count */
effective_addr2 &= 0x3F;
/* Shift count 0-31 means shift left, 32-63 means shift right */
if (effective_addr2 < 32)
{
/* Set condition code according to operand sign */
cc = (count == 0) ? 0 : (sign < 0) ? 1 : 2;
/* Set cc=3 if non-zero digits will be lost on left shift */
if (count > 0 && effective_addr2 > (VADR)((l1+1)*2 - 1 - count))
cc = 3;
/* Shift operand left */
for (i=0, j=effective_addr2; i < MAX_DECIMAL_DIGITS; i++, j++)
dec[i] = (j < MAX_DECIMAL_DIGITS) ? dec[j] : 0;
}
else
{
/* Calculate number of digits (1-32) to shift right */
effective_addr2 = 64 - effective_addr2;
/* Add the rounding digit to the leftmost of the digits
to be shifted out and propagate the carry to the left */
carry = (effective_addr2 > MAX_DECIMAL_DIGITS) ? 0 :
(dec[MAX_DECIMAL_DIGITS - effective_addr2] + i3) / 10;
count = 0;
/* Shift operand right */
for (i=MAX_DECIMAL_DIGITS-1, j=MAX_DECIMAL_DIGITS-1-effective_addr2;
i >= 0; i--, j--)
{
d = (j >= 0) ? dec[j] : 0;
d += carry;
carry = d / 10;
d %= 10;
dec[i] = d;
if (d != 0)
count = MAX_DECIMAL_DIGITS - i;
}
/* Set condition code according to operand sign */
cc = (count == 0) ? 0 : (sign < 0) ? 1 : 2;
}
/* Make sign positive if result is zero */
if (cc == 0)
sign = +1;
/* Store result into operand location */
ARCH_DEP(store_decimal) (effective_addr1, l1, b1, regs, dec, sign);
/* Set condition code */
regs->psw.cc = cc;
/* Program check if overflow and PSW program mask is set */
if (cc == 3 && regs->psw.domask)
ARCH_DEP(program_interrupt) (regs, PGM_DECIMAL_OVERFLOW_EXCEPTION);
} /* end DEF_INST(shift_and_round_decimal) */
/*-------------------------------------------------------------------*/
/* FB SP - Subtract Decimal [SS] */
/*-------------------------------------------------------------------*/
DEF_INST(subtract_decimal)
{
int l1, l2; /* Length values */
int b1, b2; /* Base register numbers */
VADR effective_addr1,
effective_addr2; /* Effective addresses */
int cc; /* Condition code */
BYTE dec1[MAX_DECIMAL_DIGITS]; /* Work area for operand 1 */
BYTE dec2[MAX_DECIMAL_DIGITS]; /* Work area for operand 2 */
BYTE dec3[MAX_DECIMAL_DIGITS]; /* Work area for result */
int count1, count2, count3; /* Significant digit counters*/
int sign1, sign2, sign3; /* Sign of operands & result */
SS(inst, execflag, regs, l1, l2, b1, effective_addr1,
b2, effective_addr2);
/* Load operands into work areas */
ARCH_DEP(load_decimal) (effective_addr1, l1, b1, regs, dec1, &count1, &sign1);
ARCH_DEP(load_decimal) (effective_addr2, l2, b2, regs, dec2, &count2, &sign2);
/* Add or subtract operand values */
if (count2 == 0)
{
/* If second operand is zero then result is first operand */
memcpy (dec3, dec1, MAX_DECIMAL_DIGITS);
count3 = count1;
sign3 = sign1;
}
else if (count1 == 0)
{
/* If first operand is zero then result is -second operand */
memcpy (dec3, dec2, MAX_DECIMAL_DIGITS);
count3 = count2;
sign3 = -sign2;
}
else if (sign1 != sign2)
{
/* If signs are opposite then add operands */
add_decimal (dec1, dec2, dec3, &count3);
sign3 = sign1;
}
else
{
/* If signs are equal then subtract operands */
subtract_decimal (dec1, dec2, dec3, &count3, &sign3);
if (sign1 < 0) sign3 = -sign3;
}
/* Set condition code */
cc = (count3 == 0) ? 0 : (sign3 < 1) ? 1 : 2;
/* Overflow if result exceeds first operand length */
if (count3 > (l1+1) * 2 - 1)
cc = 3;
/* Set positive sign if result is zero */
if (count3 == 0)
sign3 = 1;
/* Store result into first operand location */
ARCH_DEP(store_decimal) (effective_addr1, l1, b1, regs, dec3, sign3);
/* Return condition code */
regs->psw.cc = cc;
/* Program check if overflow and PSW program mask is set */
if (cc == 3 && regs->psw.domask)
ARCH_DEP(program_interrupt) (regs, PGM_DECIMAL_OVERFLOW_EXCEPTION);
} /* end DEF_INST(subtract_decimal) */
/*-------------------------------------------------------------------*/
/* F8 ZAP - Zero and Add [SS] */
/*-------------------------------------------------------------------*/
DEF_INST(zero_and_add)
{
int l1, l2; /* Length values */
int b1, b2; /* Base register numbers */
VADR effective_addr1,
effective_addr2; /* Effective addresses */
int cc; /* Condition code */
BYTE dec[MAX_DECIMAL_DIGITS]; /* Work area for operand */
int count; /* Significant digit counter */
int sign; /* Sign */
SS(inst, execflag, regs, l1, l2, b1, effective_addr1,
b2, effective_addr2);
/* Load second operand into work area */
ARCH_DEP(load_decimal) (effective_addr2, l2, b2, regs, dec, &count, &sign);
/* Set condition code */
cc = (count == 0) ? 0 : (sign < 1) ? 1 : 2;
/* Overflow if result exceeds first operand length */
if (count > (l1+1) * 2 - 1)
cc = 3;
/* Set positive sign if result is zero */
if (count == 0)
sign = +1;
/* Store result into first operand location */
ARCH_DEP(store_decimal) (effective_addr1, l1, b1, regs, dec, sign);
/* Return condition code */
regs->psw.cc = cc;
/* Program check if overflow and PSW program mask is set */
if (cc == 3 && regs->psw.domask)
ARCH_DEP(program_interrupt) (regs, PGM_DECIMAL_OVERFLOW_EXCEPTION);
} /* end DEF_INST(zero_and_add) */
#if defined(FEATURE_EXTENDED_TRANSLATION_FACILITY_2)
/*-------------------------------------------------------------------*/
/* EBC0 TP - Test Decimal [RSL] */
/*-------------------------------------------------------------------*/
DEF_INST(test_decimal)
{
int l1; /* Length value */
int b1; /* Base register number */
VADR effective_addr1; /* Effective address */
int i; /* Array subscript */
int cc = 0; /* Condition code */
BYTE pack[MAX_DECIMAL_LENGTH]; /* Packed decimal work area */
RSL(inst, execflag, regs, l1, b1, effective_addr1);
/* Fetch the packed decimal operand into the work area */
ARCH_DEP(vfetchc) (pack, l1, effective_addr1, b1, regs);
/* Test each byte of the operand */
for (i=0; ; i++)
{
/* Test the high-order digit of the byte */
if ((pack[i] & 0xF0) > 0x90)
cc = 2;
/* Exit if this is the last byte */
if (i == l1) break;
/* Test the low-order digit of the byte */
if ((pack[i] & 0x0F) > 0x09)
cc = 2;
}
/* Test the sign in the last byte */
if ((pack[i] & 0x0F) < 0x0A)
cc |= 1;
/* Return condition code */
regs->psw.cc = cc;
} /* end DEF_INST(test_decimal) */
#endif /*defined(FEATURE_EXTENDED_TRANSLATION_FACILITY_2)*/
#if !defined(_GEN_ARCH)
#if defined(_ARCHMODE2)
#define _GEN_ARCH _ARCHMODE2
#include "decimal.c"
#endif
#if defined(_ARCHMODE3)
#undef _GEN_ARCH
#define _GEN_ARCH _ARCHMODE3
#include "decimal.c"
#endif
#endif /*!defined(_GEN_ARCH)*/
|