From a94a1a9b541e4369b36bbc8a655b8ffa81a8af00 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Wed, 26 Jun 2024 09:36:28 -0400 Subject: [PATCH] Move data-conversion tool to tools/ (#2175) And remove some unnecessary files in the process. --- Cargo.toml | 2 +- data-conversion/Cargo 2.toml | 19 -- data-conversion/converted 2.BIN | 4 - data-conversion/converted 2.txt | 4 - data-conversion/converted.BIN | 4 - data-conversion/converted.txt | 4 - data-conversion/src/main 2.rs | 261 ------------------ data-conversion/src/test | 22 -- data-conversion/src/test 2 | 14 - data-conversion/tests/result 2.txt | 6 - data-conversion/tests/test 2.txt | 6 - .../data-conversion}/Cargo.toml | 0 .../data-conversion}/src/main.rs | 0 .../data-conversion}/tests/result.txt | 0 .../data-conversion}/tests/test.txt | 0 15 files changed, 1 insertion(+), 345 deletions(-) delete mode 100644 data-conversion/Cargo 2.toml delete mode 100644 data-conversion/converted 2.BIN delete mode 100644 data-conversion/converted 2.txt delete mode 100644 data-conversion/converted.BIN delete mode 100644 data-conversion/converted.txt delete mode 100644 data-conversion/src/main 2.rs delete mode 100644 data-conversion/src/test delete mode 100644 data-conversion/src/test 2 delete mode 100644 data-conversion/tests/result 2.txt delete mode 100644 data-conversion/tests/test 2.txt rename {data-conversion => tools/data-conversion}/Cargo.toml (100%) rename {data-conversion => tools/data-conversion}/src/main.rs (100%) rename {data-conversion => tools/data-conversion}/tests/result.txt (100%) rename {data-conversion => tools/data-conversion}/tests/test.txt (100%) diff --git a/Cargo.toml b/Cargo.toml index 4bcf21fb4d..9ecc2ddfcf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ members = [ "cider-dap", "fud2", "fud2/fud-core", - "data-conversion", + "tools/data-conversion", "tools/btor2/btor2i", "tools/cider-data-converter", "tools/calyx-pass-explorer", diff --git a/data-conversion/Cargo 2.toml b/data-conversion/Cargo 2.toml deleted file mode 100644 index 87bdf6fd3c..0000000000 --- a/data-conversion/Cargo 2.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -name = "data-conversion" -authors.workspace = true -license-file.workspace = true -keywords.workspace = true -repository.workspace = true -readme.workspace = true -description.workspace = true -categories.workspace = true -homepage.workspace = true -edition.workspace = true -version.workspace = true -rust-version.workspace = true - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -argh.workspace = true - diff --git a/data-conversion/converted 2.BIN b/data-conversion/converted 2.BIN deleted file mode 100644 index 97f7b9b24a..0000000000 --- a/data-conversion/converted 2.BIN +++ /dev/null @@ -1,4 +0,0 @@ -0 01111011 10011001100110011001101 -0 01111100 10011001100110011001101 -0 01111111 00011001100110011001101 -0 01111101 00000000000000000000000 diff --git a/data-conversion/converted 2.txt b/data-conversion/converted 2.txt deleted file mode 100644 index 10f60e94dd..0000000000 --- a/data-conversion/converted 2.txt +++ /dev/null @@ -1,4 +0,0 @@ -0x3DCCCCCD -0x3E4CCCCD -0x3F8CCCCD -0x3E800000 diff --git a/data-conversion/converted.BIN b/data-conversion/converted.BIN deleted file mode 100644 index 97f7b9b24a..0000000000 --- a/data-conversion/converted.BIN +++ /dev/null @@ -1,4 +0,0 @@ -0 01111011 10011001100110011001101 -0 01111100 10011001100110011001101 -0 01111111 00011001100110011001101 -0 01111101 00000000000000000000000 diff --git a/data-conversion/converted.txt b/data-conversion/converted.txt deleted file mode 100644 index 10f60e94dd..0000000000 --- a/data-conversion/converted.txt +++ /dev/null @@ -1,4 +0,0 @@ -0x3DCCCCCD -0x3E4CCCCD -0x3F8CCCCD -0x3E800000 diff --git a/data-conversion/src/main 2.rs b/data-conversion/src/main 2.rs deleted file mode 100644 index a20eae90ca..0000000000 --- a/data-conversion/src/main 2.rs +++ /dev/null @@ -1,261 +0,0 @@ -//use std::env; -use argh::FromArgs; -use std::fs::read_to_string; -use std::fs::File; -use std::io::{self, Write}; - -fn main() { - #[derive(FromArgs)] - /// get arguments to convert - struct Arguments { - /// file to convert from - #[argh(option)] - from: String, - - /// file to convery to - #[argh(option)] - to: String, - - /// type to convert from - #[argh(option)] - ftype: String, - - /// type to convert to - #[argh(option)] - totype: String, - } - - let args: Arguments = argh::from_env(); - - // Array of all possible options to convert from - let from_types: Vec = vec![ - "binary".to_string(), - "float".to_string(), - "hex".to_string(), - "fixed".to_string(), - ]; - // Array of all possible options to convert to - let to_types: Vec = - vec!["binary".to_string(), "float".to_string(), "hex".to_string()]; - - if !from_types.contains(&args.ftype) { - panic!("{} is not a valid type to convert from", args.from); - } - if !to_types.contains(&args.totype) { - panic!("{} is not a valid type to convert to", args.to); - } - - convert(&args.from, &args.to, &args.ftype, &args.totype); -} - -/// Converts [filepath_get] from type [convert_from] to type -/// [convert_to] in [filepath_send] -fn convert( - filepath_get: &String, - filepath_send: &String, - convert_from: &String, - convert_to: &String, -) { - // Create a file called converted.txt - let mut converted = File::create(filepath_send).expect("creation failed"); - - if convert_to == "binary" { - //Convert from hex to binary - if convert_from == "hex" { - for line in read_to_string(filepath_get).unwrap().lines() { - hex_to_binary(line, &mut converted) - .expect("Failed to write binary to file"); - } - //Convert from float to binary - } else if convert_from == "float" { - for line in read_to_string(filepath_get).unwrap().lines() { - float_to_binary(line, &mut converted) - .expect("Failed to write binary to file"); - } - } else if convert_from == "fixed" { - for line in read_to_string(filepath_get).unwrap().lines() { - fixed_to_binary(line, &mut converted) - .expect("Failed to write binary to file"); - } - } - } else if convert_to == "hex" { - //Convert from binary to hex - if convert_from == "binary" { - for line in read_to_string(filepath_get).unwrap().lines() { - binary_to_hex(line, &mut converted) - .expect("Failed to write hex to file"); - } - } - } else if convert_to == "float" { - //Convert from binary to float - if convert_from == "binary" { - for line in read_to_string(filepath_get).unwrap().lines() { - binary_to_float(line, &mut converted) - .expect("Failed to write float to file"); - } - } - } - - eprintln!( - "Successfully converted from {} to {} in {}", - convert_from, convert_to, filepath_send - ); -} - -/// Formats [to_format] properly for float values -fn format_binary(to_format: u32) -> String { - let binary_str = format!("{:032b}", to_format); - format!( - "{} {} {}", - &binary_str[0..1], // Sign bit - &binary_str[1..9], // Exponent - &binary_str[9..] // Significand - ) -} - -fn format_hex(to_format: u32) -> String { - format!("0x{:X}", to_format) -} - -/// Converts [float_string] to binary and appends to [filepath_send] -fn float_to_binary( - float_string: &str, - filepath_send: &mut File, -) -> std::io::Result<()> { - let float_of_string: f32; - // Convert string to float - match float_string.parse::() { - Ok(parsed_num) => float_of_string = parsed_num, - Err(_) => { - panic!("Failed to parse float from string") - } - } - - // Convert float to binary - let binary_of_float = float_of_string.to_bits(); - let formatted_binary_str = format_binary(binary_of_float); - - // Write binary string to the file - filepath_send.write_all(formatted_binary_str.as_bytes())?; - filepath_send.write_all(b"\n")?; - - Ok(()) -} - -/// Converts [hex_string] to binary and appends to [filepath_send] -fn hex_to_binary(hex_string: &str, filepath_send: &mut File) -> io::Result<()> { - // Convert hex to binary - let binary_of_hex = match u32::from_str_radix(hex_string, 16) { - Ok(value) => value, - Err(err) => { - return Err(io::Error::new(io::ErrorKind::InvalidData, err)) - } - }; - - // Format nicely - let formatted_binary_str = format!("{:b}", binary_of_hex); - - // Write binary string to the file - filepath_send.write_all(formatted_binary_str.as_bytes())?; - filepath_send.write_all(b"\n")?; - - Ok(()) -} - -fn binary_to_hex( - binary_string: &str, - filepath_send: &mut File, -) -> io::Result<()> { - let hex_of_binary = match u32::from_str_radix(binary_string, 2) { - Ok(value) => value, - Err(err) => { - return Err(io::Error::new(io::ErrorKind::InvalidData, err)) - } - }; - - let formatted_hex_str = format_hex(hex_of_binary); - - filepath_send.write(formatted_hex_str.as_bytes())?; - filepath_send.write_all(b"\n")?; - - Ok(()) -} - -fn binary_to_float( - binary_string: &str, - filepath_send: &mut File, -) -> io::Result<()> { - let binary_value = match u32::from_str_radix(binary_string, 2) { - Ok(value) => value, - Err(err) => { - return Err(io::Error::new(io::ErrorKind::InvalidData, err)) - } - }; - - // Interpret the integer as the binary representation of a floating-point number - let float_value = f32::from_bits(binary_value); - - let formated_float_str = format!("{:?}", float_value); - - filepath_send.write_all(formated_float_str.as_bytes())?; - filepath_send.write_all(b"\n")?; - - Ok(()) -} - -fn fixed_to_binary( - fixed_string: &str, - filepath_send: &mut File, - // scale: usize, -) -> io::Result<()> { - let fixed_str; - let exp_str; - let words: Vec<&str>; - - // Create an array with the elements of fixed_string delinieated by spaces - if fixed_string.contains(' ') { - // Split the input string into individual words - words = fixed_string.split_whitespace().collect(); - fixed_str = words - .get(0) - .unwrap_or_else(|| &"There is not a fixed number"); - exp_str = words.get(1).unwrap_or_else(|| &"There is no exponent"); - } else { - panic!("Input string does not contain a space."); - } - // Convert fixed value from string to int - print!("{} ", fixed_str); - let fixed_value: f32; - match fixed_str.parse::() { - Ok(parsed_num) => fixed_value = parsed_num, - Err(_) => { - panic!("Bad fixed value input") - } - } - // Convert exponent from string to float - print!("{} ", exp_str); - let exponent: f32; - match exp_str.parse::() { - Ok(parsed_num) => exponent = parsed_num, - Err(_) => { - panic!("Bad fixed value input") - } - } - - let multiplied_fixed = fixed_value * 2_f32.powf(-exponent); - print!("{} ", multiplied_fixed); - - // Convert to a 32-bit integer - let multiplied_fixed_as_i32 = multiplied_fixed as i32; - print!("{} ", multiplied_fixed_as_i32); - - // Convert to a binary string with 32 bits - let binary_of_fixed = format!("{:032b}", multiplied_fixed_as_i32); - print!("{}\n", binary_of_fixed); - - // Write binary string to the file - filepath_send.write_all(binary_of_fixed.as_bytes())?; - filepath_send.write_all(b"\n")?; - - Ok(()) -} \ No newline at end of file diff --git a/data-conversion/src/test b/data-conversion/src/test deleted file mode 100644 index f193814df2..0000000000 --- a/data-conversion/src/test +++ /dev/null @@ -1,22 +0,0 @@ -2.00 -3.4 -1.00000004 -4.5000 - -0.1 -0.2 -1.1 -0.25 - -00111101110011001100110011001101 -00111110010011001100110011001101 -00111111100011001100110011001101 -00111110100000000000000000000000 - - -3.5 -1 --3.5 -1 -10. 0 -10.5 -1 -3.625 -3 --3.625 -3 \ No newline at end of file diff --git a/data-conversion/src/test 2 b/data-conversion/src/test 2 deleted file mode 100644 index 83fc55f32f..0000000000 --- a/data-conversion/src/test 2 +++ /dev/null @@ -1,14 +0,0 @@ -2.00 -3.4 -1.00000004 -4.5000 - -0.1 -0.2 -1.1 -0.25 - -00111101110011001100110011001101 -00111110010011001100110011001101 -00111111100011001100110011001101 -00111110100000000000000000000000 diff --git a/data-conversion/tests/result 2.txt b/data-conversion/tests/result 2.txt deleted file mode 100644 index 7edb12e15d..0000000000 --- a/data-conversion/tests/result 2.txt +++ /dev/null @@ -1,6 +0,0 @@ -00000000000000000000000000000111 -11111111111111111111111111111001 -00000000000000000000000000001010 -00000000000000000000000000010101 -00000000000000000000000000011101 -11111111111111111111111111100011 diff --git a/data-conversion/tests/test 2.txt b/data-conversion/tests/test 2.txt deleted file mode 100644 index b0ab08c302..0000000000 --- a/data-conversion/tests/test 2.txt +++ /dev/null @@ -1,6 +0,0 @@ -3.5 -1 --3.5 -1 -10. 0 -10.5 -1 -3.625 -3 --3.625 -3 \ No newline at end of file diff --git a/data-conversion/Cargo.toml b/tools/data-conversion/Cargo.toml similarity index 100% rename from data-conversion/Cargo.toml rename to tools/data-conversion/Cargo.toml diff --git a/data-conversion/src/main.rs b/tools/data-conversion/src/main.rs similarity index 100% rename from data-conversion/src/main.rs rename to tools/data-conversion/src/main.rs diff --git a/data-conversion/tests/result.txt b/tools/data-conversion/tests/result.txt similarity index 100% rename from data-conversion/tests/result.txt rename to tools/data-conversion/tests/result.txt diff --git a/data-conversion/tests/test.txt b/tools/data-conversion/tests/test.txt similarity index 100% rename from data-conversion/tests/test.txt rename to tools/data-conversion/tests/test.txt