summaryrefslogtreecommitdiffstats
path: root/core/java/com
diff options
context:
space:
mode:
authorTammo Spalink <tammo@google.com>2009-04-15 19:15:37 +0800
committerTammo Spalink <tammo@google.com>2009-04-16 14:48:19 +0800
commite564b19ed2f6b8b5667648254bc6c6859bb536e7 (patch)
treeb3df8b928fb68069f7aa92ceb84549fbe0dab49f /core/java/com
parent967f7c169c0ff8723a72fce7073f591dcfab018d (diff)
downloadframeworks_base-e564b19ed2f6b8b5667648254bc6c6859bb536e7.zip
frameworks_base-e564b19ed2f6b8b5667648254bc6c6859bb536e7.tar.gz
frameworks_base-e564b19ed2f6b8b5667648254bc6c6859bb536e7.tar.bz2
replaced integer * and % with shift operations, for performance
Diffstat (limited to 'core/java/com')
-rw-r--r--core/java/com/android/internal/util/BitwiseInputStream.java12
-rw-r--r--core/java/com/android/internal/util/BitwiseOutputStream.java16
2 files changed, 14 insertions, 14 deletions
diff --git a/core/java/com/android/internal/util/BitwiseInputStream.java b/core/java/com/android/internal/util/BitwiseInputStream.java
index 5da3bc6..4757919 100644
--- a/core/java/com/android/internal/util/BitwiseInputStream.java
+++ b/core/java/com/android/internal/util/BitwiseInputStream.java
@@ -51,7 +51,7 @@ public class BitwiseInputStream {
*/
public BitwiseInputStream(byte buf[]) {
mBuf = buf;
- mEnd = buf.length * 8;
+ mEnd = buf.length << 3;
mPos = 0;
}
@@ -70,13 +70,13 @@ public class BitwiseInputStream {
* @return byte of read data (possibly partially filled, from lsb)
*/
public byte read(int bits) throws AccessException {
- int index = mPos / 8;
- int offset = 16 - (mPos % 8) - bits;
+ int index = mPos >>> 3;
+ int offset = 16 - (mPos & 0x07) - bits; // &7==%8
if ((bits < 0) || (bits > 8) || ((mPos + bits) > mEnd)) {
throw new AccessException("illegal read " +
"(pos " + mPos + ", end " + mEnd + ", bits " + bits + ")");
}
- int data = (mBuf[index] & 0xFF) << 8;
+ int data = (mBuf[index] & 0x00FF) << 8;
if (offset < 8) data |= (mBuf[index + 1] & 0xFF);
data >>>= offset;
data &= (-1 >>> (32 - bits));
@@ -92,10 +92,10 @@ public class BitwiseInputStream {
* @return newly allocated byte array of read data
*/
public byte[] readByteArray(int bits) throws AccessException {
- int bytes = (bits / 8) + ((bits % 8) > 0 ? 1 : 0);
+ int bytes = (bits >>> 3) + ((bits & 0x07) > 0 ? 1 : 0); // &7==%8
byte[] arr = new byte[bytes];
for (int i = 0; i < bytes; i++) {
- int increment = Math.min(8, bits - (i * 8));
+ int increment = Math.min(8, bits - (i << 3));
arr[i] = (byte)(read(increment) << (8 - increment));
}
return arr;
diff --git a/core/java/com/android/internal/util/BitwiseOutputStream.java b/core/java/com/android/internal/util/BitwiseOutputStream.java
index 5941bf3..17f5c7c 100644
--- a/core/java/com/android/internal/util/BitwiseOutputStream.java
+++ b/core/java/com/android/internal/util/BitwiseOutputStream.java
@@ -51,7 +51,7 @@ public class BitwiseOutputStream {
*/
public BitwiseOutputStream(int startingLength) {
mBuf = new byte[startingLength];
- mEnd = startingLength * 8;
+ mEnd = startingLength << 3;
mPos = 0;
}
@@ -61,7 +61,7 @@ public class BitwiseOutputStream {
* @return newly allocated byte array
*/
public byte[] toByteArray() {
- int len = (mPos / 8) + ((mPos % 8) > 0 ? 1 : 0);
+ int len = (mPos >>> 3) + ((mPos & 0x07) > 0 ? 1 : 0); // &7==%8
byte[] newBuf = new byte[len];
System.arraycopy(mBuf, 0, newBuf, 0, len);
return newBuf;
@@ -74,8 +74,8 @@ public class BitwiseOutputStream {
*/
private void possExpand(int bits) {
if ((mPos + bits) < mEnd) return;
- byte[] newBuf = new byte[((mPos + bits) * 2) / 8];
- System.arraycopy(mBuf, 0, newBuf, 0, mEnd / 8);
+ byte[] newBuf = new byte[(mPos + bits) >>> 2];
+ System.arraycopy(mBuf, 0, newBuf, 0, mEnd >>> 3);
mBuf = newBuf;
}
@@ -91,12 +91,12 @@ public class BitwiseOutputStream {
}
possExpand(bits);
data &= (-1 >>> (32 - bits));
- int index = mPos / 8;
- int offset = 16 - (mPos % 8) - bits;
+ int index = mPos >>> 3;
+ int offset = 16 - (mPos & 0x07) - bits; // &7==%8
data <<= offset;
mPos += bits;
mBuf[index] |= (data >>> 8);
- if (offset < 8) mBuf[index + 1] |= (data & 0xFF);
+ if (offset < 8) mBuf[index + 1] |= (data & 0x00FF);
}
/**
@@ -107,7 +107,7 @@ public class BitwiseOutputStream {
*/
public void writeByteArray(int bits, byte[] arr) throws AccessException {
for (int i = 0; i < arr.length; i++) {
- int increment = Math.min(8, bits - (i * 8));
+ int increment = Math.min(8, bits - (i << 3));
if (increment > 0) {
write(increment, (byte)(arr[i] >>> (8 - increment)));
}