My proposal for resolving add92039f433cd315f2087da9c1a0de899927d96
authorChristian Femers <femers@in.tum.de>
Mon, 26 Aug 2019 01:28:58 +0000 (03:28 +0200)
committerChristian Femers <femers@in.tum.de>
Mon, 26 Aug 2019 01:28:58 +0000 (03:28 +0200)
The choice was made based on functionality (and a bit performance) and
mostly readability.

net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java
net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java
net.mograsim.logic.core/test/net/mograsim/logic/core/types/BitVectorTest.java
net.mograsim.logic.model.am2900/test/net/mograsim/logic/model/am2900/am2910/TestableAm2910Impl.java

index 8bff357..da7b8c9 100644 (file)
@@ -18,6 +18,11 @@ public enum Bit implements StrictLogicType<Bit>
                this.symbol = symbol;
        }
 
+       /**
+        * Returns if the Bit is binary, this is only true for <code>ZERO</code> and <code>ONE</code>.
+        * 
+        * @return true if and only if <code>this == ONE || this == ZERO</code>
+        */
        public boolean isBinary()
        {
                return this == ONE || this == ZERO;
index 6a36403..e17ae6e 100644 (file)
@@ -57,43 +57,12 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                return new BitVector(bit.makeArray(length));
        }
 
-       public BigInteger getUnsignedValue()
-       {
-               if (!isBinary())
-                       throw new NumberFormatException("BitVector is non binary: " + toString());
-               byte[] bytes = new byte[(bits.length / 8) + 1];
-               for (int i = 0; i < bits.length; i++)
-               {
-                       if (Bit.ONE == bits[i])
-                       {
-                               bytes[i / 8] |= 1 << (i % 8);
-                       }
-               }
-               return new BigInteger(bytes);
-       }
-
-       public static BitVector from(BigInteger b, int length)
+       public static BitVector from(long value, int bits)
        {
-               int bitLength = b.bitLength();
-               int actualLength = Integer.min(bitLength, length);
-               Bit[] bits = new Bit[length];
-               for (int i = 0; i < actualLength; i++)
-                       bits[i] = b.testBit(i) ? Bit.ONE : Bit.ZERO;
-               if (b.signum() < 0)
-                       for (int i = actualLength; i < length; i++)
-                               bits[i] = Bit.ONE;
-               else
-                       for (int i = actualLength; i < length; i++)
-                               bits[i] = Bit.ZERO;
-               return BitVector.of(bits);
+               return from(BigInteger.valueOf(value), bits);
        }
 
-       public static BitVector of(long value, int bits)
-       {
-               return of(BigInteger.valueOf(value), bits);
-       }
-
-       public static BitVector of(BigInteger value, int bits)
+       public static BitVector from(BigInteger value, int bits)
        {
                Bit[] values = new Bit[bits];
                for (int i = 0; i < bits; i++)
@@ -129,6 +98,11 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                return bits.clone();
        }
 
+       /**
+        * Checks if all bits are {@link Bit#isBinary() binary}.
+        * 
+        * @see Bit#isBinary()
+        */
        public boolean isBinary()
        {
                for (int i = 0; i < bits.length; i++)
@@ -439,20 +413,23 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
        }
 
        /**
-        * Returns the value of the BitVector as BigInteger either unsigned or as two-complement.
-        * 
-        * @param signed if true and the BitVector represents a negative two-complement integer, an equivalent BigInteger is returned
-        * @return the value of this BitVector as BigInteger
+        * Returns the value of the BitVector as BigInteger.
         * 
+        * @throws NumberFormatException if the BitVector is not {@link #isBinary() binary}.
         */
