From 44ca740ddac765d2baa3f6f7210e986a62c19c9f Mon Sep 17 00:00:00 2001 From: Griffin Berlstein Date: Wed, 4 Dec 2024 13:57:07 -0500 Subject: [PATCH] this is a bust --- Cargo.lock | 27 +++++++++++++++------ tools/cider-data-converter/Cargo.toml | 1 + tools/cider-data-converter/src/converter.rs | 21 ++++++++++++++-- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7eba77637..4cb878c74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -173,7 +173,7 @@ version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9aca4f2e283a3de6f3c40834fc27668303e33662676e90254b5168271209ef95" dependencies = [ - "fraction", + "fraction 0.11.2", "num-bigint", "serde", ] @@ -542,6 +542,7 @@ name = "cider-data-converter" version = "0.1.0" dependencies = [ "argh", + "fraction 0.15.3", "interp", "itertools 0.11.0", "nom 7.1.3", @@ -1096,6 +1097,16 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "fraction" +version = "0.15.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f158e3ff0a1b334408dc9fb811cd99b446986f4d8b741bb08f9df1604085ae7" +dependencies = [ + "lazy_static", + "num", +] + [[package]] name = "fst-writer" version = "0.2.1" @@ -1459,7 +1470,7 @@ dependencies = [ "calyx-opt", "calyx-utils", "ciborium", - "fraction", + "fraction 0.11.2", "fst-writer", "itertools 0.11.0", "lazy_static", @@ -1703,9 +1714,9 @@ dependencies = [ [[package]] name = "num" -version = "0.4.1" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" dependencies = [ "num-bigint", "num-complex", @@ -1728,9 +1739,9 @@ dependencies = [ [[package]] name = "num-complex" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23c6602fda94a57c990fe0df199a035d83576b496aa29f4e634a8ac6004e68a6" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" dependencies = [ "num-traits", "serde", @@ -1753,9 +1764,9 @@ dependencies = [ [[package]] name = "num-iter" -version = "0.1.44" +version = "0.1.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d869c01cc0c455284163fd0092f1f93835385ccab5a98a0dcc497b2f8bf055a9" +checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" dependencies = [ "autocfg", "num-integer", diff --git a/tools/cider-data-converter/Cargo.toml b/tools/cider-data-converter/Cargo.toml index a690023d3..f349cf552 100644 --- a/tools/cider-data-converter/Cargo.toml +++ b/tools/cider-data-converter/Cargo.toml @@ -17,6 +17,7 @@ thiserror = "1.0.59" num-bigint = { version = "0.4.6" } num-rational = { version = "0.4.2" } num-traits = { version = "0.2.19" } +fraction = "0.15" nom = "7.1.3" [dev-dependencies] diff --git a/tools/cider-data-converter/src/converter.rs b/tools/cider-data-converter/src/converter.rs index 5250c1825..6bdccdd11 100644 --- a/tools/cider-data-converter/src/converter.rs +++ b/tools/cider-data-converter/src/converter.rs @@ -1,4 +1,5 @@ use super::json_data::*; +use fraction::{BigDecimal, BigFraction}; use interp::serialization::*; use itertools::Itertools; use num_bigint::{BigInt, BigUint, ToBigInt}; @@ -266,6 +267,21 @@ fn parse_bytes_fixed( BigRational::new(int.clone(), BigInt::from(1) << frac_width) } +fn big_rational_to_big_decimal(rat: BigRational) -> BigDecimal { + let is_neg = rat.is_negative(); + let (numerator, denominator) = rat.into_raw(); + let (_, numerator) = numerator.into_parts(); + let (_, denominator) = denominator.into_parts(); + let frac = if is_neg { + BigFraction::new_neg(numerator, denominator) + } else { + BigFraction::new(numerator, denominator) + }; + + let dec = BigDecimal::from_fraction(frac); + dec.set_precision(64) +} + fn format_data(declaration: &MemoryDeclaration, data: &[u8]) -> ParseVec { let width = declaration.width(); @@ -285,9 +301,10 @@ fn format_data(declaration: &MemoryDeclaration, data: &[u8]) -> ParseVec { } => { let int = parse_bytes_fixed(chunk, int_width, frac_width, signed); - let float = int.to_f64().unwrap(); + let decimal = big_rational_to_big_decimal(int); + - Number::from_f64(float).unwrap() + Number::from_str(&decimal.to_string()).unwrap() } interp::serialization::FormatInfo::IEEFloat { width,