MS floating point

This commit is contained in:
Denis Molony 2021-07-28 14:42:28 +10:00
parent f30fe1a78a
commit 09b6f66855
2 changed files with 66 additions and 3 deletions

View File

@ -166,12 +166,18 @@ public class CPMBasicFile extends BasicProgram
break;
case 0x1D:
text.append ("<" + HexFormatter.getHexString (buffer, ptr, 4, true) + ">");
String d4 = HexFormatter.floatValueMS4 (buffer, ptr) + "";
if (d4.endsWith (".0"))
d4 = d4.substring (0, d4.length () - 2);
text.append (d4 + "!");
ptr += 4;
break;
case 0x1F:
text.append ("<" + HexFormatter.getHexString (buffer, ptr, 8, true) + ">");
String d8 = HexFormatter.floatValueMS8 (buffer, ptr) + "";
if (d8.endsWith (".0"))
d8 = d8.substring (0, d8.length () - 2);
text.append (d8 + "#");
ptr += 8;
break;
@ -179,6 +185,7 @@ public class CPMBasicFile extends BasicProgram
text.append (String.format ("<%02X>", val));
}
}
System.out.println ();
ptr = nextAddress - loadAddress;
text.append ("\n");

View File

@ -11,6 +11,8 @@ public class HexFormatter
private static String[] hex =
{ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
private static MathContext mathContext = new MathContext (9);
private static MathContext mathContext4 = new MathContext (7);
private static MathContext mathContext8 = new MathContext (12);
// ---------------------------------------------------------------------------------//
public static String format (byte[] buffer)
@ -380,14 +382,68 @@ public class HexFormatter
int mantissa = (buffer[ptr + 1] & 0x7F) << 24 | (buffer[ptr + 2] & 0xFF) << 16
| (buffer[ptr + 3] & 0xFF) << 8 | (buffer[ptr + 4] & 0xFF);
boolean negative = (buffer[ptr + 1] & 0x80) > 0;
boolean negative = (buffer[ptr + 1] & 0x80) != 0;
double value = 0.5;
for (int i = 2, weight = 0x40000000; i <= 32; i++, weight >>>= 1)
if ((mantissa & weight) > 0)
value += Math.pow (0.5, i);
value *= Math.pow (2, exponent);
BigDecimal bd = new BigDecimal (value);
double rounded = bd.round (mathContext).doubleValue ();
return negative ? rounded * -1 : rounded;
}
// ---------------------------------------------------------------------------------//
public static double floatValueMS4 (byte[] buffer, int ptr)
// ---------------------------------------------------------------------------------//
{
int exponent = buffer[ptr + 3] & 0x7F; // biased 128
if (exponent == 0)
return 0.0;
int mantissa = (buffer[ptr + 2] & 0x7F) << 16 | (buffer[ptr + 1] & 0xFF) << 8
| (buffer[ptr] & 0xFF);
boolean negative = (buffer[ptr + 2] & 0x80) != 0;
double value = 0.5;
for (int i = 2, weight = 0x40_00_00; i <= 23; i++, weight >>>= 1)
if ((mantissa & weight) != 0)
value += Math.pow (0.5, i);
value *= Math.pow (2, exponent);
BigDecimal bd = new BigDecimal (value);
double rounded = bd.round (mathContext4).doubleValue ();
return negative ? rounded * -1 : rounded;
}
// ---------------------------------------------------------------------------------//
public static double floatValueMS8 (byte[] buffer, int ptr)
// ---------------------------------------------------------------------------------//
{
int exponent = buffer[ptr + 7] & 0x7F; // biased 128
if (exponent == 0)
return 0.0;
long mantissa = (long) (buffer[ptr + 6] & 0x7F) << 48
| (long) (buffer[ptr + 5] & 0xFF) << 40 | (long) (buffer[ptr + 4] & 0xFF) << 32
| (long) (buffer[ptr + 3] & 0xFF) << 24 | (buffer[ptr + 2] & 0xFF) << 16
| (buffer[ptr + 1] & 0xFF) << 8 | (buffer[ptr] & 0xFF);
boolean negative = (buffer[ptr + 6] & 0x80) != 0;
double value = 0.5;
long weight = 0x40_00_00_00_00_00_00L;
for (int i = 2; i <= 55; i++, weight >>>= 1)
if ((mantissa & weight) != 0)
value += Math.pow (0.5, i);
value *= Math.pow (2, exponent);
BigDecimal bd = new BigDecimal (value);
double rounded = bd.round (mathContext8).doubleValue ();
return negative ? rounded * -1 : rounded;
}