Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e564b19e authored by Tammo Spalink's avatar Tammo Spalink
Browse files

replaced integer * and % with shift operations, for performance

parent 967f7c16
Loading
Loading
Loading
Loading
+6 −6
Original line number Original line Diff line number Diff line
@@ -51,7 +51,7 @@ public class BitwiseInputStream {
     */
     */
    public BitwiseInputStream(byte buf[]) {
    public BitwiseInputStream(byte buf[]) {
        mBuf = buf;
        mBuf = buf;
        mEnd = buf.length * 8;
        mEnd = buf.length << 3;
        mPos = 0;
        mPos = 0;
    }
    }


@@ -70,13 +70,13 @@ public class BitwiseInputStream {
     * @return byte of read data (possibly partially filled, from lsb)
     * @return byte of read data (possibly partially filled, from lsb)
     */
     */
    public byte read(int bits) throws AccessException {
    public byte read(int bits) throws AccessException {
        int index = mPos / 8;
        int index = mPos >>> 3;
        int offset = 16 - (mPos % 8) - bits;
        int offset = 16 - (mPos & 0x07) - bits;  // &7==%8
        if ((bits < 0) || (bits > 8) || ((mPos + bits) > mEnd)) {
        if ((bits < 0) || (bits > 8) || ((mPos + bits) > mEnd)) {
            throw new AccessException("illegal read " +
            throw new AccessException("illegal read " +
                "(pos " + mPos + ", end " + mEnd + ", bits " + bits + ")");
                "(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);
        if (offset < 8) data |= (mBuf[index + 1] & 0xFF);
        data >>>= offset;
        data >>>= offset;
        data &= (-1 >>> (32 - bits));
        data &= (-1 >>> (32 - bits));
@@ -92,10 +92,10 @@ public class BitwiseInputStream {
     * @return newly allocated byte array of read data
     * @return newly allocated byte array of read data
     */
     */
    public byte[] readByteArray(int bits) throws AccessException {
    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];
        byte[] arr = new byte[bytes];
        for (int i = 0; i < bytes; i++) {
        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));
            arr[i] = (byte)(read(increment) << (8 - increment));
        }
        }
        return arr;
        return arr;
+8 −8
Original line number Original line Diff line number Diff line
@@ -51,7 +51,7 @@ public class BitwiseOutputStream {
     */
     */
    public BitwiseOutputStream(int startingLength) {
    public BitwiseOutputStream(int startingLength) {
        mBuf = new byte[startingLength];
        mBuf = new byte[startingLength];
        mEnd = startingLength * 8;
        mEnd = startingLength << 3;
        mPos = 0;
        mPos = 0;
    }
    }


@@ -61,7 +61,7 @@ public class BitwiseOutputStream {
     * @return newly allocated byte array
     * @return newly allocated byte array
     */
     */
    public byte[] toByteArray() {
    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];
        byte[] newBuf = new byte[len];
        System.arraycopy(mBuf, 0, newBuf, 0, len);
        System.arraycopy(mBuf, 0, newBuf, 0, len);
        return newBuf;
        return newBuf;
@@ -74,8 +74,8 @@ public class BitwiseOutputStream {
     */
     */
    private void possExpand(int bits) {
    private void possExpand(int bits) {
        if ((mPos + bits) < mEnd) return;
        if ((mPos + bits) < mEnd) return;
        byte[] newBuf = new byte[((mPos + bits) * 2) / 8];
        byte[] newBuf = new byte[(mPos + bits) >>> 2];
        System.arraycopy(mBuf, 0, newBuf, 0, mEnd / 8);
        System.arraycopy(mBuf, 0, newBuf, 0, mEnd >>> 3);
        mBuf = newBuf;
        mBuf = newBuf;
    }
    }


@@ -91,12 +91,12 @@ public class BitwiseOutputStream {
        }
        }
        possExpand(bits);
        possExpand(bits);
        data &= (-1 >>> (32 - bits));
        data &= (-1 >>> (32 - bits));
        int index = mPos / 8;
        int index = mPos >>> 3;
        int offset = 16 - (mPos % 8) - bits;
        int offset = 16 - (mPos & 0x07) - bits;  // &7==%8
        data <<= offset;
        data <<= offset;
        mPos += bits;
        mPos += bits;
        mBuf[index] |= (data >>> 8);
        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 {
    public void writeByteArray(int bits, byte[] arr) throws AccessException {
        for (int i = 0; i < arr.length; i++) {
        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) {
            if (increment > 0) {
                write(increment, (byte)(arr[i] >>> (8 - increment)));
                write(increment, (byte)(arr[i] >>> (8 - increment)));
            }
            }
+25 −0
Original line number Original line Diff line number Diff line
@@ -23,6 +23,8 @@ import com.android.internal.util.HexDump;
import android.test.AndroidTestCase;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.test.suitebuilder.annotation.SmallTest;


import android.util.Log;

public class BitwiseStreamsTest extends AndroidTestCase {
public class BitwiseStreamsTest extends AndroidTestCase {
    private final static String LOG_TAG = "BitwiseStreamsTest";
    private final static String LOG_TAG = "BitwiseStreamsTest";


@@ -85,4 +87,27 @@ public class BitwiseStreamsTest extends AndroidTestCase {
        for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = inStream.read(8);
        for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = inStream.read(8);
        assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup));
        assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup));
    }
    }

    @SmallTest
    public void testFive() throws Exception {
        int num_runs = 10;
        long start = android.os.SystemClock.elapsedRealtime();
        for (int run = 0; run < num_runs; run++) {
            int offset = run % 8;
            byte[] inBuf = HexDump.hexStringToByteArray("00031040900112488ea794e0");
            BitwiseOutputStream outStream = new BitwiseOutputStream(30);
            outStream.skip(offset);
            for (int i = 0; i < inBuf.length; i++) {
                outStream.write(5, inBuf[i] >>> 3);
                outStream.write(3, inBuf[i] & 0x07);
            }
            BitwiseInputStream inStream = new BitwiseInputStream(outStream.toByteArray());
            inStream.skip(offset);
            byte[] inBufDup = new byte[inBuf.length];
            for (int i = 0; i < inBufDup.length; i++) inBufDup[i] = inStream.read(8);
            assertEquals(HexDump.toHexString(inBuf), HexDump.toHexString(inBufDup));
        }
        long end = android.os.SystemClock.elapsedRealtime();
        Log.d(LOG_TAG, "repeated encode-decode took " + (end - start) + " ms");
    }
}
}