# HG changeset patch # User Claus Gittinger # Date 1454340869 -3600 # Node ID e2d82660b8558992ab84c7f7770c9027d945f975 # Parent eaa91cb0ef1b00fab091673598a194fb2ea5252d #FEATURE class: LargeInteger changed: #// #absDivMod: #divMod: 64 bit changes (tuning) diff -r eaa91cb0ef1b -r e2d82660b855 LargeInteger.st --- a/LargeInteger.st Mon Feb 01 16:21:18 2016 +0100 +++ b/LargeInteger.st Mon Feb 01 16:34:29 2016 +0100 @@ -1,6 +1,6 @@ " COPYRIGHT (c) 1994 by Claus Gittinger - All Rights Reserved + All Rights Reserved This software is furnished under a license and may be used only in accordance with the terms of that license and with the @@ -14,10 +14,10 @@ "{ NameSpace: Smalltalk }" Integer subclass:#LargeInteger - instanceVariableNames:'sign digitByteArray' - classVariableNames:'UseKarazuba' - poolDictionaries:'' - category:'Magnitude-Numbers' + instanceVariableNames:'sign digitByteArray' + classVariableNames:'UseKarazuba' + poolDictionaries:'' + category:'Magnitude-Numbers' ! !LargeInteger class methodsFor:'documentation'! @@ -25,7 +25,7 @@ copyright " COPYRIGHT (c) 1994 by Claus Gittinger - All Rights Reserved + All Rights Reserved This software is furnished under a license and may be used only in accordance with the terms of that license and with the @@ -64,12 +64,12 @@ change in the future. [author:] - Claus Gittinger + Claus Gittinger [see also:] - Number - Float Fraction FixedPoint - SmallInteger + Number + Float Fraction FixedPoint + SmallInteger " ! @@ -85,7 +85,7 @@ i printCR. v := v * i. (v / i) ~= last ifTrue:[ - self halt + self halt ]. last := v. ] @@ -257,9 +257,9 @@ |digits| msb == false ifTrue:[ - digits := aByteArrayOfDigits + digits := aByteArrayOfDigits ] ifFalse:[ - digits := aByteArrayOfDigits reversed + digits := aByteArrayOfDigits reversed ]. ^ self basicNew setDigits:digits @@ -302,9 +302,9 @@ "create and return a new LargeInteger with value taken from the argument, aSmallInteger. Notice: - this should be only used internally, since such small - largeIntegers do not normally occur in the system. - (They are used by myself) + this should be only used internally, since such small + largeIntegers do not normally occur in the system. + (They are used by myself) May change/be removed without notice." ^ self basicNew value:aSmallInteger @@ -339,14 +339,14 @@ Use a special method for this case ... " ((numberClass := aNumber class) == SmallInteger) ifTrue:[ - ^ self productFromInteger:aNumber + ^ self productFromInteger:aNumber ]. " if the argument is not a largeInteger, coerce " (numberClass == self class) ifFalse:[ - ^ self retry:#* coercing:aNumber + ^ self retry:#* coercing:aNumber ]. otherSign := aNumber sign. @@ -367,22 +367,22 @@ Use a special method for this case ... " ((numberClass := aNumber class) == SmallInteger) ifTrue:[ - ^ self sumFromInteger:aNumber + ^ self sumFromInteger:aNumber ]. " if the argument is not a largeInteger, coerce " (numberClass == self class) ifFalse:[ - ^ self retry:#+ coercing:aNumber + ^ self retry:#+ coercing:aNumber ]. otherSign := aNumber sign. (sign > 0) ifTrue:[ - "I am positive" - (otherSign > 0) ifTrue:[^ self absPlus:aNumber sign:1]. - (otherSign < 0) ifTrue:[^ self absMinus:aNumber sign:1]. - ^ self + "I am positive" + (otherSign > 0) ifTrue:[^ self absPlus:aNumber sign:1]. + (otherSign < 0) ifTrue:[^ self absMinus:aNumber sign:1]. + ^ self ]. "I am negative" (otherSign > 0) ifTrue:[^ aNumber absMinus:self sign:1]. @@ -412,47 +412,47 @@ Use a special method for this case ... " ((numberClass := aNumber class) == SmallInteger) ifTrue:[ - sign > 0 ifTrue:[ - aNumber > 0 ifTrue:[ - ^ self absFastMinus:aNumber sign:1 - ]. - ^ self absFastPlus:aNumber sign:1 - ]. - aNumber > 0 ifTrue:[ - ^ self absFastPlus:aNumber sign:-1 - ]. - ^ self absFastMinus:aNumber sign:-1 + sign > 0 ifTrue:[ + aNumber > 0 ifTrue:[ + ^ self absFastMinus:aNumber sign:1 + ]. + ^ self absFastPlus:aNumber sign:1 + ]. + aNumber > 0 ifTrue:[ + ^ self absFastPlus:aNumber sign:-1 + ]. + ^ self absFastMinus:aNumber sign:-1 ]. " if the argument is not a largeInteger, coerce " (numberClass == self class) ifFalse:[ - ^ self retry:#- coercing:aNumber + ^ self retry:#- coercing:aNumber ]. otherSign := aNumber sign. (sign > 0) ifTrue:[ - "I am positive" - (otherSign > 0) ifTrue:[ - "+large - +large" - ^ self absMinus:aNumber sign:1 - ]. - (otherSign < 0) ifTrue:[ - "+large - -large" - ^ self absPlus:aNumber sign:1 - ]. - "should not happen" - ^ self + "I am positive" + (otherSign > 0) ifTrue:[ + "+large - +large" + ^ self absMinus:aNumber sign:1 + ]. + (otherSign < 0) ifTrue:[ + "+large - -large" + ^ self absPlus:aNumber sign:1 + ]. + "should not happen" + ^ self ]. "I am negative" (otherSign > 0) ifTrue:[ - "-large - +large" - ^ self absPlus:aNumber sign:-1 + "-large - +large" + ^ self absPlus:aNumber sign:-1 ]. (otherSign < 0) ifTrue:[ - "-large - -large" - ^ self absMinus:aNumber sign:-1 + "-large - -large" + ^ self absMinus:aNumber sign:-1 ]. ^ self @@ -478,10 +478,10 @@ "return the quotient of the receiver and the argument, aNumber" aNumber isInteger ifTrue:[ - ^ Fraction numerator:self denominator:aNumber + ^ Fraction numerator:self denominator:aNumber ]. aNumber isFraction ifTrue:[ - ^ Fraction numerator:(self * aNumber denominator) denominator:(aNumber numerator) + ^ Fraction numerator:(self * aNumber denominator) denominator:(aNumber numerator) ]. "this is a q&d hack - we loose lots of precision here ..." @@ -517,7 +517,7 @@ (cls == SmallInteger) ifTrue:[ abs := aNumber. abs := abs abs. - (abs between:1 and:(SmallInteger maxBytes == 8 ifTrue:16r00ffffffffff ifFalse:16r00ffffff)) ifTrue:[ + (abs between:1 and:(SmallInteger maxBytes == 8 ifTrue:16r0fffffffffff ifFalse:16r00ffffff)) ifTrue:[ divMod := self absFastDivMod:abs. ] ifFalse:[ n := abs asLargeInteger. @@ -584,14 +584,14 @@ The returned remainder has the same sign as aNumber. The following is always true: - (receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver + (receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver Be careful with negative results: 9 // 4 -> 2, while -9 // 4 -> -3. Especially surprising: - -1 \\ 10 -> 9 (because -(1/10) is truncated towards next smaller integer, which is -1, - and -1 multiplied by 10 gives -10, so we have to add 9 to get the original -1). - -10 \\ 3 -> 2 (because -(10/3) is truncated towards next smaller integer, which is -4, - and -4 * 4 gives -12, so we need to add 2 to get the original -10. + -1 \\ 10 -> 9 (because -(1/10) is truncated towards next smaller integer, which is -1, + and -1 multiplied by 10 gives -10, so we have to add 9 to get the original -1). + -10 \\ 3 -> 2 (because -(10/3) is truncated towards next smaller integer, which is -4, + and -4 * 4 gives -12, so we need to add 2 to get the original -10. See #rem: which is the corresponding remainder for division via #quo:. @@ -600,11 +600,11 @@ |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. ]. " @@ -612,33 +612,33 @@ Use a special method for this case ... " (aNumber class == SmallInteger) ifTrue:[ - (abs between:1 and:(SmallInteger maxBytes == 8 ifTrue:16r00ffffffffff ifFalse: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 setSign:-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 setSign:-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 @@ -683,17 +683,17 @@ divMod:aNumber "return an array filled with - (self // aNumber) and (self \\ aNumber). + (self // aNumber) and (self \\ aNumber). The returned remainder has the same sign as aNumber. The following is always true: - (receiver // something) * something + (receiver \\ something) = receiver + (receiver // something) * something + (receiver \\ something) = receiver Be careful with negative results: 9 // 4 -> 2, while -9 // 4 -> -3. Especially surprising: - -1 \\ 10 -> 9 (because -(1/10) is truncated towards next smaller integer, which is -1, - and -1 multiplied by 10 gives -10, so we have to add 9 to get the original -1). - -10 \\ 3 -> 2 (because -(10/3) is truncated towards next smaller integer, which is -4, - and -4 * 4 gives -12, so we need to add 2 to get the original -10. + -1 \\ 10 -> 9 (because -(1/10) is truncated towards next smaller integer, which is -1, + and -1 multiplied by 10 gives -10, so we have to add 9 to get the original -1). + -10 \\ 3 -> 2 (because -(10/3) is truncated towards next smaller integer, which is -4, + and -4 * 4 gives -12, so we need to add 2 to get the original -10. This is redefined here for more performance (as the remainder is generated as a side effect of division)" @@ -701,25 +701,25 @@ |cls n| ((self < 0) or:[aNumber <= 0]) ifTrue:[ - ^ super divMod:aNumber + ^ super divMod:aNumber ]. "/ only handle the common case here 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:(SmallInteger maxBytes == 8 ifTrue:16r00ffffffffff ifFalse: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:16r00ffffffffffff 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. @@ -825,7 +825,7 @@ The results sign is negative if the receiver has a sign different from the args sign. The following is always true: - (receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver + (receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver " |otherSign quo abs "{ Class: SmallInteger }" | @@ -837,26 +837,26 @@ Use a special method for this case ... " (aNumber class == SmallInteger) ifTrue:[ - abs := aNumber. - abs := abs abs. - (abs between:1 and:16r00ffffff) ifTrue:[ - quo := (self absFastDivMod:abs) at:1. - (sign == otherSign) ifTrue:[^ quo]. - ^ quo setSign:-1 - ] + abs := aNumber. + abs := abs abs. + (abs between:1 and:16r00ffffff) ifTrue:[ + quo := (self absFastDivMod:abs) at:1. + (sign == otherSign) ifTrue:[^ quo]. + ^ quo setSign:-1 + ] ]. " if the argument is not a largeInteger, coerce " (aNumber class == self class) ifFalse:[ - ^ self retry:#quo: coercing:aNumber + ^ self retry:#quo: coercing:aNumber ]. sign < 0 ifTrue:[ - (sign == otherSign) ifTrue:[^ (self absDivMod:aNumber negated) at:1]. + (sign == otherSign) ifTrue:[^ (self absDivMod:aNumber negated) at:1]. ] ifFalse:[ - (sign == otherSign) ifTrue:[^ (self absDivMod:aNumber) at:1]. + (sign == otherSign) ifTrue:[^ (self absDivMod:aNumber) at:1]. ]. ^ ((self absDivMod:aNumber) at:1) setSign:-1 @@ -890,7 +890,7 @@ "return the remainder of division of the receiver by the argument, aNumber. The returned remainder has the same sign as the receiver. The following is always true: - (receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver + (receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver " |rem abs "{ Class: SmallInteger }" | @@ -900,26 +900,26 @@ Use special code for this case ... " (aNumber class == SmallInteger) ifTrue:[ - abs := aNumber. - abs := abs abs. - (abs between:1 and:16r00ffffff) ifTrue:[ - rem := (self absFastDivMod:abs) at:2. - ] ifFalse:[ - rem := self absMod:abs asLargeInteger - ]. + abs := aNumber. + abs := abs abs. + (abs between:1 and: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:#rem coercing:aNumber - ]. - - rem := self absMod:aNumber + " + if the argument is not a largeInteger, coerce + " + (aNumber class == self class) ifFalse:[ + ^ self retry:#rem coercing:aNumber + ]. + + rem := self absMod:aNumber ]. sign < 0 ifTrue:[ - ^ rem setSign:-1 + ^ rem setSign:-1 ]. ^ rem @@ -956,139 +956,139 @@ %{ /* NOCONTEXT */ if (__isSmallInteger(anInteger)) { - INT v2 = __intVal(anInteger); - INT v1; + INT v2 = __intVal(anInteger); + INT v1; #if defined(__LSBFIRST__) - v1 = *(INT *)(__byteArrayVal(__INST(digitByteArray))); + v1 = *(INT *)(__byteArrayVal(__INST(digitByteArray))); #else - unsigned char *digits = (unsigned char *)(__byteArrayVal(__INST(digitByteArray))); - - v1 = digits[0] & 0xFF; - v1 = v1 | ((digits[1] & 0xFF)<<8); - v1 = v1 | ((digits[2] & 0xFF)<<16); - v1 = v1 | ((digits[3] & 0xFF)<<24); + unsigned char *digits = (unsigned char *)(__byteArrayVal(__INST(digitByteArray))); + + v1 = digits[0] & 0xFF; + v1 = v1 | ((digits[1] & 0xFF)<<8); + v1 = v1 | ((digits[2] & 0xFF)<<16); + v1 = v1 | ((digits[3] & 0xFF)<<24); # if (__POINTER_SIZE__ == 8) - v1 = v1 | ((digits[4] & 0xFF)<<32); - v1 = v1 | ((digits[5] & 0xFF)<<40); - v1 = v1 | ((digits[6] & 0xFF)<<48); - v1 = v1 | ((digits[7] & 0xFF)<<56); + v1 = v1 | ((digits[4] & 0xFF)<<32); + v1 = v1 | ((digits[5] & 0xFF)<<40); + v1 = v1 | ((digits[6] & 0xFF)<<48); + v1 = v1 | ((digits[7] & 0xFF)<<56); #endif #endif - RETURN ( __mkSmallInteger(v1 & v2) ); + RETURN ( __mkSmallInteger(v1 & v2) ); } if (__isLargeInteger(anInteger)) { - OBJ _myDigitByteArray = __INST(digitByteArray); - OBJ _otherDigitByteArray = __LargeIntegerInstPtr(anInteger)->l_digits; - - if (__isByteArray(_myDigitByteArray) - && __isByteArray(_otherDigitByteArray)) { - unsigned char *pDigits1, *pDigits2; - int size1, size2, minSize; - union { - double d; // force align - unsigned char chars[2048+8]; - } buffer; - unsigned char *pRslt; - OBJ newDigits, newLarge; - - pDigits1 = (unsigned char *)(__byteArrayVal(_myDigitByteArray)); - pDigits2 = (unsigned char *)(__byteArrayVal(_otherDigitByteArray)); - pRslt = (void *)(buffer.chars); - - size1 = __byteArraySize(_myDigitByteArray); - size2 = __byteArraySize(_otherDigitByteArray); - minSize = (size1 < size2) ? size1 : size2; - if (minSize <= sizeof(buffer.chars)) { - int n = minSize; - - /* not worth it - but a nice try and first testbed for mmx... */ + OBJ _myDigitByteArray = __INST(digitByteArray); + OBJ _otherDigitByteArray = __LargeIntegerInstPtr(anInteger)->l_digits; + + if (__isByteArray(_myDigitByteArray) + && __isByteArray(_otherDigitByteArray)) { + unsigned char *pDigits1, *pDigits2; + int size1, size2, minSize; + union { + double d; // force align + unsigned char chars[2048+8]; + } buffer; + unsigned char *pRslt; + OBJ newDigits, newLarge; + + pDigits1 = (unsigned char *)(__byteArrayVal(_myDigitByteArray)); + pDigits2 = (unsigned char *)(__byteArrayVal(_otherDigitByteArray)); + pRslt = (void *)(buffer.chars); + + size1 = __byteArraySize(_myDigitByteArray); + size2 = __byteArraySize(_otherDigitByteArray); + minSize = (size1 < size2) ? size1 : size2; + if (minSize <= sizeof(buffer.chars)) { + int n = minSize; + + /* not worth it - but a nice try and first testbed for mmx... */ #define x__USE_MMX__ #ifdef __USE_MMX__ #ifdef __VISUALC__ - if (((INT)pRslt & 7) == 0) { // 8-byte aligned - if (((INT)pDigits1 & 7) == ((INT)pDigits2 & 7)) { // same align - while (((INT)pDigits1 & 7) && (n >= sizeof(int))) { - ((int *)pRslt)[0] = ((int *)pDigits1)[0] & ((int *)pDigits2)[0]; - pRslt += sizeof(int); - pDigits1 += sizeof(int); - pDigits2 += sizeof(int); - pDigits2 += sizeof(int); - n -= sizeof(int); - } - for (; n >= 8; n -= 8) { - __asm { - mov eax, pDigits1 - movq mm0, [eax] - mov eax, pDigits2 - movq mm1, [eax] - pand mm0, mm1 - mov eax, pRslt - movq [eax], mm0 - } - pDigits1 += 8; - pDigits2 += 8; - pRslt += 8; - } - __asm { - emms ; switch back to FPU state. - } - } - } + if (((INT)pRslt & 7) == 0) { // 8-byte aligned + if (((INT)pDigits1 & 7) == ((INT)pDigits2 & 7)) { // same align + while (((INT)pDigits1 & 7) && (n >= sizeof(int))) { + ((int *)pRslt)[0] = ((int *)pDigits1)[0] & ((int *)pDigits2)[0]; + pRslt += sizeof(int); + pDigits1 += sizeof(int); + pDigits2 += sizeof(int); + pDigits2 += sizeof(int); + n -= sizeof(int); + } + for (; n >= 8; n -= 8) { + __asm { + mov eax, pDigits1 + movq mm0, [eax] + mov eax, pDigits2 + movq mm1, [eax] + pand mm0, mm1 + mov eax, pRslt + movq [eax], mm0 + } + pDigits1 += 8; + pDigits2 += 8; + pRslt += 8; + } + __asm { + emms ; switch back to FPU state. + } + } + } #endif /* __VISUALC__ */ #endif /* __USE_MMX__ */ - for (; n >= sizeof(INT)*4; n -= sizeof(INT)*4) { - ((INT *)pRslt)[0] = ((INT *)pDigits1)[0] & ((INT *)pDigits2)[0]; - ((INT *)pRslt)[1] = ((INT *)pDigits1)[1] & ((INT *)pDigits2)[1]; - ((INT *)pRslt)[2] = ((INT *)pDigits1)[2] & ((INT *)pDigits2)[2]; - ((INT *)pRslt)[3] = ((INT *)pDigits1)[3] & ((INT *)pDigits2)[3]; - pRslt += sizeof(INT)*4; - pDigits1 += sizeof(INT)*4; - pDigits2 += sizeof(INT)*4; - } - for (; n >= sizeof(INT); n -= sizeof(INT)) { - ((INT *)pRslt)[0] = ((INT *)pDigits1)[0] & ((INT *)pDigits2)[0]; - pRslt += sizeof(INT); - pDigits1 += sizeof(INT); - pDigits2 += sizeof(INT); - } - for (; n > 0; n--) { - *pRslt = *pDigits1 & *pDigits2; - pRslt++; - pDigits1++; - pDigits2++; - } - // normalize - while ((pRslt[-1]==0) && (pRslt > buffer.chars)) { - pRslt--; - } - - // allocate result - n = pRslt-buffer.chars; - if (n <= sizeof(INT)) { - INT val = 0; - - // make it a smallInteger / 32bitInteger - while (n > 0) { - val = (val << 8) + buffer.chars[--n]; - } - RETURN (__MKUINT(val)); - } - newDigits = __MKBYTEARRAY(buffer.chars, n); - if (newDigits) { - __PROTECT__(newDigits); - newLarge = __STX___new(sizeof(struct __LargeInteger)); - __UNPROTECT__(newDigits); - if (newLarge) { - __InstPtr(newLarge)->o_class = LargeInteger; __STORE(newLarge, LargeInteger); - __LargeIntegerInstPtr(newLarge)->l_digits = newDigits; __STORE(newLarge, newDigits); - __LargeIntegerInstPtr(newLarge)->l_sign = __MKSMALLINT(1); - RETURN (newLarge); - } - } - } - } + for (; n >= sizeof(INT)*4; n -= sizeof(INT)*4) { + ((INT *)pRslt)[0] = ((INT *)pDigits1)[0] & ((INT *)pDigits2)[0]; + ((INT *)pRslt)[1] = ((INT *)pDigits1)[1] & ((INT *)pDigits2)[1]; + ((INT *)pRslt)[2] = ((INT *)pDigits1)[2] & ((INT *)pDigits2)[2]; + ((INT *)pRslt)[3] = ((INT *)pDigits1)[3] & ((INT *)pDigits2)[3]; + pRslt += sizeof(INT)*4; + pDigits1 += sizeof(INT)*4; + pDigits2 += sizeof(INT)*4; + } + for (; n >= sizeof(INT); n -= sizeof(INT)) { + ((INT *)pRslt)[0] = ((INT *)pDigits1)[0] & ((INT *)pDigits2)[0]; + pRslt += sizeof(INT); + pDigits1 += sizeof(INT); + pDigits2 += sizeof(INT); + } + for (; n > 0; n--) { + *pRslt = *pDigits1 & *pDigits2; + pRslt++; + pDigits1++; + pDigits2++; + } + // normalize + while ((pRslt[-1]==0) && (pRslt > buffer.chars)) { + pRslt--; + } + + // allocate result + n = pRslt-buffer.chars; + if (n <= sizeof(INT)) { + INT val = 0; + + // make it a smallInteger / 32bitInteger + while (n > 0) { + val = (val << 8) + buffer.chars[--n]; + } + RETURN (__MKUINT(val)); + } + newDigits = __MKBYTEARRAY(buffer.chars, n); + if (newDigits) { + __PROTECT__(newDigits); + newLarge = __STX___new(sizeof(struct __LargeInteger)); + __UNPROTECT__(newDigits); + if (newLarge) { + __InstPtr(newLarge)->o_class = LargeInteger; __STORE(newLarge, LargeInteger); + __LargeIntegerInstPtr(newLarge)->l_digits = newDigits; __STORE(newLarge, newDigits); + __LargeIntegerInstPtr(newLarge)->l_sign = __MKSMALLINT(1); + RETURN (newLarge); + } + } + } + } } %}. ^ super bitAnd:anInteger @@ -1117,11 +1117,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 @@ -1136,9 +1136,9 @@ bigNum1 := 2 raisedToInteger:512. bigNum2 := 2 raisedToInteger:510. Time millisecondsToRun:[ - 1000000 timesRepeat:[ - bigNum1 bitXor:bigNum2. - ] + 1000000 timesRepeat:[ + bigNum1 bitXor:bigNum2. + ] ] " ! @@ -1164,85 +1164,85 @@ * quickly advance over full 0-words */ if (__isByteArray(__digitByteArray)) { - int __sz = __byteArraySize(__digitByteArray); - unsigned char *__bP = __byteArrayVal(__digitByteArray); - unsigned char *__bP0 = __bP; - - sz = __MKSMALLINT(__sz); + int __sz = __byteArraySize(__digitByteArray); + unsigned char *__bP = __byteArrayVal(__digitByteArray); + unsigned char *__bP0 = __bP; + + sz = __MKSMALLINT(__sz); #ifdef __UNROLL_LOOPS__ - while (__sz > (sizeof(INT) * 4)) { - if (( ((INT *)__bP)[0] - | ((INT *)__bP)[1] - | ((INT *)__bP)[2] - | ((INT *)__bP)[3] ) != 0) break; - __sz -= sizeof(INT) * 4; - __bP += sizeof(INT) * 4; - } + while (__sz > (sizeof(INT) * 4)) { + if (( ((INT *)__bP)[0] + | ((INT *)__bP)[1] + | ((INT *)__bP)[2] + | ((INT *)__bP)[3] ) != 0) break; + __sz -= sizeof(INT) * 4; + __bP += sizeof(INT) * 4; + } #endif - while (__sz > sizeof(INT)) { - if ( ((INT *)__bP)[0] != 0 ) break; - __sz -= sizeof(INT); - __bP += sizeof(INT); - } - while (__sz > 0) { - unsigned int c; - - if ( (c = *__bP) != 0 ) { - int bitIdx = (__bP - __bP0) * 8; + while (__sz > sizeof(INT)) { + if ( ((INT *)__bP)[0] != 0 ) break; + __sz -= sizeof(INT); + __bP += sizeof(INT); + } + while (__sz > 0) { + unsigned int c; + + if ( (c = *__bP) != 0 ) { + int bitIdx = (__bP - __bP0) * 8; #ifdef __BSF - { - int index; - int t = c; - - index = __BSF(t); - RETURN ( __mkSmallInteger(index + 1 + bitIdx) ); - } + { + int index; + int t = c; + + index = __BSF(t); + RETURN ( __mkSmallInteger(index + 1 + bitIdx) ); + } #else - if (c & 0x0F) { - if (c & 0x03) { - if (c & 0x01) { - RETURN ( __mkSmallInteger( bitIdx + 1) ); - } else { - RETURN ( __mkSmallInteger( bitIdx + 2) ); - } - } else { - if (c & 0x04) { - RETURN ( __mkSmallInteger( bitIdx + 3) ); - } else { - RETURN ( __mkSmallInteger( bitIdx + 4) ); - } - } - } else { - if (c & 0x30) { - if (c & 0x10) { - RETURN ( __mkSmallInteger( bitIdx + 5) ); - } else { - RETURN ( __mkSmallInteger( bitIdx + 6) ); - } - } else { - if (c & 0x40) { - RETURN ( __mkSmallInteger( bitIdx + 7) ); - } else { - RETURN ( __mkSmallInteger( bitIdx + 8) ); - } - } - } + if (c & 0x0F) { + if (c & 0x03) { + if (c & 0x01) { + RETURN ( __mkSmallInteger( bitIdx + 1) ); + } else { + RETURN ( __mkSmallInteger( bitIdx + 2) ); + } + } else { + if (c & 0x04) { + RETURN ( __mkSmallInteger( bitIdx + 3) ); + } else { + RETURN ( __mkSmallInteger( bitIdx + 4) ); + } + } + } else { + if (c & 0x30) { + if (c & 0x10) { + RETURN ( __mkSmallInteger( bitIdx + 5) ); + } else { + RETURN ( __mkSmallInteger( bitIdx + 6) ); + } + } else { + if (c & 0x40) { + RETURN ( __mkSmallInteger( bitIdx + 7) ); + } else { + RETURN ( __mkSmallInteger( bitIdx + 8) ); + } + } + } #endif - break; - } - __sz--; - __bP++; - } - idx0 = __mkSmallInteger( __bP - __bP0 + 1 ); + break; + } + __sz--; + __bP++; + } + idx0 = __mkSmallInteger( __bP - __bP0 + 1 ); } %}. "/ never actually reached idx0 to:sz do:[:digitIndex | - (byte := digitByteArray at:digitIndex) ~~ 0 ifTrue:[ - ^ (digitIndex-1)*8 + (byte lowBit) - ] + (byte := digitByteArray at:digitIndex) ~~ 0 ifTrue:[ + ^ (digitIndex-1)*8 + (byte lowBit) + ] ]. ^ 0 "/ should not happen @@ -1266,24 +1266,24 @@ ((1 bitShift:64)-1) highBit 1 to:1000 do:[:idx | - self assert:(( 1 bitShift:idx) lowBit = (idx+1)). - self assert:(( 1 bitShift:idx) lowBit = ( 1 bitShift:idx) highBit). - self assert:(( 3 bitShift:idx) lowBit = (idx+1)). - self assert:(( 7 bitShift:idx) lowBit = (idx+1)). - self assert:(( 15 bitShift:idx) lowBit = (idx+1)). - self assert:(( 31 bitShift:idx) lowBit = (idx+1)). - self assert:(( 63 bitShift:idx) lowBit = (idx+1)). - self assert:(( 127 bitShift:idx) lowBit = (idx+1)). - self assert:(( 255 bitShift:idx) lowBit = (idx+1)). + self assert:(( 1 bitShift:idx) lowBit = (idx+1)). + self assert:(( 1 bitShift:idx) lowBit = ( 1 bitShift:idx) highBit). + self assert:(( 3 bitShift:idx) lowBit = (idx+1)). + self assert:(( 7 bitShift:idx) lowBit = (idx+1)). + self assert:(( 15 bitShift:idx) lowBit = (idx+1)). + self assert:(( 31 bitShift:idx) lowBit = (idx+1)). + self assert:(( 63 bitShift:idx) lowBit = (idx+1)). + self assert:(( 127 bitShift:idx) lowBit = (idx+1)). + self assert:(( 255 bitShift:idx) lowBit = (idx+1)). ] |num| num := (1 bitShift:1000). Time millisecondsToRun:[ - 1000000 timesRepeat:[ - num lowBit - ] + 1000000 timesRepeat:[ + num lowBit + ] ] " @@ -1295,29 +1295,29 @@ bitAt:anIntegerIndex "return the value of the index's bit (index starts at 1) as 0 or 1. Notice: the result of bitAt: on negative receivers is not - defined in the language standard (since the implementation - is free to choose any internal representation for integers)" + defined in the language standard (since the implementation + is free to choose any internal representation for integers)" %{ /* NOCONTEXT */ if (__isSmallInteger(anIntegerIndex)) { - INT idx = __smallIntegerVal(anIntegerIndex) - 1; - - if (idx >= 0) { - int v1; - int byteOffset = idx / 8; - int digitLen = __byteArraySize(__INST(digitByteArray)); - - if (digitLen < byteOffset) { - RETURN(__mkSmallInteger(0)); - } - - v1 = (__byteArrayVal(__INST(digitByteArray)))[byteOffset]; - if (v1 & (1 << (idx % 8))) { - RETURN(__mkSmallInteger(1)); - } else { - RETURN(__mkSmallInteger(0)); - } - } + INT idx = __smallIntegerVal(anIntegerIndex) - 1; + + if (idx >= 0) { + int v1; + int byteOffset = idx / 8; + int digitLen = __byteArraySize(__INST(digitByteArray)); + + if (digitLen < byteOffset) { + RETURN(__mkSmallInteger(0)); + } + + v1 = (__byteArrayVal(__INST(digitByteArray)))[byteOffset]; + if (v1 & (1 << (idx % 8))) { + RETURN(__mkSmallInteger(1)); + } else { + RETURN(__mkSmallInteger(0)); + } + } } %}. ^ super bitAt:anIntegerIndex @@ -1345,23 +1345,23 @@ |myDigitLength newDigitLength newDigitBytes byteIndexOfBitToSet| index <= 0 ifTrue:[ - ^ SubscriptOutOfBoundsSignal - raiseRequestWith:index - errorString:'index out of bounds' + ^ SubscriptOutOfBoundsSignal + raiseRequestWith:index + errorString:'index out of bounds' ]. myDigitLength := digitByteArray size. byteIndexOfBitToSet := ((index-1)//8)+1. byteIndexOfBitToSet > myDigitLength ifTrue:[ - newDigitLength := myDigitLength max:byteIndexOfBitToSet. - newDigitBytes := ByteArray new:newDigitLength. - newDigitBytes replaceFrom:1 to:myDigitLength with:digitByteArray startingAt:1. + newDigitLength := myDigitLength max:byteIndexOfBitToSet. + newDigitBytes := ByteArray new:newDigitLength. + newDigitBytes replaceFrom:1 to:myDigitLength with:digitByteArray startingAt:1. ] ifFalse:[ - newDigitBytes := digitByteArray copy + newDigitBytes := digitByteArray copy ]. newDigitBytes - at:byteIndexOfBitToSet - put:((newDigitBytes at:byteIndexOfBitToSet) setBit:(((index-1)\\8)+1)). + at:byteIndexOfBitToSet + put:((newDigitBytes at:byteIndexOfBitToSet) setBit:(((index-1)\\8)+1)). ^ self class digitBytes:newDigitBytes sign:sign " @@ -1408,19 +1408,19 @@ int count; if (__isSmallInteger(shiftCount)) { - count = __intVal(shiftCount); - count = count % 32; - - bits = __unsignedLongIntVal(self); - if (count > 0) { - bits = (bits << count) | (bits >> (32-count)); - } else { - bits = (bits >> (-count)) | (bits << (32-(-count))); - } + count = __intVal(shiftCount); + count = count % 32; + + bits = __unsignedLongIntVal(self); + if (count > 0) { + bits = (bits << count) | (bits >> (32-count)); + } else { + bits = (bits >> (-count)) | (bits << (32-(-count))); + } #if __POINTER_SIZE__ == 8 - bits &= 0xFFFFFFFFL; + bits &= 0xFFFFFFFFL; #endif - RETURN (__MKUINT(bits)); + RETURN (__MKUINT(bits)); } %}. ^ self primitiveFailed @@ -1446,21 +1446,21 @@ int count; if (__isSmallInteger(shiftCount)) { - count = __intVal(shiftCount); - if (count >= 32) { - RETURN (__mkSmallInteger(0)); - } - - bits = __unsignedLongIntVal(self); - if (count > 0) { - bits = bits << count; - } else { - bits = bits >> (-count); - } + count = __intVal(shiftCount); + if (count >= 32) { + RETURN (__mkSmallInteger(0)); + } + + bits = __unsignedLongIntVal(self); + if (count > 0) { + bits = bits << count; + } else { + bits = bits >> (-count); + } #if __POINTER_SIZE__ == 8 - bits &= 0xFFFFFFFFL; + bits &= 0xFFFFFFFFL; #endif - RETURN (__MKUINT(bits)); + RETURN (__MKUINT(bits)); } %}. ^ self primitiveFailed @@ -1716,7 +1716,7 @@ %}. l := digitByteArray size. [l ~~ 0 and:[(digitByteArray at:l) == 0]] whileTrue:[ - l := l - 1. + l := l - 1. ]. ^ l @@ -1818,206 +1818,206 @@ OBJ t; if (__INST(sign) == __mkSmallInteger(0)) { - RETURN (__mkSmallInteger(0)); + RETURN (__mkSmallInteger(0)); } t = __INST(digitByteArray); if (__isByteArray(t)) { - unsigned char *__digitBytes = __ByteArrayInstPtr(t)->ba_element; - int _idx, _idx0; - INT _val; - - _idx = _idx0 = __byteArraySize(t); - while ((_idx > 0) && (__digitBytes[_idx - 1] == 0)) { - _idx--; - } + unsigned char *__digitBytes = __ByteArrayInstPtr(t)->ba_element; + int _idx, _idx0; + INT _val; + + _idx = _idx0 = __byteArraySize(t); + while ((_idx > 0) && (__digitBytes[_idx - 1] == 0)) { + _idx--; + } #if __POINTER_SIZE__ == 8 - switch (_idx) { - case 8: - _val = __digitBytes[7]; - if (_val <= 0x40) { - _val = (_val<<8); - _val = (_val + __digitBytes[6]) << 8; - _val = (_val + __digitBytes[5]) << 8; - _val = (_val + __digitBytes[4]) << 8; - _val = (_val + __digitBytes[3]) << 8; - _val = (_val + __digitBytes[2]) << 8; - _val = (_val + __digitBytes[1]) << 8; - _val += __digitBytes[0]; - if (__INST(sign) == __mkSmallInteger(-1)) - _val = -_val; - if (__ISVALIDINTEGER(_val)) { - RETURN (__mkSmallInteger(_val)); - } - } - break; - case 7: + switch (_idx) { + case 8: + _val = __digitBytes[7]; + if (_val <= 0x40) { + _val = (_val<<8); + _val = (_val + __digitBytes[6]) << 8; + _val = (_val + __digitBytes[5]) << 8; + _val = (_val + __digitBytes[4]) << 8; + _val = (_val + __digitBytes[3]) << 8; + _val = (_val + __digitBytes[2]) << 8; + _val = (_val + __digitBytes[1]) << 8; + _val += __digitBytes[0]; + if (__INST(sign) == __mkSmallInteger(-1)) + _val = -_val; + if (__ISVALIDINTEGER(_val)) { + RETURN (__mkSmallInteger(_val)); + } + } + break; + case 7: # if defined(__LSBFIRST__) - _val = ((INT *)__digitBytes)[0] & 0x00FFFFFFFFFFFFFFL; + _val = ((INT *)__digitBytes)[0] & 0x00FFFFFFFFFFFFFFL; # else - _val = (__digitBytes[6]<<8); - _val = (_val + __digitBytes[5]) << 8; - _val = (_val + __digitBytes[4]) << 8; - _val = (_val + __digitBytes[3]) << 8; - _val = (_val + __digitBytes[2]) << 8; - _val = (_val + __digitBytes[1]) << 8; - _val += __digitBytes[0]; + _val = (__digitBytes[6]<<8); + _val = (_val + __digitBytes[5]) << 8; + _val = (_val + __digitBytes[4]) << 8; + _val = (_val + __digitBytes[3]) << 8; + _val = (_val + __digitBytes[2]) << 8; + _val = (_val + __digitBytes[1]) << 8; + _val += __digitBytes[0]; # endif - if (__INST(sign) == __mkSmallInteger(-1)) - _val = -_val; - RETURN (__mkSmallInteger(_val)); - case 6: + if (__INST(sign) == __mkSmallInteger(-1)) + _val = -_val; + RETURN (__mkSmallInteger(_val)); + case 6: # if defined(__LSBFIRST__) - _val = ((INT *)__digitBytes)[0] & 0x0000FFFFFFFFFFFFL; + _val = ((INT *)__digitBytes)[0] & 0x0000FFFFFFFFFFFFL; # else - _val = (__digitBytes[5]<<8); - _val = (_val + __digitBytes[4]) << 8; - _val = (_val + __digitBytes[3]) << 8; - _val = (_val + __digitBytes[2]) << 8; - _val = (_val + __digitBytes[1]) << 8; - _val += __digitBytes[0]; + _val = (__digitBytes[5]<<8); + _val = (_val + __digitBytes[4]) << 8; + _val = (_val + __digitBytes[3]) << 8; + _val = (_val + __digitBytes[2]) << 8; + _val = (_val + __digitBytes[1]) << 8; + _val += __digitBytes[0]; # endif - if (__INST(sign) == __mkSmallInteger(-1)) - _val = -_val; - RETURN (__mkSmallInteger(_val)); - case 5: + if (__INST(sign) == __mkSmallInteger(-1)) + _val = -_val; + RETURN (__mkSmallInteger(_val)); + case 5: # if defined(__LSBFIRST__) - _val = ((INT *)__digitBytes)[0] & 0x000000FFFFFFFFFFL; + _val = ((INT *)__digitBytes)[0] & 0x000000FFFFFFFFFFL; # else - _val = (__digitBytes[4]<<8); - _val = (_val + __digitBytes[3]) << 8; - _val = (_val + __digitBytes[2]) << 8; - _val = (_val + __digitBytes[1]) << 8; - _val += __digitBytes[0]; + _val = (__digitBytes[4]<<8); + _val = (_val + __digitBytes[3]) << 8; + _val = (_val + __digitBytes[2]) << 8; + _val = (_val + __digitBytes[1]) << 8; + _val += __digitBytes[0]; # endif - if (__INST(sign) == __mkSmallInteger(-1)) - _val = -_val; - RETURN (__mkSmallInteger(_val)); - case 4: + if (__INST(sign) == __mkSmallInteger(-1)) + _val = -_val; + RETURN (__mkSmallInteger(_val)); + case 4: # if defined(__LSBFIRST__) - _val = ((INT *)__digitBytes)[0] & 0x00000000FFFFFFFFL; + _val = ((INT *)__digitBytes)[0] & 0x00000000FFFFFFFFL; # else - _val = (__digitBytes[3]<<8); - _val = (_val + __digitBytes[2]) << 8; - _val = (_val + __digitBytes[1]) << 8; - _val += __digitBytes[0]; + _val = (__digitBytes[3]<<8); + _val = (_val + __digitBytes[2]) << 8; + _val = (_val + __digitBytes[1]) << 8; + _val += __digitBytes[0]; # endif - if (__INST(sign) == __mkSmallInteger(-1)) - _val = -_val; - RETURN (__mkSmallInteger(_val)); - case 3: + if (__INST(sign) == __mkSmallInteger(-1)) + _val = -_val; + RETURN (__mkSmallInteger(_val)); + case 3: # if defined(__LSBFIRST__) - _val = ((int *)__digitBytes)[0] & 0x00FFFFFF; + _val = ((int *)__digitBytes)[0] & 0x00FFFFFF; # else - _val = (__digitBytes[2]<<8); - _val = (_val + __digitBytes[1]) << 8; - _val += __digitBytes[0]; + _val = (__digitBytes[2]<<8); + _val = (_val + __digitBytes[1]) << 8; + _val += __digitBytes[0]; # endif - if (__INST(sign) == __mkSmallInteger(-1)) - _val = -_val; - RETURN (__mkSmallInteger(_val)); - case 2: + if (__INST(sign) == __mkSmallInteger(-1)) + _val = -_val; + RETURN (__mkSmallInteger(_val)); + case 2: # if defined(__LSBFIRST__) - _val = ((int *)__digitBytes)[0] & 0x0000FFFF; + _val = ((int *)__digitBytes)[0] & 0x0000FFFF; # else - _val = (__digitBytes[1]<<8) + __digitBytes[0]; + _val = (__digitBytes[1]<<8) + __digitBytes[0]; # endif - if (__INST(sign) == __mkSmallInteger(-1)) - _val = -_val; - RETURN (__mkSmallInteger(_val)); - case 1: - _val = __digitBytes[0]; - if (__INST(sign) == __mkSmallInteger(-1)) - _val = -_val; - RETURN (__mkSmallInteger(_val)); - case 0: - RETURN (__mkSmallInteger(0)); - - } + if (__INST(sign) == __mkSmallInteger(-1)) + _val = -_val; + RETURN (__mkSmallInteger(_val)); + case 1: + _val = __digitBytes[0]; + if (__INST(sign) == __mkSmallInteger(-1)) + _val = -_val; + RETURN (__mkSmallInteger(_val)); + case 0: + RETURN (__mkSmallInteger(0)); + + } #else - if (_idx <= 4) { - if (_idx <= 2) { - if (_idx == 0) { - RETURN (__mkSmallInteger(0)); - } - if (_idx == 1) { - _val = __digitBytes[0]; - if (__INST(sign) == __mkSmallInteger(-1)) - _val = -_val; - RETURN (__mkSmallInteger(_val)); - } + if (_idx <= 4) { + if (_idx <= 2) { + if (_idx == 0) { + RETURN (__mkSmallInteger(0)); + } + if (_idx == 1) { + _val = __digitBytes[0]; + if (__INST(sign) == __mkSmallInteger(-1)) + _val = -_val; + RETURN (__mkSmallInteger(_val)); + } # if defined(__LSBFIRST__) - _val = ((int *)__digitBytes)[0] & 0x0000FFFF; + _val = ((int *)__digitBytes)[0] & 0x0000FFFF; # else - _val = (__digitBytes[1]<<8) + __digitBytes[0]; + _val = (__digitBytes[1]<<8) + __digitBytes[0]; # endif - if (__INST(sign) == __mkSmallInteger(-1)) - _val = -_val; - RETURN (__mkSmallInteger(_val)); - } - if (_idx == 3) { + if (__INST(sign) == __mkSmallInteger(-1)) + _val = -_val; + RETURN (__mkSmallInteger(_val)); + } + if (_idx == 3) { # if defined(__LSBFIRST__) - _val = ((int *)__digitBytes)[0] & 0x00FFFFFF; + _val = ((int *)__digitBytes)[0] & 0x00FFFFFF; # else - _val = (((__digitBytes[2]<<8) + __digitBytes[1])<<8) + __digitBytes[0]; + _val = (((__digitBytes[2]<<8) + __digitBytes[1])<<8) + __digitBytes[0]; # endif - if (__INST(sign) == __mkSmallInteger(-1)) - _val = -_val; - RETURN (__mkSmallInteger(_val)); - } - _val = __digitBytes[3]; - if (_val <= 0x40) { + if (__INST(sign) == __mkSmallInteger(-1)) + _val = -_val; + RETURN (__mkSmallInteger(_val)); + } + _val = __digitBytes[3]; + if (_val <= 0x40) { # if defined(__LSBFIRST__) - _val = ((int *)__digitBytes)[0]; + _val = ((int *)__digitBytes)[0]; # else - _val = (((((_val<<8) + __digitBytes[2])<<8) + __digitBytes[1])<<8) + __digitBytes[0]; + _val = (((((_val<<8) + __digitBytes[2])<<8) + __digitBytes[1])<<8) + __digitBytes[0]; # endif - if (__INST(sign) == __mkSmallInteger(-1)) - _val = -_val; - if (__ISVALIDINTEGER(_val)) { - RETURN (__mkSmallInteger(_val)); - } - } - } + if (__INST(sign) == __mkSmallInteger(-1)) + _val = -_val; + if (__ISVALIDINTEGER(_val)) { + RETURN (__mkSmallInteger(_val)); + } + } + } #endif - if (_idx == _idx0) { - RETURN (self); - } - - /* - * must copy & cut off some (zero)bytes - */ - { - OBJ newDigits; - OBJ oldDigits; - - /* - * careful - there is no context here to protect - * the receiver ... - */ - __PROTECT__(self); - __PROTECT__(__INST(digitByteArray)); - newDigits = __BYTEARRAY_UNINITIALIZED_NEW_INT(_idx); - __UNPROTECT__(oldDigits); - __UNPROTECT__(self); - if (newDigits) { - bcopy(__ByteArrayInstPtr(oldDigits)->ba_element, - __ByteArrayInstPtr(newDigits)->ba_element, - _idx); - __INST(digitByteArray) = newDigits; __STORE(self, newDigits); - RETURN (self); - } - /* - * allocation failed ... - * ... fall through to trigger the error - */ - } + if (_idx == _idx0) { + RETURN (self); + } + + /* + * must copy & cut off some (zero)bytes + */ + { + OBJ newDigits; + OBJ oldDigits; + + /* + * careful - there is no context here to protect + * the receiver ... + */ + __PROTECT__(self); + __PROTECT__(__INST(digitByteArray)); + newDigits = __BYTEARRAY_UNINITIALIZED_NEW_INT(_idx); + __UNPROTECT__(oldDigits); + __UNPROTECT__(self); + if (newDigits) { + bcopy(__ByteArrayInstPtr(oldDigits)->ba_element, + __ByteArrayInstPtr(newDigits)->ba_element, + _idx); + __INST(digitByteArray) = newDigits; __STORE(self, newDigits); + RETURN (self); + } + /* + * allocation failed ... + * ... fall through to trigger the error + */ + } } %}. index0 := index := digitByteArray size. [(index > 0) and:[(digitByteArray at:index) == 0]] whileTrue:[ - index := index - 1 + index := index - 1 ]. "/ ((index < SmallInteger maxBytes) "/ or:[(index == SmallInteger maxBytes) @@ -2031,7 +2031,7 @@ "/ ^ val * sign "/ ]. (index ~~ index0) ifTrue:[ - digitByteArray := digitByteArray copyFrom:1 to:index + digitByteArray := digitByteArray copyFrom:1 to:index ]. ^ self ! @@ -2072,87 +2072,87 @@ overhead of producing any intermediate byte-arrays (and the scanning) " (aSmallInteger == 0) ifTrue: [ - digitByteArray := ByteArray with:0. - sign := 0. - ^ self + digitByteArray := ByteArray with:0. + sign := 0. + ^ self ]. (aSmallInteger < 0) ifTrue: [ - sign := -1. - absValue := aSmallInteger negated + sign := -1. + absValue := aSmallInteger negated ] ifFalse: [ - sign := 1. - absValue := aSmallInteger + sign := 1. + absValue := aSmallInteger ]. b1 := absValue bitAnd:16rFF. absValue := absValue bitShift:-8. absValue == 0 ifTrue:[ - digitByteArray := ByteArray with:b1 + digitByteArray := ByteArray with:b1 ] ifFalse:[ - b2 := absValue bitAnd:16rFF. - absValue := absValue bitShift:-8. - absValue == 0 ifTrue:[ - digitByteArray := ByteArray with:b1 with:b2 - ] ifFalse:[ - b3 := absValue bitAnd:16rFF. - absValue := absValue bitShift:-8. - absValue == 0 ifTrue:[ - digitByteArray := ByteArray with:b1 with:b2 with:b3 - ] ifFalse:[ - b4 := absValue bitAnd:16rFF. - absValue := absValue bitShift:-8. - absValue == 0 ifTrue:[ - digitByteArray := ByteArray with:b1 with:b2 with:b3 with:b4 - ] ifFalse:[ - b5 := absValue bitAnd:16rFF. - absValue := absValue bitShift:-8. - absValue == 0 ifTrue:[ - digitByteArray := ByteArray new:5. - digitByteArray at:1 put:b1. - digitByteArray at:2 put:b2. - digitByteArray at:3 put:b3. - digitByteArray at:4 put:b4. - digitByteArray at:5 put:b5. - ] ifFalse:[ - b6 := absValue bitAnd:16rFF. - absValue := absValue bitShift:-8. - absValue == 0 ifTrue:[ - digitByteArray := ByteArray new:6. - digitByteArray at:1 put:b1. - digitByteArray at:2 put:b2. - digitByteArray at:3 put:b3. - digitByteArray at:4 put:b4. - digitByteArray at:5 put:b5. - digitByteArray at:6 put:b6. - ] ifFalse:[ - b7 := absValue bitAnd:16rFF. - absValue := absValue bitShift:-8. - absValue == 0 ifTrue:[ - digitByteArray := ByteArray new:7. - digitByteArray at:1 put:b1. - digitByteArray at:2 put:b2. - digitByteArray at:3 put:b3. - digitByteArray at:4 put:b4. - digitByteArray at:5 put:b5. - digitByteArray at:6 put:b6. - digitByteArray at:7 put:b7. - ] ifFalse:[ - digitByteArray := ByteArray new:8. - digitByteArray at:1 put:b1. - digitByteArray at:2 put:b2. - digitByteArray at:3 put:b3. - digitByteArray at:4 put:b4. - digitByteArray at:5 put:b5. - digitByteArray at:6 put:b6. - digitByteArray at:7 put:b7. - digitByteArray at:8 put:absValue. - ] - ] - ] - ] - ] - ] + b2 := absValue bitAnd:16rFF. + absValue := absValue bitShift:-8. + absValue == 0 ifTrue:[ + digitByteArray := ByteArray with:b1 with:b2 + ] ifFalse:[ + b3 := absValue bitAnd:16rFF. + absValue := absValue bitShift:-8. + absValue == 0 ifTrue:[ + digitByteArray := ByteArray with:b1 with:b2 with:b3 + ] ifFalse:[ + b4 := absValue bitAnd:16rFF. + absValue := absValue bitShift:-8. + absValue == 0 ifTrue:[ + digitByteArray := ByteArray with:b1 with:b2 with:b3 with:b4 + ] ifFalse:[ + b5 := absValue bitAnd:16rFF. + absValue := absValue bitShift:-8. + absValue == 0 ifTrue:[ + digitByteArray := ByteArray new:5. + digitByteArray at:1 put:b1. + digitByteArray at:2 put:b2. + digitByteArray at:3 put:b3. + digitByteArray at:4 put:b4. + digitByteArray at:5 put:b5. + ] ifFalse:[ + b6 := absValue bitAnd:16rFF. + absValue := absValue bitShift:-8. + absValue == 0 ifTrue:[ + digitByteArray := ByteArray new:6. + digitByteArray at:1 put:b1. + digitByteArray at:2 put:b2. + digitByteArray at:3 put:b3. + digitByteArray at:4 put:b4. + digitByteArray at:5 put:b5. + digitByteArray at:6 put:b6. + ] ifFalse:[ + b7 := absValue bitAnd:16rFF. + absValue := absValue bitShift:-8. + absValue == 0 ifTrue:[ + digitByteArray := ByteArray new:7. + digitByteArray at:1 put:b1. + digitByteArray at:2 put:b2. + digitByteArray at:3 put:b3. + digitByteArray at:4 put:b4. + digitByteArray at:5 put:b5. + digitByteArray at:6 put:b6. + digitByteArray at:7 put:b7. + ] ifFalse:[ + digitByteArray := ByteArray new:8. + digitByteArray at:1 put:b1. + digitByteArray at:2 put:b2. + digitByteArray at:3 put:b3. + digitByteArray at:4 put:b4. + digitByteArray at:5 put:b5. + digitByteArray at:6 put:b6. + digitByteArray at:7 put:b7. + digitByteArray at:8 put:absValue. + ] + ] + ] + ] + ] + ] ] "Modified: / 26.5.1999 / 22:18:14 / cg" @@ -2166,41 +2166,41 @@ |otherSign| (aNumber class == self class) ifTrue:[ - otherSign := aNumber sign. - - (sign > 0) ifTrue:[ - "I am positive" - (otherSign > 0) ifTrue:[^ self absLess:aNumber]. - ^ false "aNumber is <= 0" - ]. - "I am negative" - (otherSign > 0) ifTrue:[^ true]. - (otherSign == 0) ifTrue:[^ true]. - ^ (aNumber absLess:self) + otherSign := aNumber sign. + + (sign > 0) ifTrue:[ + "I am positive" + (otherSign > 0) ifTrue:[^ self absLess:aNumber]. + ^ false "aNumber is <= 0" + ]. + "I am negative" + (otherSign > 0) ifTrue:[^ true]. + (otherSign == 0) ifTrue:[^ true]. + ^ (aNumber absLess:self) ]. (aNumber class == SmallInteger) ifTrue:[ - otherSign := aNumber sign. - - (sign > 0) ifTrue:[ - "I am positive" - ^ false "aNumber is <= 0" - ]. - (sign == 0) ifTrue:[ - (otherSign > 0) ifTrue:[^ true]. - ^ false - ]. - "I am negative" - ^ true + otherSign := aNumber sign. + + (sign > 0) ifTrue:[ + "I am positive" + ^ false "aNumber is <= 0" + ]. + (sign == 0) ifTrue:[ + (otherSign > 0) ifTrue:[^ true]. + ^ false + ]. + "I am negative" + ^ true ]. "/ hack for epsilon tests (aNumber class == Float) ifTrue:[ - self negative ifTrue:[ - "/ I am a large negative; so my value is definitely below SmallInteger minVal - aNumber >= SmallInteger minVal asFloat ifTrue:[^ true]. - ] ifFalse:[ - "/ I am a large positive; so my value is definitely above SmallInteger maxVal - aNumber <= SmallInteger maxVal asFloat ifTrue:[^ false]. - ]. + self negative ifTrue:[ + "/ I am a large negative; so my value is definitely below SmallInteger minVal + aNumber >= SmallInteger minVal asFloat ifTrue:[^ true]. + ] ifFalse:[ + "/ I am a large positive; so my value is definitely above SmallInteger maxVal + aNumber <= SmallInteger maxVal asFloat ifTrue:[^ false]. + ]. ]. ^ aNumber lessFromInteger:self @@ -2215,17 +2215,17 @@ "/ speed up compare to 0 (aNumber == 0) ifTrue:[ - ^ sign == 0 + ^ sign == 0 ]. (aNumber class == self class) ifFalse:[ - "/ - "/ here, we depend on the fact, that largeinteger - "/ results are always converted to smallInts, if possible. - "/ therefore, a largeInt in the smallInt range is not allowed (possible) - "/ - aNumber class == SmallInteger ifTrue:[^ false ]. - ^ aNumber equalFromInteger:self + "/ + "/ here, we depend on the fact, that largeinteger + "/ results are always converted to smallInts, if possible. + "/ therefore, a largeInt in the smallInt range is not allowed (possible) + "/ + aNumber class == SmallInteger ifTrue:[^ false ]. + ^ aNumber equalFromInteger:self ]. (aNumber sign == sign) ifFalse:[^ false]. @@ -2240,19 +2240,19 @@ |otherSign| (aNumber class == self class) ifFalse:[ - ^ (aNumber < self) + ^ (aNumber < self) ]. otherSign := aNumber sign. (sign > 0) ifTrue:[ - "I am positive" - (otherSign > 0) ifTrue:[^ aNumber absLess:self]. - ^ true "aNumber is <= 0" + "I am positive" + (otherSign > 0) ifTrue:[^ aNumber absLess:self]. + ^ true "aNumber is <= 0" ]. (sign == 0) ifTrue:[ - "I am zero" - (otherSign > 0) ifTrue:[^ false]. - ^ true + "I am zero" + (otherSign > 0) ifTrue:[^ false]. + ^ true ]. "I am negative" (otherSign > 0) ifTrue:[^ false]. @@ -2271,18 +2271,18 @@ l := digitByteArray size. l >= 8 ifTrue:[ - h := h bitXor:(digitByteArray at:l). - h := h bitXor:((digitByteArray at:l-1) bitShift:8). - h := h bitXor:((digitByteArray at:l-2) bitShift:16). - h := h bitXor:((digitByteArray at:l-3) bitShift:22). - l >= 12 ifTrue:[ - m := l // 2. - h := h bitXor:(digitByteArray at:m-1). - h := h bitXor:((digitByteArray at:m) bitShift:8). - h := h bitXor:((digitByteArray at:m+1) bitShift:16). - h := h bitXor:((digitByteArray at:m+2) bitShift:22). - ]. - ^ h + h := h bitXor:(digitByteArray at:l). + h := h bitXor:((digitByteArray at:l-1) bitShift:8). + h := h bitXor:((digitByteArray at:l-2) bitShift:16). + h := h bitXor:((digitByteArray at:l-3) bitShift:22). + l >= 12 ifTrue:[ + m := l // 2. + h := h bitXor:(digitByteArray at:m-1). + h := h bitXor:((digitByteArray at:m) bitShift:8). + h := h bitXor:((digitByteArray at:m+1) bitShift:16). + h := h bitXor:((digitByteArray at:m+2) bitShift:22). + ]. + ^ h ]. ^ (h bitShift:3) + l @@ -2307,22 +2307,22 @@ Return the result of 'anInteger - self'. The argument must be a SmallInteger." anInteger > 0 ifTrue:[ - sign > 0 ifTrue:[ - ^ self absFastMinus:anInteger sign:-1 - ]. - ^ self absFastPlus:anInteger sign:1 + sign > 0 ifTrue:[ + ^ self absFastMinus:anInteger sign:-1 + ]. + ^ self absFastPlus:anInteger sign:1 ]. anInteger == 0 ifTrue:[ - ^ self negated + ^ self negated ]. sign > 0 ifTrue:[ - ^ self absFastPlus:anInteger negated sign:-1 + ^ self absFastPlus:anInteger negated sign:-1 ]. self > anInteger ifTrue:[ - ^ self absFastMinus:anInteger asLargeInteger sign:-1 + ^ self absFastMinus:anInteger asLargeInteger sign:-1 ] ifFalse:[ - ^ anInteger asLargeInteger absFastMinus:self sign:-1 + ^ anInteger asLargeInteger absFastMinus:self sign:-1 ]. " @@ -2348,7 +2348,7 @@ "/ anInteger class == SmallInteger ifTrue:[^ false ]. anInteger class == self class ifFalse:[ - ^ super equalFromInteger:anInteger + ^ super equalFromInteger:anInteger ]. (anInteger sign == sign) ifFalse:[^ false]. ^ self absEq:anInteger @@ -2760,16 +2760,16 @@ Return the sum of the receiver and the argument, (which must be a SmallInteger)" anInteger > 0 ifTrue:[ - sign > 0 ifTrue:[ - ^ self absFastPlus:anInteger sign:1 - ]. - ^ self absFastMinus:anInteger sign:-1 + sign > 0 ifTrue:[ + ^ self absFastPlus:anInteger sign:1 + ]. + ^ self absFastMinus:anInteger sign:-1 ]. anInteger == 0 ifTrue:[ - ^ self + ^ self ]. sign > 0 ifTrue:[ - ^ self absFastMinus:anInteger sign:1 + ^ self absFastMinus:anInteger sign:1 ]. ^ self absFastPlus:anInteger sign:-1 @@ -2867,7 +2867,7 @@ ]. ]. shift := shift - 1. - divisor div2. + divisor := divisor div2. ]. ^ Array with:quo compressed with:dividend compressed @@ -2895,60 +2895,60 @@ %{ /* NOCONTEXT */ if (__isLargeInteger(aLargeInteger)) { - OBJ _digitByteArray = __INST(digitByteArray); - OBJ _otherDigitByteArray = __LargeIntegerInstPtr(aLargeInteger)->l_digits; - - if (__isByteArray(_digitByteArray) - && __isByteArray(_otherDigitByteArray)) { - INT _myLen = __byteArraySize(_digitByteArray); - INT _otherLen = __byteArraySize(_otherDigitByteArray); - - unsigned char *_otherDigits = __ByteArrayInstPtr(_otherDigitByteArray)->ba_element; - unsigned char *_myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element; - - if (_myLen == _otherLen) { + OBJ _digitByteArray = __INST(digitByteArray); + OBJ _otherDigitByteArray = __LargeIntegerInstPtr(aLargeInteger)->l_digits; + + if (__isByteArray(_digitByteArray) + && __isByteArray(_otherDigitByteArray)) { + INT _myLen = __byteArraySize(_digitByteArray); + INT _otherLen = __byteArraySize(_otherDigitByteArray); + + unsigned char *_otherDigits = __ByteArrayInstPtr(_otherDigitByteArray)->ba_element; + unsigned char *_myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element; + + if (_myLen == _otherLen) { tryAgain: - while (_myLen >= (sizeof(INT)*4)) { - if ( ((unsigned INT *)_myDigits)[0] != ((unsigned INT *)_otherDigits)[0]) { - RETURN(false); - } - if ( ((unsigned INT *)_myDigits)[1] != ((unsigned INT *)_otherDigits)[1]) { - RETURN(false); - } - if ( ((unsigned INT *)_myDigits)[2] != ((unsigned INT *)_otherDigits)[2]) { - RETURN(false); - } - if ( ((unsigned INT *)_myDigits)[3] != ((unsigned INT *)_otherDigits)[3]) { - RETURN(false); - } - _myDigits += sizeof(INT)*4; - _otherDigits += sizeof(INT)*4; - _myLen -= sizeof(INT)*4; - } - while (_myLen >= (sizeof(INT))) { - if ( *(unsigned INT *)_myDigits != *(unsigned INT *)_otherDigits) { - RETURN(false); - } - _myDigits += sizeof(INT); - _otherDigits += sizeof(INT); - _myLen -= sizeof(INT); - } - while (_myLen > 0) { - if ( *_myDigits != *_otherDigits) { - RETURN(false); - } - _myDigits++; - _otherDigits++; - _myLen--; - } - RETURN(true); - } - /* care for unnormalized ints */ - while ((_myLen > 0) && (_myDigits[_myLen-1] == 0)) _myLen--; - while ((_otherLen > 0) && (_otherDigits[_otherLen-1] == 0)) _otherLen--; - if (_myLen == _otherLen) goto tryAgain; - RETURN(false); - } + while (_myLen >= (sizeof(INT)*4)) { + if ( ((unsigned INT *)_myDigits)[0] != ((unsigned INT *)_otherDigits)[0]) { + RETURN(false); + } + if ( ((unsigned INT *)_myDigits)[1] != ((unsigned INT *)_otherDigits)[1]) { + RETURN(false); + } + if ( ((unsigned INT *)_myDigits)[2] != ((unsigned INT *)_otherDigits)[2]) { + RETURN(false); + } + if ( ((unsigned INT *)_myDigits)[3] != ((unsigned INT *)_otherDigits)[3]) { + RETURN(false); + } + _myDigits += sizeof(INT)*4; + _otherDigits += sizeof(INT)*4; + _myLen -= sizeof(INT)*4; + } + while (_myLen >= (sizeof(INT))) { + if ( *(unsigned INT *)_myDigits != *(unsigned INT *)_otherDigits) { + RETURN(false); + } + _myDigits += sizeof(INT); + _otherDigits += sizeof(INT); + _myLen -= sizeof(INT); + } + while (_myLen > 0) { + if ( *_myDigits != *_otherDigits) { + RETURN(false); + } + _myDigits++; + _otherDigits++; + _myLen--; + } + RETURN(true); + } + /* care for unnormalized ints */ + while ((_myLen > 0) && (_myDigits[_myLen-1] == 0)) _myLen--; + while ((_otherLen > 0) && (_otherDigits[_otherLen-1] == 0)) _otherLen--; + if (_myLen == _otherLen) goto tryAgain; + RETURN(false); + } } %}. @@ -2962,17 +2962,17 @@ "/ numbers to be compared ... [(digitByteArray basicAt:len1) == 0] whileTrue:[ - len1 := len1 - 1 + len1 := len1 - 1 ]. [(otherDigitByteArray basicAt:len2) == 0] whileTrue:[ - len2 := len2 - 1 + len2 := len2 - 1 ]. (len1 ~~ len2) ifTrue:[^ false]. [len1 > 0] whileTrue:[ - d1 := digitByteArray basicAt:len1. - d2 := otherDigitByteArray basicAt:len1. - (d1 ~~ d2) ifTrue:[^ false]. - len1 := len1 - 1 + d1 := digitByteArray basicAt:len1. + d2 := otherDigitByteArray basicAt:len1. + (d1 ~~ d2) ifTrue:[^ false]. + len1 := len1 - 1 ]. ^ true @@ -2991,12 +2991,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. @@ -3011,112 +3011,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; + 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 (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])); + 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]; + 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; + t = t | (rest << 32); + div = t / divisor; + rest = t % divisor; # if defined(__LSBFIRST__) - *((unsigned int *)(&resultBytes[index])) = (div & 0xFFFFFFFF); + *((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 */; + resultBytes[index+3] = div >> 24; + resultBytes[index+2] = div >> 16; + resultBytes[index+1] = div >> 8; + resultBytes[index] = div /* & 0xFF */; # endif - } - } - } - } + } + } + } + } #endif - /* - * 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; + /* + * 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; } %}. " @@ -3124,7 +3124,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 @@ -3156,7 +3156,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. @@ -3170,123 +3170,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 @@ -3321,7 +3321,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. @@ -3330,18 +3330,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 %} ]. @@ -3353,307 +3353,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 @@ -3799,49 +3799,49 @@ #if defined(__LSBFIRST__) if (__isByteArray(__INST(digitByteArray)) && __isLargeInteger(aLargeInteger)) { - OBJ _otherDigitByteArray = __LargeIntegerInstPtr(aLargeInteger)->l_digits; - - if (__isByteArray(_otherDigitByteArray)) { - unsigned char *_myDigits = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element; - unsigned char *_otherDigits = __ByteArrayInstPtr(_otherDigitByteArray)->ba_element; - INT _myLen = __byteArraySize(__INST(digitByteArray)); - - if (_myLen == __POINTER_SIZE__) { - INT _otherLen = __byteArraySize(_otherDigitByteArray); - - if (_otherLen == __POINTER_SIZE__) { - unsigned INT _myVal = *((unsigned INT *)_myDigits); - unsigned INT _otherVal = *((unsigned INT *)_otherDigits); - RETURN( (_myVal <= _otherVal) ? true : false ); - } - } + OBJ _otherDigitByteArray = __LargeIntegerInstPtr(aLargeInteger)->l_digits; + + if (__isByteArray(_otherDigitByteArray)) { + unsigned char *_myDigits = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element; + unsigned char *_otherDigits = __ByteArrayInstPtr(_otherDigitByteArray)->ba_element; + INT _myLen = __byteArraySize(__INST(digitByteArray)); + + if (_myLen == __POINTER_SIZE__) { + INT _otherLen = __byteArraySize(_otherDigitByteArray); + + if (_otherLen == __POINTER_SIZE__) { + unsigned INT _myVal = *((unsigned INT *)_myDigits); + unsigned INT _otherVal = *((unsigned INT *)_otherDigits); + RETURN( (_myVal <= _otherVal) ? true : false ); + } + } # if defined(UINT64) && (__POINTER_SIZE__ != 8) - if (_myLen == __POINTER_SIZE__) { - INT _otherLen = __byteArraySize(_otherDigitByteArray); - - if (_otherLen <= 8) { - UINT64 _myVal = (UINT64)(*((UINT *)_myDigits)); - UINT64 _otherVal = *((UINT64 *)_otherDigits); - RETURN( (_myVal <= _otherVal) ? true : false ); - } - } else { - if (_myLen <= 8) { - INT _otherLen = __byteArraySize(_otherDigitByteArray); - - if (_otherLen <= 8) { - UINT64 _myVal = (*((UINT64 *)_myDigits)); - UINT64 _otherVal = *((UINT64 *)_otherDigits); - RETURN( (_myVal <= _otherVal) ? true : false ); - } - if (_otherLen == __POINTER_SIZE__) { - UINT64 _myVal = (*((UINT64 *)_myDigits)); - UINT64 _otherVal = (UINT64) *((UINT *)_otherDigits); - RETURN( (_myVal <= _otherVal) ? true : false ); - } - } - } + if (_myLen == __POINTER_SIZE__) { + INT _otherLen = __byteArraySize(_otherDigitByteArray); + + if (_otherLen <= 8) { + UINT64 _myVal = (UINT64)(*((UINT *)_myDigits)); + UINT64 _otherVal = *((UINT64 *)_otherDigits); + RETURN( (_myVal <= _otherVal) ? true : false ); + } + } else { + if (_myLen <= 8) { + INT _otherLen = __byteArraySize(_otherDigitByteArray); + + if (_otherLen <= 8) { + UINT64 _myVal = (*((UINT64 *)_myDigits)); + UINT64 _otherVal = *((UINT64 *)_otherDigits); + RETURN( (_myVal <= _otherVal) ? true : false ); + } + if (_otherLen == __POINTER_SIZE__) { + UINT64 _myVal = (*((UINT64 *)_myDigits)); + UINT64 _otherVal = (UINT64) *((UINT *)_otherDigits); + RETURN( (_myVal <= _otherVal) ? true : false ); + } + } + } # endif /* UINT64 */ - } + } } #endif /* LSBFIRST */ %}. @@ -3856,22 +3856,22 @@ "/ numbers to be compared ... [(digitByteArray basicAt:myLen) == 0] whileTrue:[ - myLen := myLen - 1 + myLen := myLen - 1 ]. [(otherDigitByteArray basicAt:otherLen) == 0] whileTrue:[ - otherLen := otherLen - 1 + otherLen := otherLen - 1 ]. (myLen < otherLen) ifTrue:[^ true]. (myLen > otherLen) ifTrue:[^ false]. [myLen > 0] whileTrue:[ - d1 := digitByteArray basicAt:myLen. - d2 := otherDigitByteArray basicAt:myLen. - d1 == d2 ifFalse:[ - (d1 < d2) ifTrue:[^ true]. - ^ false. - ]. - myLen := myLen - 1 + d1 := digitByteArray basicAt:myLen. + d2 := otherDigitByteArray basicAt:myLen. + d1 == d2 ifFalse:[ + (d1 < d2) ifTrue:[^ true]. + ^ false. + ]. + myLen := myLen - 1 ]. ^ true @@ -4599,494 +4599,494 @@ if (__isByteArray(_digitByteArray) && __isByteArray(otherDigitByteArray)) { - int _len1, _len2, _newLen; - unsigned char *_myDigits, *_otherDigits, *_newDigits; - int _index, _carry; - int _comLen; - - _len1 = __byteArraySize(_digitByteArray); - _len2 = __byteArraySize(otherDigitByteArray); - - _otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; - _myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element; - - if (_len1 < _len2) { - _comLen = _len1; - _newLen = _len2; - if (_otherDigits[_len2 - 1] == 0xFF) _newLen++; - } else if (_len2 < _len1) { - _comLen = _len2; - _newLen = _len1; - if (_myDigits[_len1 - 1] == 0xFF) _newLen++; - } else { - /* - * there can only be an overflow from the high bytes, - * if their sum is >= 255 - * (with sum==255, a carry could still occur from the next lower bytes) - */ - _newLen = _len1; - if ((_otherDigits[_len2 - 1] + _myDigits[_len1 - 1]) >= 0xFF) { - _newLen++; - } else { - if (_newLen == sizeof(INT)) { - OBJ _uint; - - /* - * two word-sized numbers, no carry - a very common case ... - */ + int _len1, _len2, _newLen; + unsigned char *_myDigits, *_otherDigits, *_newDigits; + int _index, _carry; + int _comLen; + + _len1 = __byteArraySize(_digitByteArray); + _len2 = __byteArraySize(otherDigitByteArray); + + _otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; + _myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element; + + if (_len1 < _len2) { + _comLen = _len1; + _newLen = _len2; + if (_otherDigits[_len2 - 1] == 0xFF) _newLen++; + } else if (_len2 < _len1) { + _comLen = _len2; + _newLen = _len1; + if (_myDigits[_len1 - 1] == 0xFF) _newLen++; + } else { + /* + * there can only be an overflow from the high bytes, + * if their sum is >= 255 + * (with sum==255, a carry could still occur from the next lower bytes) + */ + _newLen = _len1; + if ((_otherDigits[_len2 - 1] + _myDigits[_len1 - 1]) >= 0xFF) { + _newLen++; + } else { + if (_newLen == sizeof(INT)) { + OBJ _uint; + + /* + * two word-sized numbers, no carry - a very common case ... + */ #if defined(__LSB_FIRST__) - unsigned INT _sum = *(unsigned INT *)_otherDigits + *(unsigned INT *)_myDigits; + unsigned INT _sum = *(unsigned INT *)_otherDigits + *(unsigned INT *)_myDigits; #else - unsigned INT _sum = __unsignedLongIntVal(self) + __unsignedLongIntVal(aLargeInteger); + unsigned INT _sum = __unsignedLongIntVal(self) + __unsignedLongIntVal(aLargeInteger); #endif /* not LSB_FIRST */ - if (_sum <= _MAX_INT) { - _uint = __mkSmallInteger(_sum * __intVal(newSign)); - } else { - _uint = __MKULARGEINT(_sum); - __LargeIntegerInstPtr(_uint)->l_sign = newSign; - } - RETURN (_uint); - } - } - _comLen = _len1; - } - resultDigitByteArray = __BYTEARRAY_UNINITIALIZED_NEW_INT(_newLen); - - /* - * must refetch - GC could have been invoked - */ - _digitByteArray = __INST(digitByteArray); - - _myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element; - _otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; - _newDigits = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; - - /* - * add them ... - */ - _index = 1; - _carry = 0; + if (_sum <= _MAX_INT) { + _uint = __mkSmallInteger(_sum * __intVal(newSign)); + } else { + _uint = __MKULARGEINT(_sum); + __LargeIntegerInstPtr(_uint)->l_sign = newSign; + } + RETURN (_uint); + } + } + _comLen = _len1; + } + resultDigitByteArray = __BYTEARRAY_UNINITIALIZED_NEW_INT(_newLen); + + /* + * must refetch - GC could have been invoked + */ + _digitByteArray = __INST(digitByteArray); + + _myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element; + _otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; + _newDigits = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; + + /* + * add them ... + */ + _index = 1; + _carry = 0; #if defined(__LSBFIRST__) # if (__POINTER_SIZE__ == 8) && defined(__GNUC__) # if 0 /* not faster (on alpha) */ - { - int _comLen7; - - /* - * have a 64bit integers; - * add quad-wise - * accessing bytes at: [index-1][index][index+1]..[index+6] - */ - _comLen7 = _comLen - 3 - 4; - while (_index <= _comLen7) { - UINT64 _sum, _t1, _t2; - - asm ("addq %5,%6,%1 /* sum */ \n\ - addq %0,%1,%1 /* plus carryIn */ \n\ - cmpult %1,%5,%2 /* was there a carry ? */ \n\ - cmpult %1,%6,%3 /* was there a carry ? */ \n\ - bis %2,%3,%0 /* carryOut */ \n\ - " - : "=r" (_carry), - "=r" (_sum), - "r" (_t1), - "r" (_t2) - : "r" (_carry), - "r" (((unsigned long *)(&(_myDigits[_index - 1])))[0]), - "r" (((unsigned long *)(&(_otherDigits[_index - 1])))[0]) - ); - /* _sum = _sum & 0xFFFFFFFF; */ - ((unsigned long *)(&(_newDigits[_index - 1])))[0] = _sum; - _index += 8; - } - } + { + int _comLen7; + + /* + * have a 64bit integers; + * add quad-wise + * accessing bytes at: [index-1][index][index+1]..[index+6] + */ + _comLen7 = _comLen - 3 - 4; + while (_index <= _comLen7) { + UINT64 _sum, _t1, _t2; + + asm ("addq %5,%6,%1 /* sum */ \n\ + addq %0,%1,%1 /* plus carryIn */ \n\ + cmpult %1,%5,%2 /* was there a carry ? */ \n\ + cmpult %1,%6,%3 /* was there a carry ? */ \n\ + bis %2,%3,%0 /* carryOut */ \n\ + " + : "=r" (_carry), + "=r" (_sum), + "r" (_t1), + "r" (_t2) + : "r" (_carry), + "r" (((unsigned long *)(&(_myDigits[_index - 1])))[0]), + "r" (((unsigned long *)(&(_otherDigits[_index - 1])))[0]) + ); + /* _sum = _sum & 0xFFFFFFFF; */ + ((unsigned long *)(&(_newDigits[_index - 1])))[0] = _sum; + _index += 8; + } + } # endif # endif /* 64bit */ # if (__POINTER_SIZE__ == 8) # if 0 /* not faster (on alpha) */ - { - int _comLen7; - - /* - * have a 64bit integers; - * add quad-wise - * accessing bytes at: [index-1][index][index+1]..[index+6] - */ - _comLen7 = _comLen - 3 - 4; - while (_index <= _comLen7) { - UINT64 _sum, _t1, _t2; - - _t1 = ((UINT64 *)(&(_myDigits[_index - 1])))[0]; - _t2 = ((UINT64 *)(&(_otherDigits[_index - 1])))[0]; - _sum = _t1 + _t2 + _carry; - ((UINT64 *)(&(_newDigits[_index - 1])))[0] = _sum; - _carry = (_sum < _t1) | (_sum < _t2); - _index += 8; - } - } + { + int _comLen7; + + /* + * have a 64bit integers; + * add quad-wise + * accessing bytes at: [index-1][index][index+1]..[index+6] + */ + _comLen7 = _comLen - 3 - 4; + while (_index <= _comLen7) { + UINT64 _sum, _t1, _t2; + + _t1 = ((UINT64 *)(&(_myDigits[_index - 1])))[0]; + _t2 = ((UINT64 *)(&(_otherDigits[_index - 1])))[0]; + _sum = _t1 + _t2 + _carry; + ((UINT64 *)(&(_newDigits[_index - 1])))[0] = _sum; + _carry = (_sum < _t1) | (_sum < _t2); + _index += 8; + } + } # endif # endif /* 64bit */ # ifdef UINT64 - { - int _comLen3; - - /* - * have a 64bit integer type; - * add int-wise - * accessing bytes at: [index-1][index][index+1][index+2] - */ - _comLen3 = _comLen - 3; - while (_index <= _comLen3) { - UINT64 _sum; - - /* do not merge the 3 lines below into one - - * (will do sign extension then, which is wrong here) - */ - _sum = (unsigned)_carry; - _sum += ((unsigned int *)(&(_myDigits[_index - 1])))[0]; - _sum += ((unsigned int *)(&(_otherDigits[_index - 1])))[0]; - _carry = _sum >> 32; - /* _sum = _sum & 0xFFFFFFFF; */ - ((unsigned int *)(&(_newDigits[_index - 1])))[0] = _sum; - _index += 4; - } - } + { + int _comLen3; + + /* + * have a 64bit integer type; + * add int-wise + * accessing bytes at: [index-1][index][index+1][index+2] + */ + _comLen3 = _comLen - 3; + while (_index <= _comLen3) { + UINT64 _sum; + + /* do not merge the 3 lines below into one - + * (will do sign extension then, which is wrong here) + */ + _sum = (unsigned)_carry; + _sum += ((unsigned int *)(&(_myDigits[_index - 1])))[0]; + _sum += ((unsigned int *)(&(_otherDigits[_index - 1])))[0]; + _carry = _sum >> 32; + /* _sum = _sum & 0xFFFFFFFF; */ + ((unsigned int *)(&(_newDigits[_index - 1])))[0] = _sum; + _index += 4; + } + } # else # if defined(__i386__) && defined(__GNUC__) && (__POINTER_SIZE__ == 4) - { - int _comLen3; - - _comLen3 = _comLen - 3 - 4; - while (_index <= _comLen3) { - unsigned int _sum, _sum2; - unsigned int __in1A, __in1B, __in2A, __in2B; - - __in1A = ((unsigned int *)(&(_myDigits[_index - 1])))[0]; - __in2A = ((unsigned int *)(&(_myDigits[_index - 1])))[1]; - __in1B = ((unsigned int *)(&(_otherDigits[_index - 1])))[0]; - __in2B = ((unsigned int *)(&(_otherDigits[_index - 1])))[1]; - - asm ("addl %%edx,%%eax \n\ - movl $0,%%edx \n\ - adcl $0,%%edx \n\ - addl %5,%%eax \n\ - adcl $0,%%edx \n\ - \n\ - addl %%edx,%%ecx \n\ - movl $0,%%edx \n\ - adcl $0,%%edx \n\ - addl %7,%%ecx \n\ - adcl $0,%%edx \n\ - " - : "=d" (_carry), - "=a" (_sum), - "=c" (_sum2) - : "0" (_carry), - "1" (__in1A), - "rm" (__in1B), - "2" (__in2A), - "rm" (__in2B) - ); - - ((unsigned *)(&(_newDigits[_index - 1])))[0] = _sum; - ((unsigned *)(&(_newDigits[_index - 1])))[1] = _sum2; - _index += 8; - } - /* - * add int-wise - * accessing bytes at: [index-1][index][index+1][index+2] - */ - _comLen3 = _comLen3 + 4; - if (_index <= _comLen3) { - unsigned int _sum; - unsigned int __inA, __inB; - - __inA = ((unsigned int *)(&(_myDigits[_index - 1])))[0]; - __inB = ((unsigned int *)(&(_otherDigits[_index - 1])))[0]; - - asm ("addl %%edx,%%eax \n\ - movl $0,%%edx \n\ - adcl $0,%%edx \n\ - addl %4,%%eax \n\ - adcl $0,%%edx" - : "=d" (_carry), - "=a" (_sum) - : "0" (_carry), - "1" (__inA), - "rm" (__inB) - ); - - ((unsigned *)(&(_newDigits[_index - 1])))[0] = _sum; - _index += 4; - } - } + { + int _comLen3; + + _comLen3 = _comLen - 3 - 4; + while (_index <= _comLen3) { + unsigned int _sum, _sum2; + unsigned int __in1A, __in1B, __in2A, __in2B; + + __in1A = ((unsigned int *)(&(_myDigits[_index - 1])))[0]; + __in2A = ((unsigned int *)(&(_myDigits[_index - 1])))[1]; + __in1B = ((unsigned int *)(&(_otherDigits[_index - 1])))[0]; + __in2B = ((unsigned int *)(&(_otherDigits[_index - 1])))[1]; + + asm ("addl %%edx,%%eax \n\ + movl $0,%%edx \n\ + adcl $0,%%edx \n\ + addl %5,%%eax \n\ + adcl $0,%%edx \n\ + \n\ + addl %%edx,%%ecx \n\ + movl $0,%%edx \n\ + adcl $0,%%edx \n\ + addl %7,%%ecx \n\ + adcl $0,%%edx \n\ + " + : "=d" (_carry), + "=a" (_sum), + "=c" (_sum2) + : "0" (_carry), + "1" (__in1A), + "rm" (__in1B), + "2" (__in2A), + "rm" (__in2B) + ); + + ((unsigned *)(&(_newDigits[_index - 1])))[0] = _sum; + ((unsigned *)(&(_newDigits[_index - 1])))[1] = _sum2; + _index += 8; + } + /* + * add int-wise + * accessing bytes at: [index-1][index][index+1][index+2] + */ + _comLen3 = _comLen3 + 4; + if (_index <= _comLen3) { + unsigned int _sum; + unsigned int __inA, __inB; + + __inA = ((unsigned int *)(&(_myDigits[_index - 1])))[0]; + __inB = ((unsigned int *)(&(_otherDigits[_index - 1])))[0]; + + asm ("addl %%edx,%%eax \n\ + movl $0,%%edx \n\ + adcl $0,%%edx \n\ + addl %4,%%eax \n\ + adcl $0,%%edx" + : "=d" (_carry), + "=a" (_sum) + : "0" (_carry), + "1" (__inA), + "rm" (__inB) + ); + + ((unsigned *)(&(_newDigits[_index - 1])))[0] = _sum; + _index += 4; + } + } # endif /* __i386__ && GNUC */ # if defined(WIN32) && defined(__BORLANDC__) && defined(__i386__) && (__POINTER_SIZE__ == 4) - { - int _comLen3; - - /* - * add long-wise - * accessing bytes at: [index-1][index][index+1][index+2] - */ - _comLen3 = _comLen - 3; - while (_index <= _comLen3) { - unsigned int _sum, _v1, _v2; - - _v1 = ((unsigned int *)(&(_myDigits[_index - 1])))[0]; - _v2 = ((unsigned int *)(&(_otherDigits[_index - 1])))[0]; - asm { - mov eax, _v1 - add eax, _v2 - mov edx, 0 - adc edx, 0 - add eax, _carry - adc edx, 0 - mov _carry, edx - mov _sum, eax - } - - ((unsigned *)(&(_newDigits[_index - 1])))[0] = _sum; - _index += 4; - } - } + { + int _comLen3; + + /* + * add long-wise + * accessing bytes at: [index-1][index][index+1][index+2] + */ + _comLen3 = _comLen - 3; + while (_index <= _comLen3) { + unsigned int _sum, _v1, _v2; + + _v1 = ((unsigned int *)(&(_myDigits[_index - 1])))[0]; + _v2 = ((unsigned int *)(&(_otherDigits[_index - 1])))[0]; + asm { + mov eax, _v1 + add eax, _v2 + mov edx, 0 + adc edx, 0 + add eax, _carry + adc edx, 0 + mov _carry, edx + mov _sum, eax + } + + ((unsigned *)(&(_newDigits[_index - 1])))[0] = _sum; + _index += 4; + } + } # endif /* __i386__ && WIN32 */ # endif /* INT64 */ - /* - * add short-wise - * accessing bytes at: [index-1][index] - */ - while (_index < _comLen) { - unsigned int _sum; - - _sum = _carry - + ((unsigned short *)(&(_myDigits[_index - 1])))[0] - + ((unsigned short *)(&(_otherDigits[_index - 1])))[0]; - _carry = _sum >> 16; - /* _sum = _sum & 0xFFFF; */ - *(unsigned short *)(&(_newDigits[_index - 1])) = _sum; - _index += 2; - } + /* + * add short-wise + * accessing bytes at: [index-1][index] + */ + while (_index < _comLen) { + unsigned int _sum; + + _sum = _carry + + ((unsigned short *)(&(_myDigits[_index - 1])))[0] + + ((unsigned short *)(&(_otherDigits[_index - 1])))[0]; + _carry = _sum >> 16; + /* _sum = _sum & 0xFFFF; */ + *(unsigned short *)(&(_newDigits[_index - 1])) = _sum; + _index += 2; + } #else # ifdef __sparc__ - /* - * add short-wise - * accessing bytes at: [index-1][index] - */ - while (_index < _comLen) { - unsigned int _sum; - unsigned short _v1, _v2; - - _v1 = ((unsigned short *)(&(_myDigits[_index - 1])))[0]; - _v2 = ((unsigned short *)(&(_otherDigits[_index - 1])))[0]; - _sum = _carry + (_v1>>8) + (_v2>>8); - _carry = _sum >> 8; - _newDigits[_index - 1] = _sum; - - _sum = _carry + (_v1 & 0xFF) + (_v2 & 0xFF); - _carry = _sum >> 8; - _newDigits[_index] = _sum; - _index += 2; - } + /* + * add short-wise + * accessing bytes at: [index-1][index] + */ + while (_index < _comLen) { + unsigned int _sum; + unsigned short _v1, _v2; + + _v1 = ((unsigned short *)(&(_myDigits[_index - 1])))[0]; + _v2 = ((unsigned short *)(&(_otherDigits[_index - 1])))[0]; + _sum = _carry + (_v1>>8) + (_v2>>8); + _carry = _sum >> 8; + _newDigits[_index - 1] = _sum; + + _sum = _carry + (_v1 & 0xFF) + (_v2 & 0xFF); + _carry = _sum >> 8; + _newDigits[_index] = _sum; + _index += 2; + } # endif #endif /* __LSBFIRST__ */ - /* - * add byte-wise - */ - while (_index <= _comLen) { - unsigned int _sum; - - _sum = _carry - + _myDigits[_index - 1] - + _otherDigits[_index - 1]; - _carry = _sum >> 8; - /* _sum = _sum & 0xFF; */ - _newDigits[_index - 1] = _sum; - _index++; - } - - /* - * rest - */ - if (_len1 > _len2) { + /* + * add byte-wise + */ + while (_index <= _comLen) { + unsigned int _sum; + + _sum = _carry + + _myDigits[_index - 1] + + _otherDigits[_index - 1]; + _carry = _sum >> 8; + /* _sum = _sum & 0xFF; */ + _newDigits[_index - 1] = _sum; + _index++; + } + + /* + * rest + */ + if (_len1 > _len2) { #if defined(__LSBFIRST__) - if (_index <= _len1) { - if ((_index - 1) & 1) { - /* odd byte */ - unsigned int _sum; - - _sum = _carry + _myDigits[_index - 1]; - _carry = _sum >> 8; - /* _sum = _sum & 0xFF; */ - _newDigits[_index - 1] = _sum; - _index++; - } - - while (_index < _len1) { - /* shorts */ - unsigned int _sum; - - _sum = _carry + *(unsigned short *)(&(_myDigits[_index - 1])); - _carry = _sum >> 16; - /* _sum = _sum & 0xFFFF; */ - *(unsigned short *)(&(_newDigits[_index - 1])) = _sum; - _index += 2; - } - - if (_index <= _len1) { - /* last byte */ - unsigned int _sum; - - _sum = _carry + _myDigits[_index - 1]; - _carry = _sum >> 8; - /* _sum = _sum & 0xFF; */ - _newDigits[_index - 1] = _sum; - _index++; - } - } + if (_index <= _len1) { + if ((_index - 1) & 1) { + /* odd byte */ + unsigned int _sum; + + _sum = _carry + _myDigits[_index - 1]; + _carry = _sum >> 8; + /* _sum = _sum & 0xFF; */ + _newDigits[_index - 1] = _sum; + _index++; + } + + while (_index < _len1) { + /* shorts */ + unsigned int _sum; + + _sum = _carry + *(unsigned short *)(&(_myDigits[_index - 1])); + _carry = _sum >> 16; + /* _sum = _sum & 0xFFFF; */ + *(unsigned short *)(&(_newDigits[_index - 1])) = _sum; + _index += 2; + } + + if (_index <= _len1) { + /* last byte */ + unsigned int _sum; + + _sum = _carry + _myDigits[_index - 1]; + _carry = _sum >> 8; + /* _sum = _sum & 0xFF; */ + _newDigits[_index - 1] = _sum; + _index++; + } + } #else - while (_index <= _len1) { - unsigned int _sum; - - _sum = _carry + _myDigits[_index - 1]; - _carry = _sum >> 8; - /* _sum = _sum & 0xFF; */ - _newDigits[_index - 1] = _sum; - _index++; - } + while (_index <= _len1) { + unsigned int _sum; + + _sum = _carry + _myDigits[_index - 1]; + _carry = _sum >> 8; + /* _sum = _sum & 0xFF; */ + _newDigits[_index - 1] = _sum; + _index++; + } #endif /* not LSB */ - } else { - if (_len2 > _len1) { + } else { + if (_len2 > _len1) { #if defined(__LSBFIRST__) - if (_index <= _len2) { - if ((_index - 1) & 1) { - /* odd byte */ - unsigned int _sum; - - _sum = _carry + _otherDigits[_index - 1]; - _carry = _sum >> 8; - /* _sum = _sum & 0xFF; */ - _newDigits[_index - 1] = _sum; - _index++; - } - - while (_index < _len2) { - /* shorts */ - unsigned int _sum; - - _sum = _carry + *(unsigned short *)(&(_otherDigits[_index - 1])); - _carry = _sum >> 16; - /* _sum = _sum & 0xFFFF; */ - *(unsigned short *)(&(_newDigits[_index - 1])) = _sum; - _index += 2; - } - - if (_index <= _len2) { - /* last byte */ - unsigned int _sum; - - _sum = _carry + _otherDigits[_index - 1]; - _carry = _sum >> 8; - /* _sum = _sum & 0xFF; */ - _newDigits[_index - 1] = _sum; - _index++; - } - } + if (_index <= _len2) { + if ((_index - 1) & 1) { + /* odd byte */ + unsigned int _sum; + + _sum = _carry + _otherDigits[_index - 1]; + _carry = _sum >> 8; + /* _sum = _sum & 0xFF; */ + _newDigits[_index - 1] = _sum; + _index++; + } + + while (_index < _len2) { + /* shorts */ + unsigned int _sum; + + _sum = _carry + *(unsigned short *)(&(_otherDigits[_index - 1])); + _carry = _sum >> 16; + /* _sum = _sum & 0xFFFF; */ + *(unsigned short *)(&(_newDigits[_index - 1])) = _sum; + _index += 2; + } + + if (_index <= _len2) { + /* last byte */ + unsigned int _sum; + + _sum = _carry + _otherDigits[_index - 1]; + _carry = _sum >> 8; + /* _sum = _sum & 0xFF; */ + _newDigits[_index - 1] = _sum; + _index++; + } + } #else - while (_index <= _len2) { - unsigned int _sum; - - _sum = _carry + _otherDigits[_index - 1]; - _carry = _sum >> 8; - /* _sum = _sum & 0xFF; */ - _newDigits[_index - 1] = _sum; - _index++; - } + while (_index <= _len2) { + unsigned int _sum; + + _sum = _carry + _otherDigits[_index - 1]; + _carry = _sum >> 8; + /* _sum = _sum & 0xFF; */ + _newDigits[_index - 1] = _sum; + _index++; + } #endif /* not LSB */ - } - } - - while (_index <= _newLen) { - unsigned int _sum; - - _sum = _carry; - _carry = _sum >> 8; - /* _sum = _sum & 0xFF; */ - _newDigits[_index - 1] = _sum; - _index++; - } + } + } + + while (_index <= _newLen) { + unsigned int _sum; + + _sum = _carry; + _carry = _sum >> 8; + /* _sum = _sum & 0xFF; */ + _newDigits[_index - 1] = _sum; + _index++; + } } %}. resultDigitByteArray notNil ifTrue:[ - result := self class basicNew. - result setDigits:resultDigitByteArray. - result setSign:newSign. + result := self class basicNew. + result setDigits:resultDigitByteArray. + result setSign:newSign. ] ifFalse:[ - len1 := digitByteArray size. - len2 := otherDigitByteArray size. - - "/ earlier versions estimated the newLength as: - "/ (len1 max:len2) + 1 - "/ and reduced the result. - "/ however, if one of the addends is smaller, - "/ the result will never require another digit, - "/ if the highest digit of the larger addent is - "/ not equal to 255. Therefore, in most cases, - "/ we can avoid the computation and resizing - "/ in #reduced. - - len1 < len2 ifTrue:[ - newLen := len2. - (otherDigitByteArray at:len2) == 16rFF ifTrue:[ - newLen := newLen + 1 - ] - ] ifFalse:[ - len2 < len1 ifTrue:[ - newLen := len1. - (digitByteArray at:len1) == 16rFF ifTrue:[ - newLen := newLen + 1 - ] - ] ifFalse:[ - newLen := len1 + 1. - ] - ]. - - result := self class basicNew numberOfDigits:newLen. - result sign:newSign. - resultDigitByteArray := result digitBytes. - - index := 1. - carry := 0. - - done := false. - [done] whileFalse:[ - sum := carry. - (index <= len1) ifTrue:[ - sum := sum + (digitByteArray basicAt:index). - (index <= len2) ifTrue:[ - sum := sum + (otherDigitByteArray basicAt:index) - ] - ] ifFalse:[ - (index <= len2) ifTrue:[ - sum := sum + (otherDigitByteArray basicAt:index) - ] ifFalse:[ - "end reached" - done := true - ] - ]. - (sum >= 16r100) ifTrue:[ - carry := 1. - sum := sum - 16r100 - ] ifFalse:[ - carry := 0 - ]. - resultDigitByteArray basicAt:index put:sum. - index := index + 1 - ]. + len1 := digitByteArray size. + len2 := otherDigitByteArray size. + + "/ earlier versions estimated the newLength as: + "/ (len1 max:len2) + 1 + "/ and reduced the result. + "/ however, if one of the addends is smaller, + "/ the result will never require another digit, + "/ if the highest digit of the larger addent is + "/ not equal to 255. Therefore, in most cases, + "/ we can avoid the computation and resizing + "/ in #reduced. + + len1 < len2 ifTrue:[ + newLen := len2. + (otherDigitByteArray at:len2) == 16rFF ifTrue:[ + newLen := newLen + 1 + ] + ] ifFalse:[ + len2 < len1 ifTrue:[ + newLen := len1. + (digitByteArray at:len1) == 16rFF ifTrue:[ + newLen := newLen + 1 + ] + ] ifFalse:[ + newLen := len1 + 1. + ] + ]. + + result := self class basicNew numberOfDigits:newLen. + result sign:newSign. + resultDigitByteArray := result digitBytes. + + index := 1. + carry := 0. + + done := false. + [done] whileFalse:[ + sum := carry. + (index <= len1) ifTrue:[ + sum := sum + (digitByteArray basicAt:index). + (index <= len2) ifTrue:[ + sum := sum + (otherDigitByteArray basicAt:index) + ] + ] ifFalse:[ + (index <= len2) ifTrue:[ + sum := sum + (otherDigitByteArray basicAt:index) + ] ifFalse:[ + "end reached" + done := true + ] + ]. + (sum >= 16r100) ifTrue:[ + carry := 1. + sum := sum - 16r100 + ] ifFalse:[ + carry := 0 + ]. + resultDigitByteArray basicAt:index put:sum. + index := index + 1 + ]. ]. ^ result compressed @@ -5096,13 +5096,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 }" @@ -5118,12 +5118,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 %{ @@ -5132,147 +5132,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); } %}. @@ -5280,26 +5280,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 @@ -5320,103 +5320,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; + 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. ]. " @@ -5440,81 +5440,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. ]. "