diff options
author | Sven Gothel <[email protected]> | 2014-02-21 07:31:12 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2014-02-21 07:31:12 +0100 |
commit | c70c730b22c847668cf475dc6f841b85297ac3ab (patch) | |
tree | a2fe4b3d4f375e9ac3625c8c3ac4724257c3e7ba | |
parent | 8022ae51a072f5198409d3c81d9979456676d0cf (diff) |
Bitstream: Add static 'long toUint32Long(int)' and 'int toUint32Int(int)' conversion functions
-rwxr-xr-x | make/scripts/runtest.sh | 4 | ||||
-rw-r--r-- | src/java/com/jogamp/common/util/Bitstream.java | 42 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/util/BitstreamData.java | 8 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/util/TestBitstream00.java | 23 |
4 files changed, 62 insertions, 15 deletions
diff --git a/make/scripts/runtest.sh b/make/scripts/runtest.sh index e2f3d1a..aa162a6 100755 --- a/make/scripts/runtest.sh +++ b/make/scripts/runtest.sh @@ -103,7 +103,7 @@ function onetest() { #onetest com.jogamp.common.util.TestValueConversion 2>&1 | tee -a $LOG #onetest com.jogamp.common.util.TestSyncRingBuffer01 $* #onetest com.jogamp.common.util.TestLFRingBuffer01 $* -#onetest com.jogamp.common.util.TestBitstream00 2>&1 | tee -a $LOG +onetest com.jogamp.common.util.TestBitstream00 2>&1 | tee -a $LOG #onetest com.jogamp.common.util.TestBitstream01 2>&1 | tee -a $LOG #onetest com.jogamp.common.util.TestBitstream02 2>&1 | tee -a $LOG #onetest com.jogamp.common.util.TestBitstream03 2>&1 | tee -a $LOG @@ -116,7 +116,7 @@ function onetest() { #onetest com.jogamp.common.nio.TestBuffersFloatDoubleConversion 2>&1 | tee -a $LOG #onetest com.jogamp.common.nio.TestPointerBufferEndian 2>&1 | tee -a $LOG #onetest com.jogamp.common.nio.TestStructAccessorEndian 2>&1 | tee -a $LOG -onetest com.jogamp.common.os.TestElfReader01 2>&1 | tee -a $LOG +#onetest com.jogamp.common.os.TestElfReader01 2>&1 | tee -a $LOG #onetest com.jogamp.gluegen.PCPPTest 2>&1 | tee -a $LOG #onetest com.jogamp.gluegen.test.junit.generation.Test1p1JavaEmitter 2>&1 | tee -a $LOG #onetest com.jogamp.gluegen.test.junit.generation.Test1p2ProcAddressEmitter 2>&1 | tee -a $LOG diff --git a/src/java/com/jogamp/common/util/Bitstream.java b/src/java/com/jogamp/common/util/Bitstream.java index 7bf0c16..419b356 100644 --- a/src/java/com/jogamp/common/util/Bitstream.java +++ b/src/java/com/jogamp/common/util/Bitstream.java @@ -1166,11 +1166,9 @@ public class Bitstream<T> { * Return incoming int32 as read via {@link #readBits31(boolean, int)} * and swap bytes if !bigEndian. * <p> - * In case the returned value shall be interpreted as unsigned, - * it shall be cast to <code>long</code> as follows: - * <pre> - * final long l = 0xffffffffL & int32; - * </pre> + * In case the returned value shall be interpreted as <code>uint32_t</code> + * utilize {@link #toUint32Long(int)} or {@link #toUint32Int(int)} for + * an appropriate conversion. * </p> * @param msbFirst if true incoming stream bit order is MSB to LSB, otherwise LSB to MSB. * @param bigEndian if false, swap incoming bytes to little-endian, otherwise leave them as little-endian. @@ -1216,11 +1214,9 @@ public class Bitstream<T> { * Return incoming int32 as read via {@link #readBits31(boolean, int)} * and swap bytes if !bigEndian. * <p> - * In case the returned value shall be interpreted as unsigned, - * it shall be cast to <code>long</code> as follows: - * <pre> - * final long l = 0xffffffffL & int32; - * </pre> + * In case the returned value shall be interpreted as <code>uint32_t</code> + * utilize {@link #toUint32Long(int)} or {@link #toUint32Int(int)} for + * an appropriate conversion. * </p> * @param bigEndian if false, swap incoming bytes to little-endian, otherwise leave them as little-endian. * @return the 32bit value, which might be unsigned or 2-complement signed value. @@ -1303,6 +1299,32 @@ public class Bitstream<T> { } } + /** + * Reinterpret the given <code>int32_t</code> value as <code>uint32_t</code>, + * i.e. perform the following cast to <code>long</code>: + * <pre> + * final long l = 0xffffffffL & int32; + * </pre> + */ + public static final long toUint32Long(final int val) { + return 0xffffffffL & val; + } + + /** + * Returns the reinterpreted given <code>int32_t</code> value + * as <code>uint32_t</code> if < {@link Integer#MAX_VALUE} + * as within an <code>int</code> storage. + * Otherwise return -1. + */ + public static final int toUint32Int(final int val) { + final long v = toUint32Long(val); + if( v > Integer.MAX_VALUE ) { + return -1; + } else { + return (int)v; + } + } + public String toString() { return String.format("Bitstream[%s]", toStringImpl()); } diff --git a/src/junit/com/jogamp/common/util/BitstreamData.java b/src/junit/com/jogamp/common/util/BitstreamData.java index 93e8ad0..b5c2c62 100644 --- a/src/junit/com/jogamp/common/util/BitstreamData.java +++ b/src/junit/com/jogamp/common/util/BitstreamData.java @@ -83,6 +83,9 @@ public class BitstreamData { public static String toHexString(int v) { return "0x"+Integer.toHexString(v); } + public static String toHexString(long v) { + return "0x"+Long.toHexString(v); + } public static final String strZeroPadding= "0000000000000000000000000000000000000000000000000000000000000000"; // 64 public static String toBinaryString(int v, int bitCount) { if( 0 == bitCount ) { @@ -113,7 +116,8 @@ public class BitstreamData { return String.format("[%0"+nibbles+"X, %s]", v, toBinaryString(v, bitCount)); } public static String toUnsignedBinaryString(final int int32) { - final long l = 0xffffffffL & int32; - return l+", "+toHexBinaryString(l, 32); + final long l = Bitstream.toUint32Long(int32); + final int i = Bitstream.toUint32Int(int32); + return "(long)"+l+", (int)"+i+", "+toHexBinaryString(l, 32); } } diff --git a/src/junit/com/jogamp/common/util/TestBitstream00.java b/src/junit/com/jogamp/common/util/TestBitstream00.java index ef1fd45..47ed1cf 100644 --- a/src/junit/com/jogamp/common/util/TestBitstream00.java +++ b/src/junit/com/jogamp/common/util/TestBitstream00.java @@ -35,10 +35,13 @@ import java.nio.IntBuffer; import java.nio.LongBuffer; import org.junit.Test; +import org.junit.Assert; import com.jogamp.common.nio.Buffers; import com.jogamp.common.os.Platform; + import static com.jogamp.common.util.BitstreamData.*; + import com.jogamp.junit.util.JunitTracer; import org.junit.FixMethodOrder; @@ -90,7 +93,25 @@ public class TestBitstream00 extends JunitTracer { } @Test - public void test01ShiftSigned() { + public void test01Uint32Conversion() { + testUInt32Conversion(1, 1); + testUInt32Conversion(Integer.MAX_VALUE, Integer.MAX_VALUE); + testUInt32Conversion(0xffff0000, -1); + testUInt32Conversion(0xffffffff, -1); + } + void testUInt32Conversion(final int int32, final int expUint32Int) { + final String int32_hStr = toHexString(int32); + final long l = Bitstream.toUint32Long(int32); + final String l_hStr = toHexString(l); + final int i = Bitstream.toUint32Int(int32); + final String i_hStr = toHexString(i); + System.err.printf("int32_t %012d %10s -> (long) %012d %10s, (int) %012d %10s%n", int32, int32_hStr, l, l_hStr, i, i_hStr); + Assert.assertEquals(int32_hStr, l_hStr); + Assert.assertEquals(expUint32Int, i); + } + + @Test + public void test02ShiftSigned() { shiftSigned(0xA0000000); // negative w/ '1010' top-nibble shiftSigned(-1); } |