adds also float formatter

This commit is contained in:
hneemann 2021-01-22 17:17:30 +01:00
parent dbe62fae17
commit ff96c0f63c
6 changed files with 81 additions and 13 deletions

View File

@ -144,6 +144,12 @@ public final class Bits {
if (str.indexOf(':') >= 0)
return decodeFixed(str);
if (str.indexOf('.') > -1) {
if (str.endsWith("d") || str.endsWith("D"))
return Double.doubleToLongBits(Double.parseDouble(str.substring(0, str.length() - 1)));
else
return Float.floatToIntBits(Float.parseFloat(str));
}
int p = 0;

View File

@ -5,6 +5,8 @@
*/
package de.neemann.digital.core;
import java.util.Objects;
/**
* The int format used to format numbers
*/
@ -52,21 +54,23 @@ public class IntFormat {
* ascii format
*/
public static final IntFormat ASCII = new IntFormat("ascii", v -> "'" + (char) v.getValue() + "'", bits -> 3);
/**
* ascii format
* fixed point format
*/
public static final IntFormat FIXED_POINT = new IntFormatFixedPoint("fixed", false);
/**
* ascii format
* signed fixed point format
*/
public static final IntFormat FIXED_POINT_SIGNED = new IntFormatFixedPoint("fixedSigned", true);
/**
* float format
*/
public static final IntFormat FLOAT = new IntFormatFloat();
/**
* All the available formats
*/
public static final IntFormat[] VALUES = new IntFormat[]{DEF, DEC, DEC_SIGNED, HEX, BIN, OCT, ASCII, FIXED_POINT, FIXED_POINT_SIGNED};
public static final IntFormat[] VALUES = new IntFormat[]{DEF, DEC, DEC_SIGNED, HEX, BIN, OCT, ASCII, FIXED_POINT, FIXED_POINT_SIGNED, FLOAT};
private final String name;
private final EditFormat format;
@ -160,7 +164,7 @@ public class IntFormat {
private static final char[] DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
private static String toHex(Value inValue) {
static String toHex(Value inValue) {
final int bits = inValue.getBits();
final int numChars = (bits - 1) / 4 + 1;
@ -248,12 +252,14 @@ public class IntFormat {
@Override
public boolean equals(Object o) {
if (this == o) return true;
return o != null && getClass() == o.getClass();
if (o == null || getClass() != o.getClass()) return false;
IntFormat intFormat = (IntFormat) o;
return name.equals(intFormat.name);
}
@Override
public int hashCode() {
return getClass().hashCode();
return Objects.hash(name);
}
interface StrLen {

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2021 Helmut Neemann.
* Use of this source code is governed by the GPL v3 license
* that can be found in the LICENSE file.
*/
package de.neemann.digital.core;
/**
* Floating point formatter
*/
public class IntFormatFloat extends IntFormat {
/**
* Creates a new float instance
*/
IntFormatFloat() {
super("float", null, bits -> 15, true);
}
@Override
public String formatToView(Value inValue) {
if (inValue.isHighZ())
return inValue.toString();
switch (inValue.getBits()) {
case 32:
return Float.toString(Float.intBitsToFloat((int) inValue.getValue()));
case 64:
return Double.toString(Double.longBitsToDouble(inValue.getValue()));
default:
return "0x" + toHex(inValue);
}
}
@Override
public String formatToEdit(Value inValue) {
if (inValue.isHighZ())
return "Z";
if (inValue.getBits() == 64)
return formatToView(inValue) + "d";
else
return formatToView(inValue);
}
}

View File

@ -1314,6 +1314,7 @@ Sind evtl. die Namen der Variablen nicht eindeutig?</string>
<string name="key_intFormat_oct">Oktal</string>
<string name="key_intFormat_fixed">Festkomma</string>
<string name="key_intFormat_fixedSigned">Festkomma mit Vorzeichen</string>
<string name="key_intFormat_float">Fließkomma</string>
<string name="key_fixedPoint">Festkommastellen</string>
<string name="key_fixedPoint_tt">Zahl der binären Stellen nach dem Komma.</string>
<string name="key_barrelSigned">Verschiebeweite hat Vorzeichen</string><!-- BarrelShifter -->

View File

@ -1295,8 +1295,9 @@
<string name="key_intFormat_def">Default</string>
<string name="key_intFormat_hex">Hex</string>
<string name="key_intFormat_oct">Octal</string>
<string name="key_intFormat_fixed">fixed point</string>
<string name="key_intFormat_fixedSigned">signed fixed point</string>
<string name="key_intFormat_fixed">Fixed Point</string>
<string name="key_intFormat_fixedSigned">Signed Fixed Point</string>
<string name="key_intFormat_float">Floating Point</string>
<string name="key_fixedPoint">fixed point digits</string>
<string name="key_fixedPoint_tt">Number of fractional binary digits</string>
<string name="key_barrelSigned">shift input has sign</string><!-- BarrelShifter -->

View File

@ -52,10 +52,12 @@ public class IntFormatTest extends TestCase {
*/
public void testBitDecodeConstraint() throws Bits.NumberFormatException {
for (IntFormat f : IntFormat.VALUES) {
if (f == IntFormat.ASCII) {
if (f.equals(IntFormat.ASCII)) {
checkConstraint(f, tableAscii); // ascii supports only 16 bit
} else if (f instanceof IntFormatFixedPoint) {
checkConstraintFixedPoint((IntFormatFixedPoint) f, tableFixedPoint); // ascii supports only 16 bit
checkConstraintFixedPoint((IntFormatFixedPoint) f, tableFixedPoint);
} else if (f.equals(IntFormat.FLOAT)) {
checkConstraint(f, tableFloat);
} else {
checkConstraint(f, table);
}
@ -93,9 +95,17 @@ public class IntFormatTest extends TestCase {
new Value(-1, 64),
};
private static final Value[] tableFloat = new Value[]{
new Value(1, 2),
new Value(Float.floatToIntBits(-1), 32),
new Value(Float.floatToIntBits(1.2f), 32),
new Value(Double.doubleToLongBits(-1), 64),
new Value(Double.doubleToLongBits(1.2f), 64),
};
private static final Value[] tableAscii = new Value[]{
new Value(65, 8),
new Value(65, 8),
new Value(66, 8),
new Value(1000, 16),
new Value(-1, 7),
new Value(-1, 7),