| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Flanderra
Revision: 23
Author: pcherkas
Date: 04 Nov 2008 17:56:28
Changes:bit logic refactoring and optimization
Files:| ... | ...@@ -15,10 +15,9 @@ | |
| 15 | 15 | public class BitUtilsTest { |
| 16 | 16 | private Log log = LogFactory.getLog(BitUtilsTest.class); |
| 17 | 17 | |
| 18 | @Ignore | |
| 18 | @Test | |
| 19 | 19 | public void testBinHex() { |
| 20 | 20 | String ptnHex = "00 01 02 03 04 05"; |
| 21 | // String ptnBin = "00000000 01010101 01010101 00110011 00110011"; | |
| 22 | 21 | |
| 23 | 22 | byte[] res1 = BitUtils.hex(ptnHex); |
| 24 | 23 | byte[] res2 = BitUtils._hex(ptnHex); |
| ... | ...@@ -42,12 +41,22 @@ | |
| 42 | 41 | bits2.setOffset(offset); |
| 43 | 42 | reverse1 = BitUtils.bin(bits1, offset); |
| 44 | 43 | reverse2 = BitUtils._bin(bits2); |
| 45 | ||
| 46 | 44 | Assert.assertEquals(reverse1.trim(), reverse2.trim()); |
| 47 | 45 | } |
| 46 | ||
| 47 | BitSet bits1a = BitUtils.bytesToBits(new byte[]{-128, 126, 127}); | |
| 48 | Bits bits2a = BitUtils._bytesToBits(new byte[]{-128, 126, 127}); | |
| 49 | Bits bits3a = BitUtils._bytesToBits(new byte[]{0,0,0}); | |
| 50 | Bits bits4a = BitUtils._bytesToBits(new byte[]{0,0,0}); | |
| 51 | for (int i = 0; i < 24; i++) { | |
| 52 | bits3a.setBit(i, bits2a.getBitAsInt(i)); | |
| 53 | bits4a.setBit(i, bits1a.get(i)); | |
| 54 | } | |
| 55 | Assert.assertEquals(BitUtils._bin(bits2a), BitUtils._bin(bits3a)); | |
| 56 | Assert.assertEquals(BitUtils._bin(bits3a), BitUtils._bin(bits4a)); | |
| 48 | 57 | } |
| 49 | 58 | |
| 50 | @Ignore | |
| 59 | @Test | |
| 51 | 60 | public void testBitIntegers() { |
| 52 | 61 | Random rint = new Random(); |
| 53 | 62 | for (long testValue = 0, i = 0; i <= 100000; testValue = rint.nextLong(), i++) { |
| ... | ...@@ -63,17 +72,145 @@ | |
| 63 | 72 | Assert.assertEquals(BitUtils._bitsToLong(result2, 0, 64, false), BitUtils._bitsToLong(result2, false)); |
| 64 | 73 | Assert.assertEquals(BitUtils._bitsToLong(result2, 0, 64, true), BitUtils._bitsToLong(result2, true)); |
| 65 | 74 | } |
| 75 | ||
| 76 | byte[] b1 = null; | |
| 77 | byte[] b2 = null; | |
| 78 | for (long testValue = BitUtils.MIN_UI8; testValue <= BitUtils.MAX_UI8; testValue++) { | |
| 79 | Assert.assertEquals(BitUtils._bin(BitUtils._bitsUI8(testValue)), BitUtils.bin(BitUtils.bitsUI8(testValue), 8)); | |
| 80 | b1 = BitUtils._bytesUI8(testValue); | |
| 81 | b2 = BitUtils.bytesUI8(testValue); | |
| 82 | Assert.assertArrayEquals(b1, b2); | |
| 83 | Assert.assertEquals(BitUtils._parseUI8(b1), testValue); | |
| 84 | ||
| 85 | Assert.assertEquals(BitUtils._bin(BitUtils._bitsUI16(testValue)), BitUtils.bin(BitUtils.bitsUI16(testValue), 16)); | |
| 86 | b1 = BitUtils._bytesUI16(testValue); | |
| 87 | b2 = BitUtils.bytesUI16(testValue); | |
| 88 | Assert.assertArrayEquals(b1, b2); | |
| 89 | Assert.assertEquals(BitUtils._parseUI16(b1), testValue); | |
| 90 | ||
| 91 | Assert.assertEquals(BitUtils._bin(BitUtils._bitsUI24(testValue)), BitUtils.bin(BitUtils.bitsUI24(testValue), 24)); | |
| 92 | b1 = BitUtils._bytesUI24(testValue); | |
| 93 | b2 = BitUtils.bytesUI24(testValue); | |
| 94 | Assert.assertArrayEquals(b1, b2); | |
| 95 | Assert.assertEquals(BitUtils._parseUI24(b1), testValue); | |
| 96 | ||
| 97 | Assert.assertEquals(BitUtils._bin(BitUtils._bitsUI32(testValue)), BitUtils.bin(BitUtils.bitsUI32(testValue), 32)); | |
| 98 | b1 = BitUtils._bytesUI32(testValue); | |
| 99 | b2 = BitUtils.bytesUI32(testValue); | |
| 100 | Assert.assertArrayEquals(b1, b2); | |
| 101 | Assert.assertEquals(BitUtils._parseUI32(b1), testValue); | |
| 102 | ||
| 103 | } | |
| 104 | ||
| 105 | for (long testValue = BitUtils.MIN_SI8; testValue <= BitUtils.MAX_SI8; testValue++) { | |
| 106 | Assert.assertEquals(BitUtils._bin(BitUtils._bitsSI8(testValue)), BitUtils.bin(BitUtils.bitsSI8(testValue), 8)); | |
| 107 | b1 = BitUtils._bytesSI8(testValue); | |
| 108 | b2 = BitUtils.bytesSI8(testValue); | |
| 109 | Assert.assertArrayEquals(b1, b2); | |
| 110 | Assert.assertEquals(BitUtils._parseSI8(b1), testValue); | |
| 111 | ||
| 112 | Assert.assertEquals(BitUtils._bin(BitUtils._bitsSI16(testValue)), BitUtils.bin(BitUtils.bitsSI16(testValue), 16)); | |
| 113 | b1 = BitUtils._bytesSI16(testValue); | |
| 114 | b2 = BitUtils.bytesSI16(testValue); | |
| 115 | Assert.assertArrayEquals(b1, b2); | |
| 116 | Assert.assertEquals(BitUtils._parseSI16(b1), testValue); | |
| 117 | ||
| 118 | Assert.assertEquals(BitUtils._bin(BitUtils._bitsSI32(testValue)), BitUtils.bin(BitUtils.bitsSI32(testValue), 32)); | |
| 119 | b1 = BitUtils._bytesSI32(testValue); | |
| 120 | b2 = BitUtils.bytesSI32(testValue); | |
| 121 | Assert.assertArrayEquals(b1, b2); | |
| 122 | Assert.assertEquals(BitUtils._parseSI32(b1), testValue); | |
| 123 | ||
| 124 | } | |
| 125 | ||
| 66 | 126 | } |
| 67 | 127 | |
| 68 | @Ignore | |
| 69 | public void testMaxBitCounts() { | |
| 70 | Random rint = new Random(); | |
| 71 | long[] longValues = new long[] { rint.nextLong(), rint.nextLong(), rint.nextLong(), rint.nextLong(), rint.nextLong(), | |
| 72 | rint.nextLong(), rint.nextLong() }; | |
| 73 | Assert.assertEquals(BitUtils.calculateMaxBitCountSI(longValues), BitUtils._calculateMaxBitCountSI(longValues)); | |
| 128 | @Test | |
| 129 | public void testBitFloats() { | |
| 130 | byte[] b1 = null; | |
| 131 | byte[] b2 = null; | |
| 132 | for (double testValue = BitUtils.MIN_FIXED88; testValue <= BitUtils.MAX_FIXED88; testValue++) { | |
| 133 | Assert.assertEquals(BitUtils._bin(BitUtils._bitsFixed88(testValue)), BitUtils.bin(BitUtils.bitsFixed88(testValue), 16)); | |
| 134 | b1 = BitUtils._bytesFixed88(testValue); | |
| 135 | b2 = BitUtils.bytesFixed88(testValue); | |
| 136 | Assert.assertArrayEquals(b1, b2); | |
| 137 | Assert.assertEquals(BitUtils._parseFixed88(b1), testValue); | |
| 138 | ||
| 139 | Assert.assertEquals(BitUtils._bin(BitUtils._bitsFixed1616(testValue)), BitUtils.bin(BitUtils.bitsFixed1616(testValue), 32)); | |
| 140 | b1 = BitUtils._bytesFixed1616(testValue); | |
| 141 | b2 = BitUtils.bytesFixed1616(testValue); | |
| 142 | Assert.assertArrayEquals(b1, b2); | |
| 143 | Assert.assertEquals(BitUtils._parseFixed1616(b1), testValue); | |
| 144 | } | |
| 145 | for (double testValue = BitUtils.MIN_FLOAT16; testValue <= BitUtils.MAX_FLOAT16; testValue++) { | |
| 146 | Assert.assertEquals(BitUtils._bin(BitUtils._bitsFloat16(testValue)), BitUtils.bin(BitUtils.bitsFloat16(testValue), 16)); | |
| 147 | b1 = BitUtils._bytesFloat16(testValue); | |
| 148 | b2 = BitUtils.bytesFloat16(testValue); | |
| 149 | Assert.assertArrayEquals(b1, b2); | |
| 150 | Assert.assertEquals(BitUtils.parseFloat16(b2), BitUtils._parseFloat16(b1), testValue/1000.0); | |
| 151 | ||
| 152 | Assert.assertEquals(BitUtils._bin(BitUtils._bitsFloat32(testValue)), BitUtils.bin(BitUtils.bitsFloat32(testValue), 32)); | |
| 153 | b1 = BitUtils._bytesFloat32(testValue); | |
| 154 | b2 = BitUtils.bytesFloat32(testValue); | |
| 155 | Assert.assertArrayEquals(b1, b2); | |
| 156 | Assert.assertEquals(BitUtils.parseFloat32(b2), BitUtils._parseFloat32(b1), testValue/1000.0); | |
| 157 | ||
| 158 | Assert.assertEquals(BitUtils._bin(BitUtils._bitsFloat64(testValue)), BitUtils.bin(BitUtils.bitsFloat64(testValue), 64)); | |
| 159 | b1 = BitUtils._bytesFloat64(testValue); | |
| 160 | b2 = BitUtils.bytesFloat64(testValue); | |
| 161 | Assert.assertArrayEquals(b1, b2); | |
| 162 | Assert.assertEquals(BitUtils.parseFloat64(b2), BitUtils._parseFloat64(b1), testValue/1000.0); | |
| 163 | ||
| 164 | } | |
| 165 | } | |
| 166 | ||
| 167 | @Test | |
| 168 | public void testEncoded() { | |
| 169 | byte[] b1 = null; | |
| 170 | byte[] b2 = null; | |
| 171 | for (byte testValue = 0; testValue < 5; testValue++) { | |
| 172 | byte[] arr = new byte[]{testValue, | |
| 173 | (byte) (testValue + 1), | |
| 174 | (byte) (testValue + 1), | |
| 175 | (byte) (testValue + 1), | |
| 176 | (byte) (testValue + 1)}; | |
| 177 | Assert.assertArrayEquals(BitUtils.bytesEncodedU32(arr), BitUtils._bytesEncodedU32(arr)); | |
| 178 | Assert.assertEquals(BitUtils.bin(BitUtils.bitsEncodedU32(arr), 32), BitUtils._bin(BitUtils._bitsEncodedU32(arr))); | |
| 179 | } | |
| 74 | 180 | } |
| 75 | 181 | |
| 76 | @Ignore | |
| 182 | ||
| 183 | @Test | |
| 184 | public void testBitArrays() { | |
| 185 | BitSet b1 = null; | |
| 186 | Bits b2 = null; | |
| 187 | for (long testValue = BitUtils.MIN_SI8; testValue <= BitUtils.MAX_SI8; testValue++) { | |
| 188 | b1 = BitUtils.bitsSBArray(testValue, 8); | |
| 189 | b2 = BitUtils._bitsSBArray(testValue, 8); | |
| 190 | Assert.assertEquals(BitUtils.bin(b1, 8), BitUtils._bin(b2)); | |
| 191 | Assert.assertEquals(BitUtils.parseSBArray(b1, 8), testValue); | |
| 192 | Assert.assertEquals(BitUtils._parseSBArray(b2), testValue); | |
| 193 | } | |
| 194 | ||
| 195 | for (long testValue = BitUtils.MIN_UI8; testValue <= BitUtils.MAX_UI8; testValue++) { | |
| 196 | b1 = BitUtils.bitsUBArray(testValue, 8); | |
| 197 | b2 = BitUtils._bitsUBArray(testValue, 8); | |
| 198 | Assert.assertEquals(BitUtils.bin(b1, 8), BitUtils._bin(b2)); | |
| 199 | Assert.assertEquals(BitUtils.parseUBArray(b1, 8), testValue); | |
| 200 | Assert.assertEquals(BitUtils._parseUBArray(b2), testValue); | |
| 201 | } | |
| 202 | ||
| 203 | for (double testValue = BitUtils.MIN_FIXED1616; testValue <= BitUtils.MAX_FIXED1616; testValue++) { | |
| 204 | b1 = BitUtils.bitsFBArray(testValue, 32); | |
| 205 | b2 = BitUtils._bitsFBArray(testValue, 32); | |
| 206 | Assert.assertEquals(BitUtils.bin(b1, 32), BitUtils._bin(b2)); | |
| 207 | Assert.assertEquals(BitUtils.parseFBArray(b1, 32), testValue); | |
| 208 | Assert.assertEquals(BitUtils._parseFBArray(b2), testValue); | |
| 209 | } | |
| 210 | ||
| 211 | } | |
| 212 | ||
| 213 | @Test | |
| 77 | 214 | public void testFlipBytes() { |
| 78 | 215 | Random rint = new Random(); |
| 79 | 216 | for (long testValue = 0, i = 0; i <= 100000; testValue = rint.nextLong(), i++) { |
| ... | ...@@ -90,32 +227,57 @@ | |
| 90 | 227 | BitSet result1 = BitUtils.longToBits(testValue, 64); |
| 91 | 228 | Bits result2 = BitUtils._longToBits(testValue, 64); |
| 92 | 229 | Assert.assertEquals( |
| 230 | BitUtils._bin(result2), | |
| 231 | BitUtils._bin( | |
| 232 | BitUtils._concat( | |
| 233 | BitUtils._concat( | |
| 234 | BitUtils._sub(result2, 0, 19), | |
| 235 | BitUtils._sub(result2, 20, 39)), | |
| 236 | BitUtils._concat( | |
| 237 | BitUtils._sub(result2, 40, 59), | |
| 238 | BitUtils._sub(result2, 60, 63) | |
| 239 | )) | |
| 240 | ) | |
| 241 | ); | |
| 242 | ||
| 243 | Assert.assertEquals( | |
| 244 | BitUtils._bin(result2), | |
| 245 | BitUtils._bin( | |
| 246 | BitUtils._concat(new Bits[]{ | |
| 247 | BitUtils._sub(result2, 0, 20), | |
| 248 | BitUtils._sub(result2, 21, 40), | |
| 249 | BitUtils._sub(result2, 41, 60), | |
| 250 | BitUtils._sub(result2, 61, 63) | |
| 251 | })) | |
| 252 | ); | |
| 253 | Assert.assertEquals( | |
| 93 | 254 | BitUtils.bin(BitUtils.concat(result1, result1, 64, 64), 128), |
| 94 | 255 | BitUtils._bin(BitUtils._concat(result2, result2))); |
| 95 | 256 | Assert.assertEquals(BitUtils._bin(BitUtils._sub(BitUtils._concat(result2, result2), 0, 63)), |
| 96 | 257 | BitUtils._bin(BitUtils._sub(BitUtils._concat(result2, result2), 64, 127))); |
| 97 | 258 | } |
| 98 | 259 | } |
| 260 | ||
| 261 | @Test | |
| 262 | public void testMaxBitCount() { | |
| 263 | for(long i = -50; i < 50; i++){ | |
| 264 | Assert.assertEquals( | |
| 265 | BitUtils.calculateMaxBitCountSI(new long []{i}), | |
| 266 | BitUtils._calculateMaxBitCountSI(new long []{i})); | |
| 267 | } | |
| 99 | 268 | |
| 269 | for(long i = 0; i < 100; i++){ | |
| 270 | long[] arr = new long []{i}; | |
| 271 | Assert.assertEquals( | |
| 272 | BitUtils.calculateMaxBitCountUI(arr), | |
| 273 | BitUtils._calculateMaxBitCountUI(arr)); | |
| 274 | } | |
| 100 | 275 | |
| 101 | ||
| 102 | ||
| 103 | ||
| 104 | ||
| 105 | ||
| 106 | ||
| 107 | ||
| 108 | ||
| 109 | ||
| 110 | ||
| 111 | ||
| 112 | ||
| 113 | ||
| 114 | ||
| 115 | ||
| 116 | ||
| 117 | ||
| 118 | ||
| 119 | ||
| 120 | ||
| 276 | for(long i = 0; i < 100; i++){ | |
| 277 | long[] arr = new long []{i}; | |
| 278 | Assert.assertEquals( | |
| 279 | BitUtils._calculateMaxBitCountUI(arr) + 1, | |
| 280 | BitUtils._calculateMaxBitCountSI(arr)); | |
| 281 | } | |
| 282 | } | |
| 121 | 283 | } |
| ... | ...@@ -2,7 +2,6 @@ | |
| 2 | 2 | |
| 3 | 3 | import java.io.ByteArrayOutputStream; |
| 4 | 4 | import java.io.IOException; |
| 5 | import java.text.NumberFormat; | |
| 6 | 5 | import java.util.Arrays; |
| 7 | 6 | import java.util.BitSet; |
| 8 | 7 | |
| ... | ...@@ -81,6 +80,9 @@ | |
| 81 | 80 | // /////////////////////////////////////////// |
| 82 | 81 | // utility methods for basic types |
| 83 | 82 | // /////////////////////////////////////////// |
| 83 | /** | |
| 84 | * @deprecated | |
| 85 | */ | |
| 84 | 86 | public static BitSet bitsUI8(long val) { |
| 85 | 87 | Assert.inRange(val, MIN_UI8, MAX_UI8); |
| 86 | 88 | BitSet bitset = longToBits(val, 8); |
| ... | ...@@ -88,187 +90,246 @@ | |
| 88 | 90 | } |
| 89 | 91 | |
| 90 | 92 | public static Bits _bitsUI8(long val) { |
| 91 | //TODO: implement this | |
| 92 | throw new IllegalStateException("not implemented yet"); | |
| 93 | Assert.inRange(val, MIN_UI8, MAX_UI8); | |
| 94 | Bits bits = _longToBits(val, 8); | |
| 95 | return bits; | |
| 93 | 96 | } |
| 94 | 97 | |
| 98 | /** | |
| 99 | * @deprecated | |
| 100 | */ | |
| 95 | 101 | public static byte[] bytesUI8(long val) { |
| 96 | 102 | return bitsToBytes(bitsUI8(val), 8); |
| 97 | 103 | } |
| 98 | 104 | |
| 99 | 105 | public static byte[] _bytesUI8(long val) { |
| 100 | //TODO: implement this | |
| 101 | throw new IllegalStateException("not implemented yet"); | |
| 106 | return _bitsToBytes(_bitsUI8(val)); | |
| 102 | 107 | } |
| 103 | 108 | |
| 109 | /** | |
| 110 | * @deprecated | |
| 111 | */ | |
| 104 | 112 | public static long parseUI8(byte[] val) { |
| 105 | 113 | return bytesToLong(new byte[] { val[0] }, false); |
| 106 | 114 | } |
| 107 | 115 | |
| 108 | 116 | public static long _parseUI8(byte[] val) { |
| 109 | //TODO: implement this | |
| 110 | throw new IllegalStateException("not implemented yet"); | |
| 117 | return bytesToLong(new byte[] { val[0] }, false); | |
| 111 | 118 | } |
| 112 | 119 | |
| 120 | /** | |
| 121 | * @deprecated | |
| 122 | */ | |
| 113 | 123 | public static BitSet bitsUI16(long val) { |
| 114 | 124 | Assert.inRange(val, MIN_UI16, MAX_UI16); |
| 115 | 125 | return flipBytes(longToBits(val, 16), 2); |
| 116 | 126 | } |
| 117 | 127 | |
| 118 | 128 | public static Bits _bitsUI16(long val) { |
| 119 | //TODO: implement this | |
| 120 | throw new IllegalStateException("not implemented yet"); | |
| 129 | Assert.inRange(val, MIN_UI16, MAX_UI16); | |
| 130 | return _flipBytes(_longToBits(val, 16)); | |
| 121 | 131 | } |
| 122 | 132 | |
| 133 | /** | |
| 134 | * @deprecated | |
| 135 | */ | |
| 123 | 136 | public static byte[] bytesUI16(long val) { |
| 124 | 137 | return bitsToBytes(bitsUI16(val), 16); |
| 125 | 138 | } |
| 126 | 139 | |
| 127 | 140 | public static byte[] _bytesUI16(long val) { |
| 128 | //TODO: implement this | |
| 129 | throw new IllegalStateException("not implemented yet"); | |
| 141 | return _bitsToBytes(_bitsUI16(val)); | |
| 130 | 142 | } |
| 131 | 143 | |
| 144 | /** | |
| 145 | * @deprecated | |
| 146 | */ | |
| 132 | 147 | public static long parseUI16(byte[] val) { |
| 133 | 148 | return bytesToLong(new byte[] { val[0], val[1] }, false); |
| 134 | 149 | } |
| 135 | 150 | |
| 136 | 151 | public static long _parseUI16(byte[] val) { |
| 137 | //TODO: implement this | |
| 138 | throw new IllegalStateException("not implemented yet"); | |
| 152 | return bytesToLong(new byte[] { val[0], val[1] }, false); | |
| 139 | 153 | } |
| 140 | 154 | |
| 155 | /** | |
| 156 | * @deprecated | |
| 157 | */ | |
| 141 | 158 | public static BitSet bitsUI24(long val) { |
| 142 | 159 | Assert.inRange(val, MIN_UI24, MAX_UI24); |
| 143 | 160 | return flipBytes(longToBits(val, 24), 3); |
| 144 | 161 | } |
| 145 | 162 | |
| 146 | 163 | public static Bits _bitsUI24(long val) { |
| 147 | //TODO: implement this | |
| 148 | throw new IllegalStateException("not implemented yet"); | |
| 164 | Assert.inRange(val, MIN_UI24, MAX_UI24); | |
| 165 | return _flipBytes(_longToBits(val, 24)); | |
| 149 | 166 | } |
| 150 | 167 | |
| 168 | /** | |
| 169 | * @deprecated | |
| 170 | */ | |
| 151 | 171 | public static byte[] bytesUI24(long val) { |
| 152 | 172 | return bitsToBytes(bitsUI24(val), 24); |
| 153 | 173 | } |
| 154 | 174 | |
| 155 | 175 | public static byte[] _bytesUI24(long val) { |
| 156 | //TODO: implement this | |
| 157 | throw new IllegalStateException("not implemented yet"); | |
| 176 | return _bitsToBytes(_bitsUI24(val)); | |
| 158 | 177 | } |
| 159 | 178 | |
| 179 | /** | |
| 180 | * @deprecated | |
| 181 | */ | |
| 160 | 182 | public static long parseUI24(byte[] val) { |
| 161 | 183 | return bytesToLong(new byte[] { val[0], val[1], val[2] }, false); |
| 162 | 184 | } |
| 163 | 185 | |
| 164 | 186 | public static long _parseUI24(byte[] val) { |
| 165 | //TODO: implement this | |
| 166 | throw new IllegalStateException("not implemented yet"); | |
| 187 | return bytesToLong(new byte[] { val[0], val[1], val[2] }, false); | |
| 167 | 188 | } |
| 168 | 189 | |
| 190 | /** | |
| 191 | * @deprecated | |
| 192 | */ | |
| 169 | 193 | public static BitSet bitsUI32(long val) { |
| 170 | 194 | Assert.inRange(val, MIN_UI32, MAX_UI32); |
| 171 | 195 | return flipBytes(longToBits(val, 32), 4); |
| 172 | 196 | } |
| 173 | 197 | |
| 174 | 198 | public static Bits _bitsUI32(long val) { |
| 175 | //TODO: implement this | |
| 176 | throw new IllegalStateException("not implemented yet"); | |
| 199 | Assert.inRange(val, MIN_UI32, MAX_UI32); | |
| 200 | return _flipBytes(_longToBits(val, 32)); | |
| 177 | 201 | } |
| 178 | 202 | |
| 203 | /** | |
| 204 | * @deprecated | |
| 205 | */ | |
| 179 | 206 | public static byte[] bytesUI32(long val) { |
| 180 | 207 | return bitsToBytes(bitsUI32(val), 32); |
| 181 | 208 | } |
| 182 | 209 | |
| 183 | 210 | public static byte[] _bytesUI32(long val) { |
| 184 | //TODO: implement this | |
| 185 | throw new IllegalStateException("not implemented yet"); | |
| 211 | return _bitsToBytes(_bitsUI32(val)); | |
| 186 | 212 | } |
| 187 | 213 | |
| 214 | /** | |
| 215 | * @deprecated | |
| 216 | */ | |
| 188 | 217 | public static long parseUI32(byte[] val) { |
| 189 | 218 | return bytesToLong(new byte[] { val[0], val[1], val[2], val[3] }, false); |
| 190 | 219 | } |
| 191 | 220 | |
| 192 | 221 | public static long _parseUI32(byte[] val) { |
| 193 | //TODO: implement this | |
| 194 | throw new IllegalStateException("not implemented yet"); | |
| 222 | return bytesToLong(new byte[] { val[0], val[1], val[2], val[3] }, false); | |
| 195 | 223 | } |
| 196 | 224 | |
| 225 | /** | |
| 226 | * @deprecated | |
| 227 | */ | |
| 197 | 228 | public static BitSet bitsSI8(long val) { |
| 198 | 229 | Assert.inRange(val, MIN_SI8, MAX_SI8); |
| 199 | 230 | return longToBits(val, 8); |
| 200 | 231 | } |
| 201 | 232 | |
| 202 | 233 | public static Bits _bitsSI8(long val) { |
| 203 | //TODO: implement this | |
| 204 | throw new IllegalStateException("not implemented yet"); | |
| 234 | Assert.inRange(val, MIN_SI8, MAX_SI8); | |
| 235 | return _longToBits(val, 8); | |
| 205 | 236 | } |
| 206 | 237 | |
| 238 | /** | |
| 239 | * @deprecated | |
| 240 | */ | |
| 207 | 241 | public static byte[] bytesSI8(long val) { |
| 208 | 242 | return bitsToBytes(bitsSI8(val), 8); |
| 209 | 243 | } |
| 210 | 244 | |
| 211 | 245 | public static byte[] _bytesSI8(long val) { |
| 212 | //TODO: implement this | |
| 213 | throw new IllegalStateException("not implemented yet"); | |
| 246 | return _bitsToBytes(_bitsSI8(val)); | |
| 214 | 247 | } |
| 215 | 248 | |
| 249 | /** | |
| 250 | * @deprecated | |
| 251 | */ | |
| 216 | 252 | public static long parseSI8(byte[] val) { |
| 217 | 253 | return bytesToLong(new byte[] { val[0] }, true); |
| 218 | 254 | } |
| 219 | 255 | |
| 220 | 256 | public static long _parseSI8(byte[] val) { |
| 221 | //TODO: implement this | |
| 222 | throw new IllegalStateException("not implemented yet"); | |
| 257 | return bytesToLong(new byte[] { val[0] }, true); | |
| 223 | 258 | } |
| 224 | 259 | |
| 260 | /** | |
| 261 | * @deprecated | |
| 262 | */ | |
| 225 | 263 | public static BitSet bitsSI16(long val) { |
| 226 | 264 | Assert.inRange(val, MIN_SI16, MAX_SI16); |
| 227 | 265 | return flipBytes(longToBits(val, 16), 2); |
| 228 | 266 | } |
| 229 | 267 | |
| 230 | 268 | public static Bits _bitsSI16(long val) { |
| 231 | //TODO: implement this | |
| 232 | throw new IllegalStateException("not implemented yet"); | |
| 269 | Assert.inRange(val, MIN_SI16, MAX_SI16); | |
| 270 | return _flipBytes(_longToBits(val, 16)); | |
| 233 | 271 | } |
| 234 | 272 | |
| 273 | /** | |
| 274 | * @deprecated | |
| 275 | */ | |
| 235 | 276 | public static byte[] bytesSI16(long val) { |
| 236 | 277 | return bitsToBytes(bitsSI16(val), 16); |
| 237 | 278 | } |
| 238 | 279 | |
| 239 | 280 | public static byte[] _bytesSI16(long val) { |
| 240 | //TODO: implement this | |
| 241 | throw new IllegalStateException("not implemented yet"); | |
| 281 | return _bitsToBytes(_bitsSI16(val)); | |
| 242 | 282 | } |
| 243 | 283 | |
| 284 | /** | |
| 285 | * @deprecated | |
| 286 | */ | |
| 244 | 287 | public static long parseSI16(byte[] val) { |
| 245 | 288 | return bytesToLong(new byte[] { val[0], val[1] }, true); |
| 246 | 289 | } |
| 247 | 290 | |
| 248 | 291 | public static long _parseSI16(byte[] val) { |
| 249 | //TODO: implement this | |
| 250 | throw new IllegalStateException("not implemented yet"); | |
| 292 | return bytesToLong(new byte[] { val[0], val[1] }, true); | |
| 251 | 293 | } |
| 252 | 294 | |
| 295 | /** | |
| 296 | * @deprecated | |
| 297 | */ | |
| 253 | 298 | public static BitSet bitsSI32(long val) { |
| 254 | 299 | Assert.inRange(val, MIN_SI32, MAX_SI32); |
| 255 | 300 | return flipBytes(longToBits(val, 32), 4); |
| 256 | 301 | } |
| 257 | 302 | |
| 258 | 303 | public static Bits _bitsSI32(long val) { |
| 259 | //TODO: implement this | |
| 260 | throw new IllegalStateException("not implemented yet"); | |
| 304 | Assert.inRange(val, MIN_SI32, MAX_SI32); | |
| 305 | return _flipBytes(_longToBits(val, 32)); | |
| 261 | 306 | } |
| 262 | 307 | |
| 308 | /** | |
| 309 | * @deprecated | |
| 310 | */ | |
| 263 | 311 | public static byte[] bytesSI32(long val) { |
| 264 | 312 | return bitsToBytes(bitsSI32(val), 32); |
| 265 | 313 | } |
| 266 | 314 | |
| 267 | 315 | public static byte[] _bytesSI32(long val) { |
| 268 | //TODO: implement this | |
| 269 | throw new IllegalStateException("not implemented yet"); | |
| 316 | return _bitsToBytes(_bitsSI32(val)); | |
| 317 | } | |
| 318 | ||
| 319 | /** | |
| 320 | * @deprecated | |
| 321 | */ | |
| 322 | public static long parseSI32(byte[] val) { | |
| 323 | return bytesToLong(new byte[] { val[0], val[1], val[2], val[3] }, true); | |
| 324 | } | |
| 325 | ||
| 326 | public static long _parseSI32(byte[] val) { | |
| 327 | return bytesToLong(new byte[] { val[0], val[1], val[2], val[3] }, true); | |
| 270 | 328 | } |
| 271 | 329 | |
| 330 | /** | |
| 331 | * @deprecated | |
| 332 | */ | |
| 272 | 333 | public static byte[] bytesSTRING(String val) { |
| 273 | 334 | try { |
| 274 | 335 | if (val == null || val.equals("")) { |
| ... | ...@@ -285,48 +346,58 @@ | |
| 285 | 346 | } |
| 286 | 347 | |
| 287 | 348 | public static byte[] _bytesSTRING(String val) { |
| 288 | //TODO: implement this | |
| 289 | throw new IllegalStateException("not implemented yet"); | |
| 290 | } | |
| 291 | ||
| 292 | ||
| 293 | public static long parseSI32(byte[] val) { | |
| 294 | return bytesToLong(new byte[] { val[0], val[1], val[2], val[3] }, true); | |
| 295 | } | |
| 296 | ||
| 297 | public static long _parseSI32(byte[] val) { | |
| 298 | //TODO: implement this | |
| 299 | throw new IllegalStateException("not implemented yet"); | |
| 349 | try { | |
| 350 | if (val == null || val.equals("")) { | |
| 351 | return new byte[] { 0x00 }; | |
| 352 | } | |
| 353 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
| 354 | baos.write(val.getBytes("UTF-8")); | |
| 355 | baos.write(0x00); | |
| 356 | return baos.toByteArray(); | |
| 357 | } catch (IOException e) { | |
| 358 | log.error(e); | |
| 359 | throw new RuntimeException(e); | |
| 360 | } | |
| 300 | 361 | } |
| 301 | 362 | |
| 363 | /** | |
| 364 | * @deprecated | |
| 365 | */ | |
| 302 | 366 | public static BitSet bitsFixed88(double val) { |
| 303 | 367 | Assert.inRange(val, MIN_FIXED88, MAX_FIXED88); |
| 304 | 368 | return bitsSI16((short) (val * 256.0)); |
| 305 | 369 | } |
| 306 | 370 | |
| 307 | 371 | public static Bits _bitsFixed88(double val){ |
| 308 | //TODO: implement this | |
| 309 | throw new IllegalStateException("not implemented yet"); | |
| 372 | Assert.inRange(val, MIN_FIXED88, MAX_FIXED88); | |
| 373 | return _bitsSI16((short) (val * 256.0)); | |
| 310 | 374 | } |
| 311 | 375 | |
| 376 | /** | |
| 377 | * @deprecated | |
| 378 | */ | |
| 312 | 379 | public static byte[] bytesFixed88(double val) { |
| 313 | 380 | return bitsToBytes(bitsFixed88(val), 16); |
| 314 | 381 | } |
| 315 | 382 | |
| 316 | 383 | public static byte[] _bytesFixed88(double val) { |
| 317 | //TODO: implement this | |
| 318 | throw new IllegalStateException("not implemented yet"); | |
| 384 | return _bitsToBytes(_bitsFixed88(val)); | |
| 319 | 385 | } |
| 320 | 386 | |
| 387 | /** | |
| 388 | * @deprecated | |
| 389 | */ | |
| 321 | 390 | public static double parseFixed88(byte[] val) { |
| 322 | 391 | return ((double) parseSI16(val)) / 256.0; |
| 323 | 392 | } |
| 324 | 393 | |
| 325 | 394 | public static double _parseFixed88(byte[] val) { |
| 326 | //TODO: implement this | |
| 327 | throw new IllegalStateException("not implemented yet"); | |
| 395 | return ((double) _parseSI16(val)) / 256.0; | |
| 328 | 396 | } |
| 329 | 397 | |
| 398 | /** | |
| 399 | * @deprecated | |
| 400 | */ | |
| 330 | 401 | public static BitSet bitsFixed1616(double val) { |
| 331 | 402 | Assert.inRange(val, MIN_FIXED1616, MAX_FIXED1616); |
| 332 | 403 | BitSet result = new BitSet(32); |
| ... | ...@@ -335,28 +406,35 @@ | |
| 335 | 406 | } |
| 336 | 407 | |
| 337 | 408 | public static Bits _bitsFixed1616(double val) { |
| 338 | //TODO: implement this | |
| 339 | throw new IllegalStateException("not implemented yet"); | |
| 409 | Assert.inRange(val, MIN_FIXED1616, MAX_FIXED1616); | |
| 410 | return _bitsSI32((int) (val * 65536.0)); | |
| 340 | 411 | } |
| 341 | 412 | |
| 413 | /** | |
| 414 | * @deprecated | |
| 415 | */ | |
| 342 | 416 | public static byte[] bytesFixed1616(double val) { |
| 343 | 417 | return bitsToBytes(bitsFixed1616(val), 32); |
| 344 | 418 | } |
| 345 | 419 | |
| 346 | 420 | public static byte[] _bytesFixed1616(double val) { |
| 347 | //TODO: implement this | |
| 348 | throw new IllegalStateException("not implemented yet"); | |
| 421 | return _bitsToBytes(_bitsFixed1616(val)); | |
| 349 | 422 | } |
| 350 | 423 | |
| 424 | /** | |
| 425 | * @deprecated | |
| 426 | */ | |
| 351 | 427 | public static double parseFixed1616(byte[] val) { |
| 352 | 428 | return ((double) parseSI32(val)) / 65536.0; |
| 353 | 429 | } |
| 354 | 430 | |
| 355 | 431 | public static double _parseFixed1616(byte[] val) { |
| 356 | //TODO: implement this | |
| 357 | throw new IllegalStateException("not implemented yet"); | |
| 432 | return ((double) _parseSI32(val)) / 65536.0; | |
| 358 | 433 | } |
| 359 | 434 | |
| 435 | /** | |
| 436 | * @deprecated | |
| 437 | */ | |
| 360 | 438 | public static BitSet bitsFloat16(double val) { |
| 361 | 439 | Assert.inRange(val, MIN_FLOAT16, MAX_FLOAT16); |
| 362 | 440 | int bits32 = Float.floatToIntBits((float) val); |
| ... | ...@@ -386,19 +464,47 @@ | |
| 386 | 464 | } |
| 387 | 465 | |
| 388 | 466 | public static Bits _bitsFloat16(double val) { |
| 389 | //TODO: implement this | |
| 390 | throw new IllegalStateException("not implemented yet"); | |
| 467 | Assert.inRange(val, MIN_FLOAT16, MAX_FLOAT16); | |
| 468 | int bits32 = Float.floatToIntBits((float) val); | |
| 469 | int sign = Math.abs((bits32 & 0x80000000) >> 31); | |
| 470 | int exponent32 = (bits32 & 0x7f800000) >> 23; | |
| 471 | int mantissa32 = bits32 & 0x7fffff; | |
| 472 | int exponent16 = 0; | |
| 473 | if (exponent32 != 0) { | |
| 474 | if (exponent32 == 0xff) { | |
| 475 | exponent16 = 0x1f; | |
| 476 | } else { | |
| 477 | exponent16 = exponent32 - 127 + 15; | |
| 478 | } | |
| 479 | } | |
| 480 | int mantissa16 = 0; | |
| 481 | if (exponent16 < 0) { | |
| 482 | exponent16 = 0; // positive/negative zero | |
| 483 | } else if (exponent16 > 0x1f) { | |
| 484 | exponent16 = 0x1f; // infinity | |
| 485 | } else { | |
| 486 | mantissa16 = mantissa32 >> 13; | |
| 487 | } | |
| 488 | int bits16 = sign << 15; | |
| 489 | bits16 |= exponent16 << 10; | |
| 490 | bits16 |= mantissa16; | |
| 491 | return _bitsUI16(bits16); | |
| 391 | 492 | } |
| 392 | 493 | |
| 494 | /** | |
| 495 | * @deprecated | |
| 496 | */ | |
| 393 | 497 | public static byte[] bytesFloat16(double val) { |
| 394 | 498 | return bitsToBytes(bitsFloat16(val), 16); |
| 395 | 499 | } |
| 396 | 500 | |
| 397 | 501 | public static byte[] _bytesFloat16(double val) { |
| 398 | //TODO: implement this | |
| 399 | throw new IllegalStateException("not implemented yet"); | |
| 502 | return _bitsToBytes(_bitsFloat16(val)); | |
| 400 | 503 | } |
| 401 | 504 | |
| 505 | /** | |
| 506 | * @deprecated | |
| 507 | */ | |
| 402 | 508 | public static double parseFloat16(byte[] val) { |
| 403 | 509 | BitSet bits16 = bytesToBits(val); |
| 404 | 510 | bits16 = flipBytes(bits16, 2); |
| ... | ...@@ -422,9 +528,6 @@ | |
| 422 | 528 | exponent32 = exponent16 + 127 - 15; |
| 423 | 529 | mantissa32 = mantissa16 << 13; |
| 424 | 530 | bits32.set(0, sign); |
| 425 | // bits32 = concat(bits32, bitsUBArray(exponent32, 8), 1, 8); | |
| 426 | // bits32 = concat(bits32, bitsUBArray(mantissa32, 23), 9, 23); | |
| 427 | // intbits32 = (int) bitsToLong(bits32, 32, true); | |
| 428 | 531 | intbits32 |= sign ? 1 : 0 << 31; |
| 429 | 532 | intbits32 |= exponent32 << 23; |
| 430 | 533 | intbits32 |= mantissa32; |
| ... | ...@@ -433,38 +536,70 @@ | |
| 433 | 536 | } |
| 434 | 537 | |
| 435 | 538 | public static double _parseFloat16(byte[] val) { |
| 436 | //TODO: implement this | |
| 437 | throw new IllegalStateException("not implemented yet"); | |
| 539 | Bits bits16 = _flipBytes(_bytesToBits(val)); | |
| 540 | boolean sign = false; | |
| 541 | int exponent16 = 0; | |
| 542 | int exponent32 = 0; | |
| 543 | long mantissa16 = 0; | |
| 544 | long mantissa32 = 0; | |
| 545 | int intbits32 = 0; | |
| 546 | float result; | |
| 547 | sign = bits16.getBitAsBoolean(0); | |
| 548 | for (int i = 1; i < 6; i++) { | |
| 549 | exponent16 |= ((long) (bits16.getBitAsInt(i)) << (6 - i - 1)); | |
| 550 | } | |
| 551 | ||
| 552 | for (int i = 6; i < 16; i++) { | |
| 553 | mantissa16 |= ((long) (bits16.getBitAsInt(i)) << (16 - i - 1)); | |
| 554 | } | |
| 555 | ||
| 556 | exponent32 = exponent16 + 127 - 15; | |
| 557 | mantissa32 = mantissa16 << 13; | |
| 558 | intbits32 |= sign ? 1 : 0 << 31; | |
| 559 | intbits32 |= exponent32 << 23; | |
| 560 | intbits32 |= mantissa32; | |
| 561 | result = Float.intBitsToFloat(intbits32); | |
| 562 | return result; | |
| 438 | 563 | } |
| 439 | 564 | |
| 565 | /** | |
| 566 | * @deprecated | |
| 567 | */ | |
| 440 | 568 | public static BitSet bitsFloat32(double val) { |
| 441 | 569 | Assert.inRange(val, MIN_FLOAT32, MAX_FLOAT32); |
| 442 | 570 | return bitsSI32(Float.floatToIntBits((float) val)); |
| 443 | 571 | } |
| 444 | 572 | |
| 445 | 573 | public static Bits _bitsFloat32(double val) { |
| 446 | //TODO: implement this | |
| 447 | throw new IllegalStateException("not implemented yet"); | |
| 574 | Assert.inRange(val, MIN_FLOAT32, MAX_FLOAT32); | |
| 575 | return _bitsSI32(Float.floatToIntBits((float) val)); | |
| 448 | 576 | } |
| 449 | 577 | |
| 578 | /** | |
| 579 | * @deprecated | |
| 580 | */ | |
| 450 | 581 | public static double parseFloat32(byte[] val) { |
| 451 | 582 | return Float.intBitsToFloat((int) parseSI32(val)); |
| 452 | 583 | } |
| 453 | 584 | |
| 454 | 585 | public static double _parseFloat32(byte[] val) { |
| 455 | //TODO: implement this | |
| 456 | throw new IllegalStateException("not implemented yet"); | |
| 586 | return Float.intBitsToFloat((int) _parseSI32(val)); | |
| 457 | 587 | } |
| 458 | 588 | |
| 589 | /** | |
| 590 | * @deprecated | |
| 591 | */ | |
| 459 | 592 | public static byte[] bytesFloat32(double val) { |
| 460 | 593 | return bitsToBytes(bitsFloat32(val), 32); |
| 461 | 594 | } |
| 462 | 595 | |
| 463 | 596 | public static byte[] _bytesFloat32(double val) { |
| 464 | //TODO: implement this | |
| 465 | throw new IllegalStateException("not implemented yet"); | |
| 597 | return _bitsToBytes(_bitsFloat32(val)); | |
| 466 | 598 | } |
| 467 | 599 | |
| 600 | /** | |
| 601 | * @deprecated | |
| 602 | */ | |
| 468 | 603 | public static BitSet bitsFloat64(double val) { |
| 469 | 604 | Assert.inRange(val, MIN_FLOAT64, MAX_FLOAT64); |
| 470 | 605 | BitSet result; |
| ... | ...@@ -489,147 +624,217 @@ | |
| 489 | 624 | } |
| 490 | 625 | |
| 491 | 626 | public static Bits _bitsFloat64(double val) { |
| 492 | //TODO: imp |