From 09b6f668557bcfa6e2596f9c5935db0efb8d2449 Mon Sep 17 00:00:00 2001 From: Denis Molony Date: Wed, 28 Jul 2021 14:42:28 +1000 Subject: [PATCH] MS floating point --- .../diskbrowser/applefile/CPMBasicFile.java | 11 +++- .../diskbrowser/utilities/HexFormatter.java | 58 ++++++++++++++++++- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/com/bytezone/diskbrowser/applefile/CPMBasicFile.java b/src/com/bytezone/diskbrowser/applefile/CPMBasicFile.java index 0a1fdef..bd0d4e1 100644 --- a/src/com/bytezone/diskbrowser/applefile/CPMBasicFile.java +++ b/src/com/bytezone/diskbrowser/applefile/CPMBasicFile.java @@ -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"); diff --git a/src/com/bytezone/diskbrowser/utilities/HexFormatter.java b/src/com/bytezone/diskbrowser/utilities/HexFormatter.java index 6d75d35..7c85817 100755 --- a/src/com/bytezone/diskbrowser/utilities/HexFormatter.java +++ b/src/com/bytezone/diskbrowser/utilities/HexFormatter.java @@ -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; }