LargeInteger.st
changeset 1133 961f2b095c22
parent 1065 77b25fb9f9d7
child 1210 b2fbf119dbbd
equal deleted inserted replaced
1132:b09ce8542506 1133:961f2b095c22
   734      val   "{ Class: SmallInteger }" |
   734      val   "{ Class: SmallInteger }" |
   735 
   735 
   736 %{  /* NOCONTEXT */
   736 %{  /* NOCONTEXT */
   737     OBJ t;
   737     OBJ t;
   738 
   738 
   739     if (_INST(sign) == _MKSMALLINT(0)) {
   739     if (__INST(sign) == __MKSMALLINT(0)) {
   740 	RETURN (_MKSMALLINT(0));
   740 	RETURN (__MKSMALLINT(0));
   741     }
   741     }
   742 
   742 
   743     t = _INST(digitByteArray);
   743     t = __INST(digitByteArray);
   744     if (__isByteArray(t)) {
   744     if (__isByteArray(t)) {
   745 	unsigned char *_digitBytes = _ByteArrayInstPtr(t)->ba_element;
   745 	unsigned char *_digitBytes = __ByteArrayInstPtr(t)->ba_element;
   746 	int _idx = _byteArraySize(t);
   746 	int _idx = _byteArraySize(t);
   747 	int _val;
   747 	int _val;
   748 
   748 
   749 	while ((_idx > 0) && (_digitBytes[_idx - 1] == 0)) {
   749 	while ((_idx > 0) && (_digitBytes[_idx - 1] == 0)) {
   750 	    _idx--;
   750 	    _idx--;
   751 	}
   751 	}
   752 	switch (_idx) {
   752 	switch (_idx) {
   753 	    case 0:
   753 	    case 0:
   754 		RETURN (_MKSMALLINT(0));
   754 		RETURN (__MKSMALLINT(0));
   755 		break;
   755 		break;
   756 
   756 
   757 	    case 1:
   757 	    case 1:
   758 		_val = _digitBytes[0];
   758 		_val = _digitBytes[0];
   759 		if (_INST(sign) == _MKSMALLINT(-1))
   759 		if (__INST(sign) == __MKSMALLINT(-1))
   760 		    _val = -_val;
   760 		    _val = -_val;
   761 		RETURN (_MKSMALLINT(_val));
   761 		RETURN (__MKSMALLINT(_val));
   762                 
   762                 
   763 	    case 2:
   763 	    case 2:
   764 		_val = (_digitBytes[1]<<8) + _digitBytes[0];
   764 		_val = (_digitBytes[1]<<8) + _digitBytes[0];
   765 		if (_INST(sign) == _MKSMALLINT(-1))
   765 		if (__INST(sign) == __MKSMALLINT(-1))
   766 		    _val = -_val;
   766 		    _val = -_val;
   767 		RETURN (_MKSMALLINT(_val));
   767 		RETURN (__MKSMALLINT(_val));
   768 
   768 
   769 	    case 3:
   769 	    case 3:
   770 		_val = (((_digitBytes[2]<<8) + _digitBytes[1])<<8) + _digitBytes[0];
   770 		_val = (((_digitBytes[2]<<8) + _digitBytes[1])<<8) + _digitBytes[0];
   771 		if (_INST(sign) == _MKSMALLINT(-1))
   771 		if (__INST(sign) == __MKSMALLINT(-1))
   772 		    _val = -_val;
   772 		    _val = -_val;
   773 		RETURN (_MKSMALLINT(_val));
   773 		RETURN (__MKSMALLINT(_val));
   774 
   774 
   775 	    case 4:
   775 	    case 4:
   776 		_val = _digitBytes[3];
   776 		_val = _digitBytes[3];
   777 		if (_val <= 0x40) {
   777 		if (_val <= 0x40) {
   778 		    _val = (((((_val<<8) + _digitBytes[2])<<8) + _digitBytes[1])<<8) + _digitBytes[0];
   778 		    _val = (((((_val<<8) + _digitBytes[2])<<8) + _digitBytes[1])<<8) + _digitBytes[0];
   779 		    if (_INST(sign) == _MKSMALLINT(-1))
   779 		    if (__INST(sign) == __MKSMALLINT(-1))
   780 			_val = -_val;
   780 			_val = -_val;
   781 		    if ((_val >= _MIN_INT) && (_val <= _MAX_INT)) {
   781 		    if ((_val >= _MIN_INT) && (_val <= _MAX_INT)) {
   782 			RETURN (_MKSMALLINT(_val));
   782 			RETURN (__MKSMALLINT(_val));
   783 		    }
   783 		    }
   784 		}
   784 		}
   785 		break;
   785 		break;
   786 
   786 
   787 	    default:
   787 	    default:
   998     val := num.
   998     val := num.
   999 
   999 
  1000     ok := false.
  1000     ok := false.
  1001 %{
  1001 %{
  1002     if (__bothSmallInteger(len, val)
  1002     if (__bothSmallInteger(len, val)
  1003      && __isByteArray(_INST(digitByteArray))
  1003      && __isByteArray(__INST(digitByteArray))
  1004      && __isByteArray(resultDigitByteArray)) {
  1004      && __isByteArray(resultDigitByteArray)) {
  1005 	int _l = _intVal(len);
  1005 	int _l = __intVal(len);
  1006 	int _v = _intVal(val);
  1006 	int _v = __intVal(val);
  1007 	unsigned _carry = 0;
  1007 	unsigned _carry = 0;
  1008 	unsigned _prod;
  1008 	unsigned _prod;
  1009 	unsigned char *digitP = _ByteArrayInstPtr(_INST(digitByteArray))->ba_element;
  1009 	unsigned char *digitP = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element;
  1010 	unsigned char *resultP = _ByteArrayInstPtr(resultDigitByteArray)->ba_element;
  1010 	unsigned char *resultP = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
  1011 
  1011 
  1012 	while (_l-- > 0) {
  1012 	while (_l-- > 0) {
  1013 	    _prod = *digitP++ * _v + _carry;
  1013 	    _prod = *digitP++ * _v + _carry;
  1014 	    *resultP++ = _prod & 0xFF;
  1014 	    *resultP++ = _prod & 0xFF;
  1015 	    _carry = _prod >> 8;
  1015 	    _carry = _prod >> 8;
  1187     count := digitByteArray size.
  1187     count := digitByteArray size.
  1188     result := self class basicNew numberOfDigits:count.
  1188     result := self class basicNew numberOfDigits:count.
  1189     newDigitByteArray := result digits.
  1189     newDigitByteArray := result digits.
  1190     ok := false.
  1190     ok := false.
  1191 %{
  1191 %{
  1192     if (__isByteArray(_INST(digitByteArray))
  1192     if (__isByteArray(__INST(digitByteArray))
  1193      && __isByteArray(newDigitByteArray)
  1193      && __isByteArray(newDigitByteArray)
  1194      && __bothSmallInteger(count, aSmallInteger)) {
  1194      && __bothSmallInteger(count, aSmallInteger)) {
  1195 	unsigned int rest = 0;
  1195 	unsigned int rest = 0;
  1196 	int index = _intVal(count);
  1196 	int index = __intVal(count);
  1197 	int divisor = _intVal(aSmallInteger);
  1197 	int divisor = __intVal(aSmallInteger);
  1198 	unsigned char *digitBytes = _ByteArrayInstPtr(_INST(digitByteArray))->ba_element;
  1198 	unsigned char *digitBytes = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element;
  1199 	unsigned char *resultBytes = _ByteArrayInstPtr(newDigitByteArray)->ba_element;
  1199 	unsigned char *resultBytes = __ByteArrayInstPtr(newDigitByteArray)->ba_element;
  1200 
  1200 
  1201 	while (index > 0) {
  1201 	while (index > 0) {
  1202 	    unsigned int t;
  1202 	    unsigned int t;
  1203 
  1203 
  1204 	    index--;
  1204 	    index--;
  1205 	    t = digitBytes[index];
  1205 	    t = digitBytes[index];
  1206 	    t = t | (rest << 8);
  1206 	    t = t | (rest << 8);
  1207 	    resultBytes[index] = t / divisor;
  1207 	    resultBytes[index] = t / divisor;
  1208 	    rest = t % divisor;
  1208 	    rest = t % divisor;
  1209 	}
  1209 	}
  1210 	prevRest = _MKSMALLINT(rest);
  1210 	prevRest = __MKSMALLINT(rest);
  1211 	ok = true;
  1211 	ok = true;
  1212     }
  1212     }
  1213 %}.
  1213 %}.
  1214     "
  1214     "
  1215      slow code - not normally reached
  1215      slow code - not normally reached
  1430 
  1430 
  1431     result := LargeInteger basicNew numberOfDigits:(len1 + len2 + 1).
  1431     result := LargeInteger basicNew numberOfDigits:(len1 + len2 + 1).
  1432     resultDigitByteArray := result digits.
  1432     resultDigitByteArray := result digits.
  1433     ok := false.
  1433     ok := false.
  1434 %{
  1434 %{
  1435     if (__isByteArray(_INST(digitByteArray))
  1435     if (__isByteArray(__INST(digitByteArray))
  1436      && __isByteArray(otherDigitByteArray)
  1436      && __isByteArray(otherDigitByteArray)
  1437      && __isByteArray(resultDigitByteArray)
  1437      && __isByteArray(resultDigitByteArray)
  1438      && __bothSmallInteger(len1, len2)) {
  1438      && __bothSmallInteger(len1, len2)) {
  1439 	unsigned char *myBytes = _ByteArrayInstPtr(_INST(digitByteArray))->ba_element;
  1439 	unsigned char *myBytes = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element;
  1440 	unsigned char *otherBytes = _ByteArrayInstPtr(otherDigitByteArray)->ba_element;
  1440 	unsigned char *otherBytes = __ByteArrayInstPtr(otherDigitByteArray)->ba_element;
  1441 	unsigned char *resultBytes = _ByteArrayInstPtr(resultDigitByteArray)->ba_element;
  1441 	unsigned char *resultBytes = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
  1442 	int _index1, _index2, _dstIndex, _idx;
  1442 	int _index1, _index2, _dstIndex, _idx;
  1443 	unsigned _prod, _carry, _v;
  1443 	unsigned _prod, _carry, _v;
  1444 
  1444 
  1445 	for (_index1 = 0; _index1 < _intVal(len1); _index1++) {
  1445 	for (_index1 = 0; _index1 < __intVal(len1); _index1++) {
  1446 	    for (_index2 = 0; _index2 < _intVal(len2); _index2++) {
  1446 	    for (_index2 = 0; _index2 < __intVal(len2); _index2++) {
  1447 		_dstIndex = _index1 + _index2;
  1447 		_dstIndex = _index1 + _index2;
  1448 		_prod = myBytes[_index1] * otherBytes[_index2];
  1448 		_prod = myBytes[_index1] * otherBytes[_index2];
  1449 		_prod += resultBytes[_dstIndex];
  1449 		_prod += resultBytes[_dstIndex];
  1450 		resultBytes[_dstIndex] = _prod & 0xFF;
  1450 		resultBytes[_dstIndex] = _prod & 0xFF;
  1451 		_carry = _prod >> 8;
  1451 		_carry = _prod >> 8;
  1607 ! !
  1607 ! !
  1608 
  1608 
  1609 !LargeInteger class methodsFor:'documentation'!
  1609 !LargeInteger class methodsFor:'documentation'!
  1610 
  1610 
  1611 version
  1611 version
  1612     ^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.23 1996-03-05 11:29:56 cg Exp $'
  1612     ^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.24 1996-04-02 22:02:08 cg Exp $'
  1613 ! !
  1613 ! !