--- a/LargeInteger.st Mon Feb 24 12:48:45 2014 +0100
+++ b/LargeInteger.st Mon Feb 24 15:38:03 2014 +0100
@@ -688,43 +688,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:[
@@ -739,7 +739,7 @@
"/ ]
].
"/ cg - can share the digits ...
- newNumber := LargeInteger digitBytes:digitByteArray.
+ newNumber := self class digitBytes:digitByteArray.
newNumber sign:(sign negated).
^ newNumber
!
@@ -1093,7 +1093,7 @@
newBytes := digitByteArray copy.
newBytes bitXorBytesFrom:1 to:len1 with:anInteger digits startingAt:1
].
- ^ (LargeInteger digitBytes:newBytes) compressed
+ ^ (self class digitBytes:newBytes) compressed
"
(16r112233445566778899 bitXor:16rFF ) printStringRadix:16 '112233445566778866'
@@ -1921,6 +1921,15 @@
^ 40
!
+nilAllInstvars
+ digitByteArray nilAllInstvars.
+ digitByteArray := sign := nil.
+
+ "
+ 100 factorial nilAllInstvars
+ "
+!
+
value:aSmallInteger
"setup my contents to represent the same value as aSmallInteger.
This method will fail, if the argument is not a smallInteger.
@@ -2643,22 +2652,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 := LargeInteger digitBytes:digitByteArray copy. "/ self simpleDeepCopy sign:1.
+ 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 := LargeInteger 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).
@@ -2666,14 +2675,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
@@ -2796,12 +2805,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.
@@ -2816,76 +2825,76 @@
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;
+ 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]));
+ t = *((unsigned short *)(&digitBytes[index]));
#else
- t = digitBytes[index+1];
- t = (t << 8) | digitBytes[index];
+ t = digitBytes[index+1];
+ t = (t << 8) | digitBytes[index];
#endif
- t = t | (rest << 16);
- div = t / divisor;
- rest = t % divisor;
+ t = t | (rest << 16);
+ div = t / divisor;
+ rest = t % divisor;
#if defined(__LSBFIRST__)
- *((unsigned short *)(&resultBytes[index])) = (div & 0xFFFF);
+ *((unsigned short *)(&resultBytes[index])) = (div & 0xFFFF);
#else
- resultBytes[index+1] = div >> 8;
- resultBytes[index] = div /* & 0xFF */;
+ 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;
+ }
+ }
+ }
+ 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;
}
%}.
"
@@ -2893,7 +2902,7 @@
(could also do a primitiveFailure here)
"
ok ifFalse:[
- ^ self absDivMod:(LargeInteger value:aPositiveSmallInteger).
+ ^ self absDivMod:(self class value:aPositiveSmallInteger).
].
^ Array with:(result compressed) with:prevRest
@@ -2925,7 +2934,7 @@
((aSmallInteger < (SmallInteger minVal + 255))
or:[aSmallInteger > (SmallInteger maxVal - 255)]) ifTrue:[
- ^ self absMinus:(LargeInteger value:aSmallInteger) sign:newSign.
+ ^ self absMinus:(self class value:aSmallInteger) sign:newSign.
].
len := digitByteArray size.
@@ -2940,123 +2949,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
@@ -3091,7 +3100,7 @@
((aSmallInteger < (SmallInteger minVal + 255))
or:[aSmallInteger > (SmallInteger maxVal - 255)]) ifTrue:[
- ^ self absPlus:(LargeInteger value:aSmallInteger) sign:newSign.
+ ^ self absPlus:(self class value:aSmallInteger) sign:newSign.
].
len := rsltLen := digitByteArray size.
@@ -3100,18 +3109,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
%}
].
@@ -3124,307 +3133,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
@@ -3935,34 +3944,34 @@
shift "{ Class: SmallInteger }" |
anInteger == 0 ifTrue:[
- ^ ZeroDivide raiseRequestWith:thisContext
+ ^ ZeroDivide raiseRequestWith:thisContext
].
self = anInteger ifTrue:[
- ^ 0
+ ^ 0
].
shift := self highBit - anInteger highBit.
- dividend := LargeInteger digitBytes:digitByteArray copy. "/ self simpleDeepCopy sign:1.
+ 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
@@ -3995,7 +4004,7 @@
otherDigitByteArray := aLargeInteger digitBytes.
len2 := otherDigitByteArray size.
- result := LargeInteger basicNew numberOfDigits:(len1 + len2).
+ result := self class basicNew numberOfDigits:(len1 + len2).
resultDigitByteArray := result digitBytes.
ok := false.
%{
@@ -5299,10 +5308,10 @@
!LargeInteger class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.216 2014-01-25 21:21:15 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.217 2014-02-24 14:38:03 stefan Exp $'
!
version_CVS
- ^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.216 2014-01-25 21:21:15 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.217 2014-02-24 14:38:03 stefan Exp $'
! !