--- a/LargeInteger.st Wed Nov 05 21:46:42 2014 +0100
+++ b/LargeInteger.st Wed Nov 05 23:35:20 2014 +0100
@@ -290,8 +290,6 @@
"Modified: / 8.5.1998 / 21:40:41 / cg"
! !
-
-
!LargeInteger class methodsFor:'queries'!
isBuiltInClass
@@ -473,7 +471,7 @@
The result is truncated toward negative infinity and negative,
if the operands signs differ.
The following is always true:
- (receiver // aNumber) * aNumber + (receiver \\ aNUmber) = receiver
+ (receiver // aNumber) * aNumber + (receiver \\ aNUmber) = receiver
"
|cls divMod quo abs "{ Class: SmallInteger }" n|
@@ -486,38 +484,38 @@
Use a special method for this case ...
"
(cls == SmallInteger) ifTrue:[
- abs := aNumber.
- abs := abs abs.
- (abs between:1 and:16r00ffffff) ifTrue:[
- divMod := self absFastDivMod:abs.
- ] ifFalse:[
- n := abs asLargeInteger.
- ].
+ abs := aNumber.
+ abs := abs abs.
+ (abs between:1 and:(SmallInteger maxBytes == 8 ifTrue:16r00ffffffffff ifFalse:16r00ffffff)) ifTrue:[
+ divMod := self absFastDivMod:abs.
+ ] ifFalse:[
+ n := abs asLargeInteger.
+ ].
] ifFalse:[
- "
- if the argument is not a largeInteger, coerce
- "
- (cls == self class) ifFalse:[
- ^ self retry:#// coercing:aNumber
- ].
- n := aNumber
+ "
+ if the argument is not a largeInteger, coerce
+ "
+ (cls == self class) ifFalse:[
+ ^ self retry:#// coercing:aNumber
+ ].
+ n := aNumber
].
divMod isNil ifTrue:[
- divMod := self absDivMod:n.
+ divMod := self absDivMod:n.
].
quo := divMod at:1.
(sign == aNumber sign) ifFalse:[
- "/ adjust for truncation if negative and there is a remainder ...
- "/ be careful: there is one special case to care for here:
- "/ if quo is maxInt+1, the negation can be represented as a smallInt.
- quo := quo sign:-1.
- (divMod at:2) == 0 ifFalse:[
- ^ quo - 1
- ].
- quo digitLength == SmallInteger maxBytes ifTrue:[
- ^ quo compressed
- ].
+ "/ adjust for truncation if negative and there is a remainder ...
+ "/ be careful: there is one special case to care for here:
+ "/ if quo is maxInt+1, the negation can be represented as a smallInt.
+ quo := quo sign:-1.
+ (divMod at:2) == 0 ifFalse:[
+ ^ quo - 1
+ ].
+ quo digitLength == SmallInteger maxBytes ifTrue:[
+ ^ quo compressed
+ ].
].
^ quo
@@ -553,17 +551,17 @@
negative infinity. The remainder has the same sign as aNumber.
m < |aNumber| AND there is an integer k with (k * aNumber + m) = self
The following is always true:
- (receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver
+ (receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver
Compare with #rem:"
|abs rem negativeDivisor|
aNumber negative ifTrue:[
- negativeDivisor := true.
- abs := aNumber negated.
+ negativeDivisor := true.
+ abs := aNumber negated.
] ifFalse:[
- negativeDivisor := false.
- abs := aNumber.
+ negativeDivisor := false.
+ abs := aNumber.
].
"
@@ -571,33 +569,33 @@
Use a special method for this case ...
"
(aNumber class == SmallInteger) ifTrue:[
- (abs between:1 and:16r00ffffff) ifTrue:[
- rem := (self absFastDivMod:abs) at:2.
- ] ifFalse:[
- rem := self absMod:abs asLargeInteger
- ].
+ (abs between:1 and:(SmallInteger maxBytes == 8 ifTrue:16r00ffffffffff ifFalse:16r00ffffff)) ifTrue:[
+ rem := (self absFastDivMod:abs) at:2.
+ ] ifFalse:[
+ rem := self absMod:abs asLargeInteger
+ ].
] ifFalse:[
- "
- if the argument is not a largeInteger, coerce
- "
- (aNumber class == self class) ifFalse:[
- ^ self retry:#\\ coercing:aNumber
- ].
-
- rem := self absMod:abs.
+ "
+ if the argument is not a largeInteger, coerce
+ "
+ (aNumber class == self class) ifFalse:[
+ ^ self retry:#\\ coercing:aNumber
+ ].
+
+ rem := self absMod:abs.
].
rem = 0 ifFalse:[
- negativeDivisor ifTrue:[
- rem := rem sign:-1
- ].
- (self negative ~~ negativeDivisor) ifTrue:[
- "different sign, so remainder would have been negative.
- rem has been rounded toward zero, this code will simulate
- rounding to negative infinity."
-
- rem := aNumber - rem.
- ].
+ negativeDivisor ifTrue:[
+ rem := rem sign:-1
+ ].
+ (self negative ~~ negativeDivisor) ifTrue:[
+ "different sign, so remainder would have been negative.
+ rem has been rounded toward zero, this code will simulate
+ rounding to negative infinity."
+
+ rem := aNumber - rem.
+ ].
].
^ rem
@@ -650,19 +648,19 @@
cls := aNumber class.
(cls == SmallInteger) ifTrue:[
- "
- this is the common case, dividing by a SmallInteger.
- Use a special method for this case ...
- "
- (aNumber between:1 and:16r00ffffff) ifTrue:[
- ^ self absFastDivMod:aNumber abs.
- ].
- n := aNumber asLargeInteger.
+ "
+ this is the common case, dividing by a SmallInteger.
+ Use a special method for this case ...
+ "
+ (aNumber between:1 and:(SmallInteger maxBytes == 8 ifTrue:16r00ffffffffff ifFalse:16r00ffffff)) ifTrue:[
+ ^ self absFastDivMod:aNumber abs.
+ ].
+ n := aNumber asLargeInteger.
] ifFalse:[
- (cls == self class) ifFalse:[
- ^ super divMod:aNumber
- ].
- n := aNumber.
+ (cls == self class) ifFalse:[
+ ^ super divMod:aNumber
+ ].
+ n := aNumber.
].
^ self absDivMod:n abs
@@ -688,43 +686,43 @@
special case for SmallInteger minVal
"
sign == 1 ifTrue:[
- sz := digitByteArray size.
+ sz := digitByteArray size.
%{
- int idx;
- unsigned char *bp;
-
- bp = (unsigned char *)(__ByteArrayInstPtr(__INST(digitByteArray))->ba_element);
- idx = __intVal(sz);
-
- while ((idx > 1) && (bp[idx-1] == 0)) idx--;
-
- if (idx == sizeof(INT)) {
+ int idx;
+ unsigned char *bp;
+
+ bp = (unsigned char *)(__ByteArrayInstPtr(__INST(digitByteArray))->ba_element);
+ idx = __intVal(sz);
+
+ while ((idx > 1) && (bp[idx-1] == 0)) idx--;
+
+ if (idx == sizeof(INT)) {
#if defined(__LSBFIRST__)
# if __POINTER_SIZE__ == 8
- if ( ((unsigned INT *)bp)[0] == 0x4000000000000000L)
+ if ( ((unsigned INT *)bp)[0] == 0x4000000000000000L)
# else
- if ( ((unsigned INT *)bp)[0] == 0x40000000)
+ if ( ((unsigned INT *)bp)[0] == 0x40000000)
# endif
#else
- /*
- * generic code
- */
- if ((bp[idx-1] == 0x40)
- && (bp[idx-2] == 0)
- && (bp[idx-3] == 0)
- && (bp[idx-4] == 0)
+ /*
+ * generic code
+ */
+ if ((bp[idx-1] == 0x40)
+ && (bp[idx-2] == 0)
+ && (bp[idx-3] == 0)
+ && (bp[idx-4] == 0)
# if __POINTER_SIZE__ == 8
- && (bp[idx-5] == 0)
- && (bp[idx-6] == 0)
- && (bp[idx-7] == 0)
- && (bp[idx-8] == 0)
+ && (bp[idx-5] == 0)
+ && (bp[idx-6] == 0)
+ && (bp[idx-7] == 0)
+ && (bp[idx-8] == 0)
# endif
- )
+ )
#endif
- {
- RETURN (__mkSmallInteger(_MIN_INT));
- }
- }
+ {
+ RETURN (__mkSmallInteger(_MIN_INT));
+ }
+ }
%}.
"/ sz == 4 ifTrue:[
"/ (digitByteArray at:1) == 0 ifTrue:[
@@ -1087,11 +1085,11 @@
anInteger class ~~ LargeInteger ifTrue:[^ super bitXor:anInteger].
(len1 := anInteger digitLength) > (len2 := self digitLength) ifTrue:[
- newBytes := anInteger digitBytes copy.
- newBytes bitXorBytesFrom:1 to:len2 with:digitByteArray startingAt:1
+ newBytes := anInteger digitBytes copy.
+ newBytes bitXorBytesFrom:1 to:len2 with:digitByteArray startingAt:1
] ifFalse:[
- newBytes := digitByteArray copy.
- newBytes bitXorBytesFrom:1 to:len1 with:anInteger digits startingAt:1
+ newBytes := digitByteArray copy.
+ newBytes bitXorBytesFrom:1 to:len1 with:anInteger digits startingAt:1
].
^ (self class digitBytes:newBytes) compressed
@@ -1106,9 +1104,9 @@
bigNum1 := 2 raisedToInteger:512.
bigNum2 := 2 raisedToInteger:510.
Time millisecondsToRun:[
- 1000000 timesRepeat:[
- bigNum1 bitXor:bigNum2.
- ]
+ 1000000 timesRepeat:[
+ bigNum1 bitXor:bigNum2.
+ ]
]
"
!
@@ -1448,17 +1446,17 @@
unsigned INT swapped;
swapped = ( (__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[0]) << 24)
- | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[1]) << 16)
- | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[2]) << 8)
- | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[3]));
+ | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[1]) << 16)
+ | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[2]) << 8)
+ | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[3]));
RETURN (__MKUINT(swapped));
%}.
"
- (LargeInteger value:16r11223344) byteSwapped hexPrintString
- (LargeInteger value:16r44332211) byteSwapped hexPrintString
- 16r88776655 byteSwapped hexPrintString
- 16r11223344 byteSwapped hexPrintString
+ (LargeInteger value:16r11223344) byteSwapped hexPrintString
+ (LargeInteger value:16r44332211) byteSwapped hexPrintString
+ 16r88776655 byteSwapped hexPrintString
+ 16r11223344 byteSwapped hexPrintString
"
"Created: / 31-01-2012 / 11:07:42 / cg"
@@ -1475,14 +1473,14 @@
unsigned INT swapped;
swappedHI = ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[0]) << 24)
- | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[1]) << 16)
- | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[2]) << 8)
- | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[3]));
+ | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[1]) << 16)
+ | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[2]) << 8)
+ | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[3]));
if (__byteArraySize(__INST(digitByteArray)) > 4) {
- swappedLO = ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[4]) << 24)
- | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[5]) << 16)
- | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[6]) << 8)
- | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[7]));
+ swappedLO = ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[4]) << 24)
+ | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[5]) << 16)
+ | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[6]) << 8)
+ | ((__ByteArrayInstPtr(__INST(digitByteArray))->ba_element[7]));
}
#if __POINTER_SIZE__ == 8
@@ -1494,12 +1492,12 @@
%}.
"
- (LargeInteger value:16r11223344) byteSwapped64 hexPrintString
- (LargeInteger value:16r44332211) byteSwapped64 hexPrintString
- (LargeInteger value:16r1122334455667788) byteSwapped64 hexPrintString
- (LargeInteger value:16r8877665544332211) byteSwapped64 hexPrintString
- 16r88776655 byteSwapped hexPrintString
- 16r11223344 byteSwapped hexPrintString
+ (LargeInteger value:16r11223344) byteSwapped64 hexPrintString
+ (LargeInteger value:16r44332211) byteSwapped64 hexPrintString
+ (LargeInteger value:16r1122334455667788) byteSwapped64 hexPrintString
+ (LargeInteger value:16r8877665544332211) byteSwapped64 hexPrintString
+ 16r88776655 byteSwapped hexPrintString
+ 16r11223344 byteSwapped hexPrintString
"
"Created: / 31-01-2012 / 11:07:42 / cg"
@@ -2652,22 +2650,22 @@
shift "{ Class: SmallInteger }" |
anInteger == 0 ifTrue:[
- ^ ZeroDivide raiseRequestWith:thisContext
+ ^ ZeroDivide raiseRequestWith:thisContext
].
self = anInteger ifTrue:[
- ^ Array with:1 with:0
+ ^ Array with:1 with:0
].
shift := self highBit - anInteger highBit.
dividend := self class digitBytes:digitByteArray copy. "/ self simpleDeepCopy sign:1.
shift < 0 ifTrue:[
- ^ Array with:0 with:dividend compressed.
+ ^ Array with:0 with:dividend compressed.
].
shift == 0 ifTrue:[
- divisor := self class digitBytes:(anInteger digitBytes copy). "/ anInteger simpleDeepCopy.
+ divisor := self class digitBytes:(anInteger digitBytes copy). "/ anInteger simpleDeepCopy.
] ifFalse:[
- divisor := anInteger bitShift:shift.
+ divisor := anInteger bitShift:shift.
].
quo := self class basicNew numberOfDigits:((shift // 8) + 1).
@@ -2675,14 +2673,14 @@
shift := shift + 1.
[shift > 0] whileTrue:[
- (dividend absLess:divisor) ifFalse:[
- digits bitSetAt:shift.
- (dividend absSubtract: divisor) ifFalse:[ "result == 0"
- ^ Array with:quo compressed with:dividend compressed
- ].
- ].
- shift := shift - 1.
- divisor div2.
+ (dividend absLess:divisor) ifFalse:[
+ digits bitSetAt:shift.
+ (dividend absSubtract: divisor) ifFalse:[ "result == 0"
+ ^ Array with:quo compressed with:dividend compressed
+ ].
+ ].
+ shift := shift - 1.
+ divisor div2.
].
^ Array with:quo compressed with:dividend compressed
@@ -2805,12 +2803,12 @@
ok|
aPositiveSmallInteger == 0 ifTrue:[
- ^ ZeroDivide raiseRequestWith:thisContext
+ ^ ZeroDivide raiseRequestWith:thisContext
].
"This cannot happen (if always normalized)
self < aPositiveSmallInteger ifTrue:[
- ^ Array with:0 with:self
+ ^ Array with:0 with:self
].
"
count := digitByteArray size.
@@ -2825,76 +2823,112 @@
if (__isByteArray(__digits)
&& __isByteArray(newDigitByteArray)
&& __bothSmallInteger(count, aPositiveSmallInteger)) {
- unsigned INT rest = 0;
- int index = __intVal(count);
- int index0;
- unsigned INT divisor = __intVal(aPositiveSmallInteger);
- unsigned char *digitBytes = __ByteArrayInstPtr(__digits)->ba_element;
- unsigned char *resultBytes = __ByteArrayInstPtr(newDigitByteArray)->ba_element;
-
- index0 = index - 1;
-
- /*
- * divide short-wise
- */
- if (divisor <= 0xFFFF) {
- if ((index & 1) == 0) { /* even number of bytes */
- while (index > 1) {
- unsigned INT t;
- unsigned INT div;
-
- index -= 2;
-#if defined(__LSBFIRST__)
- t = *((unsigned short *)(&digitBytes[index]));
-#else
- t = digitBytes[index+1];
- t = (t << 8) | digitBytes[index];
-#endif
- t = t | (rest << 16);
- div = t / divisor;
- rest = t % divisor;
-#if defined(__LSBFIRST__)
- *((unsigned short *)(&resultBytes[index])) = (div & 0xFFFF);
-#else
- resultBytes[index+1] = div >> 8;
- resultBytes[index] = div /* & 0xFF */;
+ unsigned INT rest = 0;
+ int index = __intVal(count);
+ int index0;
+ unsigned INT divisor = __intVal(aPositiveSmallInteger);
+ unsigned char *digitBytes = __ByteArrayInstPtr(__digits)->ba_element;
+ unsigned char *resultBytes = __ByteArrayInstPtr(newDigitByteArray)->ba_element;
+
+ index0 = index - 1;
+
+# if (__POINTER_SIZE__ == 8)
+ if (sizeof(int) == 4) {
+ /*
+ * divide int-wise
+ */
+ if (divisor <= 0xFFFFFFFF) {
+ if ((index & 3) == 0) { /* even number of int32's */
+ while (index > 3) {
+ unsigned INT t;
+ unsigned INT div;
+
+ index -= 4;
+# if defined(__LSBFIRST__)
+ t = *((unsigned int *)(&digitBytes[index]));
+# else
+ t = digitBytes[index+3];
+ t = (t << 8) | digitBytes[index+2];
+ t = (t << 8) | digitBytes[index+1];
+ t = (t << 8) | digitBytes[index];
+# endif
+ t = t | (rest << 32);
+ div = t / divisor;
+ rest = t % divisor;
+# if defined(__LSBFIRST__)
+ *((unsigned int *)(&resultBytes[index])) = (div & 0xFFFFFFFF);
+# else
+ resultBytes[index+3] = div >> 24;
+ resultBytes[index+2] = div >> 16;
+ resultBytes[index+1] = div >> 8;
+ resultBytes[index] = div /* & 0xFF */;
+# endif
+ }
+ }
+ }
+ }
#endif
- }
- }
- }
- while (index > 0) {
- unsigned INT t;
-
- index--;
- t = digitBytes[index];
- t = t | (rest << 8);
- resultBytes[index] = t / divisor;
- rest = t % divisor;
- }
- prevRest = __mkSmallInteger(rest);
-
- /*
- * no need to normalize ?
- */
- index = index0;
- while ((index >= sizeof(INT)) && (resultBytes[index]==0)) {
- index--;
- }
-
- if (index == index0) {
- if (index > sizeof(INT)) {
- RETURN ( __ARRAY_WITH2(result, prevRest));
- }
- if ((index == sizeof(INT))
- && resultBytes[index0] >= 0x40) {
- RETURN ( __ARRAY_WITH2(result, prevRest));
- }
- }
-
- /*
- * must compress
- */
- ok = true;
+ /*
+ * divide short-wise
+ */
+ if (divisor <= 0xFFFF) {
+ if ((index & 1) == 0) { /* even number of bytes */
+ while (index > 1) {
+ unsigned INT t;
+ unsigned INT div;
+
+ index -= 2;
+#if defined(__LSBFIRST__)
+ t = *((unsigned short *)(&digitBytes[index]));
+#else
+ t = digitBytes[index+1];
+ t = (t << 8) | digitBytes[index];
+#endif
+ t = t | (rest << 16);
+ div = t / divisor;
+ rest = t % divisor;
+#if defined(__LSBFIRST__)
+ *((unsigned short *)(&resultBytes[index])) = (div & 0xFFFF);
+#else
+ resultBytes[index+1] = div >> 8;
+ resultBytes[index] = div /* & 0xFF */;
+#endif
+ }
+ }
+ }
+ while (index > 0) {
+ unsigned INT t;
+
+ index--;
+ t = digitBytes[index];
+ t = t | (rest << 8);
+ resultBytes[index] = t / divisor;
+ rest = t % divisor;
+ }
+ prevRest = __mkSmallInteger(rest);
+
+ /*
+ * no need to normalize ?
+ */
+ index = index0;
+ while ((index >= sizeof(INT)) && (resultBytes[index]==0)) {
+ index--;
+ }
+
+ if (index == index0) {
+ if (index > sizeof(INT)) {
+ RETURN ( __ARRAY_WITH2(result, prevRest));
+ }
+ if ((index == sizeof(INT))
+ && resultBytes[index0] >= 0x40) {
+ RETURN ( __ARRAY_WITH2(result, prevRest));
+ }
+ }
+
+ /*
+ * must compress
+ */
+ ok = true;
}
%}.
"
@@ -2902,7 +2936,7 @@
(could also do a primitiveFailure here)
"
ok ifFalse:[
- ^ self absDivMod:(self class value:aPositiveSmallInteger).
+ ^ self absDivMod:(self class value:aPositiveSmallInteger).
].
^ Array with:(result compressed) with:prevRest
@@ -2934,7 +2968,7 @@
((aSmallInteger < (SmallInteger minVal + 255))
or:[aSmallInteger > (SmallInteger maxVal - 255)]) ifTrue:[
- ^ self absMinus:(self class value:aSmallInteger) sign:newSign.
+ ^ self absMinus:(self class value:aSmallInteger) sign:newSign.
].
len := digitByteArray size.
@@ -2949,123 +2983,123 @@
%{
if (__isByteArray(__INST(digitByteArray))
&& __isByteArray(resultDigitByteArray)) {
- unsigned INT __borrow = __intVal(borrow);
- INT __diff;
- int __index = 1;
- int __len = __intVal(len);
- unsigned char *__digitP = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element;
- unsigned char *__resultP = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
- int __len3;
+ unsigned INT __borrow = __intVal(borrow);
+ INT __diff;
+ int __index = 1;
+ int __len = __intVal(len);
+ unsigned char *__digitP = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element;
+ unsigned char *__resultP = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
+ int __len3;
#if defined(__LSBFIRST__)
# if (__POINTER_SIZE__ == 8)
- /*
- * subtract int-wise
- */
- __len3 = __len - 3;
- while (__index < __len3) {
- /* do not make this into one expression - ask cg why */
- __diff = ((unsigned int *)(__digitP + __index-1))[0];
- __diff -= (__borrow & 0xFFFFFFFFL);
- __borrow >>= 32;
- if (__diff < 0) {
- /* __diff += 0x100000000; */
- __borrow++;
- }
- ((unsigned int *)(__resultP+__index-1))[0] = __diff;
- __index += 4;
- }
+ /*
+ * subtract int-wise
+ */
+ __len3 = __len - 3;
+ while (__index < __len3) {
+ /* do not make this into one expression - ask cg why */
+ __diff = ((unsigned int *)(__digitP + __index-1))[0];
+ __diff -= (__borrow & 0xFFFFFFFFL);
+ __borrow >>= 32;
+ if (__diff < 0) {
+ /* __diff += 0x100000000; */
+ __borrow++;
+ }
+ ((unsigned int *)(__resultP+__index-1))[0] = __diff;
+ __index += 4;
+ }
# endif
- /*
- * subtract short-wise
- */
- while (__index < __len) {
- /* do not make this into one expression - ask cg why */
- __diff = ((unsigned short *)(__digitP+__index-1))[0];
- __diff -= (__borrow & 0xFFFF);
- __borrow >>= 16;
- if (__diff < 0) {
- /* __diff += 0x10000; */
- __borrow++;
- } else {
- if (__borrow == 0) {
- ((unsigned short *)(__resultP+__index-1))[0] = __diff;
- __index += 2;
-
- /* nothing more to subtract .. */
- while (__index < __len) {
- ((unsigned short *)(__resultP+__index-1))[0] = ((unsigned short *)(__digitP+__index-1))[0];
- __index += 2;
- }
- if (__index <= __len) {
- __resultP[__index-1] = __digitP[__index-1];
- }
- break;
- }
- }
- ((unsigned short *)(__resultP+__index-1))[0] = __diff;
- __index += 2;
- }
+ /*
+ * subtract short-wise
+ */
+ while (__index < __len) {
+ /* do not make this into one expression - ask cg why */
+ __diff = ((unsigned short *)(__digitP+__index-1))[0];
+ __diff -= (__borrow & 0xFFFF);
+ __borrow >>= 16;
+ if (__diff < 0) {
+ /* __diff += 0x10000; */
+ __borrow++;
+ } else {
+ if (__borrow == 0) {
+ ((unsigned short *)(__resultP+__index-1))[0] = __diff;
+ __index += 2;
+
+ /* nothing more to subtract .. */
+ while (__index < __len) {
+ ((unsigned short *)(__resultP+__index-1))[0] = ((unsigned short *)(__digitP+__index-1))[0];
+ __index += 2;
+ }
+ if (__index <= __len) {
+ __resultP[__index-1] = __digitP[__index-1];
+ }
+ break;
+ }
+ }
+ ((unsigned short *)(__resultP+__index-1))[0] = __diff;
+ __index += 2;
+ }
#endif
- /*
- * subtract byte-wise
- */
- while (__index <= __len) {
- __diff = __digitP[__index-1];
- __diff -= (__borrow & 0xFF);
- __borrow >>= 8;
- if (__diff < 0) {
- /* __diff += 0x100; */
- __borrow++;
- } else {
- if (__borrow == 0) {
- __resultP[__index-1] = __diff;
- __index++;
-
- /* nothing more to subtract .. */
- while (__index <= __len) {
- __resultP[__index-1] = __digitP[__index-1];
- __index++;
- }
- break;
- }
- }
- __resultP[__index-1] = __diff;
- __index++;
- }
- lastDigit = __mkSmallInteger( __resultP[__index-1-1] );
- ok = true;
+ /*
+ * subtract byte-wise
+ */
+ while (__index <= __len) {
+ __diff = __digitP[__index-1];
+ __diff -= (__borrow & 0xFF);
+ __borrow >>= 8;
+ if (__diff < 0) {
+ /* __diff += 0x100; */
+ __borrow++;
+ } else {
+ if (__borrow == 0) {
+ __resultP[__index-1] = __diff;
+ __index++;
+
+ /* nothing more to subtract .. */
+ while (__index <= __len) {
+ __resultP[__index-1] = __digitP[__index-1];
+ __index++;
+ }
+ break;
+ }
+ }
+ __resultP[__index-1] = __diff;
+ __index++;
+ }
+ lastDigit = __mkSmallInteger( __resultP[__index-1-1] );
+ ok = true;
}
%}.
ok == true ifFalse:[ "/ cannot happen
- index := 1.
- [borrow ~~ 0] whileTrue:[
- (index <= len) ifTrue:[
- diff := (digitByteArray basicAt:index) - (borrow bitAnd:16rFF).
- borrow := borrow bitShift:-8.
- diff < 0 ifTrue:[
- diff := diff + 256.
- borrow := borrow + 1.
- ]
- ] ifFalse:[
- diff := borrow bitAnd:255.
- borrow := borrow bitShift:-8.
- ].
- resultDigitByteArray basicAt:index put:(lastDigit := diff).
- index := index + 1
- ].
- [index <= len] whileTrue:[
- resultDigitByteArray basicAt:index put:(lastDigit := digitByteArray basicAt:index).
- index := index + 1
- ].
- (index <= rsltLen) ifTrue:[
- lastDigit := 0.
- ]
+ index := 1.
+ [borrow ~~ 0] whileTrue:[
+ (index <= len) ifTrue:[
+ diff := (digitByteArray basicAt:index) - (borrow bitAnd:16rFF).
+ borrow := borrow bitShift:-8.
+ diff < 0 ifTrue:[
+ diff := diff + 256.
+ borrow := borrow + 1.
+ ]
+ ] ifFalse:[
+ diff := borrow bitAnd:255.
+ borrow := borrow bitShift:-8.
+ ].
+ resultDigitByteArray basicAt:index put:(lastDigit := diff).
+ index := index + 1
+ ].
+ [index <= len] whileTrue:[
+ resultDigitByteArray basicAt:index put:(lastDigit := digitByteArray basicAt:index).
+ index := index + 1
+ ].
+ (index <= rsltLen) ifTrue:[
+ lastDigit := 0.
+ ]
].
(lastDigit == 0 or:[rsltLen <= SmallInteger maxBytes]) ifTrue:[
- ^ result compressed.
+ ^ result compressed.
].
^ result
@@ -3100,7 +3134,7 @@
((aSmallInteger < (SmallInteger minVal + 255))
or:[aSmallInteger > (SmallInteger maxVal - 255)]) ifTrue:[
- ^ self absPlus:(self class value:aSmallInteger) sign:newSign.
+ ^ self absPlus:(self class value:aSmallInteger) sign:newSign.
].
len := rsltLen := digitByteArray size.
@@ -3109,18 +3143,18 @@
"/ if it is 255 (since the other number is definitely smaller)
"/
(digitByteArray at:len) == 16rFF ifTrue:[
- rsltLen := len + 1.
+ rsltLen := len + 1.
] ifFalse:[
- "/ or the argument has something in the high byte ..
+ "/ or the argument has something in the high byte ..
%{
#if __POINTER_SIZE__ == 8
- if (__intVal(aSmallInteger) & 0xFF00000000000000L) {
- rsltLen = __mkSmallInteger(__intVal(len) + 1);
- }
+ if (__intVal(aSmallInteger) & 0xFF00000000000000L) {
+ rsltLen = __mkSmallInteger(__intVal(len) + 1);
+ }
#else
- if (__intVal(aSmallInteger) & 0xFF000000) {
- rsltLen = __mkSmallInteger(__intVal(len) + 1);
- }
+ if (__intVal(aSmallInteger) & 0xFF000000) {
+ rsltLen = __mkSmallInteger(__intVal(len) + 1);
+ }
#endif
%}
].
@@ -3133,307 +3167,307 @@
if (__isByteArray(__INST(digitByteArray))
&& __isByteArray(resultDigitByteArray)
&& __isSmallInteger(aSmallInteger)) {
- /* carry is NOT unsigned (see negation below) */
- INT __carry = __intVal(aSmallInteger);
- int __index = 1;
- int __len = __intVal(len);
- unsigned char *__src = (unsigned char *)(__ByteArrayInstPtr(__INST(digitByteArray))->ba_element);
- unsigned char *__dst = (unsigned char *)(__ByteArrayInstPtr(resultDigitByteArray)->ba_element);
- INT __ptrDelta = __dst - __src;
- unsigned char *__srcLast = __src + __len - 1;
- int __rsltLen = __intVal(rsltLen);
-
- if (__carry < 0) {
- __carry = -__carry;
- }
+ /* carry is NOT unsigned (see negation below) */
+ INT __carry = __intVal(aSmallInteger);
+ int __index = 1;
+ int __len = __intVal(len);
+ unsigned char *__src = (unsigned char *)(__ByteArrayInstPtr(__INST(digitByteArray))->ba_element);
+ unsigned char *__dst = (unsigned char *)(__ByteArrayInstPtr(resultDigitByteArray)->ba_element);
+ INT __ptrDelta = __dst - __src;
+ unsigned char *__srcLast = __src + __len - 1;
+ int __rsltLen = __intVal(rsltLen);
+
+ if (__carry < 0) {
+ __carry = -__carry;
+ }
#if defined(__LSBFIRST__)
# if defined(__i386__) && defined(__GNUC__) && (__POINTER_SIZE__ == 4)
# if 0 /* NOTICE - the code below is 20% slower ... - why */
- /*
- * add long-wise
- */
- asm(" jecxz nothingToDo \n\
- movl %%eax, %%esi /* __src input */ \n\
- movl %%ebx, %%edi /* __dst input */ \n\
- \n\
- /* the first 4-byte int */ \n\
- lodsl /* fetch */ \n\
- addl %%edx, %%eax /* add */ \n\
- stosl /* store */ \n\
- leal -1(%%ecx),%%ecx /* do not clobber carry */ \n\
- jecxz doneLoop /* any more ? */ \n\
- /* remaining 4-byte ints */ \n\
- jmp addLoop \n\
- \n\
- .align 8 \n\
- addLoop: \n\
- movl 0(%%esi), %%ebx /* fetch */ \n\
- jnc copyLoop2 \n\
- movl $0, %%eax \n\
- leal 4(%%esi), %%esi \n\
- adcl %%ebx, %%eax /* & add carry from prev int */\n\
- leal 8(%%edi), %%edi \n\
- movl %%eax, -8(%%edi) /* store */ \n\
- leal -1(%%ecx),%%ecx /* do not clobber carry */ \n\
- jecxz doneLoop /* any more ? */ \n\
- \n\
- movl 0(%%esi), %%ebx /* fetch */ \n\
- movl $0, %%eax \n\
- leal 4(%%esi), %%esi \
- adcl %%ebx, %%eax /* & add carry from prev int */\n\
- movl %%eax, -4(%%edi) /* store */ \n\
- \n\
- loop addLoop \n\
- jmp doneLoop \n\
- \n\
- .align 8 \n\
- copyLoop: \n\
- movl 0(%%esi), %%ebx \n\
- copyLoop2: \n\
- add $4, %%esi \n\
- add $4, %%edi \n\
- movl %%ebx, -4(%%edi) \n\
- loop copyLoop \n\
- \n\
- doneLoop: \n\
- movl $0, %%edx /* do not clobber carry (xorl clears it) */ \n\
- adcl $0, %%edx \n\
- movl %%esi, %%eax /* __src output */ \n\
- nothingToDo: \n\
- " : "=d" ((unsigned long)(__carry)),
- "=a" (__src)
- : "1" (__src),
- "b" (__dst),
- "c" (__len / 4),
- "0" (__carry)
- : "esi", "edi");
+ /*
+ * add long-wise
+ */
+ asm(" jecxz nothingToDo \n\
+ movl %%eax, %%esi /* __src input */ \n\
+ movl %%ebx, %%edi /* __dst input */ \n\
+ \n\
+ /* the first 4-byte int */ \n\
+ lodsl /* fetch */ \n\
+ addl %%edx, %%eax /* add */ \n\
+ stosl /* store */ \n\
+ leal -1(%%ecx),%%ecx /* do not clobber carry */ \n\
+ jecxz doneLoop /* any more ? */ \n\
+ /* remaining 4-byte ints */ \n\
+ jmp addLoop \n\
+ \n\
+ .align 8 \n\
+ addLoop: \n\
+ movl 0(%%esi), %%ebx /* fetch */ \n\
+ jnc copyLoop2 \n\
+ movl $0, %%eax \n\
+ leal 4(%%esi), %%esi \n\
+ adcl %%ebx, %%eax /* & add carry from prev int */\n\
+ leal 8(%%edi), %%edi \n\
+ movl %%eax, -8(%%edi) /* store */ \n\
+ leal -1(%%ecx),%%ecx /* do not clobber carry */ \n\
+ jecxz doneLoop /* any more ? */ \n\
+ \n\
+ movl 0(%%esi), %%ebx /* fetch */ \n\
+ movl $0, %%eax \n\
+ leal 4(%%esi), %%esi \
+ adcl %%ebx, %%eax /* & add carry from prev int */\n\
+ movl %%eax, -4(%%edi) /* store */ \n\
+ \n\
+ loop addLoop \n\
+ jmp doneLoop \n\
+ \n\
+ .align 8 \n\
+ copyLoop: \n\
+ movl 0(%%esi), %%ebx \n\
+ copyLoop2: \n\
+ add $4, %%esi \n\
+ add $4, %%edi \n\
+ movl %%ebx, -4(%%edi) \n\
+ loop copyLoop \n\
+ \n\
+ doneLoop: \n\
+ movl $0, %%edx /* do not clobber carry (xorl clears it) */ \n\
+ adcl $0, %%edx \n\
+ movl %%esi, %%eax /* __src output */ \n\
+ nothingToDo: \n\
+ " : "=d" ((unsigned long)(__carry)),
+ "=a" (__src)
+ : "1" (__src),
+ "b" (__dst),
+ "c" (__len / 4),
+ "0" (__carry)
+ : "esi", "edi");
# else
- {
- unsigned char *__srcLastX;
-
- __srcLastX = __srcLast - 3 - 4;
- while (__src <= __srcLastX) {
- unsigned int __sum, __sum2;
- unsigned __digit1, __digit2;
-
- __digit1 = ((unsigned *)__src)[0];
- __digit2 = ((unsigned *)__src)[1];
- asm ("addl %%edx,%%ecx \n\
- adcl $0, %%eax \n\
- movl $0, %%edx \n\
- adcl $0, %%edx"
- : "=d" ((unsigned long)(__carry)),
- "=c" ((unsigned long)(__sum)),
- "=a" ((unsigned long)(__sum2))
- : "0" ((unsigned long)(__carry)),
- "1" (__digit1),
- "2" (__digit2));
-
- ((unsigned int *)(__src + __ptrDelta))[0] = __sum;
- ((unsigned int *)(__src + __ptrDelta))[1] = __sum2;
-
- __src += 8;
-
- if (__carry == 0) {
- while (__src <= __srcLastX) {
- /* copy over words */
- ((unsigned int *)(__src + __ptrDelta))[0] = ((unsigned int *)__src)[0];
- ((unsigned int *)(__src + __ptrDelta))[1] = ((unsigned int *)__src)[1];
- __src += 8;
- }
- while (__src <= __srcLast) {
- /* copy over bytes */
- __src[__ptrDelta] = __src[0];
- __src ++;
- }
- goto doneSource;
- }
- }
-
- __srcLastX = __srcLastX + 4;
- if (__src <= __srcLastX) {
- unsigned int __sum, __digit;
-
- __digit = ((unsigned *)__src)[0];
-
- asm ("addl %%eax,%%edx \n\
- movl $0,%%eax \n\
- adcl $0,%%eax"
- : "=a" ((unsigned long)(__carry)),
- "=d" ((unsigned long)(__sum))
- : "0" ((unsigned long)(__carry)),
- "1" (__digit) );
-
- ((unsigned int *)(__src + __ptrDelta))[0] = __sum;
- __src += 4;
-
- if (__carry == 0) {
- while (__src <= __srcLast) {
- /* copy over bytes */
- __src[__ptrDelta] = __src[0];
- __src ++;
- }
- goto doneSource;
- }
- }
- }
+ {
+ unsigned char *__srcLastX;
+
+ __srcLastX = __srcLast - 3 - 4;
+ while (__src <= __srcLastX) {
+ unsigned int __sum, __sum2;
+ unsigned __digit1, __digit2;
+
+ __digit1 = ((unsigned *)__src)[0];
+ __digit2 = ((unsigned *)__src)[1];
+ asm ("addl %%edx,%%ecx \n\
+ adcl $0, %%eax \n\
+ movl $0, %%edx \n\
+ adcl $0, %%edx"
+ : "=d" ((unsigned long)(__carry)),
+ "=c" ((unsigned long)(__sum)),
+ "=a" ((unsigned long)(__sum2))
+ : "0" ((unsigned long)(__carry)),
+ "1" (__digit1),
+ "2" (__digit2));
+
+ ((unsigned int *)(__src + __ptrDelta))[0] = __sum;
+ ((unsigned int *)(__src + __ptrDelta))[1] = __sum2;
+
+ __src += 8;
+
+ if (__carry == 0) {
+ while (__src <= __srcLastX) {
+ /* copy over words */
+ ((unsigned int *)(__src + __ptrDelta))[0] = ((unsigned int *)__src)[0];
+ ((unsigned int *)(__src + __ptrDelta))[1] = ((unsigned int *)__src)[1];
+ __src += 8;
+ }
+ while (__src <= __srcLast) {
+ /* copy over bytes */
+ __src[__ptrDelta] = __src[0];
+ __src ++;
+ }
+ goto doneSource;
+ }
+ }
+
+ __srcLastX = __srcLastX + 4;
+ if (__src <= __srcLastX) {
+ unsigned int __sum, __digit;
+
+ __digit = ((unsigned *)__src)[0];
+
+ asm ("addl %%eax,%%edx \n\
+ movl $0,%%eax \n\
+ adcl $0,%%eax"
+ : "=a" ((unsigned long)(__carry)),
+ "=d" ((unsigned long)(__sum))
+ : "0" ((unsigned long)(__carry)),
+ "1" (__digit) );
+
+ ((unsigned int *)(__src + __ptrDelta))[0] = __sum;
+ __src += 4;
+
+ if (__carry == 0) {
+ while (__src <= __srcLast) {
+ /* copy over bytes */
+ __src[__ptrDelta] = __src[0];
+ __src ++;
+ }
+ goto doneSource;
+ }
+ }
+ }
# endif
# else /* not i386-GNUC */
# if defined(WIN32) && defined(__BORLANDC__) && defined(__i386__) && (__POINTER_SIZE__ == 4)
- {
- unsigned char *__srcLast4;
-
- /*
- * add long-wise
- */
- __srcLast4 = __srcLast - 3;
- while (__src <= __srcLast4) {
- unsigned int __sum;
-
- __sum = ((unsigned int *)__src)[0];
- asm {
- mov eax, __sum
- add eax, __carry
- mov edx, 0
- adc edx, 0
- mov __sum, eax
- mov __carry, edx
- }
-
- ((unsigned int *)(__src + __ptrDelta))[0] = __sum;
- __src += 4;
- if (__carry == 0) {
- while (__src <= __srcLast4) {
- /* copy over words */
- ((unsigned int *)(__src + __ptrDelta))[0] = ((unsigned int *)__src)[0];
- __src += 4;
- }
- while (__src <= __srcLast) {
- /* copy over bytes */
- __src[__ptrDelta] = __src[0];
- __src ++;
- }
- goto doneSource;
- }
- }
- }
+ {
+ unsigned char *__srcLast4;
+
+ /*
+ * add long-wise
+ */
+ __srcLast4 = __srcLast - 3;
+ while (__src <= __srcLast4) {
+ unsigned int __sum;
+
+ __sum = ((unsigned int *)__src)[0];
+ asm {
+ mov eax, __sum
+ add eax, __carry
+ mov edx, 0
+ adc edx, 0
+ mov __sum, eax
+ mov __carry, edx
+ }
+
+ ((unsigned int *)(__src + __ptrDelta))[0] = __sum;
+ __src += 4;
+ if (__carry == 0) {
+ while (__src <= __srcLast4) {
+ /* copy over words */
+ ((unsigned int *)(__src + __ptrDelta))[0] = ((unsigned int *)__src)[0];
+ __src += 4;
+ }
+ while (__src <= __srcLast) {
+ /* copy over bytes */
+ __src[__ptrDelta] = __src[0];
+ __src ++;
+ }
+ goto doneSource;
+ }
+ }
+ }
# else /* not i386-WIN32 */
# if defined(__LSBFIRST__) && (__POINTER_SIZE__ == 8)
- {
- unsigned char *__srcLast4;
-
- /*
- * add long-wise
- */
- __srcLast4 = __srcLast - 3;
- while (__src <= __srcLast4) {
- unsigned INT __sum;
-
- __sum = ((unsigned int *)__src)[0] + __carry;
- ((unsigned int *)(__src + __ptrDelta))[0] = __sum /* & 0xFFFF */;
- __src += 4;
- __carry = __sum >> 32;
- if (__carry == 0) {
- while (__src <= __srcLast4) {
- /* copy over words */
- ((unsigned int *)(__src + __ptrDelta))[0] = ((unsigned int *)__src)[0];
- __src += 4;
- }
- while (__src <= __srcLast) {
- /* copy over bytes */
- __src[__ptrDelta] = __src[0];
- __src ++;
- }
- goto doneSource;
- }
- }
- }
+ {
+ unsigned char *__srcLast4;
+
+ /*
+ * add long-wise
+ */
+ __srcLast4 = __srcLast - 3;
+ while (__src <= __srcLast4) {
+ unsigned INT __sum;
+
+ __sum = ((unsigned int *)__src)[0] + __carry;
+ ((unsigned int *)(__src + __ptrDelta))[0] = __sum /* & 0xFFFF */;
+ __src += 4;
+ __carry = __sum >> 32;
+ if (__carry == 0) {
+ while (__src <= __srcLast4) {
+ /* copy over words */
+ ((unsigned int *)(__src + __ptrDelta))[0] = ((unsigned int *)__src)[0];
+ __src += 4;
+ }
+ while (__src <= __srcLast) {
+ /* copy over bytes */
+ __src[__ptrDelta] = __src[0];
+ __src ++;
+ }
+ goto doneSource;
+ }
+ }
+ }
# endif /* LSB+64bit */
# endif /* __i386__ & WIN32 */
# endif /* __i386__ & GNUC */
- /*
- * add short-wise
- */
- while (__src < __srcLast) {
- __carry += ((unsigned short *)__src)[0];
- ((unsigned short *)(__src + __ptrDelta))[0] = __carry /* & 0xFFFF */;
- __carry >>= 16;
- __src += 2;
- }
- /*
- * last (odd) byte
- */
- if (__src <= __srcLast) {
- __carry += __src[0];
- __src[__ptrDelta] = __carry /* & 0xFF */;
- __carry >>= 8;
- __src++;
- }
+ /*
+ * add short-wise
+ */
+ while (__src < __srcLast) {
+ __carry += ((unsigned short *)__src)[0];
+ ((unsigned short *)(__src + __ptrDelta))[0] = __carry /* & 0xFFFF */;
+ __carry >>= 16;
+ __src += 2;
+ }
+ /*
+ * last (odd) byte
+ */
+ if (__src <= __srcLast) {
+ __carry += __src[0];
+ __src[__ptrDelta] = __carry /* & 0xFF */;
+ __carry >>= 8;
+ __src++;
+ }
#else /* not __LSBFIRST__ */
- /*
- * add byte-wise
- */
- while (__src <= __srcLast) {
- __carry += __src[0];
- __src[__ptrDelta] = __carry /* & 0xFF */;
- __src++;
- __carry >>= 8;
-
- if (__carry == 0) {
- while (__src <= __srcLast) {
- /* copy over rest */
- __src[__ptrDelta] = __src[0];
- __src++;
- }
- goto doneSource;
- }
- }
+ /*
+ * add byte-wise
+ */
+ while (__src <= __srcLast) {
+ __carry += __src[0];
+ __src[__ptrDelta] = __carry /* & 0xFF */;
+ __src++;
+ __carry >>= 8;
+
+ if (__carry == 0) {
+ while (__src <= __srcLast) {
+ /* copy over rest */
+ __src[__ptrDelta] = __src[0];
+ __src++;
+ }
+ goto doneSource;
+ }
+ }
#endif /* __LSBFIRST__ */
doneSource: ;
- /*
- * now, at most one other byte is to be stored ...
- */
- if (__len < __rsltLen) {
- __src[__ptrDelta] = __carry /* & 0xFF */;
- __src++;
- }
-
- if (__src[__ptrDelta-1]) { /* lastDigit */
- RETURN (result);
- }
- ok = true;
+ /*
+ * now, at most one other byte is to be stored ...
+ */
+ if (__len < __rsltLen) {
+ __src[__ptrDelta] = __carry /* & 0xFF */;
+ __src++;
+ }
+
+ if (__src[__ptrDelta-1]) { /* lastDigit */
+ RETURN (result);
+ }
+ ok = true;
}
%}.
ok ~~ true ifTrue:[
- index := 1.
- carry := aSmallInteger abs.
-
- [carry ~~ 0] whileTrue:[
- (index <= len) ifTrue:[
- carry := (digitByteArray basicAt:index) + carry.
- ].
- resultDigitByteArray basicAt:index put:(lastDigit := carry bitAnd:16rFF).
- carry := carry bitShift:-8.
- index := index + 1
- ].
-
- (index <= rsltLen) ifTrue:[
- [index <= len] whileTrue:[
- resultDigitByteArray basicAt:index put:(digitByteArray basicAt:index).
- index := index + 1
- ].
- lastDigit := 0.
- ].
-
- (lastDigit ~~ 0 and:[rsltLen > SmallInteger maxBytes]) ifTrue:[
- ^ result
- ].
+ index := 1.
+ carry := aSmallInteger abs.
+
+ [carry ~~ 0] whileTrue:[
+ (index <= len) ifTrue:[
+ carry := (digitByteArray basicAt:index) + carry.
+ ].
+ resultDigitByteArray basicAt:index put:(lastDigit := carry bitAnd:16rFF).
+ carry := carry bitShift:-8.
+ index := index + 1
+ ].
+
+ (index <= rsltLen) ifTrue:[
+ [index <= len] whileTrue:[
+ resultDigitByteArray basicAt:index put:(digitByteArray basicAt:index).
+ index := index + 1
+ ].
+ lastDigit := 0.
+ ].
+
+ (lastDigit ~~ 0 and:[rsltLen > SmallInteger maxBytes]) ifTrue:[
+ ^ result
+ ].
].
^ result compressed
@@ -3944,34 +3978,34 @@
shift "{ Class: SmallInteger }" |
anInteger == 0 ifTrue:[
- ^ ZeroDivide raiseRequestWith:thisContext
+ ^ ZeroDivide raiseRequestWith:thisContext
].
self = anInteger ifTrue:[
- ^ 0
+ ^ 0
].
shift := self highBit - anInteger highBit.
dividend := self class digitBytes:digitByteArray copy. "/ self simpleDeepCopy sign:1.
shift < 0 ifTrue:[
- ^ dividend compressed.
+ ^ dividend compressed.
].
shift == 0 ifTrue:[
- divisor := LargeInteger digitBytes:(anInteger digitBytes copy). "/ anInteger simpleDeepCopy
+ divisor := LargeInteger digitBytes:(anInteger digitBytes copy). "/ anInteger simpleDeepCopy
] ifFalse:[
- divisor := anInteger bitShift:shift.
+ divisor := anInteger bitShift:shift.
].
shift := shift + 1.
[shift > 0] whileTrue:[
- (dividend absLess:divisor) ifFalse:[
- (dividend absSubtract: divisor) ifFalse:[ "result == 0"
- ^ dividend compressed
- ].
- ].
- shift := shift - 1.
- divisor div2.
+ (dividend absLess:divisor) ifFalse:[
+ (dividend absSubtract: divisor) ifFalse:[ "result == 0"
+ ^ dividend compressed
+ ].
+ ].
+ shift := shift - 1.
+ divisor div2.
].
^ dividend compressed
@@ -4012,273 +4046,273 @@
&& __isByteArray(otherDigitByteArray)
&& __isByteArray(resultDigitByteArray)
&& __bothSmallInteger(len1, len2)) {
- unsigned char *myBytes = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element;
- unsigned char *otherBytes = __ByteArrayInstPtr(otherDigitByteArray)->ba_element;
- unsigned char *resultBytes = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
- unsigned char *_p1, *_pResult0, *_p1Last, *_p2Last, *_pResultLast;
- unsigned char *_pResultLastShort, *_pResultLastInt;
- unsigned INT _v;
- int _len1 = __intVal(len1);
- int _len2 = __intVal(len2);
-
- _p1Last = myBytes + _len1 - 1; /* the last byte */
- _p2Last = otherBytes + _len2 - 1; /* the last byte */
- _pResult0 = resultBytes;
- _pResultLast = resultBytes + _len1 + _len2 - 1; /* the last byte */
-
- /*
- * aaa...aaa f1[0] * f2
- * bbb...bbb f1[1] * f2
- * ccc...ccc f1[2] * f2
- * ...
- * xxx...xxx f1[high] * f2
- *
- * start short-wise
- * bounds: (16rFFFF * 16rFFFF) + 16rFFFF -> FFFF0000
- */
- _p1 = myBytes;
+ unsigned char *myBytes = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element;
+ unsigned char *otherBytes = __ByteArrayInstPtr(otherDigitByteArray)->ba_element;
+ unsigned char *resultBytes = __ByteArrayInstPtr(resultDigitByteArray)->ba_element;
+ unsigned char *_p1, *_pResult0, *_p1Last, *_p2Last, *_pResultLast;
+ unsigned char *_pResultLastShort, *_pResultLastInt;
+ unsigned INT _v;
+ int _len1 = __intVal(len1);
+ int _len2 = __intVal(len2);
+
+ _p1Last = myBytes + _len1 - 1; /* the last byte */
+ _p2Last = otherBytes + _len2 - 1; /* the last byte */
+ _pResult0 = resultBytes;
+ _pResultLast = resultBytes + _len1 + _len2 - 1; /* the last byte */
+
+ /*
+ * aaa...aaa f1[0] * f2
+ * bbb...bbb f1[1] * f2
+ * ccc...ccc f1[2] * f2
+ * ...
+ * xxx...xxx f1[high] * f2
+ *
+ * start short-wise
+ * bounds: (16rFFFF * 16rFFFF) + 16rFFFF -> FFFF0000
+ */
+ _p1 = myBytes;
#if defined(__LSBFIRST__) && (__POINTER_SIZE__ == 8)
- _pResultLastInt = _pResultLast - 3; /* the last int */
-
- /* loop over ints of f1 */
- for (; _p1 <= (_p1Last-3); _p1 += 4, _pResult0 += 4) {
- unsigned char *_pResult, *_p2;
- unsigned INT word1 = ((unsigned int *)_p1)[0];
-
- _pResult = _pResult0;
- _p2 = otherBytes;
-
- /* loop over ints of f2 */
- while (_p2 <= (_p2Last-3)) {
- _v = (word1 * (unsigned INT)(((unsigned int *)_p2)[0])) + ((unsigned INT)((unsigned int *)_pResult)[0]);
- ((unsigned int *)_pResult)[0] = _v /* & 0xFFFFFFFF */;
- _v >>= 32; /* now _v contains the carry*/
- _pResult += 4;
- if (_v) {
- unsigned char *_pResult1;
-
- /* distribute carry - int-wise, then byte-wise */
- for (_pResult1 = _pResult; _v; _pResult1 += 4) {
- if (_pResult1 > _pResultLastInt) break;
- _v += (unsigned INT)(((unsigned int *)_pResult1)[0]);
- ((unsigned int *)_pResult1)[0] = _v /* & 0xFFFFFFFF */;
- _v >>= 32;
- }
- for (; _v; _pResult1++) {
- _v += _pResult1[0];
- _pResult1[0] = _v /* & 0xFF */;
- _v >>= 8;
- }
- }
- _p2 += 4;
- }
-
- /* possible odd highByte of f2 */
- while (_p2 <= _p2Last) {
- _v = (word1 * (unsigned INT)((unsigned char *)_p2[0])) + (unsigned INT)((unsigned char *)_pResult[0]);
-
- ((unsigned char *)_pResult)[0] = _v /* & 0xFFFFFFFF */;
- _v >>= 8; /* now _v contains the carry*/
- _pResult++;
- if (_v) {
- unsigned char *_pResult1 = _pResult;
-
- /* distribute carry byte-wise */
- for (; _v; _pResult1++) {
- _v += _pResult1[0];
- _pResult1[0] = _v /* & 0xFF */;
- _v >>= 8;
- }
- }
- _p2++;
- }
- }
+ _pResultLastInt = _pResultLast - 3; /* the last int */
+
+ /* loop over ints of f1 */
+ for (; _p1 <= (_p1Last-3); _p1 += 4, _pResult0 += 4) {
+ unsigned char *_pResult, *_p2;
+ unsigned INT word1 = ((unsigned int *)_p1)[0];
+
+ _pResult = _pResult0;
+ _p2 = otherBytes;
+
+ /* loop over ints of f2 */
+ while (_p2 <= (_p2Last-3)) {
+ _v = (word1 * (unsigned INT)(((unsigned int *)_p2)[0])) + ((unsigned INT)((unsigned int *)_pResult)[0]);
+ ((unsigned int *)_pResult)[0] = _v /* & 0xFFFFFFFF */;
+ _v >>= 32; /* now _v contains the carry*/
+ _pResult += 4;
+ if (_v) {
+ unsigned char *_pResult1;
+
+ /* distribute carry - int-wise, then byte-wise */
+ for (_pResult1 = _pResult; _v; _pResult1 += 4) {
+ if (_pResult1 > _pResultLastInt) break;
+ _v += (unsigned INT)(((unsigned int *)_pResult1)[0]);
+ ((unsigned int *)_pResult1)[0] = _v /* & 0xFFFFFFFF */;
+ _v >>= 32;
+ }
+ for (; _v; _pResult1++) {
+ _v += _pResult1[0];
+ _pResult1[0] = _v /* & 0xFF */;
+ _v >>= 8;
+ }
+ }
+ _p2 += 4;
+ }
+
+ /* possible odd highByte of f2 */
+ while (_p2 <= _p2Last) {
+ _v = (word1 * (unsigned INT)((unsigned char *)_p2[0])) + (unsigned INT)((unsigned char *)_pResult[0]);
+
+ ((unsigned char *)_pResult)[0] = _v /* & 0xFFFFFFFF */;
+ _v >>= 8; /* now _v contains the carry*/
+ _pResult++;
+ if (_v) {
+ unsigned char *_pResult1 = _pResult;
+
+ /* distribute carry byte-wise */
+ for (; _v; _pResult1++) {
+ _v += _pResult1[0];
+ _pResult1[0] = _v /* & 0xFF */;
+ _v >>= 8;
+ }
+ }
+ _p2++;
+ }
+ }
#endif /* 64bit */
- /* possible odd high short of f1 (or shortLoop, if not 64bit) */
- _pResultLastShort = _pResultLast - 1; /* the last short */
-
- for (; _p1 < _p1Last; _p1 += 2, _pResult0 += 2) {
- unsigned char *_pResult, *_p2;
- unsigned int short1 = ((unsigned short *)_p1)[0];
+ /* possible odd high short of f1 (or shortLoop, if not 64bit) */
+ _pResultLastShort = _pResultLast - 1; /* the last short */
+
+ for (; _p1 < _p1Last; _p1 += 2, _pResult0 += 2) {
+ unsigned char *_pResult, *_p2;
+ unsigned int short1 = ((unsigned short *)_p1)[0];
#if !defined(__LSBFIRST__)
- short1 = ((short1 >> 8) & 0xFF) | ((short1 & 0xFF) << 8);
+ short1 = ((short1 >> 8) & 0xFF) | ((short1 & 0xFF) << 8);
#endif
- _pResult = _pResult0;
- _p2 = otherBytes;
-
- /* loop over shorts of f2 */
- while (_p2 < _p2Last) {
+ _pResult = _pResult0;
+ _p2 = otherBytes;
+
+ /* loop over shorts of f2 */
+ while (_p2 < _p2Last) {
#if !defined(__LSBFIRST__)
- unsigned int _short2;
- unsigned int _short3;
-
- _short2 = ((unsigned short *)_p2)[0];
- _short2 = ((_short2 >> 8) /* & 0xFF */) | ((_short2 & 0xFF) << 8);
- _short3 = ((unsigned short *)_pResult)[0];
- _short3 = ((_short3 >> 8) /* & 0xFF */) | ((_short3 & 0xFF) << 8);
- _v = (short1 * _short2) + _short3;
- _pResult[0] = _v;
- _pResult[1] = _v >> 8;
+ unsigned int _short2;
+ unsigned int _short3;
+
+ _short2 = ((unsigned short *)_p2)[0];
+ _short2 = ((_short2 >> 8) /* & 0xFF */) | ((_short2 & 0xFF) << 8);
+ _short3 = ((unsigned short *)_pResult)[0];
+ _short3 = ((_short3 >> 8) /* & 0xFF */) | ((_short3 & 0xFF) << 8);
+ _v = (short1 * _short2) + _short3;
+ _pResult[0] = _v;
+ _pResult[1] = _v >> 8;
#else /* __LSBFIRST__ */
- _v = (short1 * ((unsigned short *)_p2)[0]) + ((unsigned short *)_pResult)[0];
- ((unsigned short *)_pResult)[0] = _v /* & 0xFFFF */;
+ _v = (short1 * ((unsigned short *)_p2)[0]) + ((unsigned short *)_pResult)[0];
+ ((unsigned short *)_pResult)[0] = _v /* & 0xFFFF */;
#endif
- _v >>= 16; /* now _v contains the carry*/
- _pResult += 2;
- if (_v) {
- unsigned char *_pResult1;
-
- /* distribute carry - short-wise, then byte-wise */
- _pResult1 = _pResult;
+ _v >>= 16; /* now _v contains the carry*/
+ _pResult += 2;
+ if (_v) {
+ unsigned char *_pResult1;
+
+ /* distribute carry - short-wise, then byte-wise */
+ _pResult1 = _pResult;
#if defined(__LSBFIRST__)
- for (; _v; _pResult1 += 2) {
- if (_pResult1 > _pResultLastShort) break;
- _v += ((unsigned short *)_pResult1)[0];
- ((unsigned short *)_pResult1)[0] = _v /* & 0xFFFF */;
- _v >>= 16;
- }
+ for (; _v; _pResult1 += 2) {
+ if (_pResult1 > _pResultLastShort) break;
+ _v += ((unsigned short *)_pResult1)[0];
+ ((unsigned short *)_pResult1)[0] = _v /* & 0xFFFF */;
+ _v >>= 16;
+ }
#endif
- for (; _v; _pResult1++) {
- _v += _pResult1[0];
- _pResult1[0] = _v /* & 0xFF */;
- _v >>= 8;
- }
- }
- _p2 += 2;
- }
-
- /* possible odd highByte of f2 */
- if (_p2 <= _p2Last) {
+ for (; _v; _pResult1++) {
+ _v += _pResult1[0];
+ _pResult1[0] = _v /* & 0xFF */;
+ _v >>= 8;
+ }
+ }
+ _p2 += 2;
+ }
+
+ /* possible odd highByte of f2 */
+ if (_p2 <= _p2Last) {
#if !defined(__LSBFIRST__)
- unsigned int _short3;
-
- _short3 = ((unsigned short *)_pResult)[0];
- _short3 = ((_short3 >> 8) /* & 0xFF */) | ((_short3 & 0xFF) << 8);
- _v = (short1 * _p2[0]) + _short3;
- _pResult[0] = _v;
- _pResult[1] = _v >> 8;
+ unsigned int _short3;
+
+ _short3 = ((unsigned short *)_pResult)[0];
+ _short3 = ((_short3 >> 8) /* & 0xFF */) | ((_short3 & 0xFF) << 8);
+ _v = (short1 * _p2[0]) + _short3;
+ _pResult[0] = _v;
+ _pResult[1] = _v >> 8;
#else /* __LSBFIRST__ */
- _v = (short1 * _p2[0]) + ((unsigned short *)_pResult)[0];
- ((unsigned short *)_pResult)[0] = _v /* & 0xFFFF */;
+ _v = (short1 * _p2[0]) + ((unsigned short *)_pResult)[0];
+ ((unsigned short *)_pResult)[0] = _v /* & 0xFFFF */;
#endif
- _v >>= 16; /* now _v contains the carry*/
- _pResult += 2;
- if (_v) {
- unsigned char *_pResult1;
-
- /* distribute carry - short-wise, then byte-wise */
- _pResult1 = _pResult;
+ _v >>= 16; /* now _v contains the carry*/
+ _pResult += 2;
+ if (_v) {
+ unsigned char *_pResult1;
+
+ /* distribute carry - short-wise, then byte-wise */
+ _pResult1 = _pResult;
#if defined(__LSBFIRST__)
- for (; _v; _pResult1 += 2) {
- if (_pResult1 > _pResultLastShort) break;
- _v += ((unsigned short *)_pResult1)[0];
- ((unsigned short *)_pResult1)[0] = _v /* & 0xFFFF */;
- _v >>= 16;
- }
+ for (; _v; _pResult1 += 2) {
+ if (_pResult1 > _pResultLastShort) break;
+ _v += ((unsigned short *)_pResult1)[0];
+ ((unsigned short *)_pResult1)[0] = _v /* & 0xFFFF */;
+ _v >>= 16;
+ }
#endif
- for (; _v; _pResult1++) {
- _v += _pResult1[0];
- _pResult1[0] = _v /* & 0xFF */;
- _v >>= 8;
- }
- }
- _p2++;
- }
- }
-
- /* possible odd highByte of f1 (or byteLoop, if above is ever disabled) */
- for (; _p1 <= _p1Last; _p1++, _pResult0++) {
- unsigned char *_pResult, *_p2;
- unsigned int byte1 = _p1[0];
-
- _pResult = _pResult0;
- _p2 = otherBytes;
-
- /* loop over shorts of f2 */
- while (_p2 < _p2Last) {
+ for (; _v; _pResult1++) {
+ _v += _pResult1[0];
+ _pResult1[0] = _v /* & 0xFF */;
+ _v >>= 8;
+ }
+ }
+ _p2++;
+ }
+ }
+
+ /* possible odd highByte of f1 (or byteLoop, if above is ever disabled) */
+ for (; _p1 <= _p1Last; _p1++, _pResult0++) {
+ unsigned char *_pResult, *_p2;
+ unsigned int byte1 = _p1[0];
+
+ _pResult = _pResult0;
+ _p2 = otherBytes;
+
+ /* loop over shorts of f2 */
+ while (_p2 < _p2Last) {
#if !defined(__LSBFIRST__)
- unsigned int _short2;
- unsigned int _short3;
-
- _short2 = ((unsigned short *)_p2)[0];
- _short2 = ((_short2 >> 8) /* & 0xFF */) | ((_short2 & 0xFF) << 8);
- _short3 = ((unsigned short *)_pResult)[0];
- _short3 = ((_short3 >> 8) /* & 0xFF */) | ((_short3 & 0xFF) << 8);
- _v = (byte1 * _short2) + _short3;
- _pResult[0] = _v;
- _pResult[1] = _v >> 8;
+ unsigned int _short2;
+ unsigned int _short3;
+
+ _short2 = ((unsigned short *)_p2)[0];
+ _short2 = ((_short2 >> 8) /* & 0xFF */) | ((_short2 & 0xFF) << 8);
+ _short3 = ((unsigned short *)_pResult)[0];
+ _short3 = ((_short3 >> 8) /* & 0xFF */) | ((_short3 & 0xFF) << 8);
+ _v = (byte1 * _short2) + _short3;
+ _pResult[0] = _v;
+ _pResult[1] = _v >> 8;
#else /* __LSBFIRST__ */
- _v = (byte1 * ((unsigned short *)_p2)[0]) + ((unsigned short *)_pResult)[0];
- ((unsigned short *)_pResult)[0] = _v /* & 0xFFFF */;
+ _v = (byte1 * ((unsigned short *)_p2)[0]) + ((unsigned short *)_pResult)[0];
+ ((unsigned short *)_pResult)[0] = _v /* & 0xFFFF */;
#endif
- _v >>= 16; /* now _v contains the carry*/
- _pResult += 2;
- if (_v) {
- unsigned char *_pResult1;
-
- /* distribute carry - short-wise, then byte-wise */
- _pResult1 = _pResult;
+ _v >>= 16; /* now _v contains the carry*/
+ _pResult += 2;
+ if (_v) {
+ unsigned char *_pResult1;
+
+ /* distribute carry - short-wise, then byte-wise */
+ _pResult1 = _pResult;
#if defined(__LSBFIRST__)
- for (_pResult1 = _pResult; _v; _pResult1 += 2) {
- if (_pResult1 > _pResultLastShort) break;
- _v += ((unsigned short *)_pResult1)[0];
- ((unsigned short *)_pResult1)[0] = _v /* & 0xFFFF */;
- _v >>= 16;
- }
+ for (_pResult1 = _pResult; _v; _pResult1 += 2) {
+ if (_pResult1 > _pResultLastShort) break;
+ _v += ((unsigned short *)_pResult1)[0];
+ ((unsigned short *)_pResult1)[0] = _v /* & 0xFFFF */;
+ _v >>= 16;
+ }
#endif /* __LSBFIRST__ */
- for (; _v; _pResult1++) {
- _v += _pResult1[0];
- _pResult1[0] = _v /* & 0xFF */;
- _v >>= 8;
- }
- }
- _p2 += 2;
- }
-
- /* possible odd highByte of f2 (or byteLoop, if not __LSBFIRST__) */
- while (_p2 <= _p2Last) {
- _v = (byte1 * _p2[0]) + _pResult[0];
- _pResult[0] = _v /* & 0xFF */;
- _v >>= 8; /* now _v contains the carry*/
- _pResult++;
- if (_v) {
- unsigned char *_pResult1;
-
- /* distribute carry */
- for (_pResult1 = _pResult; _v; _pResult1++) {
- _v += _pResult1[0];
- _pResult1[0] = _v /* & 0xFF */;
- _v >>= 8;
- }
- }
- _p2++;
- }
- }
- ok = true;
+ for (; _v; _pResult1++) {
+ _v += _pResult1[0];
+ _pResult1[0] = _v /* & 0xFF */;
+ _v >>= 8;
+ }
+ }
+ _p2 += 2;
+ }
+
+ /* possible odd highByte of f2 (or byteLoop, if not __LSBFIRST__) */
+ while (_p2 <= _p2Last) {
+ _v = (byte1 * _p2[0]) + _pResult[0];
+ _pResult[0] = _v /* & 0xFF */;
+ _v >>= 8; /* now _v contains the carry*/
+ _pResult++;
+ if (_v) {
+ unsigned char *_pResult1;
+
+ /* distribute carry */
+ for (_pResult1 = _pResult; _v; _pResult1++) {
+ _v += _pResult1[0];
+ _pResult1[0] = _v /* & 0xFF */;
+ _v >>= 8;
+ }
+ }
+ _p2++;
+ }
+ }
+ ok = true;
}
%}.
ok ifFalse:[
- 1 to:len1 do:[:index1 |
- 1 to:len2 do:[:index2 |
- dstIndex := index1 + index2 - 1.
- prod := (digitByteArray basicAt:index1) * (otherDigitByteArray basicAt:index2).
- prod := prod + (resultDigitByteArray basicAt:dstIndex).
- resultDigitByteArray basicAt:dstIndex put:(prod bitAnd:16rFF).
- carry := prod bitShift:-8.
- carry ~~ 0 ifTrue:[
- idx := dstIndex + 1.
- [carry ~~ 0] whileTrue:[
- v := (resultDigitByteArray basicAt:idx) + carry.
- resultDigitByteArray basicAt:idx put:(v bitAnd:255).
- carry := v bitShift:-8.
- idx := idx + 1
- ]
- ]
- ]
- ].
+ 1 to:len1 do:[:index1 |
+ 1 to:len2 do:[:index2 |
+ dstIndex := index1 + index2 - 1.
+ prod := (digitByteArray basicAt:index1) * (otherDigitByteArray basicAt:index2).
+ prod := prod + (resultDigitByteArray basicAt:dstIndex).
+ resultDigitByteArray basicAt:dstIndex put:(prod bitAnd:16rFF).
+ carry := prod bitShift:-8.
+ carry ~~ 0 ifTrue:[
+ idx := dstIndex + 1.
+ [carry ~~ 0] whileTrue:[
+ v := (resultDigitByteArray basicAt:idx) + carry.
+ resultDigitByteArray basicAt:idx put:(v bitAnd:255).
+ carry := v bitShift:-8.
+ idx := idx + 1
+ ]
+ ]
+ ]
+ ].
].
^ result compressed
!
@@ -4802,13 +4836,13 @@
absSubtract:aLargeInteger
"private helper for division:
- destructively subtract aLargeInteger from myself
- AND return true, if the result is non-zero, false otherwise.
- (i.e. this method has both a return value and a side-effect
- on the receiver)
- Only allowed for positive receiver and argument
- The receiver must be >= the argument.
- The receiver must be a temporary scratch-number"
+ destructively subtract aLargeInteger from myself
+ AND return true, if the result is non-zero, false otherwise.
+ (i.e. this method has both a return value and a side-effect
+ on the receiver)
+ Only allowed for positive receiver and argument
+ The receiver must be >= the argument.
+ The receiver must be a temporary scratch-number"
|otherDigitByteArray
len1 "{ Class: SmallInteger }"
@@ -4824,12 +4858,12 @@
otherDigitByteArray := aLargeInteger digitBytes.
len2 := otherDigitByteArray size.
len2 > len1 ifTrue:[
- [(otherDigitByteArray at:len2) == 0] whileTrue:[
- len2 := len2 - 1
- ].
- len2 > len1 ifTrue:[
- self error:'operation failed' "/ may not be called that way
- ].
+ [(otherDigitByteArray at:len2) == 0] whileTrue:[
+ len2 := len2 - 1
+ ].
+ len2 > len1 ifTrue:[
+ self error:'operation failed' "/ may not be called that way
+ ].
].
"/ knowing that len2 is <= len1
%{
@@ -4838,147 +4872,147 @@
if (__isByteArray(_digitByteArray)
&& __isByteArray(otherDigitByteArray)) {
- int _len1 = __intVal(len1),
- _len2 = __intVal(len2);
- unsigned char *_myDigits, *_otherDigits;
- int _index = 1, _borrow = 0;
- INT _diff;
- int anyBitNonZero = 0;
-
- _otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element;
- _myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element;
+ int _len1 = __intVal(len1),
+ _len2 = __intVal(len2);
+ unsigned char *_myDigits, *_otherDigits;
+ int _index = 1, _borrow = 0;
+ INT _diff;
+ int anyBitNonZero = 0;
+
+ _otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element;
+ _myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element;
#if defined(__LSBFIRST__)
# if __POINTER_SIZE__ == 8
- {
- int _len2Q;
- /*
- * subtract int-wise
- */
- _len2Q = _len2-2;
- while (_index < _len2Q) {
- /* do not combine the expression below (may lead to unsigned result on some machines */
- _diff = ((unsigned int *)(_myDigits+_index-1))[0];
- _diff -= ((unsigned int *)(_otherDigits+_index-1))[0];
- _diff -= _borrow;
- if (_diff >= 0) {
- _borrow = 0;
- } else {
- _borrow = 1;
- /* _diff += 0x10000; */
- }
- ((unsigned int *)(_myDigits+_index-1))[0] = _diff;
- anyBitNonZero |= (_diff & 0xFFFFFFFFL);
- _index += 4;
- }
- }
+ {
+ int _len2Q;
+ /*
+ * subtract int-wise
+ */
+ _len2Q = _len2-2;
+ while (_index < _len2Q) {
+ /* do not combine the expression below (may lead to unsigned result on some machines */
+ _diff = ((unsigned int *)(_myDigits+_index-1))[0];
+ _diff -= ((unsigned int *)(_otherDigits+_index-1))[0];
+ _diff -= _borrow;
+ if (_diff >= 0) {
+ _borrow = 0;
+ } else {
+ _borrow = 1;
+ /* _diff += 0x10000; */
+ }
+ ((unsigned int *)(_myDigits+_index-1))[0] = _diff;
+ anyBitNonZero |= (_diff & 0xFFFFFFFFL);
+ _index += 4;
+ }
+ }
# endif
- /*
- * subtract short-wise
- */
- while (_index < _len2) {
- /* do not combine the expression below (may lead to unsigned result on some machines */
- _diff = ((unsigned short *)(_myDigits+_index-1))[0];
- _diff -= ((unsigned short *)(_otherDigits+_index-1))[0];
- _diff -= _borrow;
- if (_diff >= 0) {
- _borrow = 0;
- } else {
- _borrow = 1;
- /* _diff += 0x10000; */
- }
- ((unsigned short *)(_myDigits+_index-1))[0] = _diff;
- anyBitNonZero |= (_diff & 0xFFFF);
- _index += 2;
- }
-
- if (_index <= _len2) {
- /*
- * cannot continue with shorts - there is an odd number of
- * bytes in the minuent
- */
- } else {
- while (_index < _len1) {
- /* do not combine the expression below (may lead to unsigned result on some machines */
- _diff = ((unsigned short *)(_myDigits+_index-1))[0];
- _diff -= _borrow;
- if (_diff >= 0) {
- /* _borrow = 0; */
- ((unsigned short *)(_myDigits+_index-1))[0] = _diff;
- anyBitNonZero |= (_diff & 0xFFFF);
- _index += 2;
- while (_index < _len1) {
- anyBitNonZero |= ((unsigned short *)(_myDigits+_index-1))[0];
- if (anyBitNonZero) {
- RETURN (true);
- }
- _index += 2;
- }
- /* last odd index */
- if (_index <= _len1) {
- anyBitNonZero |= _myDigits[_index - 1];;
- if (anyBitNonZero) {
- RETURN (true);
- }
- _index++;
- }
- RETURN (anyBitNonZero ? true : false);
- }
- _borrow = 1;
- /* _diff += 0x10000; */
-
- ((unsigned short *)(_myDigits+_index-1))[0] = _diff;
- anyBitNonZero |= (_diff & 0xFFFF);
- _index += 2;
- }
- }
+ /*
+ * subtract short-wise
+ */
+ while (_index < _len2) {
+ /* do not combine the expression below (may lead to unsigned result on some machines */
+ _diff = ((unsigned short *)(_myDigits+_index-1))[0];
+ _diff -= ((unsigned short *)(_otherDigits+_index-1))[0];
+ _diff -= _borrow;
+ if (_diff >= 0) {
+ _borrow = 0;
+ } else {
+ _borrow = 1;
+ /* _diff += 0x10000; */
+ }
+ ((unsigned short *)(_myDigits+_index-1))[0] = _diff;
+ anyBitNonZero |= (_diff & 0xFFFF);
+ _index += 2;
+ }
+
+ if (_index <= _len2) {
+ /*
+ * cannot continue with shorts - there is an odd number of
+ * bytes in the minuent
+ */
+ } else {
+ while (_index < _len1) {
+ /* do not combine the expression below (may lead to unsigned result on some machines */
+ _diff = ((unsigned short *)(_myDigits+_index-1))[0];
+ _diff -= _borrow;
+ if (_diff >= 0) {
+ /* _borrow = 0; */
+ ((unsigned short *)(_myDigits+_index-1))[0] = _diff;
+ anyBitNonZero |= (_diff & 0xFFFF);
+ _index += 2;
+ while (_index < _len1) {
+ anyBitNonZero |= ((unsigned short *)(_myDigits+_index-1))[0];
+ if (anyBitNonZero) {
+ RETURN (true);
+ }
+ _index += 2;
+ }
+ /* last odd index */
+ if (_index <= _len1) {
+ anyBitNonZero |= _myDigits[_index - 1];;
+ if (anyBitNonZero) {
+ RETURN (true);
+ }
+ _index++;
+ }
+ RETURN (anyBitNonZero ? true : false);
+ }
+ _borrow = 1;
+ /* _diff += 0x10000; */
+
+ ((unsigned short *)(_myDigits+_index-1))[0] = _diff;
+ anyBitNonZero |= (_diff & 0xFFFF);
+ _index += 2;
+ }
+ }
#endif
- /*
- * subtract byte-wise
- */
- while (_index <= _len2) {
- /* do not combine the expression below (may lead to unsigned result on some machines */
- _diff = _myDigits[_index - 1];
- _diff -= _otherDigits[_index - 1];
- _diff -= _borrow;
- if (_diff >= 0) {
- _borrow = 0;
- } else {
- _borrow = 1;
- /* _diff += 0x100; */
- }
- _myDigits[_index - 1] = _diff;
- anyBitNonZero |= (_diff & 0xFF);
- _index++;
- }
-
- while (_index <= _len1) {
- /* do not combine the expression below (may lead to unsigned result on some machines */
- _diff = _myDigits[_index - 1];
- _diff -= _borrow;
- if (_diff >= 0) {
- /* _borrow = 0; */
- _myDigits[_index - 1] = _diff;
- anyBitNonZero |= (_diff & 0xFF);
- _index++;
- while (_index <= _len1) {
- anyBitNonZero |= _myDigits[_index - 1];
- if (anyBitNonZero) {
- RETURN (true);
- }
- _index++;
- }
- break;
- }
- _borrow = 1;
- /* _diff += 0x100; */
-
- _myDigits[_index - 1] = _diff;
- anyBitNonZero |= (_diff & 0xFF);
- _index++;
- }
- RETURN (anyBitNonZero ? true : false);
+ /*
+ * subtract byte-wise
+ */
+ while (_index <= _len2) {
+ /* do not combine the expression below (may lead to unsigned result on some machines */
+ _diff = _myDigits[_index - 1];
+ _diff -= _otherDigits[_index - 1];
+ _diff -= _borrow;
+ if (_diff >= 0) {
+ _borrow = 0;
+ } else {
+ _borrow = 1;
+ /* _diff += 0x100; */
+ }
+ _myDigits[_index - 1] = _diff;
+ anyBitNonZero |= (_diff & 0xFF);
+ _index++;
+ }
+
+ while (_index <= _len1) {
+ /* do not combine the expression below (may lead to unsigned result on some machines */
+ _diff = _myDigits[_index - 1];
+ _diff -= _borrow;
+ if (_diff >= 0) {
+ /* _borrow = 0; */
+ _myDigits[_index - 1] = _diff;
+ anyBitNonZero |= (_diff & 0xFF);
+ _index++;
+ while (_index <= _len1) {
+ anyBitNonZero |= _myDigits[_index - 1];
+ if (anyBitNonZero) {
+ RETURN (true);
+ }
+ _index++;
+ }
+ break;
+ }
+ _borrow = 1;
+ /* _diff += 0x100; */
+
+ _myDigits[_index - 1] = _diff;
+ anyBitNonZero |= (_diff & 0xFF);
+ _index++;
+ }
+ RETURN (anyBitNonZero ? true : false);
}
%}.
@@ -4986,26 +5020,26 @@
borrow := 0.
[index <= len1] whileTrue:[
- diff := borrow.
- diff := diff + (digitByteArray basicAt:index).
- index <= len2 ifTrue:[
- diff := diff - (otherDigitByteArray basicAt:index).
- ].
-
- "/ workaround for
- "/ gcc code generator bug
-
- (diff >= 0) ifTrue:[
- borrow := 0
- ] ifFalse:[
- borrow := -1.
- diff := diff + 16r100
- ].
- diff ~~ 0 ifTrue:[
- notZero := true
- ].
- digitByteArray basicAt:index put:diff.
- index := index + 1
+ diff := borrow.
+ diff := diff + (digitByteArray basicAt:index).
+ index <= len2 ifTrue:[
+ diff := diff - (otherDigitByteArray basicAt:index).
+ ].
+
+ "/ workaround for
+ "/ gcc code generator bug
+
+ (diff >= 0) ifTrue:[
+ borrow := 0
+ ] ifFalse:[
+ borrow := -1.
+ diff := diff + 16r100
+ ].
+ diff ~~ 0 ifTrue:[
+ notZero := true
+ ].
+ digitByteArray basicAt:index put:diff.
+ index := index + 1
].
^ notZero
@@ -5026,103 +5060,103 @@
OBJ __digits = __INST(digitByteArray);
if (__isByteArray(__digits)) {
- int __nBytes = __byteArraySize(__digits);
- unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element;
- unsigned INT __this, __next;
- int __idx;
-
- if (__nBytes == 1) {
- __bp[0] >>= 1;
- RETURN (self);
- }
-
- __idx = 1;
-
-#if defined(__LSBFIRST__)
+ int __nBytes = __byteArraySize(__digits);
+ unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element;
+ unsigned INT __this, __next;
+ int __idx;
+
+ if (__nBytes == 1) {
+ __bp[0] >>= 1;
+ RETURN (self);
+ }
+
+ __idx = 1;
+
+#if defined(__LSBFIRST__)
# if (__POINTER_SIZE__ == 8)
- if (sizeof(unsigned INT) == 8) {
- int __endIndex = __nBytes - 8;
-
- if (__idx < __endIndex) {
- __this = ((unsigned INT *)__bp)[0];
-
- while (__idx < __endIndex) {
- __next = ((unsigned INT *)__bp)[1];
- __this = (__this >> 1) /* & 0x7FFFFFFFFFFFFFF */;
- __this |= __next << 63;
- ((unsigned INT *)__bp)[0] = __this;
- __this = __next;
- __bp += 8;
- __idx += 8;
- }
- }
-
- if (__idx < (__nBytes - 4)) {
- __this = ((unsigned int *)__bp)[0];
-
- __next = ((unsigned int *)__bp)[1];
- __this = (__this >> 1) /* & 0x7FFFFFF */;
- __this |= __next << 31;
- ((unsigned int *)__bp)[0] = __this;
- __this = __next;
- __bp += 4;
- __idx += 4;
- }
- if (__idx < (__nBytes - 2)) {
- __this = ((unsigned short *)__bp)[0];
-
- __next = ((unsigned short *)__bp)[1];
- __this = (__this >> 1) /* & 0x7FFFFFF */;
- __this |= __next << 15;
- ((unsigned short *)__bp)[0] = __this;
- __this = __next;
- __bp += 2;
- __idx += 2;
- }
- }
+ if (sizeof(unsigned INT) == 8) {
+ int __endIndex = __nBytes - 8;
+
+ if (__idx < __endIndex) {
+ __this = ((unsigned INT *)__bp)[0];
+
+ while (__idx < __endIndex) {
+ __next = ((unsigned INT *)__bp)[1];
+ __this = (__this >> 1) /* & 0x7FFFFFFFFFFFFFF */;
+ __this |= __next << 63;
+ ((unsigned INT *)__bp)[0] = __this;
+ __this = __next;
+ __bp += 8;
+ __idx += 8;
+ }
+ }
+
+ if (__idx < (__nBytes - 4)) {
+ __this = ((unsigned int *)__bp)[0];
+
+ __next = ((unsigned int *)__bp)[1];
+ __this = (__this >> 1) /* & 0x7FFFFFF */;
+ __this |= __next << 31;
+ ((unsigned int *)__bp)[0] = __this;
+ __this = __next;
+ __bp += 4;
+ __idx += 4;
+ }
+ if (__idx < (__nBytes - 2)) {
+ __this = ((unsigned short *)__bp)[0];
+
+ __next = ((unsigned short *)__bp)[1];
+ __this = (__this >> 1) /* & 0x7FFFFFF */;
+ __this |= __next << 15;
+ ((unsigned short *)__bp)[0] = __this;
+ __this = __next;
+ __bp += 2;
+ __idx += 2;
+ }
+ }
# else
- if (sizeof(unsigned int) == 4) {
- int __endIndex = __nBytes - 4;
-
- if (__idx < __endIndex) {
- __this = ((unsigned int *)__bp)[0];
-
- while (__idx < __endIndex) {
- __next = ((unsigned int *)__bp)[1];
- __this = (__this >> 1) /* & 0x7FFFFFF */;
- __this |= __next << 31;
- ((unsigned int *)__bp)[0] = __this;
- __this = __next;
- __bp += 4;
- __idx += 4;
- }
- }
- }
+ if (sizeof(unsigned int) == 4) {
+ int __endIndex = __nBytes - 4;
+
+ if (__idx < __endIndex) {
+ __this = ((unsigned int *)__bp)[0];
+
+ while (__idx < __endIndex) {
+ __next = ((unsigned int *)__bp)[1];
+ __this = (__this >> 1) /* & 0x7FFFFFF */;
+ __this |= __next << 31;
+ ((unsigned int *)__bp)[0] = __this;
+ __this = __next;
+ __bp += 4;
+ __idx += 4;
+ }
+ }
+ }
# endif
#endif
- __this = __bp[0];
- while (__idx < __nBytes) {
- __next = __bp[1];
- __this >>= 1;
- __this |= __next << 7;
- __bp[0] = __this;
- __this = __next;
- __bp++;
- __idx++;
- }
- __bp[0] = __this >> 1;
- RETURN (self);
+ __this = __bp[0];
+ while (__idx < __nBytes) {
+ __next = __bp[1];
+ __this >>= 1;
+ __this |= __next << 7;
+ __bp[0] = __this;
+ __this = __next;
+ __bp++;
+ __idx++;
+ }
+ __bp[0] = __this >> 1;
+ RETURN (self);
}
%}.
prevBit := 0.
digitByteArray size to:1 by:-1 do:[:idx |
- |thisByte|
-
- thisByte := digitByteArray at:idx.
- digitByteArray at:idx put:((thisByte bitShift:-1) bitOr:prevBit).
- prevBit := (thisByte bitAnd:1) bitShift:7.
+ |thisByte|
+
+ thisByte := digitByteArray at:idx.
+ digitByteArray at:idx put:((thisByte bitShift:-1) bitOr:prevBit).
+ prevBit := (thisByte bitAnd:1) bitShift:7.
].
"
@@ -5146,81 +5180,81 @@
b := digitByteArray at:nBytes.
(b bitAnd:16r80) ~~ 0 ifTrue:[
- "/ need another byte
- nBytes := nBytes + 1.
- t := ByteArray uninitializedNew:nBytes.
- t replaceFrom:1 to:nBytes-1 with:digitByteArray startingAt:1.
- t at:nBytes put:0.
- digitByteArray := t.
+ "/ need another byte
+ nBytes := nBytes + 1.
+ t := ByteArray uninitializedNew:nBytes.
+ t replaceFrom:1 to:nBytes-1 with:digitByteArray startingAt:1.
+ t at:nBytes put:0.
+ digitByteArray := t.
].
%{
OBJ __digits = __INST(digitByteArray);
if (__isByteArray(__digits)) {
- int __nBytes = __intVal(nBytes);
- unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element;
- unsigned INT __carry = 0, __newCarry;
+ int __nBytes = __intVal(nBytes);
+ unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element;
+ unsigned INT __carry = 0, __newCarry;
#if defined(__LSBFIRST__)
# if (__POINTER_SIZE__ == 8)
- if (sizeof(unsigned INT) == 8) {
- while (__nBytes >= 8) {
- unsigned INT __this;
-
- __this = ((unsigned INT *)__bp)[0];
- __newCarry = (__this >> 63) /* & 1 */;
- ((unsigned INT *)__bp)[0] = (__this << 1) | __carry;
- __carry = __newCarry;
- __bp += 8;
- __nBytes -= 8;
- }
- }
+ if (sizeof(unsigned INT) == 8) {
+ while (__nBytes >= 8) {
+ unsigned INT __this;
+
+ __this = ((unsigned INT *)__bp)[0];
+ __newCarry = (__this >> 63) /* & 1 */;
+ ((unsigned INT *)__bp)[0] = (__this << 1) | __carry;
+ __carry = __newCarry;
+ __bp += 8;
+ __nBytes -= 8;
+ }
+ }
# endif
- if (sizeof(unsigned int) == 4) {
- while (__nBytes >= 4) {
- unsigned int __this;
-
- __this = ((unsigned int *)__bp)[0];
- __newCarry = (__this >> 31) /* & 1 */;
- ((unsigned int *)__bp)[0] = (__this << 1) | __carry;
- __carry = __newCarry;
- __bp += 4;
- __nBytes -= 4;
- }
- }
- if (__nBytes >= 2) {
- unsigned short __this;
-
- __this = ((unsigned short *)__bp)[0];
- __newCarry = (__this >> 15) /* & 1 */;
- ((unsigned short *)__bp)[0] = (__this << 1) | __carry;
- __carry = __newCarry;
- __bp += 2;
- __nBytes -= 2;
- }
+ if (sizeof(unsigned int) == 4) {
+ while (__nBytes >= 4) {
+ unsigned int __this;
+
+ __this = ((unsigned int *)__bp)[0];
+ __newCarry = (__this >> 31) /* & 1 */;
+ ((unsigned int *)__bp)[0] = (__this << 1) | __carry;
+ __carry = __newCarry;
+ __bp += 4;
+ __nBytes -= 4;
+ }
+ }
+ if (__nBytes >= 2) {
+ unsigned short __this;
+
+ __this = ((unsigned short *)__bp)[0];
+ __newCarry = (__this >> 15) /* & 1 */;
+ ((unsigned short *)__bp)[0] = (__this << 1) | __carry;
+ __carry = __newCarry;
+ __bp += 2;
+ __nBytes -= 2;
+ }
#endif /* LSBFIRST */
- while (__nBytes) {
- unsigned char __this;
-
- __this = __bp[0];
- __newCarry = (__this >> 7) /* & 1 */;
- __bp[0] = (__this << 1) | __carry;
- __carry = __newCarry;
- __bp++;
- __nBytes--;
- }
- RETURN (self);
+ while (__nBytes) {
+ unsigned char __this;
+
+ __this = __bp[0];
+ __newCarry = (__this >> 7) /* & 1 */;
+ __bp[0] = (__this << 1) | __carry;
+ __carry = __newCarry;
+ __bp++;
+ __nBytes--;
+ }
+ RETURN (self);
}
%}.
prevBit := 0.
1 to:digitByteArray size do:[:idx |
- |thisByte|
-
- thisByte := digitByteArray at:idx.
- digitByteArray at:idx put:(((thisByte bitShift:1) bitAnd:16rFF) bitOr:prevBit).
- prevBit := (thisByte bitShift:-7) bitAnd:1.
+ |thisByte|
+
+ thisByte := digitByteArray at:idx.
+ digitByteArray at:idx put:(((thisByte bitShift:1) bitAnd:16rFF) bitOr:prevBit).
+ prevBit := (thisByte bitShift:-7) bitAnd:1.
].
"
@@ -5308,10 +5342,10 @@
!LargeInteger class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.218 2014-06-03 07:37:15 stefan Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.219 2014-11-05 22:35:20 cg Exp $'
!
version_CVS
- ^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.218 2014-06-03 07:37:15 stefan Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.219 2014-11-05 22:35:20 cg Exp $'
! !