From 584076a7b826a62a18ffa923d398cc0e3d213ce4 Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Wed, 22 Mar 2017 23:26:04 +0900 Subject: [PATCH 01/13] Start release 1.103.3-SNAPSHOT --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e4801ff..0400767 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ io.github.olyutorskii doubdabc - 1.103.2 + 1.103.3-SNAPSHOT jar DoubDabC From cd6729555ccd151e0b6c494f4bc0fb85f932eee9 Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Tue, 28 Mar 2017 19:26:09 +0900 Subject: [PATCH 02/13] Split count leading zero method. --- .../olyutorskii/doubdabc/BcdRegister.java | 65 ++++++++++++++----- .../olyutorskii/doubdabc/BcdRegisterTest.java | 30 ++++++++- 2 files changed, 79 insertions(+), 16 deletions(-) diff --git a/src/main/java/io/github/olyutorskii/doubdabc/BcdRegister.java b/src/main/java/io/github/olyutorskii/doubdabc/BcdRegister.java index 9782af5..e2deb9b 100644 --- a/src/main/java/io/github/olyutorskii/doubdabc/BcdRegister.java +++ b/src/main/java/io/github/olyutorskii/doubdabc/BcdRegister.java @@ -203,6 +203,53 @@ public static int toBiQuinary(int iVal){ return result; } + /** + * Count Leading Zero nibbles. + * + * + * + * @param iVal int value + * @return Zero nibbles + */ + public static int clzNibble(int iVal){ + if(iVal == 0){ + return PRIM_SLOTS; + } + + int b2; + if((iVal & 0xff_ff_00_00) == 0){ + b2 = 0b0100; + iVal <<= 16; + }else{ + b2 = 0b0000; + } + + int b1; + if((iVal & 0xff_00_00_00) == 0){ + b1 = 0b0010; + iVal <<= 8; + }else{ + b1 = 0b0000; + } + + int b0; + if((iVal & 0xf0_00_00_00) == 0){ + b0 = 0b0001; + }else{ + b0 = 0b0000; + } + + return b2 | b1 | b0; + } + /** * Clear all decimal digits to Zero. @@ -388,22 +435,10 @@ private int calcPrecision(){ for(int iIdx = idxMax; iIdx >= 0; iIdx--){ int iVal = this.ibuf[iIdx]; - if(iVal == 0){ - result -= PRIM_SLOTS; - }else{ - // Count Leading Zero nibbles(BCD) - if((iVal & 0xff_ff_00_00) == 0){ - result -= 4; - iVal <<= 16; - } - if((iVal & 0xff_00_00_00) == 0){ - result -= 2; - iVal <<= 8; - } - if((iVal & 0xf0_00_00_00) == 0){ - result -= 1; - } + int clz = clzNibble(iVal); + result -= clz; + if(clz != PRIM_SLOTS){ break; } } diff --git a/src/test/java/io/github/olyutorskii/doubdabc/BcdRegisterTest.java b/src/test/java/io/github/olyutorskii/doubdabc/BcdRegisterTest.java index c3d4f49..c2acf53 100644 --- a/src/test/java/io/github/olyutorskii/doubdabc/BcdRegisterTest.java +++ b/src/test/java/io/github/olyutorskii/doubdabc/BcdRegisterTest.java @@ -328,7 +328,7 @@ public void testPushLsb_int() { * Test of getMaxDigits method, of class BcdRegister. */ @Test - public void testGetDigitsHolder() { + public void testGetMaxDigits() { System.out.println("getMaxDigits"); BcdRegister bs; @@ -524,4 +524,32 @@ public void testMersennePrime(){ return; } + /** + * Test of clzNibble method, of class BcdRegister. + */ + @Test + public void testClzNibble() { + System.out.println("clzNibble"); + + assertEquals(8, BcdRegister.clzNibble(0x00000000)); + assertEquals(7, BcdRegister.clzNibble(0x0000000f)); + assertEquals(6, BcdRegister.clzNibble(0x000000ff)); + assertEquals(5, BcdRegister.clzNibble(0x00000fff)); + assertEquals(4, BcdRegister.clzNibble(0x0000ffff)); + assertEquals(3, BcdRegister.clzNibble(0x000fffff)); + assertEquals(2, BcdRegister.clzNibble(0x00ffffff)); + assertEquals(1, BcdRegister.clzNibble(0x0fffffff)); + assertEquals(0, BcdRegister.clzNibble(0xffffffff)); + + assertEquals(7, BcdRegister.clzNibble(0x00000001)); + assertEquals(6, BcdRegister.clzNibble(0x00000010)); + assertEquals(1, BcdRegister.clzNibble(0x01000000)); + assertEquals(0, BcdRegister.clzNibble(0x10000000)); + + assertEquals(6, BcdRegister.clzNibble(0x0000001f)); + assertEquals(0, BcdRegister.clzNibble(0x1fffffff)); + + return; + } + } From 37a3749f8ee1af4a4fe78f43ac9e2355aa6d2da5 Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Tue, 28 Mar 2017 20:46:25 +0900 Subject: [PATCH 03/13] Overloading toBiQuinary() & clzNibble() with long arg. --- .../olyutorskii/doubdabc/BcdRegister.java | 110 ++++++++++++++++- .../olyutorskii/doubdabc/BcdRegisterTest.java | 116 +++++++++++++++++- 2 files changed, 223 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/olyutorskii/doubdabc/BcdRegister.java b/src/main/java/io/github/olyutorskii/doubdabc/BcdRegister.java index e2deb9b..89b5a97 100644 --- a/src/main/java/io/github/olyutorskii/doubdabc/BcdRegister.java +++ b/src/main/java/io/github/olyutorskii/doubdabc/BcdRegister.java @@ -57,6 +57,7 @@ public class BcdRegister { private static final int BYTE_BITSIZE = Byte.SIZE; private static final int BCD_BITSIZE = 4; private static final int PRIM_SLOTS = PRIM_BITSIZE / BCD_BITSIZE; + private static final int LONG_SLOTS = Long.SIZE / BCD_BITSIZE; private static final int LSB_PRIMMASK = 0b1; private static final int MSB_PRIMMASK = 0b1 << (PRIM_BITSIZE - 1); @@ -185,7 +186,7 @@ public static int toBiQuinary(int iVal){ int result; int bVal; - bVal = (iVal >>> 24); + bVal = iVal >>> 24; result = BQ_TBL[bVal]; bVal = (iVal >>> 16) & BYTE_MASK; @@ -203,6 +204,58 @@ public static int toBiQuinary(int iVal){ return result; } + /** + * Convert each 4bit width PackedBCD to Bi-quinary coded decimal. + * + *

If each nibble(PackedBCD) in long is greater than 4, + * add 3 to nibble. + * + *

"nibble overflow" doesn't occur if valid PackedBCD before. + * Undefined result if invalid PackedBCD value before. + * + *

[0,1,2,3,4,5,6,7,8,9] → [0,1,2,3,4,8,9,A,B,C] + * + * @param lVal long value + * @return modified value + */ + public static long toBiQuinary(long lVal){ + long result; + int bVal; + + bVal = (int)(lVal >>> 56); + result = BQ_TBL[bVal]; + + bVal = (int)((lVal >>> 48) & BYTE_MASK); + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + bVal = (int)((lVal >>> 40) & BYTE_MASK); + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + bVal = (int)((lVal >>> 32) & BYTE_MASK); + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + bVal = (int)((lVal >>> 24) & BYTE_MASK); + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + bVal = (int)((lVal >>> 16) & BYTE_MASK); + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + bVal = (int)((lVal >>> 8) & BYTE_MASK); + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + bVal = (int)(lVal & BYTE_MASK); + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + return result; + } + /** * Count Leading Zero nibbles. * @@ -250,6 +303,61 @@ public static int clzNibble(int iVal){ return b2 | b1 | b0; } + /** + * Count Leading Zero nibbles. + * + *

    + *
  • Return 0 if 0xffffffffffffffff. + *
  • Return 0 if 0x1fffffffffffffff. + *
  • Return 3 if 0x000fffffffffffff. + *
  • Return 5 if 0x0000010000000000. + *
  • Return 15 if 0x000000000000000f. + *
  • Return 15 if 0x0000000000000001. + *
  • Return 16 if 0x0000000000000000. + *
+ * + * @param lVal long value + * @return Zero nibbles + */ + public static int clzNibble(long lVal){ + if(lVal == 0){ + return LONG_SLOTS; + } + + int b3; + if((lVal & 0xff_ff_ff_ff_00_00_00_00L) == 0){ + b3 = 0b1000; + lVal <<= 32; + }else{ + b3 = 0b0000; + } + + int b2; + if((lVal & 0xff_ff_00_00_00_00_00_00L) == 0){ + b2 = 0b0100; + lVal <<= 16; + }else{ + b2 = 0b0000; + } + + int b1; + if((lVal & 0xff_00_00_00_00_00_00_00L) == 0){ + b1 = 0b0010; + lVal <<= 8; + }else{ + b1 = 0b0000; + } + + int b0; + if((lVal & 0xf0_00_00_00_00_00_00_00L) == 0){ + b0 = 0b0001; + }else{ + b0 = 0b0000; + } + + return b3 | b2 | b1 | b0; + } + /** * Clear all decimal digits to Zero. diff --git a/src/test/java/io/github/olyutorskii/doubdabc/BcdRegisterTest.java b/src/test/java/io/github/olyutorskii/doubdabc/BcdRegisterTest.java index c2acf53..f3b452c 100644 --- a/src/test/java/io/github/olyutorskii/doubdabc/BcdRegisterTest.java +++ b/src/test/java/io/github/olyutorskii/doubdabc/BcdRegisterTest.java @@ -99,7 +99,7 @@ public void testGetDigit() { * Test of toBiQuinary method, of class BcdRegister. */ @Test - public void testToBiQuinary() { + public void testToBiQuinary_int() { System.out.println("toBiQuinary"); int result; @@ -167,6 +167,82 @@ public void testToBiQuinary() { return; } + /** + * Test of toBiQuinary method, of class BcdRegister. + */ + @Test + public void testToBiQuinary_long() { + System.out.println("toBiQuinary"); + long result; + + result = BcdRegister.toBiQuinary(0x00L); + assertEquals(0x00L, result); + result = BcdRegister.toBiQuinary(0x01L); + assertEquals(0x01L, result); + result = BcdRegister.toBiQuinary(0x02L); + assertEquals(0x02L, result); + result = BcdRegister.toBiQuinary(0x03L); + assertEquals(0x03L, result); + result = BcdRegister.toBiQuinary(0x04L); + assertEquals(0x04L, result); + result = BcdRegister.toBiQuinary(0x05L); + assertEquals(0x08L, result); + result = BcdRegister.toBiQuinary(0x06L); + assertEquals(0x09L, result); + result = BcdRegister.toBiQuinary(0x07L); + assertEquals(0x0aL, result); + result = BcdRegister.toBiQuinary(0x08L); + assertEquals(0x0bL, result); + result = BcdRegister.toBiQuinary(0x09L); + assertEquals(0x0cL, result); + + result = BcdRegister.toBiQuinary(0x9999L); + assertEquals(0xccccL, result); + + result = BcdRegister.toBiQuinary(0x1234567890L); + assertEquals(0x123489abc0L, result); + + result = BcdRegister.toBiQuinary(0x40L); + assertEquals(0x40L, result); + result = BcdRegister.toBiQuinary(0x50L); + assertEquals(0x80L, result); + + result = BcdRegister.toBiQuinary(0x400L); + assertEquals(0x400L, result); + result = BcdRegister.toBiQuinary(0x500L); + assertEquals(0x800L, result); + + result = BcdRegister.toBiQuinary(0x4000L); + assertEquals(0x4000L, result); + result = BcdRegister.toBiQuinary(0x5000L); + assertEquals(0x8000L, result); + + result = BcdRegister.toBiQuinary(0x4444444444444444L); + assertEquals(0x4444444444444444L, result); + result = BcdRegister.toBiQuinary(0x5555555555555555L); + assertEquals(0x8888888888888888L, result); + + result = BcdRegister.toBiQuinary(0x4000000000000000L); + assertEquals(0x4000000000000000L, result); + + result = BcdRegister.toBiQuinary(0x5000000000000000L); + assertEquals(0x8000000000000000L, result); + + result = BcdRegister.toBiQuinary(0x6000000000000000L); + assertEquals(0x9000000000000000L, result); + + result = BcdRegister.toBiQuinary(0x7000000000000000L); + assertEquals(0xa000000000000000L, result); + + result = BcdRegister.toBiQuinary(0x8000000000000000L); + assertEquals(0xb000000000000000L, result); + + result = BcdRegister.toBiQuinary(0x9000000000000000L); + assertEquals(0xc000000000000000L, result); + + return; + } + /** * Test of toString method, of class BcdRegister. */ @@ -528,7 +604,7 @@ public void testMersennePrime(){ * Test of clzNibble method, of class BcdRegister. */ @Test - public void testClzNibble() { + public void testClzNibble_int() { System.out.println("clzNibble"); assertEquals(8, BcdRegister.clzNibble(0x00000000)); @@ -552,4 +628,40 @@ public void testClzNibble() { return; } + /** + * Test of clzNibble method, of class BcdRegister. + */ + @Test + public void testClzNibble_long() { + System.out.println("clzNibble"); + + assertEquals(16, BcdRegister.clzNibble(0x0000000000000000L)); + assertEquals(15, BcdRegister.clzNibble(0x000000000000000fL)); + assertEquals(14, BcdRegister.clzNibble(0x00000000000000ffL)); + assertEquals(13, BcdRegister.clzNibble(0x0000000000000fffL)); + assertEquals(12, BcdRegister.clzNibble(0x000000000000ffffL)); + assertEquals(11, BcdRegister.clzNibble(0x00000000000fffffL)); + assertEquals(10, BcdRegister.clzNibble(0x0000000000ffffffL)); + assertEquals( 9, BcdRegister.clzNibble(0x000000000fffffffL)); + assertEquals( 8, BcdRegister.clzNibble(0x00000000ffffffffL)); + assertEquals( 7, BcdRegister.clzNibble(0x0000000fffffffffL)); + assertEquals( 6, BcdRegister.clzNibble(0x000000ffffffffffL)); + assertEquals( 5, BcdRegister.clzNibble(0x00000fffffffffffL)); + assertEquals( 4, BcdRegister.clzNibble(0x0000ffffffffffffL)); + assertEquals( 3, BcdRegister.clzNibble(0x000fffffffffffffL)); + assertEquals( 2, BcdRegister.clzNibble(0x00ffffffffffffffL)); + assertEquals( 1, BcdRegister.clzNibble(0x0fffffffffffffffL)); + assertEquals( 0, BcdRegister.clzNibble(0xffffffffffffffffL)); + + assertEquals(15, BcdRegister.clzNibble(0x0000000000000001L)); + assertEquals(14, BcdRegister.clzNibble(0x0000000000000010L)); + assertEquals( 1, BcdRegister.clzNibble(0x0100000000000000L)); + assertEquals( 0, BcdRegister.clzNibble(0x1000000000000000L)); + + assertEquals(14, BcdRegister.clzNibble(0x000000000000001fL)); + assertEquals( 0, BcdRegister.clzNibble(0x1fffffffffffffffL)); + + return; + } + } From 6adde129056454a6111f576eb67557db978b2073 Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Tue, 28 Mar 2017 22:13:56 +0900 Subject: [PATCH 04/13] move BCD utilities --- .../olyutorskii/doubdabc/BcdRegister.java | 237 +--------------- .../github/olyutorskii/doubdabc/BcdUtils.java | 268 ++++++++++++++++++ .../olyutorskii/doubdabc/BcdRegisterTest.java | 212 -------------- .../olyutorskii/doubdabc/BcdUtilsTest.java | 252 ++++++++++++++++ 4 files changed, 527 insertions(+), 442 deletions(-) create mode 100644 src/main/java/io/github/olyutorskii/doubdabc/BcdUtils.java create mode 100644 src/test/java/io/github/olyutorskii/doubdabc/BcdUtilsTest.java diff --git a/src/main/java/io/github/olyutorskii/doubdabc/BcdRegister.java b/src/main/java/io/github/olyutorskii/doubdabc/BcdRegister.java index 89b5a97..5f93165 100644 --- a/src/main/java/io/github/olyutorskii/doubdabc/BcdRegister.java +++ b/src/main/java/io/github/olyutorskii/doubdabc/BcdRegister.java @@ -54,17 +54,13 @@ public class BcdRegister { private static final int PRIM_BITSIZE = Integer.SIZE; - private static final int BYTE_BITSIZE = Byte.SIZE; - private static final int BCD_BITSIZE = 4; - private static final int PRIM_SLOTS = PRIM_BITSIZE / BCD_BITSIZE; - private static final int LONG_SLOTS = Long.SIZE / BCD_BITSIZE; + private static final int PRIM_SLOTS = + PRIM_BITSIZE / BcdUtils.BCD_BITSIZE; private static final int LSB_PRIMMASK = 0b1; private static final int MSB_PRIMMASK = 0b1 << (PRIM_BITSIZE - 1); private static final int NIBBLE_MASK = // 0b1111 - (0b1 << BCD_BITSIZE) - 1; - private static final int BYTE_MASK = // 0b1111_1111 - (0b1 << BYTE_BITSIZE) - 1; + (0b1 << BcdUtils.BCD_BITSIZE) - 1; private static final int PRIMIDX_SHIFT = 3; // [ / 8] [>>> 3] private static final int NBLIDX_MASK = // [mod 8] [& 0b111] @@ -79,39 +75,10 @@ public class BcdRegister { 'A', 'B', 'C', 'D', 'E', 'F', }; - private static final int[] BQ_TBL; - - - static{ - // build lookup table for Packed-BCD to Bi-quinary conversion - BQ_TBL = new int[256]; - - int idx = 0; - for(int highDec = 0; highDec < 16; highDec++){ - int highBq; - if (highDec >= 10) highBq = 0x0; - else if(highDec >= 5) highBq = highDec + 3; - else highBq = highDec; - - for(int lowDec = 0; lowDec < 16; lowDec++){ - int lowBq; - if (lowDec >= 10) lowBq = 0x0; - else if(lowDec >= 5) lowBq = lowDec + 3; - else lowBq = lowDec; - - int bqNblNbl = (highBq << BCD_BITSIZE) | lowBq; - - BQ_TBL[idx++] = bqNblNbl; - } - } - - assert idx == BQ_TBL.length; - } - static{ assert 0b1 << PRIMIDX_SHIFT == PRIM_SLOTS; assert (-1 & NBLIDX_MASK) == PRIM_SLOTS - 1; - assert HEXCH_TBL.length == 0b1 << BCD_BITSIZE; + assert HEXCH_TBL.length == 0b1 << BcdUtils.BCD_BITSIZE; } @@ -168,196 +135,6 @@ private static int fittingContainer(int digits){ return result; } - /** - * Convert each 4bit width PackedBCD to Bi-quinary coded decimal. - * - *

If each nibble(PackedBCD) in int is greater than 4, - * add 3 to nibble. - * - *

"nibble overflow" doesn't occur if valid PackedBCD before. - * Undefined result if invalid PackedBCD value before. - * - *

[0,1,2,3,4,5,6,7,8,9] → [0,1,2,3,4,8,9,A,B,C] - * - * @param iVal int value - * @return modified value - */ - public static int toBiQuinary(int iVal){ - int result; - int bVal; - - bVal = iVal >>> 24; - result = BQ_TBL[bVal]; - - bVal = (iVal >>> 16) & BYTE_MASK; - result <<= BYTE_BITSIZE; - result |= BQ_TBL[bVal]; - - bVal = (iVal >>> 8) & BYTE_MASK; - result <<= BYTE_BITSIZE; - result |= BQ_TBL[bVal]; - - bVal = iVal & BYTE_MASK; - result <<= BYTE_BITSIZE; - result |= BQ_TBL[bVal]; - - return result; - } - - /** - * Convert each 4bit width PackedBCD to Bi-quinary coded decimal. - * - *

If each nibble(PackedBCD) in long is greater than 4, - * add 3 to nibble. - * - *

"nibble overflow" doesn't occur if valid PackedBCD before. - * Undefined result if invalid PackedBCD value before. - * - *

[0,1,2,3,4,5,6,7,8,9] → [0,1,2,3,4,8,9,A,B,C] - * - * @param lVal long value - * @return modified value - */ - public static long toBiQuinary(long lVal){ - long result; - int bVal; - - bVal = (int)(lVal >>> 56); - result = BQ_TBL[bVal]; - - bVal = (int)((lVal >>> 48) & BYTE_MASK); - result <<= BYTE_BITSIZE; - result |= BQ_TBL[bVal]; - - bVal = (int)((lVal >>> 40) & BYTE_MASK); - result <<= BYTE_BITSIZE; - result |= BQ_TBL[bVal]; - - bVal = (int)((lVal >>> 32) & BYTE_MASK); - result <<= BYTE_BITSIZE; - result |= BQ_TBL[bVal]; - - bVal = (int)((lVal >>> 24) & BYTE_MASK); - result <<= BYTE_BITSIZE; - result |= BQ_TBL[bVal]; - - bVal = (int)((lVal >>> 16) & BYTE_MASK); - result <<= BYTE_BITSIZE; - result |= BQ_TBL[bVal]; - - bVal = (int)((lVal >>> 8) & BYTE_MASK); - result <<= BYTE_BITSIZE; - result |= BQ_TBL[bVal]; - - bVal = (int)(lVal & BYTE_MASK); - result <<= BYTE_BITSIZE; - result |= BQ_TBL[bVal]; - - return result; - } - - /** - * Count Leading Zero nibbles. - * - *

    - *
  • Return 0 if 0xffffffff. - *
  • Return 0 if 0x1fffffff. - *
  • Return 3 if 0x000fffff. - *
  • Return 5 if 0x00000100. - *
  • Return 7 if 0x0000000f. - *
  • Return 7 if 0x00000001. - *
  • Return 8 if 0x00000000. - *
- * - * @param iVal int value - * @return Zero nibbles - */ - public static int clzNibble(int iVal){ - if(iVal == 0){ - return PRIM_SLOTS; - } - - int b2; - if((iVal & 0xff_ff_00_00) == 0){ - b2 = 0b0100; - iVal <<= 16; - }else{ - b2 = 0b0000; - } - - int b1; - if((iVal & 0xff_00_00_00) == 0){ - b1 = 0b0010; - iVal <<= 8; - }else{ - b1 = 0b0000; - } - - int b0; - if((iVal & 0xf0_00_00_00) == 0){ - b0 = 0b0001; - }else{ - b0 = 0b0000; - } - - return b2 | b1 | b0; - } - - /** - * Count Leading Zero nibbles. - * - *
    - *
  • Return 0 if 0xffffffffffffffff. - *
  • Return 0 if 0x1fffffffffffffff. - *
  • Return 3 if 0x000fffffffffffff. - *
  • Return 5 if 0x0000010000000000. - *
  • Return 15 if 0x000000000000000f. - *
  • Return 15 if 0x0000000000000001. - *
  • Return 16 if 0x0000000000000000. - *
- * - * @param lVal long value - * @return Zero nibbles - */ - public static int clzNibble(long lVal){ - if(lVal == 0){ - return LONG_SLOTS; - } - - int b3; - if((lVal & 0xff_ff_ff_ff_00_00_00_00L) == 0){ - b3 = 0b1000; - lVal <<= 32; - }else{ - b3 = 0b0000; - } - - int b2; - if((lVal & 0xff_ff_00_00_00_00_00_00L) == 0){ - b2 = 0b0100; - lVal <<= 16; - }else{ - b2 = 0b0000; - } - - int b1; - if((lVal & 0xff_00_00_00_00_00_00_00L) == 0){ - b1 = 0b0010; - lVal <<= 8; - }else{ - b1 = 0b0000; - } - - int b0; - if((lVal & 0xf0_00_00_00_00_00_00_00L) == 0){ - b0 = 0b0001; - }else{ - b0 = 0b0000; - } - - return b3 | b2 | b1 | b0; - } - /** * Clear all decimal digits to Zero. @@ -484,7 +261,7 @@ public boolean pushLsb(int carryOver){ for(int idx = 0; idx < buflen; idx++){ int oldVal = this.ibuf[idx]; - int fixVal = toBiQuinary(oldVal); + int fixVal = BcdUtils.toBiQuinary(oldVal); int newVal = fixVal << 1; if(lastMsbTest != 0) newVal |= LSB_PRIMMASK; @@ -543,7 +320,7 @@ private int calcPrecision(){ for(int iIdx = idxMax; iIdx >= 0; iIdx--){ int iVal = this.ibuf[iIdx]; - int clz = clzNibble(iVal); + int clz = BcdUtils.clzNibble(iVal); result -= clz; if(clz != PRIM_SLOTS){ @@ -603,7 +380,7 @@ private void dumpNibble(StringBuilder sb, int nibble){ int b3 = (nibble >> 3) & LSB_PRIMMASK; int b2 = (nibble >> 2) & LSB_PRIMMASK; int b1 = (nibble >> 1) & LSB_PRIMMASK; - int b0 = (nibble ) & LSB_PRIMMASK; + int b0 = nibble & LSB_PRIMMASK; char c3 = HEXCH_TBL[b3]; char c2 = HEXCH_TBL[b2]; diff --git a/src/main/java/io/github/olyutorskii/doubdabc/BcdUtils.java b/src/main/java/io/github/olyutorskii/doubdabc/BcdUtils.java new file mode 100644 index 0000000..628d1e7 --- /dev/null +++ b/src/main/java/io/github/olyutorskii/doubdabc/BcdUtils.java @@ -0,0 +1,268 @@ +/* + * License : The MIT License + * Copyright(c) 2017 olyutorskii + */ + +package io.github.olyutorskii.doubdabc; + +/** + * Utilities for Packed BCD layouted on primitive value. + * + *

Each decimal columns are layouted 4bit nibble width + * on primitive value. + * + *

Lower decimal digit is layouted on lower bits. + * + *

Each decimal digit overlaps Packed-BCD and Bi-quinary coded decimal. + * + * @see + * Binary-coded decimal (Wikipedia) + * + * @see + * Bi-quinary coded decimal (Wikipedia) + * + */ +public final class BcdUtils { + + /** bit-size of BCD(nibble). */ + public static final int BCD_BITSIZE = 4; + + private static final int BYTE_BITSIZE = Byte.SIZE; + private static final int BYTE_MASK = // 0b1111_1111 + (0b1 << BYTE_BITSIZE) - 1; + + private static final int INT_SLOTS = Integer.SIZE / BCD_BITSIZE; + private static final int LONG_SLOTS = Long.SIZE / BCD_BITSIZE; + + private static final int[] BQ_TBL; + + static{ + // build lookup table for Packed-BCD to Bi-quinary conversion + BQ_TBL = new int[256]; + + int idx = 0; + for(int highDec = 0; highDec < 16; highDec++){ + int highBq; + if (highDec >= 10) highBq = 0x0; + else if(highDec >= 5) highBq = highDec + 3; + else highBq = highDec; + + for(int lowDec = 0; lowDec < 16; lowDec++){ + int lowBq; + if (lowDec >= 10) lowBq = 0x0; + else if(lowDec >= 5) lowBq = lowDec + 3; + else lowBq = lowDec; + + int bqNblNbl = (highBq << BCD_BITSIZE) | lowBq; + + BQ_TBL[idx++] = bqNblNbl; + } + } + + assert idx == BQ_TBL.length; + } + + + /** + * Hiden constructor. + */ + private BcdUtils(){ + assert false; + } + + + /** + * Convert each 4bit width PackedBCD to Bi-quinary coded decimal. + * + *

If each nibble(PackedBCD) in int is greater than 4, + * add 3 to nibble. + * + *

"nibble overflow" doesn't occur if valid PackedBCD before. + * Undefined result if invalid PackedBCD value before. + * + *

[0,1,2,3,4,5,6,7,8,9] → [0,1,2,3,4,8,9,A,B,C] + * + * @param iVal int value + * @return modified value + */ + public static int toBiQuinary(int iVal) { + int result; + + int bVal; + + bVal = iVal >>> 24; + result = BQ_TBL[bVal]; + + bVal = (iVal >>> 16) & BYTE_MASK; + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + bVal = (iVal >>> 8) & BYTE_MASK; + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + bVal = iVal & BYTE_MASK; + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + return result; + } + + /** + * Convert each 4bit width PackedBCD to Bi-quinary coded decimal. + * + *

If each nibble(PackedBCD) in long is greater than 4, + * add 3 to nibble. + * + *

"nibble overflow" doesn't occur if valid PackedBCD before. + * Undefined result if invalid PackedBCD value before. + * + *

[0,1,2,3,4,5,6,7,8,9] → [0,1,2,3,4,8,9,A,B,C] + * + * @param lVal long value + * @return modified value + */ + public static long toBiQuinary(long lVal) { + long result; + + int bVal; + + bVal = (int) (lVal >>> 56); + result = BQ_TBL[bVal]; + + bVal = (int) ((lVal >>> 48) & BYTE_MASK); + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + bVal = (int) ((lVal >>> 40) & BYTE_MASK); + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + bVal = (int) ((lVal >>> 32) & BYTE_MASK); + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + bVal = (int) ((lVal >>> 24) & BYTE_MASK); + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + bVal = (int) ((lVal >>> 16) & BYTE_MASK); + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + bVal = (int) ((lVal >>> 8) & BYTE_MASK); + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + bVal = (int) (lVal & BYTE_MASK); + result <<= BYTE_BITSIZE; + result |= BQ_TBL[bVal]; + + return result; + } + + /** + * Count Leading Zero nibbles. + * + *

    + *
  • Return 0 if 0xffffffff. + *
  • Return 0 if 0x1fffffff. + *
  • Return 3 if 0x000fffff. + *
  • Return 5 if 0x00000100. + *
  • Return 7 if 0x0000000f. + *
  • Return 7 if 0x00000001. + *
  • Return 8 if 0x00000000. + *
+ * + * @param iVal int value + * @return Zero nibbles + */ + public static int clzNibble(int iVal){ + if(iVal == 0){ + return INT_SLOTS; + } + + int b2; + if((iVal & 0xff_ff_00_00) == 0){ + b2 = 0b0100; + iVal <<= 16; + }else{ + b2 = 0b0000; + } + + int b1; + if((iVal & 0xff_00_00_00) == 0){ + b1 = 0b0010; + iVal <<= 8; + }else{ + b1 = 0b0000; + } + + int b0; + if((iVal & 0xf0_00_00_00) == 0){ + b0 = 0b0001; + }else{ + b0 = 0b0000; + } + + return b2 | b1 | b0; + } + + /** + * Count Leading Zero nibbles. + * + *
    + *
  • Return 0 if 0xffffffffffffffff. + *
  • Return 0 if 0x1fffffffffffffff. + *
  • Return 3 if 0x000fffffffffffff. + *
  • Return 5 if 0x0000010000000000. + *
  • Return 15 if 0x000000000000000f. + *
  • Return 15 if 0x0000000000000001. + *
  • Return 16 if 0x0000000000000000. + *
+ * + * @param lVal long value + * @return Zero nibbles + */ + public static int clzNibble(long lVal){ + if(lVal == 0){ + return LONG_SLOTS; + } + + int b3; + if((lVal & 0xff_ff_ff_ff_00_00_00_00L) == 0){ + b3 = 0b1000; + lVal <<= 32; + }else{ + b3 = 0b0000; + } + + int b2; + if((lVal & 0xff_ff_00_00_00_00_00_00L) == 0){ + b2 = 0b0100; + lVal <<= 16; + }else{ + b2 = 0b0000; + } + + int b1; + if((lVal & 0xff_00_00_00_00_00_00_00L) == 0){ + b1 = 0b0010; + lVal <<= 8; + }else{ + b1 = 0b0000; + } + + int b0; + if((lVal & 0xf0_00_00_00_00_00_00_00L) == 0){ + b0 = 0b0001; + }else{ + b0 = 0b0000; + } + + return b3 | b2 | b1 | b0; + } + +} diff --git a/src/test/java/io/github/olyutorskii/doubdabc/BcdRegisterTest.java b/src/test/java/io/github/olyutorskii/doubdabc/BcdRegisterTest.java index f3b452c..ae7a160 100644 --- a/src/test/java/io/github/olyutorskii/doubdabc/BcdRegisterTest.java +++ b/src/test/java/io/github/olyutorskii/doubdabc/BcdRegisterTest.java @@ -95,154 +95,6 @@ public void testGetDigit() { return; } - /** - * Test of toBiQuinary method, of class BcdRegister. - */ - @Test - public void testToBiQuinary_int() { - System.out.println("toBiQuinary"); - - int result; - - result = BcdRegister.toBiQuinary(0x00); - assertEquals(0x00, result); - result = BcdRegister.toBiQuinary(0x01); - assertEquals(0x01, result); - result = BcdRegister.toBiQuinary(0x02); - assertEquals(0x02, result); - result = BcdRegister.toBiQuinary(0x03); - assertEquals(0x03, result); - result = BcdRegister.toBiQuinary(0x04); - assertEquals(0x04, result); - result = BcdRegister.toBiQuinary(0x05); - assertEquals(0x08, result); - result = BcdRegister.toBiQuinary(0x06); - assertEquals(0x09, result); - result = BcdRegister.toBiQuinary(0x07); - assertEquals(0x0a, result); - result = BcdRegister.toBiQuinary(0x08); - assertEquals(0x0b, result); - result = BcdRegister.toBiQuinary(0x09); - assertEquals(0x0c, result); - - result = BcdRegister.toBiQuinary(0x40); - assertEquals(0x40, result); - result = BcdRegister.toBiQuinary(0x50); - assertEquals(0x80, result); - - result = BcdRegister.toBiQuinary(0x400); - assertEquals(0x400, result); - result = BcdRegister.toBiQuinary(0x500); - assertEquals(0x800, result); - - result = BcdRegister.toBiQuinary(0x4000); - assertEquals(0x4000, result); - result = BcdRegister.toBiQuinary(0x5000); - assertEquals(0x8000, result); - - result = BcdRegister.toBiQuinary(0x9999); - assertEquals(0xcccc, result); - - result = BcdRegister.toBiQuinary(0x3456); - assertEquals(0x3489, result); - - result = BcdRegister.toBiQuinary(0x40_00_00_00); - assertEquals(0x40_00_00_00, result); - - result = BcdRegister.toBiQuinary(0x50_00_00_00); - assertEquals(0x80_00_00_00, result); - - result = BcdRegister.toBiQuinary(0x60_00_00_00); - assertEquals(0x90_00_00_00, result); - - result = BcdRegister.toBiQuinary(0x70_00_00_00); - assertEquals(0xa0_00_00_00, result); - - result = BcdRegister.toBiQuinary(0x80_00_00_00); - assertEquals(0xb0_00_00_00, result); - - result = BcdRegister.toBiQuinary(0x90_00_00_00); - assertEquals(0xc0_00_00_00, result); - - return; - } - - /** - * Test of toBiQuinary method, of class BcdRegister. - */ - @Test - public void testToBiQuinary_long() { - System.out.println("toBiQuinary"); - long result; - - result = BcdRegister.toBiQuinary(0x00L); - assertEquals(0x00L, result); - result = BcdRegister.toBiQuinary(0x01L); - assertEquals(0x01L, result); - result = BcdRegister.toBiQuinary(0x02L); - assertEquals(0x02L, result); - result = BcdRegister.toBiQuinary(0x03L); - assertEquals(0x03L, result); - result = BcdRegister.toBiQuinary(0x04L); - assertEquals(0x04L, result); - result = BcdRegister.toBiQuinary(0x05L); - assertEquals(0x08L, result); - result = BcdRegister.toBiQuinary(0x06L); - assertEquals(0x09L, result); - result = BcdRegister.toBiQuinary(0x07L); - assertEquals(0x0aL, result); - result = BcdRegister.toBiQuinary(0x08L); - assertEquals(0x0bL, result); - result = BcdRegister.toBiQuinary(0x09L); - assertEquals(0x0cL, result); - - result = BcdRegister.toBiQuinary(0x9999L); - assertEquals(0xccccL, result); - - result = BcdRegister.toBiQuinary(0x1234567890L); - assertEquals(0x123489abc0L, result); - - result = BcdRegister.toBiQuinary(0x40L); - assertEquals(0x40L, result); - result = BcdRegister.toBiQuinary(0x50L); - assertEquals(0x80L, result); - - result = BcdRegister.toBiQuinary(0x400L); - assertEquals(0x400L, result); - result = BcdRegister.toBiQuinary(0x500L); - assertEquals(0x800L, result); - - result = BcdRegister.toBiQuinary(0x4000L); - assertEquals(0x4000L, result); - result = BcdRegister.toBiQuinary(0x5000L); - assertEquals(0x8000L, result); - - result = BcdRegister.toBiQuinary(0x4444444444444444L); - assertEquals(0x4444444444444444L, result); - result = BcdRegister.toBiQuinary(0x5555555555555555L); - assertEquals(0x8888888888888888L, result); - - result = BcdRegister.toBiQuinary(0x4000000000000000L); - assertEquals(0x4000000000000000L, result); - - result = BcdRegister.toBiQuinary(0x5000000000000000L); - assertEquals(0x8000000000000000L, result); - - result = BcdRegister.toBiQuinary(0x6000000000000000L); - assertEquals(0x9000000000000000L, result); - - result = BcdRegister.toBiQuinary(0x7000000000000000L); - assertEquals(0xa000000000000000L, result); - - result = BcdRegister.toBiQuinary(0x8000000000000000L); - assertEquals(0xb000000000000000L, result); - - result = BcdRegister.toBiQuinary(0x9000000000000000L); - assertEquals(0xc000000000000000L, result); - - return; - } - /** * Test of toString method, of class BcdRegister. */ @@ -600,68 +452,4 @@ public void testMersennePrime(){ return; } - /** - * Test of clzNibble method, of class BcdRegister. - */ - @Test - public void testClzNibble_int() { - System.out.println("clzNibble"); - - assertEquals(8, BcdRegister.clzNibble(0x00000000)); - assertEquals(7, BcdRegister.clzNibble(0x0000000f)); - assertEquals(6, BcdRegister.clzNibble(0x000000ff)); - assertEquals(5, BcdRegister.clzNibble(0x00000fff)); - assertEquals(4, BcdRegister.clzNibble(0x0000ffff)); - assertEquals(3, BcdRegister.clzNibble(0x000fffff)); - assertEquals(2, BcdRegister.clzNibble(0x00ffffff)); - assertEquals(1, BcdRegister.clzNibble(0x0fffffff)); - assertEquals(0, BcdRegister.clzNibble(0xffffffff)); - - assertEquals(7, BcdRegister.clzNibble(0x00000001)); - assertEquals(6, BcdRegister.clzNibble(0x00000010)); - assertEquals(1, BcdRegister.clzNibble(0x01000000)); - assertEquals(0, BcdRegister.clzNibble(0x10000000)); - - assertEquals(6, BcdRegister.clzNibble(0x0000001f)); - assertEquals(0, BcdRegister.clzNibble(0x1fffffff)); - - return; - } - - /** - * Test of clzNibble method, of class BcdRegister. - */ - @Test - public void testClzNibble_long() { - System.out.println("clzNibble"); - - assertEquals(16, BcdRegister.clzNibble(0x0000000000000000L)); - assertEquals(15, BcdRegister.clzNibble(0x000000000000000fL)); - assertEquals(14, BcdRegister.clzNibble(0x00000000000000ffL)); - assertEquals(13, BcdRegister.clzNibble(0x0000000000000fffL)); - assertEquals(12, BcdRegister.clzNibble(0x000000000000ffffL)); - assertEquals(11, BcdRegister.clzNibble(0x00000000000fffffL)); - assertEquals(10, BcdRegister.clzNibble(0x0000000000ffffffL)); - assertEquals( 9, BcdRegister.clzNibble(0x000000000fffffffL)); - assertEquals( 8, BcdRegister.clzNibble(0x00000000ffffffffL)); - assertEquals( 7, BcdRegister.clzNibble(0x0000000fffffffffL)); - assertEquals( 6, BcdRegister.clzNibble(0x000000ffffffffffL)); - assertEquals( 5, BcdRegister.clzNibble(0x00000fffffffffffL)); - assertEquals( 4, BcdRegister.clzNibble(0x0000ffffffffffffL)); - assertEquals( 3, BcdRegister.clzNibble(0x000fffffffffffffL)); - assertEquals( 2, BcdRegister.clzNibble(0x00ffffffffffffffL)); - assertEquals( 1, BcdRegister.clzNibble(0x0fffffffffffffffL)); - assertEquals( 0, BcdRegister.clzNibble(0xffffffffffffffffL)); - - assertEquals(15, BcdRegister.clzNibble(0x0000000000000001L)); - assertEquals(14, BcdRegister.clzNibble(0x0000000000000010L)); - assertEquals( 1, BcdRegister.clzNibble(0x0100000000000000L)); - assertEquals( 0, BcdRegister.clzNibble(0x1000000000000000L)); - - assertEquals(14, BcdRegister.clzNibble(0x000000000000001fL)); - assertEquals( 0, BcdRegister.clzNibble(0x1fffffffffffffffL)); - - return; - } - } diff --git a/src/test/java/io/github/olyutorskii/doubdabc/BcdUtilsTest.java b/src/test/java/io/github/olyutorskii/doubdabc/BcdUtilsTest.java new file mode 100644 index 0000000..a7b8fcb --- /dev/null +++ b/src/test/java/io/github/olyutorskii/doubdabc/BcdUtilsTest.java @@ -0,0 +1,252 @@ +/* + * License : The MIT License + * Copyright(c) 2017 olyutorskii + */ + +package io.github.olyutorskii.doubdabc; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * + */ +public class BcdUtilsTest { + + public BcdUtilsTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of toBiQuinary method, of class BcdRegister. + */ + @Test + public void testToBiQuinary_int() { + System.out.println("toBiQuinary"); + + int result; + + result = BcdUtils.toBiQuinary(0x00); + assertEquals(0x00, result); + result = BcdUtils.toBiQuinary(0x01); + assertEquals(0x01, result); + result = BcdUtils.toBiQuinary(0x02); + assertEquals(0x02, result); + result = BcdUtils.toBiQuinary(0x03); + assertEquals(0x03, result); + result = BcdUtils.toBiQuinary(0x04); + assertEquals(0x04, result); + result = BcdUtils.toBiQuinary(0x05); + assertEquals(0x08, result); + result = BcdUtils.toBiQuinary(0x06); + assertEquals(0x09, result); + result = BcdUtils.toBiQuinary(0x07); + assertEquals(0x0a, result); + result = BcdUtils.toBiQuinary(0x08); + assertEquals(0x0b, result); + result = BcdUtils.toBiQuinary(0x09); + assertEquals(0x0c, result); + + result = BcdUtils.toBiQuinary(0x40); + assertEquals(0x40, result); + result = BcdUtils.toBiQuinary(0x50); + assertEquals(0x80, result); + + result = BcdUtils.toBiQuinary(0x400); + assertEquals(0x400, result); + result = BcdUtils.toBiQuinary(0x500); + assertEquals(0x800, result); + + result = BcdUtils.toBiQuinary(0x4000); + assertEquals(0x4000, result); + result = BcdUtils.toBiQuinary(0x5000); + assertEquals(0x8000, result); + + result = BcdUtils.toBiQuinary(0x9999); + assertEquals(0xcccc, result); + + result = BcdUtils.toBiQuinary(0x3456); + assertEquals(0x3489, result); + + result = BcdUtils.toBiQuinary(0x40_00_00_00); + assertEquals(0x40_00_00_00, result); + + result = BcdUtils.toBiQuinary(0x50_00_00_00); + assertEquals(0x80_00_00_00, result); + + result = BcdUtils.toBiQuinary(0x60_00_00_00); + assertEquals(0x90_00_00_00, result); + + result = BcdUtils.toBiQuinary(0x70_00_00_00); + assertEquals(0xa0_00_00_00, result); + + result = BcdUtils.toBiQuinary(0x80_00_00_00); + assertEquals(0xb0_00_00_00, result); + + result = BcdUtils.toBiQuinary(0x90_00_00_00); + assertEquals(0xc0_00_00_00, result); + + return; + } + + /** + * Test of toBiQuinary method, of class BcdRegister. + */ + @Test + public void testToBiQuinary_long() { + System.out.println("toBiQuinary"); + long result; + + result = BcdUtils.toBiQuinary(0x00L); + assertEquals(0x00L, result); + result = BcdUtils.toBiQuinary(0x01L); + assertEquals(0x01L, result); + result = BcdUtils.toBiQuinary(0x02L); + assertEquals(0x02L, result); + result = BcdUtils.toBiQuinary(0x03L); + assertEquals(0x03L, result); + result = BcdUtils.toBiQuinary(0x04L); + assertEquals(0x04L, result); + result = BcdUtils.toBiQuinary(0x05L); + assertEquals(0x08L, result); + result = BcdUtils.toBiQuinary(0x06L); + assertEquals(0x09L, result); + result = BcdUtils.toBiQuinary(0x07L); + assertEquals(0x0aL, result); + result = BcdUtils.toBiQuinary(0x08L); + assertEquals(0x0bL, result); + result = BcdUtils.toBiQuinary(0x09L); + assertEquals(0x0cL, result); + + result = BcdUtils.toBiQuinary(0x9999L); + assertEquals(0xccccL, result); + + result = BcdUtils.toBiQuinary(0x1234567890L); + assertEquals(0x123489abc0L, result); + + result = BcdUtils.toBiQuinary(0x40L); + assertEquals(0x40L, result); + result = BcdUtils.toBiQuinary(0x50L); + assertEquals(0x80L, result); + + result = BcdUtils.toBiQuinary(0x400L); + assertEquals(0x400L, result); + result = BcdUtils.toBiQuinary(0x500L); + assertEquals(0x800L, result); + + result = BcdUtils.toBiQuinary(0x4000L); + assertEquals(0x4000L, result); + result = BcdUtils.toBiQuinary(0x5000L); + assertEquals(0x8000L, result); + + result = BcdUtils.toBiQuinary(0x4444444444444444L); + assertEquals(0x4444444444444444L, result); + result = BcdUtils.toBiQuinary(0x5555555555555555L); + assertEquals(0x8888888888888888L, result); + + result = BcdUtils.toBiQuinary(0x4000000000000000L); + assertEquals(0x4000000000000000L, result); + + result = BcdUtils.toBiQuinary(0x5000000000000000L); + assertEquals(0x8000000000000000L, result); + + result = BcdUtils.toBiQuinary(0x6000000000000000L); + assertEquals(0x9000000000000000L, result); + + result = BcdUtils.toBiQuinary(0x7000000000000000L); + assertEquals(0xa000000000000000L, result); + + result = BcdUtils.toBiQuinary(0x8000000000000000L); + assertEquals(0xb000000000000000L, result); + + result = BcdUtils.toBiQuinary(0x9000000000000000L); + assertEquals(0xc000000000000000L, result); + + return; + } + + /** + * Test of clzNibble method, of class BcdRegister. + */ + @Test + public void testClzNibble_int() { + System.out.println("clzNibble"); + + assertEquals(8, BcdUtils.clzNibble(0x00000000)); + assertEquals(7, BcdUtils.clzNibble(0x0000000f)); + assertEquals(6, BcdUtils.clzNibble(0x000000ff)); + assertEquals(5, BcdUtils.clzNibble(0x00000fff)); + assertEquals(4, BcdUtils.clzNibble(0x0000ffff)); + assertEquals(3, BcdUtils.clzNibble(0x000fffff)); + assertEquals(2, BcdUtils.clzNibble(0x00ffffff)); + assertEquals(1, BcdUtils.clzNibble(0x0fffffff)); + assertEquals(0, BcdUtils.clzNibble(0xffffffff)); + + assertEquals(7, BcdUtils.clzNibble(0x00000001)); + assertEquals(6, BcdUtils.clzNibble(0x00000010)); + assertEquals(1, BcdUtils.clzNibble(0x01000000)); + assertEquals(0, BcdUtils.clzNibble(0x10000000)); + + assertEquals(6, BcdUtils.clzNibble(0x0000001f)); + assertEquals(0, BcdUtils.clzNibble(0x1fffffff)); + + return; + } + + /** + * Test of clzNibble method, of class BcdRegister. + */ + @Test + public void testClzNibble_long() { + System.out.println("clzNibble"); + + assertEquals(16, BcdUtils.clzNibble(0x0000000000000000L)); + assertEquals(15, BcdUtils.clzNibble(0x000000000000000fL)); + assertEquals(14, BcdUtils.clzNibble(0x00000000000000ffL)); + assertEquals(13, BcdUtils.clzNibble(0x0000000000000fffL)); + assertEquals(12, BcdUtils.clzNibble(0x000000000000ffffL)); + assertEquals(11, BcdUtils.clzNibble(0x00000000000fffffL)); + assertEquals(10, BcdUtils.clzNibble(0x0000000000ffffffL)); + assertEquals( 9, BcdUtils.clzNibble(0x000000000fffffffL)); + assertEquals( 8, BcdUtils.clzNibble(0x00000000ffffffffL)); + assertEquals( 7, BcdUtils.clzNibble(0x0000000fffffffffL)); + assertEquals( 6, BcdUtils.clzNibble(0x000000ffffffffffL)); + assertEquals( 5, BcdUtils.clzNibble(0x00000fffffffffffL)); + assertEquals( 4, BcdUtils.clzNibble(0x0000ffffffffffffL)); + assertEquals( 3, BcdUtils.clzNibble(0x000fffffffffffffL)); + assertEquals( 2, BcdUtils.clzNibble(0x00ffffffffffffffL)); + assertEquals( 1, BcdUtils.clzNibble(0x0fffffffffffffffL)); + assertEquals( 0, BcdUtils.clzNibble(0xffffffffffffffffL)); + + assertEquals(15, BcdUtils.clzNibble(0x0000000000000001L)); + assertEquals(14, BcdUtils.clzNibble(0x0000000000000010L)); + assertEquals( 1, BcdUtils.clzNibble(0x0100000000000000L)); + assertEquals( 0, BcdUtils.clzNibble(0x1000000000000000L)); + + assertEquals(14, BcdUtils.clzNibble(0x000000000000001fL)); + assertEquals( 0, BcdUtils.clzNibble(0x1fffffffffffffffL)); + + return; + } + +} From 78c1c3d859b3b9719505659585dde19b3fcb1a36 Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Wed, 29 Mar 2017 23:23:49 +0900 Subject: [PATCH 05/13] Refactoring toBiQuinary() --- .../github/olyutorskii/doubdabc/BcdUtils.java | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/main/java/io/github/olyutorskii/doubdabc/BcdUtils.java b/src/main/java/io/github/olyutorskii/doubdabc/BcdUtils.java index 628d1e7..0a523c5 100644 --- a/src/main/java/io/github/olyutorskii/doubdabc/BcdUtils.java +++ b/src/main/java/io/github/olyutorskii/doubdabc/BcdUtils.java @@ -87,25 +87,20 @@ private BcdUtils(){ * @param iVal int value * @return modified value */ - public static int toBiQuinary(int iVal) { - int result; - - int bVal; - - bVal = iVal >>> 24; - result = BQ_TBL[bVal]; - - bVal = (iVal >>> 16) & BYTE_MASK; - result <<= BYTE_BITSIZE; - result |= BQ_TBL[bVal]; - - bVal = (iVal >>> 8) & BYTE_MASK; - result <<= BYTE_BITSIZE; - result |= BQ_TBL[bVal]; - - bVal = iVal & BYTE_MASK; - result <<= BYTE_BITSIZE; - result |= BQ_TBL[bVal]; + public static int toBiQuinary(int iVal){ + int idx3 = iVal >>> 24; + int idx2 = iVal >>> 16 & BYTE_MASK; + int idx1 = iVal >>> 8 & BYTE_MASK; + int idx0 = iVal & BYTE_MASK; + + int b3 = BQ_TBL[idx3]; + int b2 = BQ_TBL[idx2]; + int b1 = BQ_TBL[idx1]; + int b0 = BQ_TBL[idx0]; + + int result = + ((b3 << BYTE_BITSIZE) | b2) << 16 + | ((b1 << BYTE_BITSIZE) | b0); return result; } From 8ced0344cbc7c2806f96c6d11be2dd56e95bffa7 Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Thu, 30 Mar 2017 18:25:02 +0900 Subject: [PATCH 06/13] Add BcdArrays --- .../olyutorskii/doubdabc/BcdArrays.java | 111 ++++++++++++++++++ .../olyutorskii/doubdabc/BcdArraysTest.java | 100 ++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java create mode 100644 src/test/java/io/github/olyutorskii/doubdabc/BcdArraysTest.java diff --git a/src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java b/src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java new file mode 100644 index 0000000..61b96ac --- /dev/null +++ b/src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java @@ -0,0 +1,111 @@ +/* + * License : The MIT License + * Copyright(c) 2017 olyutorskii + */ + +package io.github.olyutorskii.doubdabc; + +/** + * BCD array utilities. + */ +public final class BcdArrays { + + private static final int INT_SLOTS = + Integer.SIZE / BcdUtils.BCD_BITSIZE; + + private static final int MSB_INTMASK = 0b1 << (Integer.SIZE - 1); + private static final int LSB_INTMASK = 0b1; + + private static final int BCD_MASK = + (0b1 << BcdUtils.BCD_BITSIZE) - 1; + private static final int ARABICUC_MASK = 0b0011_0000; + + + /** + * Hidden constructor. + */ + private BcdArrays(){ + assert false; + } + + + /** + * Convert int value to Arabic-decimal character sequence. + * + *

Output nothing if value 0. + * + *

sign-bit is treated as unsigned. + * There is no minus-sign output. + * + * @param iVal int value + * @param cbuf char output + * @param offset output start offset + * @return output length + */ + public static int int2ArabicArray(final int iVal, + final char[] cbuf, final int offset){ + int iHigh = 0b0; + int iLow = 0b0; + + boolean skipLeading = true; + + for(int bitPos = 0; bitPos < Integer.SIZE; bitPos++){ + final int scanMask = MSB_INTMASK >>> bitPos; + final int bitAppear = iVal & scanMask; + + if(skipLeading){ + if(bitAppear == 0b0) continue; + skipLeading = false; + } + + int bqVal; + int halfCarry; + int shiftedBcd; + + bqVal = BcdUtils.toBiQuinary(iLow); + halfCarry = bqVal & MSB_INTMASK; + shiftedBcd = bqVal << 1; + + if(bitAppear == 0b0){ + iLow = shiftedBcd; + }else{ + iLow = shiftedBcd | LSB_INTMASK; + } + + bqVal = BcdUtils.toBiQuinary(iHigh); + shiftedBcd = bqVal << 1; + + if(halfCarry == 0b0){ + iHigh = shiftedBcd; + }else{ + iHigh = shiftedBcd | LSB_INTMASK; + } + } + + int cidx = offset; + final int clzLow; + + if(iHigh == 0b0){ + clzLow = BcdUtils.clzNibble(iLow); + }else{ + clzLow = 0; + + final int clzHigh = BcdUtils.clzNibble(iHigh); + + for(int bcdCt = clzHigh; bcdCt < INT_SLOTS; bcdCt++){ + final int nibble = (iHigh >>> ((7 - bcdCt) << 2)) & BCD_MASK; + final char decimalCh = (char)(nibble | ARABICUC_MASK); + cbuf[cidx++] = decimalCh; + } + } + + for(int bcdCt = clzLow; bcdCt < INT_SLOTS; bcdCt++){ + final int nibble = (iLow >>> ((7 - bcdCt) << 2)) & BCD_MASK; + final char decimalCh = (char)(nibble | ARABICUC_MASK); + cbuf[cidx++] = decimalCh; + } + + return cidx - offset; + } + +} diff --git a/src/test/java/io/github/olyutorskii/doubdabc/BcdArraysTest.java b/src/test/java/io/github/olyutorskii/doubdabc/BcdArraysTest.java new file mode 100644 index 0000000..e311b51 --- /dev/null +++ b/src/test/java/io/github/olyutorskii/doubdabc/BcdArraysTest.java @@ -0,0 +1,100 @@ +/* + * License : The MIT License + * Copyright(c) 2017 olyutorskii + */ + +package io.github.olyutorskii.doubdabc; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * + */ +public class BcdArraysTest { + + public BcdArraysTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of int2ArabicArray method, of class BcdArrays. + */ + @Test + public void testInt2ArabicArray() { + System.out.println("int2ArabicArray"); + + int result; + char[] buf; + + buf = new char[100]; + + result = BcdArrays.int2ArabicArray(1, buf, 0); + assertEquals(result, 1); + assertEquals("1", new String(buf, 0, result)); + + result = BcdArrays.int2ArabicArray(1234567890, buf, 0); + assertEquals(result, 10); + assertEquals("1234567890", new String(buf, 0, result)); + + result = BcdArrays.int2ArabicArray(99999999, buf, 0); + assertEquals(result, 8); + assertEquals("99999999", new String(buf, 0, result)); + + result = BcdArrays.int2ArabicArray(100000000, buf, 0); + assertEquals(result, 9); + assertEquals("100000000", new String(buf, 0, result)); + + result = BcdArrays.int2ArabicArray(0, buf, 0); + assertEquals(result, 0); + assertEquals("", new String(buf, 0, result)); + + result = BcdArrays.int2ArabicArray(-1, buf, 0); + assertEquals(result, 10); + assertEquals("4294967295", new String(buf, 0, result)); + + result = BcdArrays.int2ArabicArray(Integer.MAX_VALUE, buf, 0); + assertEquals(result, 10); + assertEquals("2147483647", new String(buf, 0, result)); + + result = BcdArrays.int2ArabicArray(Integer.MIN_VALUE, buf, 0); + assertEquals(result, 10); + assertEquals("2147483648", new String(buf, 0, result)); + + result = BcdArrays.int2ArabicArray(234, buf, 0); + assertEquals(result, 3); + assertEquals('2', buf[0]); + assertEquals('3', buf[1]); + assertEquals('4', buf[2]); + + buf[0] = 'A'; + buf[1] = 'B'; + buf[2] = 'C'; + result = BcdArrays.int2ArabicArray(234, buf, 2); + assertEquals(result, 3); + assertEquals("AB234", new String(buf, 0, 2+result)); + + return; + } + +} From 9d8e04647a0edce866d387a48b51cf7dac46b7d7 Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Thu, 30 Mar 2017 22:39:00 +0900 Subject: [PATCH 07/13] Optimize int2ArabicArray() --- .../olyutorskii/doubdabc/BcdArrays.java | 20 +++++++++++++++---- .../olyutorskii/doubdabc/BcdArraysTest.java | 8 ++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java b/src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java index 61b96ac..95d8c55 100644 --- a/src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java +++ b/src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java @@ -48,22 +48,23 @@ public static int int2ArabicArray(final int iVal, int iLow = 0b0; boolean skipLeading = true; + boolean noHalfCarry = true; + int sig = 0; - for(int bitPos = 0; bitPos < Integer.SIZE; bitPos++){ - final int scanMask = MSB_INTMASK >>> bitPos; + for(int bitCt = 0; bitCt < Integer.SIZE; bitCt++){ + final int scanMask = MSB_INTMASK >>> bitCt; final int bitAppear = iVal & scanMask; if(skipLeading){ if(bitAppear == 0b0) continue; skipLeading = false; } + sig++; int bqVal; - int halfCarry; int shiftedBcd; bqVal = BcdUtils.toBiQuinary(iLow); - halfCarry = bqVal & MSB_INTMASK; shiftedBcd = bqVal << 1; if(bitAppear == 0b0){ @@ -72,6 +73,17 @@ public static int int2ArabicArray(final int iVal, iLow = shiftedBcd | LSB_INTMASK; } + // through iHigh + // if iLow has 8-bcd "67108863"(2^26-1) or lower + if(sig < 27) continue; + + int halfCarry = bqVal & MSB_INTMASK; + + if(noHalfCarry){ + if(halfCarry == 0b0) continue; + noHalfCarry = false; + } + bqVal = BcdUtils.toBiQuinary(iHigh); shiftedBcd = bqVal << 1; diff --git a/src/test/java/io/github/olyutorskii/doubdabc/BcdArraysTest.java b/src/test/java/io/github/olyutorskii/doubdabc/BcdArraysTest.java index e311b51..e7acdd5 100644 --- a/src/test/java/io/github/olyutorskii/doubdabc/BcdArraysTest.java +++ b/src/test/java/io/github/olyutorskii/doubdabc/BcdArraysTest.java @@ -65,6 +65,14 @@ public void testInt2ArabicArray() { assertEquals(result, 9); assertEquals("100000000", new String(buf, 0, result)); + result = BcdArrays.int2ArabicArray(67_108_863, buf, 0); + assertEquals(result, 8); + assertEquals("67108863", new String(buf, 0, result)); + + result = BcdArrays.int2ArabicArray(67_108_864, buf, 0); + assertEquals(result, 8); + assertEquals("67108864", new String(buf, 0, result)); + result = BcdArrays.int2ArabicArray(0, buf, 0); assertEquals(result, 0); assertEquals("", new String(buf, 0, result)); From 6753341d26fb2531a58b4a82dbff6c3d83a24aea Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Mon, 24 Apr 2017 11:10:31 +0900 Subject: [PATCH 08/13] Remove Writer class --- CHANGELOG.md | 6 + README.md | 9 +- .../doubdabc/io/DecimalWriter.java | 110 ----------- .../olyutorskii/doubdabc/io/package-info.java | 12 -- .../doubdabc/io/DecimalWriterTest.java | 177 ------------------ 5 files changed, 8 insertions(+), 306 deletions(-) delete mode 100644 src/main/java/io/github/olyutorskii/doubdabc/io/DecimalWriter.java delete mode 100644 src/main/java/io/github/olyutorskii/doubdabc/io/package-info.java delete mode 100644 src/test/java/io/github/olyutorskii/doubdabc/io/DecimalWriterTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index f3fa285..22c1ca7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ DoubDabC Changelog ## WIP Released on 20XX-XX-XX +## v2.101.2 +Released on 2017-XX-XX +- Extended Writer class removed. SeeJarabraDix new project. +- Split BcdUtils class +- Add BcdArrays + ## v1.103.2 Released on 2017-03-22 - Add DecimalWriter which supports print(int) #16 diff --git a/README.md b/README.md index 8da394c..3832da5 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ that supports **binary integer value to decimal sequence conversion**. * Yes, it will substitute implementations such as -`Integer.toString(int)`, `System.out(=PrintStream).println(int)` and so on. +`Integer.toString(int)` and so on. ## DoubDabC implementation ## @@ -47,15 +47,10 @@ as Arabic numeral characters\(0-9\) sequence output. * `CharSequence` wrapper class is provided. -* Extended `Writer` class is provided -which supports `print(int)` & `print(long)` methods -like `PrintWriter`. - ## Limitations ## -* DoubDabC does not support negative values -with the exception of `DecimalWriter`. +* DoubDabC does not support negative values. Signed-values are treated as Unsigned-value like `Integer.toUnsignedString(int)`. Let's convert absolute value that follows minus\(-\) sign. diff --git a/src/main/java/io/github/olyutorskii/doubdabc/io/DecimalWriter.java b/src/main/java/io/github/olyutorskii/doubdabc/io/DecimalWriter.java deleted file mode 100644 index 6d3190f..0000000 --- a/src/main/java/io/github/olyutorskii/doubdabc/io/DecimalWriter.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * License : The MIT License - * Copyright(c) 2017 olyutorskii - */ - -package io.github.olyutorskii.doubdabc.io; - -import io.github.olyutorskii.doubdabc.BcdRegister; -import io.github.olyutorskii.doubdabc.BitFeeder; -import io.github.olyutorskii.doubdabc.DecimalOut; -import java.io.FilterWriter; -import java.io.IOException; -import java.io.Writer; - -/** - * Decimal-form-integer writer with Double-Dabble algorithm. - * - *

It's similar to {@link java.io.PrintWriter} - * - * @see java.io.PrintWriter - */ -public class DecimalWriter extends FilterWriter{ - - private final BcdRegister bcd; - private final DecimalOut decOut; - - - /** - * Constructor. - * @param out char output stream - */ - public DecimalWriter(Writer out){ - super(out); - - this.bcd = new BcdRegister(BcdRegister.MAX_COL_UINT64); - this.decOut = new DecimalOut(this.bcd); - - return; - } - - - /** - * Print signed int value. - * - *

It's similar to {@link java.io.PrintWriter#print(int)}. - * - * @param iVal int value - * @throws IOException output error - * @see java.io.PrintWriter#print(int) - */ - public void print(int iVal) throws IOException{ - boolean negSign; - int absVal; - - if(iVal < 0){ - negSign = true; - absVal = -iVal; - // -2147483648 does not change. OK, NP. - }else{ - negSign = false; - absVal = iVal; - } - - synchronized(this.bcd){ - BitFeeder.feedUInt32(this.bcd, absVal); - - synchronized(this.lock){ - if(negSign) append('-'); - this.decOut.writeDigits(this); - } - } - - return; - } - - /** - * Print signed long value. - * - *

It's similar to {@link java.io.PrintWriter#print(long)}. - * - * @param lVal long value - * @throws IOException output error - * @see java.io.PrintWriter#print(long) - */ - public void print(long lVal) throws IOException{ - boolean negSign; - long absVal; - - if(lVal < 0){ - negSign = true; - absVal = -lVal; - // -9223372036854775808 does not change. OK, NP. - }else{ - negSign = false; - absVal = lVal; - } - - synchronized(this.bcd){ - BitFeeder.feedUInt64(this.bcd, absVal); - - synchronized(this.lock){ - if(negSign) append('-'); - this.decOut.writeDigits(this); - } - } - - return; - } - -} diff --git a/src/main/java/io/github/olyutorskii/doubdabc/io/package-info.java b/src/main/java/io/github/olyutorskii/doubdabc/io/package-info.java deleted file mode 100644 index 9b14f93..0000000 --- a/src/main/java/io/github/olyutorskii/doubdabc/io/package-info.java +++ /dev/null @@ -1,12 +0,0 @@ -/* - * License : The MIT License - * Copyright(c) 2017 olyutorskii - */ - -/** - * Extended I/O-classes with Double-Dabble algorithm. - */ - -package io.github.olyutorskii.doubdabc.io; - -/* EOF */ diff --git a/src/test/java/io/github/olyutorskii/doubdabc/io/DecimalWriterTest.java b/src/test/java/io/github/olyutorskii/doubdabc/io/DecimalWriterTest.java deleted file mode 100644 index 04ccb61..0000000 --- a/src/test/java/io/github/olyutorskii/doubdabc/io/DecimalWriterTest.java +++ /dev/null @@ -1,177 +0,0 @@ -/* - * License : The MIT License - * Copyright(c) 2017 olyutorskii - */ - -package io.github.olyutorskii.doubdabc.io; - -import java.io.CharArrayWriter; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * - */ -public class DecimalWriterTest { - - public DecimalWriterTest() { - } - - @BeforeClass - public static void setUpClass() { - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of print method, of class DecimalWriter. - */ - @Test - public void testPrint_int() throws Exception { - System.out.println("print"); - - DecimalWriter writer; - CharArrayWriter out; - - out = new CharArrayWriter(); - writer = new DecimalWriter(out); - - out.reset(); - writer.print(1); - assertEquals("1", out.toString()); - - out.reset(); - writer.print(0); - assertEquals("0", out.toString()); - - out.reset(); - writer.print(-1); - assertEquals("-1", out.toString()); - - out.reset(); - writer.print(999); - assertEquals("999", out.toString()); - - out.reset(); - writer.print(-999); - assertEquals("-999", out.toString()); - - out.reset(); - writer.print(1_234_567_890); - assertEquals("1234567890", out.toString()); - - out.reset(); - writer.print(-1_234_567_890); - assertEquals("-1234567890", out.toString()); - - out.reset(); - writer.print(Integer.MAX_VALUE - 1); - assertEquals("2147483646", out.toString()); - - out.reset(); - writer.print(Integer.MAX_VALUE); - assertEquals("2147483647", out.toString()); - - out.reset(); - writer.print(Integer.MIN_VALUE); - assertEquals("-2147483648", out.toString()); - - out.reset(); - writer.print(Integer.MIN_VALUE + 1); - assertEquals("-2147483647", out.toString()); - - return; - } - - /** - * Test of print method, of class DecimalWriter. - */ - @Test - public void testPrint_long() throws Exception { - System.out.println("print"); - - DecimalWriter writer; - CharArrayWriter out; - - out = new CharArrayWriter(); - writer = new DecimalWriter(out); - - out.reset(); - writer.print(1L); - assertEquals("1", out.toString()); - - out.reset(); - writer.print(0L); - assertEquals("0", out.toString()); - - out.reset(); - writer.print(-1L); - assertEquals("-1", out.toString()); - - out.reset(); - writer.print(999L); - assertEquals("999", out.toString()); - - out.reset(); - writer.print(-999L); - assertEquals("-999", out.toString()); - - out.reset(); - writer.print(1_234_567_890L); - assertEquals("1234567890", out.toString()); - - out.reset(); - writer.print(-1_234_567_890L); - assertEquals("-1234567890", out.toString()); - - out.reset(); - writer.print((long)Integer.MAX_VALUE); - assertEquals("2147483647", out.toString()); - - out.reset(); - writer.print((long)Integer.MAX_VALUE + 1L); - assertEquals("2147483648", out.toString()); - - out.reset(); - writer.print((long)Integer.MIN_VALUE); - assertEquals("-2147483648", out.toString()); - - out.reset(); - writer.print((long)Integer.MIN_VALUE - 1L); - assertEquals("-2147483649", out.toString()); - - out.reset(); - writer.print(Long.MAX_VALUE - 1L); - assertEquals("9223372036854775806", out.toString()); - - out.reset(); - writer.print(Long.MAX_VALUE); - assertEquals("9223372036854775807", out.toString()); - - out.reset(); - writer.print(Long.MIN_VALUE); - assertEquals("-9223372036854775808", out.toString()); - - out.reset(); - writer.print(Long.MIN_VALUE + 1L); - assertEquals("-9223372036854775807", out.toString()); - - return; - } - -} From 7642e1ef3b0d8f51e73551884edea0be03f25aa8 Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Mon, 24 Apr 2017 14:26:38 +0900 Subject: [PATCH 09/13] merge DecimalText to BcdSequence --- CHANGELOG.md | 1 + README.md | 3 +- .../olyutorskii/doubdabc/BcdArrays.java | 3 ++ .../{DecimalText.java => BcdSequence.java} | 47 +++++++++++-------- ...imalTextTest.java => BcdSequenceTest.java} | 25 +++++----- 5 files changed, 46 insertions(+), 33 deletions(-) rename src/main/java/io/github/olyutorskii/doubdabc/{DecimalText.java => BcdSequence.java} (76%) rename src/test/java/io/github/olyutorskii/doubdabc/{DecimalTextTest.java => BcdSequenceTest.java} (87%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22c1ca7..0900eae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Released on 2017-XX-XX - Extended Writer class removed. SeeJarabraDix new project. - Split BcdUtils class - Add BcdArrays +- Merge DecimalText to BcdSequence ## v1.103.2 Released on 2017-03-22 diff --git a/README.md b/README.md index 3832da5..929ee33 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,8 @@ ## What is DoubDabC ? ## * **DoubDabC** is a Java library -that supports **binary integer value to decimal sequence conversion**. +that supports **binary integer value to decimal sequence conversion** +with alternative algorithm. * Yes, it will substitute implementations such as `Integer.toString(int)` and so on. diff --git a/src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java b/src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java index 95d8c55..a10dfae 100644 --- a/src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java +++ b/src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java @@ -7,6 +7,9 @@ /** * BCD array utilities. + * + *

DDA implementations without any instance field + * but int input only supported. */ public final class BcdArrays { diff --git a/src/main/java/io/github/olyutorskii/doubdabc/DecimalText.java b/src/main/java/io/github/olyutorskii/doubdabc/BcdSequence.java similarity index 76% rename from src/main/java/io/github/olyutorskii/doubdabc/DecimalText.java rename to src/main/java/io/github/olyutorskii/doubdabc/BcdSequence.java index 02d6102..1e6c2c5 100644 --- a/src/main/java/io/github/olyutorskii/doubdabc/DecimalText.java +++ b/src/main/java/io/github/olyutorskii/doubdabc/BcdSequence.java @@ -5,16 +5,14 @@ package io.github.olyutorskii.doubdabc; -import java.nio.CharBuffer; - /** * CharSequence wrapping access to Decimal digits. */ -public class DecimalText implements CharSequence{ +public class BcdSequence implements CharSequence{ private final BcdRegister decimal; private final int[] intBuf; - private final CharBuffer textOut; + private final char[] charBuf; /** @@ -23,14 +21,14 @@ public class DecimalText implements CharSequence{ * @param decimal BCD register * @throws NullPointerException argument is null */ - public DecimalText(BcdRegister decimal) throws NullPointerException{ + public BcdSequence(BcdRegister decimal) throws NullPointerException{ super(); this.decimal = decimal; int digits = this.decimal.getMaxDigits(); this.intBuf = new int[digits]; - this.textOut = CharBuffer.allocate(digits); + this.charBuf = new char[digits]; return; } @@ -92,7 +90,9 @@ public CharSequence subSequence(int start, int end) int precision = this.decimal.getPrecision(); if(end > precision) throw new IndexOutOfBoundsException(); - String result = encodeText(start, end); + copyChar(start, end); + String result =new String(this.charBuf, start, end - start); + return result; } @@ -104,34 +104,41 @@ public CharSequence subSequence(int start, int end) @Override public String toString(){ int precision = this.decimal.getPrecision(); - String result = encodeText(0, precision); + buildChar(); + String result =new String(this.charBuf, 0, precision); return result; } /** - * Encode int array to text string. - * - * @param start start position - * @param end end position - * @return text + * Build char array data. + * @return digits length */ - private String encodeText(int start, int end){ + private int buildChar(){ + int precision = this.decimal.getPrecision(); + int result = copyChar(0, precision); + return result; + } + + /** + * Build ranged char array data. + * @param start start + * @param end end + * @return digits length + */ + private int copyChar(int start, int end){ + int length = end - start; this.decimal.toIntArray(this.intBuf, 0); - this.textOut.clear(); for(int idx = start; idx < end; idx++){ int digit = this.intBuf[idx]; // map [0 - 9](int) to ['0' - '9'](char) char decimalCh = (char)( digit | 0b0011_0000 ); - this.textOut.put(decimalCh); + this.charBuf[idx] = decimalCh; } - this.textOut.flip(); - String result = this.textOut.toString(); - - return result; + return length; } } diff --git a/src/test/java/io/github/olyutorskii/doubdabc/DecimalTextTest.java b/src/test/java/io/github/olyutorskii/doubdabc/BcdSequenceTest.java similarity index 87% rename from src/test/java/io/github/olyutorskii/doubdabc/DecimalTextTest.java rename to src/test/java/io/github/olyutorskii/doubdabc/BcdSequenceTest.java index 79156b6..444be58 100644 --- a/src/test/java/io/github/olyutorskii/doubdabc/DecimalTextTest.java +++ b/src/test/java/io/github/olyutorskii/doubdabc/BcdSequenceTest.java @@ -1,6 +1,7 @@ /* - * License : The MIT License - * Copyright(c) 2017 olyutorskii + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. */ package io.github.olyutorskii.doubdabc; @@ -16,9 +17,9 @@ /** * */ -public class DecimalTextTest { +public class BcdSequenceTest { - public DecimalTextTest() { + public BcdSequenceTest() { } @BeforeClass @@ -45,10 +46,10 @@ public void testLength() { System.out.println("length"); BcdRegister bs; - DecimalText chseq; + BcdSequence chseq; bs = new BcdRegister(3); - chseq = new DecimalText(bs); + chseq = new BcdSequence(bs); assertEquals(1, chseq.length()); @@ -67,10 +68,10 @@ public void testCharAt() { System.out.println("charAt"); BcdRegister bs; - DecimalText chseq; + BcdSequence chseq; bs = new BcdRegister(3); - chseq = new DecimalText(bs); + chseq = new BcdSequence(bs); assertEquals('0', chseq.charAt(0)); @@ -107,10 +108,10 @@ public void testSubSequence() { System.out.println("subSequence"); BcdRegister bs; - DecimalText chseq; + BcdSequence chseq; bs = new BcdRegister(3); - chseq = new DecimalText(bs); + chseq = new BcdSequence(bs); assertEquals("0", chseq.subSequence(0, 1).toString()); assertEquals("", chseq.subSequence(0, 0).toString()); @@ -158,11 +159,11 @@ public void testToString() { System.out.println("toString"); BcdRegister bs; - DecimalText chseq; + BcdSequence chseq; String result; bs = new BcdRegister(10); - chseq = new DecimalText(bs); + chseq = new BcdSequence(bs); result = chseq.toString(); assertEquals("0", result); From 4dae8497db278135655932932579fa71f1fb503b Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Mon, 24 Apr 2017 18:30:03 +0900 Subject: [PATCH 10/13] merge DecimalOut to BcdSequence --- CHANGELOG.md | 4 +- .../olyutorskii/doubdabc/BcdSequence.java | 91 ++++++- .../olyutorskii/doubdabc/DecimalOut.java | 150 ------------ .../olyutorskii/doubdabc/BcdSequenceTest.java | 171 +++++++++++++- .../olyutorskii/doubdabc/DecimalOutTest.java | 223 ------------------ 5 files changed, 257 insertions(+), 382 deletions(-) delete mode 100644 src/main/java/io/github/olyutorskii/doubdabc/DecimalOut.java delete mode 100644 src/test/java/io/github/olyutorskii/doubdabc/DecimalOutTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 0900eae..b739e4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,10 +6,10 @@ Released on 20XX-XX-XX ## v2.101.2 Released on 2017-XX-XX -- Extended Writer class removed. SeeJarabraDix new project. - Split BcdUtils class - Add BcdArrays -- Merge DecimalText to BcdSequence +- Extended Writer class removed. See JarabraDix new project. +- Merge DecimalText & DecimalOut to BcdSequence ## v1.103.2 Released on 2017-03-22 diff --git a/src/main/java/io/github/olyutorskii/doubdabc/BcdSequence.java b/src/main/java/io/github/olyutorskii/doubdabc/BcdSequence.java index 1e6c2c5..a988661 100644 --- a/src/main/java/io/github/olyutorskii/doubdabc/BcdSequence.java +++ b/src/main/java/io/github/olyutorskii/doubdabc/BcdSequence.java @@ -5,11 +5,24 @@ package io.github.olyutorskii.doubdabc; +import java.io.IOException; +import java.io.Writer; +import java.nio.BufferOverflowException; +import java.nio.CharBuffer; +import java.nio.ReadOnlyBufferException; + /** - * CharSequence wrapping access to Decimal digits. + * BCD Character sequence holder. + * + *

It has {@link java.lang.CharSequence} API. + * + *

There is some API for character-output classes. */ public class BcdSequence implements CharSequence{ + private static final int ARABIC_UC_MASK = 0b0011_0000; + + private final BcdRegister decimal; private final int[] intBuf; private final char[] charBuf; @@ -34,6 +47,78 @@ public BcdSequence(BcdRegister decimal) throws NullPointerException{ } + /** + * Write digits to Appendable. + * + * @param app output + * @return digits length + * @throws IOException If an I/O error occurs + */ + public int flushDigitTo(Appendable app) throws IOException{ + int length = buildChar(); + + for(int idx = 0; idx < length; idx++){ + char decimalCh = this.charBuf[idx]; + app.append(decimalCh); + } + + return length; + } + + /** + * Write digits to CharBuffer. + * + * @param cBuf output + * @return digits length + * @throws BufferOverflowException + * If buffer's current position is not smaller than its limit + * @throws ReadOnlyBufferException + * If buffer is read-only + */ + public int flushDigitTo(CharBuffer cBuf) + throws BufferOverflowException, ReadOnlyBufferException{ + int length = buildChar(); + cBuf.put(this.charBuf, 0, length); + return length; + } + + /** + * Write digits to Writer. + * + * @param writer output + * @return digits length + * @throws IOException If an I/O error occurs + */ + public int flushDigitTo(Writer writer) throws IOException{ + int length = buildChar(); + writer.write(this.charBuf, 0, length); + return length; + } + + /** + * Write digits to StringBuffer. + * + * @param buf output + * @return digits length + */ + public int flushDigitTo(StringBuffer buf){ + int length = buildChar(); + buf.append(this.charBuf, 0, length); + return length; + } + + /** + * Write digits to StringBuilder. + * + * @param buf output + * @return digits length + */ + public int flushDigitTo(StringBuilder buf){ + int length = buildChar(); + buf.append(this.charBuf, 0, length); + return length; + } + /** * Return digits columns. * @@ -66,7 +151,7 @@ public char charAt(int index) throws IndexOutOfBoundsException{ int digit = this.decimal.getDigit(digitPos); // map [0 - 9](int) to ['0' - '9'](char) - char result = (char)( digit | 0b0011_0000 ); + char result = (char)( digit | ARABIC_UC_MASK ); return result; } @@ -133,7 +218,7 @@ private int copyChar(int start, int end){ int digit = this.intBuf[idx]; // map [0 - 9](int) to ['0' - '9'](char) - char decimalCh = (char)( digit | 0b0011_0000 ); + char decimalCh = (char)( digit | ARABIC_UC_MASK ); this.charBuf[idx] = decimalCh; } diff --git a/src/main/java/io/github/olyutorskii/doubdabc/DecimalOut.java b/src/main/java/io/github/olyutorskii/doubdabc/DecimalOut.java deleted file mode 100644 index a587f32..0000000 --- a/src/main/java/io/github/olyutorskii/doubdabc/DecimalOut.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * License : The MIT License - * Copyright(c) 2017 olyutorskii - */ - -package io.github.olyutorskii.doubdabc; - -import java.io.IOException; -import java.io.Writer; -import java.nio.BufferOverflowException; -import java.nio.CharBuffer; -import java.nio.ReadOnlyBufferException; - -/** - * Output decimal number characters. - * - *

Negative value is not supported. - */ -public class DecimalOut { - - private final BcdRegister decimal; - - private final int[] intBuf; - private final char[] charBuf; - - - /** - * Constructor. - * - * @param decimal BCD register - * @throws NullPointerException argument is null - */ - public DecimalOut(BcdRegister decimal) throws NullPointerException{ - super(); - - this.decimal = decimal; - - int digits = this.decimal.getMaxDigits(); - this.intBuf = new int[digits]; - this.charBuf = new char[digits]; - - return; - } - - - /** - * Return digits length. - * - *

Letters of length will be written to the output. - * - * @return digits length - */ - public int getDigitsLength(){ - int result = this.decimal.getPrecision(); - return result; - } - - /** - * Build char array data. - * @return digits length - */ - private int buildChar(){ - int precision = this.decimal.getPrecision(); - this.decimal.toIntArray(this.intBuf, 0); - - for(int idx = 0; idx < precision; idx++){ - int digit = this.intBuf[idx]; - - // map [0 - 9](int) to ['0' - '9'](char) - char decimalCh = (char)( digit | 0b0011_0000 ); - - this.charBuf[idx] = decimalCh; - } - - return precision; - } - - /** - * Write digits to Appendable. - * - * @param app output - * @return digits length - * @throws IOException If an I/O error occurs - */ - public int writeDigits(Appendable app) throws IOException{ - int precision = buildChar(); - - for(int idx = 0; idx < precision; idx++){ - char decimalCh = this.charBuf[idx]; - app.append(decimalCh); - } - - return precision; - } - - /** - * Write digits to CharBuffer. - * - * @param cBuf output - * @return digits length - * @throws BufferOverflowException - * If buffer's current position is not smaller than its limit - * @throws ReadOnlyBufferException - * If buffer is read-only - */ - public int writeDigits(CharBuffer cBuf) - throws BufferOverflowException, ReadOnlyBufferException{ - int precision = buildChar(); - cBuf.put(this.charBuf, 0, precision); - return precision; - } - - /** - * Write digits to Writer. - * - * @param writer output - * @return digits length - * @throws IOException If an I/O error occurs - */ - public int writeDigits(Writer writer) throws IOException{ - int precision = buildChar(); - writer.write(this.charBuf, 0, precision); - return precision; - } - - /** - * Write digits to StringBuffer. - * - * @param buf output - * @return digits length - */ - public int writeDigits(StringBuffer buf){ - int precision = buildChar(); - buf.append(this.charBuf, 0, precision); - return precision; - } - - /** - * Write digits to StringBuilder. - * - * @param buf output - * @return digits length - */ - public int writeDigits(StringBuilder buf){ - int precision = buildChar(); - buf.append(this.charBuf, 0, precision); - return precision; - } - -} diff --git a/src/test/java/io/github/olyutorskii/doubdabc/BcdSequenceTest.java b/src/test/java/io/github/olyutorskii/doubdabc/BcdSequenceTest.java index 444be58..09c2650 100644 --- a/src/test/java/io/github/olyutorskii/doubdabc/BcdSequenceTest.java +++ b/src/test/java/io/github/olyutorskii/doubdabc/BcdSequenceTest.java @@ -6,6 +6,14 @@ package io.github.olyutorskii.doubdabc; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.io.StringWriter; +import java.io.Writer; +import java.nio.BufferOverflowException; +import java.nio.CharBuffer; +import java.nio.ReadOnlyBufferException; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -39,7 +47,162 @@ public void tearDown() { } /** - * Test of length method, of class DecimalText. + * Test of flushDigitTo method, of class BcdSequence. + * @throws IOException io error + */ + @Test + public void testFlushDigitTo_Appendable() throws IOException { + System.out.println("flushDigitTo"); + + BcdRegister bs; + BcdSequence ba; + ByteArrayOutputStream os; + Appendable app; + + bs = new BcdRegister(10); + ba = new BcdSequence(bs); + + TestValue.push9876543210(bs); + + os = new ByteArrayOutputStream(); + app = new PrintStream(os, true, "UTF-8"); + ba.flushDigitTo(app); + + assertEquals(10, os.size()); + byte[] result = os.toByteArray(); + + assertEquals((byte)'9', result[0]); + assertEquals((byte)'8', result[1]); + assertEquals((byte)'7', result[2]); + assertEquals((byte)'6', result[3]); + assertEquals((byte)'5', result[4]); + assertEquals((byte)'4', result[5]); + assertEquals((byte)'3', result[6]); + assertEquals((byte)'2', result[7]); + assertEquals((byte)'1', result[8]); + assertEquals((byte)'0', result[9]); + + return; + } + + /** + * Test of flushDigitTo method, of class BcdSequence. + */ + @Test + public void testFlushDigitTo_CharBuffer() { + System.out.println("flushDigitTo"); + + BcdRegister bs; + BcdSequence ba; + CharBuffer cbuf; + + bs = new BcdRegister(10); + ba = new BcdSequence(bs); + + TestValue.push9876543210(bs); + + cbuf = CharBuffer.allocate(10); + ba.flushDigitTo(cbuf); + + cbuf.flip(); + + assertEquals("9876543210", cbuf.toString()); + + cbuf.clear(); + cbuf = cbuf.asReadOnlyBuffer(); + try{ + ba.flushDigitTo(cbuf); + fail(); + }catch(ReadOnlyBufferException e){ + // GOOD + } + + cbuf = CharBuffer.allocate(9); + try{ + ba.flushDigitTo(cbuf); + fail(); + }catch(BufferOverflowException e){ + // GOOD + } + + return; + } + + /** + * Test of flushDigitTo method, of class BcdSequence. + * @throws IOException + */ + @Test + public void testFlushDigitTo_Writer() throws IOException { + System.out.println("flushDigitTo"); + + BcdRegister bs; + BcdSequence ba; + Writer writer; + + bs = new BcdRegister(10); + ba = new BcdSequence(bs); + + TestValue.push9876543210(bs); + + writer = new StringWriter(); + ba.flushDigitTo(writer); + + assertEquals("9876543210", writer.toString()); + + return; + } + + /** + * Test of flushDigitTo method, of class BcdSequence. + */ + @Test + public void testFlushDigitTo_StringBuffer() { + System.out.println("flushDigitTo"); + + BcdRegister bs; + BcdSequence ba; + StringBuffer text; + + bs = new BcdRegister(10); + ba = new BcdSequence(bs); + + TestValue.push9876543210(bs); + + text = new StringBuffer(); + ba.flushDigitTo(text); + + assertEquals("9876543210", text.toString()); + + return; + } + + /** + * Test of flushDigitTo method, of class BcdSequence. + */ + @Test + public void testFlushDigitTo_StringBuilder() { + System.out.println("flushDigitTo"); + + BcdRegister bs; + BcdSequence ba; + StringBuilder text; + + bs = new BcdRegister(10); + ba = new BcdSequence(bs); + + TestValue.push9876543210(bs); + + text = new StringBuilder(); + ba.flushDigitTo(text); + + assertEquals("9876543210", text.toString()); + + return; + } + + /** + * Test of length method, of class BcdSequence. */ @Test public void testLength() { @@ -61,7 +224,7 @@ public void testLength() { } /** - * Test of charAt method, of class DecimalText. + * Test of charAt method, of class BcdSequence. */ @Test public void testCharAt() { @@ -101,7 +264,7 @@ public void testCharAt() { } /** - * Test of subSequence method, of class DecimalText. + * Test of subSequence method, of class BcdSequence. */ @Test public void testSubSequence() { @@ -152,7 +315,7 @@ public void testSubSequence() { } /** - * Test of toString method, of class DecimalText. + * Test of toString method, of class BcdSequence. */ @Test public void testToString() { diff --git a/src/test/java/io/github/olyutorskii/doubdabc/DecimalOutTest.java b/src/test/java/io/github/olyutorskii/doubdabc/DecimalOutTest.java deleted file mode 100644 index 18668b2..0000000 --- a/src/test/java/io/github/olyutorskii/doubdabc/DecimalOutTest.java +++ /dev/null @@ -1,223 +0,0 @@ -/* - * License : The MIT License - * Copyright(c) 2017 olyutorskii - */ - -package io.github.olyutorskii.doubdabc; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import java.io.StringWriter; -import java.io.Writer; -import java.nio.BufferOverflowException; -import java.nio.CharBuffer; -import java.nio.ReadOnlyBufferException; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * - */ -public class DecimalOutTest { - - public DecimalOutTest() { - } - - @BeforeClass - public static void setUpClass() { - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of getDigitsLength method, of class DecimalOut. - */ - @Test - public void testGetDigitsLength() { - System.out.println("getDigitsLength"); - - BcdRegister bs; - DecimalOut ba; - - bs = new BcdRegister(15); - ba = new DecimalOut(bs); - assertEquals(1, ba.getDigitsLength()); - - TestValue.push9876543210(bs); - assertEquals(10, ba.getDigitsLength()); - - return; - } - - /** - * Test of writeDigits method, of class DecimalOut. - * @throws IOException io error - */ - @Test - public void testWriteDigits_Appendable() throws IOException { - System.out.println("writeDigits"); - - BcdRegister bs; - DecimalOut ba; - ByteArrayOutputStream os; - Appendable app; - - bs = new BcdRegister(10); - ba = new DecimalOut(bs); - - TestValue.push9876543210(bs); - - os = new ByteArrayOutputStream(); - app = new PrintStream(os, true, "UTF-8"); - ba.writeDigits(app); - - assertEquals(10, os.size()); - byte[] result = os.toByteArray(); - - assertEquals((byte)'9', result[0]); - assertEquals((byte)'8', result[1]); - assertEquals((byte)'7', result[2]); - assertEquals((byte)'6', result[3]); - assertEquals((byte)'5', result[4]); - assertEquals((byte)'4', result[5]); - assertEquals((byte)'3', result[6]); - assertEquals((byte)'2', result[7]); - assertEquals((byte)'1', result[8]); - assertEquals((byte)'0', result[9]); - - return; - } - - /** - * Test of writeDigits method, of class DecimalOut. - */ - @Test - public void testWriteDigits_CharBuffer() { - System.out.println("writeDigits"); - - BcdRegister bs; - DecimalOut ba; - CharBuffer cbuf; - - bs = new BcdRegister(10); - ba = new DecimalOut(bs); - - TestValue.push9876543210(bs); - - cbuf = CharBuffer.allocate(10); - ba.writeDigits(cbuf); - - cbuf.flip(); - - assertEquals("9876543210", cbuf.toString()); - - cbuf.clear(); - cbuf = cbuf.asReadOnlyBuffer(); - try{ - ba.writeDigits(cbuf); - fail(); - }catch(ReadOnlyBufferException e){ - // GOOD - } - - cbuf = CharBuffer.allocate(9); - try{ - ba.writeDigits(cbuf); - fail(); - }catch(BufferOverflowException e){ - // GOOD - } - - return; - } - - /** - * Test of writeDigits method, of class DecimalOut. - * @throws IOException - */ - @Test - public void testWriteDigits_Writer() throws IOException { - System.out.println("writeDigits"); - - BcdRegister bs; - DecimalOut ba; - Writer writer; - - bs = new BcdRegister(10); - ba = new DecimalOut(bs); - - TestValue.push9876543210(bs); - - writer = new StringWriter(); - ba.writeDigits(writer); - - assertEquals("9876543210", writer.toString()); - - return; - } - - /** - * Test of writeDigits method, of class DecimalOut. - */ - @Test - public void testWriteDigits_StringBuffer() { - System.out.println("writeDigits"); - - BcdRegister bs; - DecimalOut ba; - StringBuffer text; - - bs = new BcdRegister(10); - ba = new DecimalOut(bs); - - TestValue.push9876543210(bs); - - text = new StringBuffer(); - ba.writeDigits(text); - - assertEquals("9876543210", text.toString()); - - return; - } - - /** - * Test of writeDigits method, of class DecimalOut. - */ - @Test - public void testWriteDigits_StringBuilder() { - System.out.println("writeDigits"); - - BcdRegister bs; - DecimalOut ba; - StringBuilder text; - - bs = new BcdRegister(10); - ba = new DecimalOut(bs); - - TestValue.push9876543210(bs); - - text = new StringBuilder(); - ba.writeDigits(text); - - assertEquals("9876543210", text.toString()); - - return; - } - -} From c53382d14cfb518d18b907670e7550d550f34839 Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Mon, 24 Apr 2017 21:49:32 +0900 Subject: [PATCH 11/13] modify comments --- CHANGELOG.md | 6 +++--- README.md | 3 +-- .../java/io/github/olyutorskii/doubdabc/BcdArrays.java | 7 +++---- src/main/java/io/github/olyutorskii/doubdabc/BcdUtils.java | 2 +- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b739e4c..319acd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,10 +6,10 @@ Released on 20XX-XX-XX ## v2.101.2 Released on 2017-XX-XX -- Split BcdUtils class -- Add BcdArrays +- Merge DecimalText and DecimalOut to BcdSequence - Extended Writer class removed. See JarabraDix new project. -- Merge DecimalText & DecimalOut to BcdSequence +- Split BcdUtils class from BcdRegister +- Add BcdArrays ## v1.103.2 Released on 2017-03-22 diff --git a/README.md b/README.md index 929ee33..5d6c677 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # DoubDabC # -[![Build Status](https://travis-ci.org/olyutorskii/DoubDabC.svg?branch=master)] -(https://travis-ci.org/olyutorskii/DoubDabC) +[![Build Status](https://travis-ci.org/olyutorskii/DoubDabC.svg?branch=master)](https://travis-ci.org/olyutorskii/DoubDabC) ----------------------------------------------------------------------- diff --git a/src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java b/src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java index a10dfae..137ba5e 100644 --- a/src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java +++ b/src/main/java/io/github/olyutorskii/doubdabc/BcdArrays.java @@ -6,10 +6,7 @@ package io.github.olyutorskii.doubdabc; /** - * BCD array utilities. - * - *

DDA implementations without any instance field - * but int input only supported. + * BCD array utilities but experimental. */ public final class BcdArrays { @@ -40,6 +37,8 @@ private BcdArrays(){ *

sign-bit is treated as unsigned. * There is no minus-sign output. * + *

DDA implementations without any instance field. + * * @param iVal int value * @param cbuf char output * @param offset output start offset diff --git a/src/main/java/io/github/olyutorskii/doubdabc/BcdUtils.java b/src/main/java/io/github/olyutorskii/doubdabc/BcdUtils.java index 0a523c5..010abab 100644 --- a/src/main/java/io/github/olyutorskii/doubdabc/BcdUtils.java +++ b/src/main/java/io/github/olyutorskii/doubdabc/BcdUtils.java @@ -257,7 +257,7 @@ public static int clzNibble(long lVal){ b0 = 0b0000; } - return b3 | b2 | b1 | b0; + return (b3 | b2) | (b1 | b0); } } From d332ea09ddcade420e6f5b5d23a260223a658f1d Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Mon, 24 Apr 2017 22:28:03 +0900 Subject: [PATCH 12/13] modify comments --- src/main/java/io/github/olyutorskii/doubdabc/package-info.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/github/olyutorskii/doubdabc/package-info.java b/src/main/java/io/github/olyutorskii/doubdabc/package-info.java index 753921e..cbdaf31 100644 --- a/src/main/java/io/github/olyutorskii/doubdabc/package-info.java +++ b/src/main/java/io/github/olyutorskii/doubdabc/package-info.java @@ -5,7 +5,8 @@ /** * DoubDabC is a Java library that supports - * binary integer value to decimal sequence conversion. + * binary integer value to decimal sequence conversion + * with alternative algorithm. * *

DoubDabC implements Double-Dabble algorithm. * Double-Dabble is a radix-conversion algorithm From e386434e1bc0d6b253786b4680fc546e62027f12 Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Mon, 24 Apr 2017 22:34:07 +0900 Subject: [PATCH 13/13] prepare v2.101.2 --- CHANGELOG.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 319acd8..d67ccd2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ DoubDabC Changelog Released on 20XX-XX-XX ## v2.101.2 -Released on 2017-XX-XX +Released on 2017-04-24 - Merge DecimalText and DecimalOut to BcdSequence - Extended Writer class removed. See JarabraDix new project. - Split BcdUtils class from BcdRegister diff --git a/pom.xml b/pom.xml index 0400767..8aafff0 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ io.github.olyutorskii doubdabc - 1.103.3-SNAPSHOT + 2.101.2 jar DoubDabC