-       public BigInteger toBigInteger(boolean signed)
+       public BigInteger getUnsignedValue()
        {
                if (!isBinary())
                        throw new NumberFormatException(this + " is not binary");
-               BigInteger val = new BigInteger(toString(), 2);
-               if (signed && bits[0] == Bit.ONE)
-                       val = val.not().setBit(val.bitLength()).add(BigInteger.ONE);
-               return val;
+               byte[] bytes = new byte[(bits.length / 8) + 1];
+               for (int i = 0; i < bits.length; i++)
+               {
+                       if (Bit.ONE == bits[i])
+                       {
+                               bytes[i / 8] |= 1 << (i % 8);
+                       }
+               }
+               return new BigInteger(bytes);
        }
 
        /**
index 6c4670f..11b5feb 100644 (file)
@@ -38,15 +38,6 @@ class BitVectorTest
                assertEquals(BitVector.of(X, X, X), BitVector.of(X, 3));
        }
 
-       @Test
-       void testFrom()
-       {
-               assertEquals(BitVector.parse("101"), BitVector.from(BigInteger.valueOf(0b101), 3));
-               assertEquals(BitVector.parse("01010"), BitVector.from(BigInteger.valueOf(0b01010), 5));
-               assertEquals(BitVector.parse("10101"), BitVector.from(BigInteger.valueOf(-11), 5));
-               assertEquals(BitVector.parse("0000"), BitVector.from(BigInteger.valueOf(0), 4));
-       }
-
        @Test
        void testGetUnsignedValue()
        {
@@ -58,34 +49,22 @@ class BitVectorTest
 
        }
 
-       @Test
-       void testToBigInteger()
-       {
-               assertEquals(BigInteger.valueOf(0b101), BitVector.parse("101").toBigInteger(false));
-               assertEquals(BigInteger.valueOf(0b01010), BitVector.parse("01010").toBigInteger(false));
-               assertEquals(BigInteger.valueOf(0), BitVector.parse("0000").toBigInteger(false));
-
-//             assertEquals(BigInteger.valueOf(-11), BitVector.parse("10101").toBigInteger(true)); TODO
-
-               assertThrows(NumberFormatException.class, () -> BitVector.parse("00X1").toBigInteger(false));
-       }
-
        @Test
        void testOfLongInt()
        {
-               assertEquals(BitVector.parse("101"), BitVector.of(0b101L, 3));
-               assertEquals(BitVector.parse("01010"), BitVector.of(0b01010L, 5));
-               assertEquals(BitVector.parse("10101"), BitVector.of(-11L, 5));
-               assertEquals(BitVector.parse("0000"), BitVector.of(0L, 4));
+               assertEquals(BitVector.parse("101"), BitVector.from(0b101L, 3));
+               assertEquals(BitVector.parse("01010"), BitVector.from(0b01010L, 5));
+               assertEquals(BitVector.parse("10101"), BitVector.from(-11L, 5));
+               assertEquals(BitVector.parse("0000"), BitVector.from(0L, 4));
        }
 
        @Test
        void testOfBigIntegerInt()
        {
-               assertEquals(BitVector.parse("101"), BitVector.of(BigInteger.valueOf(0b101), 3));
-               assertEquals(BitVector.parse("01010"), BitVector.of(BigInteger.valueOf(0b01010), 5));
-               assertEquals(BitVector.parse("10101"), BitVector.of(BigInteger.valueOf(-11), 5));
-               assertEquals(BitVector.parse("0000"), BitVector.of(BigInteger.valueOf(0), 4));
+               assertEquals(BitVector.parse("101"), BitVector.from(BigInteger.valueOf(0b101), 3));
+               assertEquals(BitVector.parse("01010"), BitVector.from(BigInteger.valueOf(0b01010), 5));
+               assertEquals(BitVector.parse("10101"), BitVector.from(BigInteger.valueOf(-11), 5));
+               assertEquals(BitVector.parse("0000"), BitVector.from(BigInteger.valueOf(0), 4));
        }
 
        @Test
index 1695eae..f3686aa 100644 (file)
@@ -54,7 +54,7 @@ public class TestableAm2910Impl implements TestableAm2910
        @Override
        public void setInstruction(Am2910_Inst inst)
        {
-               I.setState(BitVector.of(inst.ordinal(), 4));
+               I.setState(BitVector.from(inst.ordinal(), 4));
        }
 
        @Override