1 package net.mograsim.machine.isa;
3 import java.math.BigDecimal;
4 import java.math.BigInteger;
6 import net.mograsim.logic.core.types.Bit;
7 import net.mograsim.logic.core.types.BitVector;
8 import net.mograsim.logic.core.types.BitVector.BitVectorMutator;
9 import net.mograsim.machine.isa.types.AsmNumberFormatException;
11 public class AsmFloatOperand implements AsmOperand
13 private final int size;
14 private final int mantissa;
15 private final int exponent;
17 public AsmFloatOperand(int size)
43 if (size > 128 && size % 32 == 0)
45 exponent = (int) Math.round(Math.log(size) / Math.log(2)) - 13;
46 mantissa = size - exponent - 1;
49 throw new IllegalArgumentException("Illegal floating point size: " + size);
54 public AsmFloatOperand(int exponent, int mantissa)
56 if (exponent < 1 || mantissa < 1)
57 throw new IllegalArgumentException("illegal floating point specification: e=" + exponent + ", m=" + mantissa);
58 this.exponent = exponent;
59 this.mantissa = mantissa;
60 this.size = exponent + mantissa + 1;
70 public BitVector parse(String s) throws AsmNumberFormatException
72 String cleaned = s.replace("_", "").toLowerCase();
73 int len = cleaned.length();
77 return bigDecToBitVector(new BigDecimal(cleaned));
79 catch (NumberFormatException e)
81 throw new AsmNumberFormatException(e, "Error parsing %s: no valid float format", s);
85 BitVector bigDecToBitVector(BigDecimal bi) throws AsmNumberFormatException
87 BigInteger raw = bi.unscaledValue();
88 int mantLen = raw.bitLength();
90 int bitLength = raw.bitLength() - (bi.signum() - 1) / 2;
92 throw new AsmNumberFormatException("Error parsing %s: bit count %d exceeds size %d", bi, bitLength, size);
93 BitVectorMutator bvm = BitVectorMutator.ofWidth(size);
94 for (int i = 0; i < size; i++)
95 bvm.setLSBit(i, Bit.of(raw.testBit(i)));
96 return bvm.toBitVector();