*
• Left Shifting
• *
• Right Shifting
• - *
• Bit clearingting
• + *
• Bit clearing
• *
• Bit setting
• *
• Bit counting
• *
• Bit testing
• - *
• Getting of the lowest bit setted
• + *
• Getting of the lowest bit set
• *
* All operations are provided in immutable way, and some in both mutable and * immutable. @@ -47,8 +47,9 @@ static int setTrueCoded(int arr[], final int length) { int i; // Until the first set bit, the bits are equal (i.e. 0) - for (i = 0; (i < length) && (arr[i] == 0); i++) + for (i = 0; (i < length) && (arr[i] == 0); i++) { ; + } if (i == length) { return 1; } @@ -71,8 +72,9 @@ int i; if (val.sign < 0) { - for (i = 0; val.digits[i] == 0; i++) + for (i = 0; val.digits[i] == 0; i++) { ; + } // We reduce the problem to the positive case. if (i == val.numberLength - 1) { highDigit--; @@ -93,8 +95,9 @@ bCount += Integer.bitCount(val.digits[i]); } } else {// (sign < 0) - for (i = 0; val.digits[i] == 0; i++) + for (i = 0; val.digits[i] == 0; i++) { ; + } // this digit absorbs the carry bCount += Integer.bitCount(-val.digits[i]); for (i++; i < val.numberLength; i++) { @@ -170,8 +173,9 @@ int bitCount = numberOfBits & 31; int i; - for (i = 0; (i < intCount) && (digits[i] == 0); i++) + for (i = 0; (i < intCount) && (digits[i] == 0); i++) { ; + } return ((i != intCount) || (digits[i] << (32 - bitCount) != 0)); } @@ -237,8 +241,9 @@ if (source.sign < 0) { // Checking if the dropped bits are zeros (the remainder equals to // 0) - for (i = 0; (i < intCount) && (source.digits[i] == 0); i++) + for (i = 0; (i < intCount) && (source.digits[i] == 0); i++) { ; + } // If the remainder is not zero, add 1 to the result if ((i < intCount) || ((count > 0) && ((source.digits[i] << (32 - count)) != 0))) { Modified: incubator/harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/Conversion.java URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/Conversion.java?view=diff&rev=451829&r1=451828&r2=451829 ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/Conversion.java (original) +++ incubator/harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/Conversion.java Sun Oct 1 15:31:42 2006 @@ -119,7 +119,7 @@ } if (numberLength == 1) { int highDigit = digits[numberLength - 1]; - long v = (long) highDigit & 0xFFFFFFFFL; + long v = highDigit & 0xFFFFFFFFL; if (sign < 0) { v = -v; } @@ -159,8 +159,9 @@ for (i = 0; i < delta && currentChar > 0; i++) { result[--currentChar] = '0'; } - for (i = tempLen - 1; (i > 0) && (temp[i] == 0); i--) + for (i = tempLen - 1; (i > 0) && (temp[i] == 0); i--) { ; + } tempLen = i + 1; if ((tempLen == 1) && (temp[0] == 0)) { // the quotient is 0 break; @@ -225,71 +226,70 @@ result1.append(-scale); return result1.toString(); } + } + // one 32-bit unsigned value may contains 10 decimal digits + resLengthInChars = numberLength * 10 + 1 + 7; + // Explanation why +1+7: + // +1 - one char for sign if needed. + // +7 - For "special case 2" (see below) we have 7 free chars for + // inserting necessary scaled digits. + result = new char[resLengthInChars + 1]; + // alocated [resLengthInChars+1] charactes. + // a free latest character may be used for "special case 1" (see + // below) + currentChar = resLengthInChars; + if (numberLength == 1) { + int highDigit = digits[0]; + if (highDigit < 0) { + long v = highDigit & 0xFFFFFFFFL; + do { + long prev = v; + v /= 10; + result[--currentChar] = (char) (0x0030 + ((int) (prev - v * 10))); + } while (v != 0); + } else { + int v = highDigit; + do { + int prev = v; + v /= 10; + result[--currentChar] = (char) (0x0030 + (prev - v * 10)); + } while (v != 0); + } } else { - // one 32-bit unsigned value may contains 10 decimal digits - resLengthInChars = numberLength * 10 + 1 + 7; - // Explanation why +1+7: - // +1 - one char for sign if needed. - // +7 - For "special case 2" (see below) we have 7 free chars for - // inserting necessary scaled digits. - result = new char[resLengthInChars + 1]; - // alocated [resLengthInChars+1] charactes. - // a free latest character may be used for "special case 1" (see - // below) - currentChar = resLengthInChars; - if (numberLength == 1) { - int highDigit = digits[0]; - if (highDigit < 0) { - long v = (long) highDigit & 0xFFFFFFFFL; - do { - long prev = v; - v /= 10; - result[--currentChar] = (char) (0x0030 + ((int) (prev - v * 10))); - } while (v != 0); - } else { - int v = highDigit; - do { - int prev = v; - v /= 10; - result[--currentChar] = (char) (0x0030 + (prev - v * 10)); - } while (v != 0); + int temp[] = new int[numberLength]; + int tempLen = numberLength; + System.arraycopy(digits, 0, temp, 0, tempLen); + BIG_LOOP: while (true) { + // divide the array of digits by bigRadix and convert + // remainders + // to characters collecting them in the char array + long result11 = 0; + for (int i1 = tempLen - 1; i1 >= 0; i1--) { + long temp1 = (result11 << 32) + + (temp[i1] & 0xFFFFFFFFL); + long res = divideLongByBillion(temp1); + temp[i1] = (int) res; + result11 = (int) (res >> 32); } - } else { - int temp[] = new int[numberLength]; - int tempLen = numberLength; - System.arraycopy(digits, 0, temp, 0, tempLen); - BIG_LOOP: while (true) { - // divide the array of digits by bigRadix and convert - // remainders - // to characters collecting them in the char array - long result11 = 0; - for (int i1 = tempLen - 1; i1 >= 0; i1--) { - long temp1 = (result11 << 32) - + ((long) temp[i1] & 0xFFFFFFFFL); - long res = divideLongByBillion(temp1); - temp[i1] = (int) res; - result11 = (int) (res >> 32); - } - int resDigit = (int) result11; - int previous = currentChar; - do { - result[--currentChar] = (char) (0x0030 + (resDigit % 10)); - } while (((resDigit /= 10) != 0) && (currentChar != 0)); - int delta = 9 - previous + currentChar; - for (int i = 0; (i < delta) && (currentChar > 0); i++) { - result[--currentChar] = '0'; - } - int j = tempLen - 1; - for (; temp[j] == 0; j--) { - if (j == 0) { // means temp[0] == 0 - break BIG_LOOP; - } - } - tempLen = j + 1; + int resDigit = (int) result11; + int previous = currentChar; + do { + result[--currentChar] = (char) (0x0030 + (resDigit % 10)); + } while (((resDigit /= 10) != 0) && (currentChar != 0)); + int delta = 9 - previous + currentChar; + for (int i = 0; (i < delta) && (currentChar > 0); i++) { + result[--currentChar] = '0'; } - while (result[currentChar] == '0') { - currentChar++; + int j = tempLen - 1; + for (; temp[j] == 0; j--) { + if (j == 0) { // means temp[0] == 0 + break BIG_LOOP; + } } + tempLen = j + 1; + } + while (result[currentChar] == '0') { + currentChar++; } } boolean negNumber = (sign < 0); @@ -314,42 +314,40 @@ } return new String(result, currentChar, resLengthInChars - currentChar + 1); - } else { - // special case 2 - for (int j = 2; j < -exponent + 1; j++) { - result[--currentChar] = '0'; - } - result[--currentChar] = '.'; + } + // special case 2 + for (int j = 2; j < -exponent + 1; j++) { result[--currentChar] = '0'; - if (negNumber) { - result[--currentChar] = '-'; - } - return new String(result, currentChar, resLengthInChars - - currentChar); } - } else { - int startPoint = currentChar + 1; - int endPoint = resLengthInChars; - StringBuffer result1 = new StringBuffer(16 + endPoint - startPoint); + result[--currentChar] = '.'; + result[--currentChar] = '0'; if (negNumber) { - result1.append('-'); - } - if (endPoint - startPoint >= 1) { - result1.append(result[currentChar]); - result1.append('.'); - result1.append(result, currentChar + 1, resLengthInChars - - currentChar - 1); - } else { - result1.append(result, currentChar, resLengthInChars - - currentChar); - } - result1.append('E'); - if (exponent > 0) { - result1.append('+'); + result[--currentChar] = '-'; } - result1.append(Integer.toString(exponent)); - return result1.toString(); + return new String(result, currentChar, resLengthInChars + - currentChar); + } + int startPoint = currentChar + 1; + int endPoint = resLengthInChars; + StringBuffer result1 = new StringBuffer(16 + endPoint - startPoint); + if (negNumber) { + result1.append('-'); + } + if (endPoint - startPoint >= 1) { + result1.append(result[currentChar]); + result1.append('.'); + result1.append(result, currentChar + 1, resLengthInChars + - currentChar - 1); + } else { + result1.append(result, currentChar, resLengthInChars + - currentChar); } + result1.append('E'); + if (exponent > 0) { + result1.append('+'); + } + result1.append(Integer.toString(exponent)); + return result1.toString(); } /* can process only 32-bit numbers */ @@ -380,26 +378,25 @@ result1.append( (scale == Integer.MIN_VALUE) ? "2147483648" : Integer.toString(-scale)); //\$NON-NLS-1\$ return result1.toString(); } - } else { - // one 32-bit unsigned value may contains 10 decimal digits - resLengthInChars = 18; - // Explanation why +1+7: - // +1 - one char for sign if needed. - // +7 - For "special case 2" (see below) we have 7 free chars for - // inserting necessary scaled digits. - result = new char[resLengthInChars+1]; - // alocated [resLengthInChars+1] charactes. - // a free latest character may be used for "special case 1" (see below) - currentChar = resLengthInChars; - long v = value; - do { - long prev = v; - v /= 10; - result[--currentChar] = (char) (0x0030 + (prev - v * 10)); - } while (v != 0); } + // one 32-bit unsigned value may contains 10 decimal digits + resLengthInChars = 18; + // Explanation why +1+7: + // +1 - one char for sign if needed. + // +7 - For "special case 2" (see below) we have 7 free chars for + // inserting necessary scaled digits. + result = new char[resLengthInChars+1]; + // Allocated [resLengthInChars+1] characters. + // a free latest character may be used for "special case 1" (see below) + currentChar = resLengthInChars; + long v = value; + do { + long prev = v; + v /= 10; + result[--currentChar] = (char) (0x0030 + (prev - v * 10)); + } while (v != 0); - long exponent = (long)resLengthInChars - (long)currentChar - (long)scale - 1L; + long exponent = (long)resLengthInChars - (long)currentChar - scale - 1L; if (scale == 0) { if (negNumber) { result[--currentChar] = '-'; @@ -418,39 +415,37 @@ result[--currentChar] = '-'; } return new String(result, currentChar, resLengthInChars - currentChar + 1); - } else { - // special case 2 - for (int j = 2; j < -exponent + 1; j++) { - result[--currentChar] = '0'; - } - result[--currentChar] = '.'; + } + // special case 2 + for (int j = 2; j < -exponent + 1; j++) { result[--currentChar] = '0'; - if (negNumber) { - result[--currentChar] = '-'; - } - return new String(result, currentChar, resLengthInChars - currentChar); } - } else { - int startPoint = currentChar + 1; - int endPoint = resLengthInChars; - StringBuffer result1 = new StringBuffer(16+endPoint-startPoint); + result[--currentChar] = '.'; + result[--currentChar] = '0'; if (negNumber) { - result1.append('-'); - } - if (endPoint - startPoint >= 1) { - result1.append(result[currentChar]); - result1.append('.'); - result1.append(result,currentChar+1,resLengthInChars - currentChar-1); - } else { - result1.append(result,currentChar,resLengthInChars - currentChar); - } - result1.append('E'); - if (exponent > 0) { - result1.append('+'); + result[--currentChar] = '-'; } - result1.append(Long.toString(exponent)); - return result1.toString(); + return new String(result, currentChar, resLengthInChars - currentChar); + } + int startPoint = currentChar + 1; + int endPoint = resLengthInChars; + StringBuffer result1 = new StringBuffer(16+endPoint-startPoint); + if (negNumber) { + result1.append('-'); + } + if (endPoint - startPoint >= 1) { + result1.append(result[currentChar]); + result1.append('.'); + result1.append(result,currentChar+1,resLengthInChars - currentChar-1); + } else { + result1.append(result,currentChar,resLengthInChars - currentChar); + } + result1.append('E'); + if (exponent > 0) { + result1.append('+'); } + result1.append(Long.toString(exponent)); + return result1.toString(); } static long divideLongByBillion(long a) { @@ -515,7 +510,7 @@ } mantissa >>= 1; // drop the rounding bit long resSign = (val.sign < 0) ? 0x8000000000000000L : 0; - exponent = ((long) (1023 + exponent) << 52) & 0x7FF0000000000000L; + exponent = ((1023 + exponent) << 52) & 0x7FF0000000000000L; long result = resSign | exponent | mantissa; return Double.longBitsToDouble(result); } Modified: incubator/harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/Division.java URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/Division.java?view=diff&rev=451829&r1=451828&r2=451829 ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/Division.java (original) +++ incubator/harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/Division.java Sun Oct 1 15:31:42 2006 @@ -39,7 +39,7 @@ * * * @author Intel Middleware Product Division - * @author Instituto Tecnológico de Córdoba + * @author Instituto Tecnologico de Cordoba */ class Division { @@ -88,7 +88,7 @@ // set guessDigit to the largest unsigned int value guessDigit = -1; } else { - long product = ((((long) normA[j] & 0xffffffffL) << 32) + ((long) normA[j - 1] & 0xffffffffL)); + long product = (((normA[j] & 0xffffffffL) << 32) + (normA[j - 1] & 0xffffffffL)); long res = Division.divideLongByInt(product, firstDivisorDigit); guessDigit = (int) res; // the quotient of divideLongByInt int rem = (int) (res >> 32); // the remainder of @@ -106,16 +106,16 @@ break; } // leftHand always fits in an unsigned long - leftHand = ((long) guessDigit & 0xffffffffL) - * ((long) normB[normBLength - 2] & 0xffffffffL); + leftHand = (guessDigit & 0xffffffffL) + * (normB[normBLength - 2] & 0xffffffffL); /* * rightHand can overflow; in this case the loop * condition will be true in the next step of the loop */ rightHand = ((long) rem << 32) - + ((long) normA[j - 2] & 0xffffffffL); - long longR = ((long) rem & 0xffffffffL) - + ((long) firstDivisorDigit & 0xffffffffL); + + (normA[j - 2] & 0xffffffffL); + long longR = (rem & 0xffffffffL) + + (firstDivisorDigit & 0xffffffffL); /* * checks that longR does not fit in an unsigned int; * this ensures that rightHand will overflow unsigned @@ -134,22 +134,22 @@ if (guessDigit != 0) { int borrow = Division.multiplyAndSubtract(normA, j - normBLength, normB, normBLength, - (long) guessDigit & 0xffffffffL); + guessDigit & 0xffffffffL); // Step D5: check the borrow if (borrow != 0) { // Step D6: compensating addition guessDigit--; long carry = 0; for (int k = 0; k < normBLength; k++) { - carry += ((long) normA[j - normBLength + k] & 0xffffffffL) - + ((long) normB[k] & 0xffffffffL); + carry += (normA[j - normBLength + k] & 0xffffffffL) + + (normB[k] & 0xffffffffL); normA[j - normBLength + k] = (int) carry; carry >>>= 32; } } } if (quot != null) { - quot[i] = (int) guessDigit; + quot[i] = guessDigit; } // Step D7 j--; @@ -162,10 +162,9 @@ // reuse normB BitLevel.shiftRight(normB, normBLength, normA, 0, divisorShift); return normB; - } else { - System.arraycopy(normA, 0, normB, 0, bLength); - return normA; } + System.arraycopy(normA, 0, normB, 0, bLength); + return normA; } /** @@ -182,10 +181,10 @@ final int divisor) { long rem = 0; - long bLong = (long) divisor & 0xffffffffL; + long bLong = divisor & 0xffffffffL; for (int i = srcLength - 1; i >= 0; i--) { - long temp = (rem << 32) | ((long) src[i] & 0xffffffffL); + long temp = (rem << 32) | (src[i] & 0xffffffffL); long quot; if (temp >= 0) { quot = (temp / bLong); @@ -236,7 +235,7 @@ long result = 0; for (int i = srcLength - 1; i >= 0; i--) { - long temp = (result << 32) + ((long) src[i] & 0xffffffffL); + long temp = (result << 32) + (src[i] & 0xffffffffL); long res = divideLongByInt(temp, divisor); result = (int) (res >> 32); } @@ -268,7 +267,7 @@ static long divideLongByInt(long a, int b) { long quot; long rem; - long bLong = (long) b & 0xffffffffL; + long bLong = b & 0xffffffffL; if (a >= 0) { quot = (a / bLong); @@ -314,8 +313,8 @@ int valLen = val.numberLength; int valSign = val.sign; if (valLen == 1) { - long a = ((long) valDigits[0] & 0xffffffffL); - long b = ((long) divisor & 0xffffffffL); + long a = (valDigits[0] & 0xffffffffL); + long b = (divisor & 0xffffffffL); long quo = a / b; long rem = a % b; if (valSign != divisorSign) { @@ -326,20 +325,19 @@ } return new BigInteger[] { BigInteger.valueOf(quo), BigInteger.valueOf(rem) }; - } else { - int quotientLength = valLen; - int quotientSign = ((valSign == divisorSign) ? 1 : -1); - int quotientDigits[] = new int[quotientLength]; - int remainderDigits[]; - remainderDigits = new int[] { Division.divideArrayByInt( - quotientDigits, valDigits, valLen, divisor) }; - BigInteger result0 = new BigInteger(quotientSign, quotientLength, - quotientDigits); - BigInteger result1 = new BigInteger(valSign, 1, remainderDigits); - result0.cutOffLeadingZeroes(); - result1.cutOffLeadingZeroes(); - return new BigInteger[] { result0, result1 }; } + int quotientLength = valLen; + int quotientSign = ((valSign == divisorSign) ? 1 : -1); + int quotientDigits[] = new int[quotientLength]; + int remainderDigits[]; + remainderDigits = new int[] { Division.divideArrayByInt( + quotientDigits, valDigits, valLen, divisor) }; + BigInteger result0 = new BigInteger(quotientSign, quotientLength, + quotientDigits); + BigInteger result1 = new BigInteger(valSign, 1, remainderDigits); + result0.cutOffLeadingZeroes(); + result1.cutOffLeadingZeroes(); + return new BigInteger[] { result0, result1 }; } /** @@ -360,7 +358,7 @@ int productInt; for (i = 0; i < bLen; i++) { - product = c * ((long) b[i] & 0xffffffffL); + product = c * (b[i] & 0xffffffffL); productInt = (int) product; productInt += carry; carry = (int) (product >> 32) @@ -371,8 +369,8 @@ } a[start + i] = productInt; } - product = ((long) a[start + i] & 0xffffffffL) - - ((long) carry & 0xffffffffL); + product = (a[start + i] & 0xffffffffL) + - (carry & 0xffffffffL); a[start + i] = (int) product; carry = (int) (product >> 32); // -1 or 0 return carry; @@ -701,12 +699,14 @@ t[s] = t[s + 1] + (int) C; } // skipping leading zeros - for (i = t.length - 1; (i > 0) && (t[i] == 0); i--) + for (i = t.length - 1; (i > 0) && (t[i] == 0); i--) { ; + } if (i == s - 1) { - for (; (i >= 0) && (t[i] == n[i]); i--) + for (; (i >= 0) && (t[i] == n[i]); i--) { ; + } lower = (i >= 0) && (t[i] & 0xFFFFFFFFL) < (n[i] & 0xFFFFFFFFL); } else { lower = (i < s - 1); Modified: incubator/harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/Elementary.java URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/Elementary.java?view=diff&rev=451829&r1=451828&r2=451829 ============================================================================== --- incubator/harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/Elementary.java (original) +++ incubator/harmony/enhanced/classlib/trunk/modules/math/src/main/java/java/math/Elementary.java Sun Oct 1 15:31:42 2006 @@ -19,7 +19,7 @@ /** * Static library that provides the basic arithmetic mutable operations for - * {@link BigInteger}. The operations that porvides are listed below. + * {@link BigInteger}. The operations provided are listed below. *
*