Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inter rep #2313

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
709d0fa
Intermediate Representation functions added and are now default. Will…
Angelica-Schell Aug 6, 2024
51f4ee8
Addded some dependencies
Angelica-Schell Aug 6, 2024
ab02074
Added spec for intermediate representation functions
Angelica-Schell Aug 8, 2024
c71eb92
formatting
Angelica-Schell Aug 8, 2024
c317b63
fixed dependencies
Angelica-Schell Aug 8, 2024
a134703
Updated from 32 bit width to 64
Angelica-Schell Aug 9, 2024
cbd9d31
Updated thresholds and corrected error in using thresholds
Angelica-Schell Aug 9, 2024
eac7ba3
updated intermediate rep functions into helper functions and implemen…
Angelica-Schell Aug 10, 2024
85f6e21
Corrected binary_to_intermediate by requiring bit width from the comm…
Angelica-Schell Aug 12, 2024
b44ea8c
Updated from_hex to match the implementation of from_binary
Angelica-Schell Aug 12, 2024
cb240e2
corrected bit width command line input so that it is optional"
Angelica-Schell Aug 14, 2024
de786e5
Seperation of NumType and FileType
Angelica-Schell Sep 18, 2024
1c154e1
Added U8 Vector Representation
Angelica-Schell Sep 19, 2024
21df9c2
Started modularizing
Angelica-Schell Sep 19, 2024
15f3cb6
worked on u8 vector -> ir, and edited hex and binary -> u8 vector fun…
Angelica-Schell Sep 25, 2024
dd256e4
updated u8 to IR float function
Angelica-Schell Oct 22, 2024
5e8e9ee
added float_to_fixed_ir function
Angelica-Schell Oct 24, 2024
f7171d7
Merge branch 'main' into inter-rep
Angelica-Schell Oct 24, 2024
e2f6384
Formatting again
Angelica-Schell Oct 24, 2024
f58434e
commiting cargo.lock dependency changes for bignum crate
Angelica-Schell Oct 24, 2024
a6fb13a
Merge branch 'inter-rep' of https://github.com/calyxir/calyx into int…
Angelica-Schell Oct 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tools/data-conversion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ rust-version.workspace = true

[dependencies]
argh.workspace = true

num-bigint = "0.4"
num-traits = "0.2"
361 changes: 361 additions & 0 deletions tools/data-conversion/src/fast_track.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,361 @@
use std::fs::File;
use std::io::stdout;
use std::io::{self, Write};

/// Formats [to_format] properly for float values
pub fn format_binary(to_format: u64) -> String {
let binary_str = format!("{:064b}", to_format);
format!(
"{} {} {}",
&binary_str[0..1], // Sign bit
&binary_str[1..9], // Exponent
&binary_str[9..] // Significand
)
}

pub fn format_hex(to_format: u64) -> String {
format!("0x{:X}", to_format)
}

/// Converts a string representation of a floating-point number to its binary
/// format and appends the result to the specified file.
///
/// This function takes a string slice representing a floating-point number,
/// converts it to a 64-bit floating-point number (`f64`), then converts this
/// number to its binary representation. The binary representation is formatted
/// as a string and written to the specified file, followed by a newline.
///
/// # Arguments
///
/// * `float_string` - A string slice containing the floating-point number to be converted.
/// * `filepath_send` - A mutable reference to a `File` where the binary representation
/// will be appended.
///
/// # Returns
///
/// This function returns a `std::io::Result<()>` which is `Ok` if the operation
/// is successful, or an `Err` if an I/O error occurs while writing to the file.
///
/// # Panics
///
/// This function will panic if the input string cannot be parsed as a floating-point number.
pub fn float_to_binary(
float_string: &str,
filepath_send: &mut Option<File>,
) -> std::io::Result<()> {
let float_of_string: f64;
// Convert string to float
match float_string.parse::<f64>() {
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);

if let Some(file) = filepath_send.as_mut() {
file.write_all(formatted_binary_str.as_bytes())?;
file.write_all(b"\n")?;
} else {
stdout().write_all(formatted_binary_str.as_bytes())?;
stdout().write_all(b"\n")?;
}

Ok(())
}

/// Converts a string representation of a hexadecimal number to its binary
/// format and appends the result to the specified file.
///
/// This function takes a string slice representing a hexadecimal number,
/// converts it to a 64-bit integer (`u64`), then converts this number to its
/// binary representation. The binary representation is formatted as a string
/// and written to the specified file, followed by a newline.
///
/// # Arguments
///
/// * `hex_string` - A string slice containing the hexadecimal number to be converted.
/// * `filepath_send` - A mutable reference to a `File` where the binary representation
/// will be appended.
///
/// # Returns
///
/// This function returns a `std::io::Result<()>` which is `Ok` if the operation
/// is successful, or an `Err` if an I/O error occurs while writing to the file.
///
/// # Error
///
/// This function will panic if the input string cannot be parsed as a hexadecimal number.
///
/// This does not differentiate between floating and fixed. It just treats any hex as an integer.
pub fn hex_to_binary(
hex_string: &str,
filepath_send: &mut Option<File>,
) -> io::Result<()> {
// Convert hex to binary
let binary_of_hex = u64::from_str_radix(hex_string, 16)
.expect("Failed to parse hex string");

// Format nicely
let formatted_binary_str = format!("{:b}", binary_of_hex);

// Write binary string to the file

if let Some(file) = filepath_send.as_mut() {
// Write binary string to the file
file.write_all(formatted_binary_str.as_bytes())?;
file.write_all(b"\n")?;
} else {
stdout().write_all(formatted_binary_str.as_bytes())?;
stdout().write_all(b"\n")?;
}

Ok(())
}

/// Converts a string representation of a binary number to its hexadecimal
/// format and appends the result to the specified file.
///
/// This function takes a string slice representing a binary number,
/// converts it to a 64-bit integer (`u64`), then converts this number to its
/// hexadecimal representation. The hexadecimal representation is formatted
/// as a string and written to the specified file, followed by a newline.
///
/// # Arguments
///
/// * `binary_string` - A string slice containing the binary number to be converted.
/// * `filepath_send` - A mutable reference to a `File` where the hexadecimal representation
/// will be appended.
///
/// # Returns
///
/// This function returns a `std::io::Result<()>` which is `Ok` if the operation
/// is successful, or an `Err` if an I/O error occurs while writing to the file.
///
/// # Panics
///
/// This function will panic if the input string cannot be parsed as a binary number.
pub fn binary_to_hex(
binary_string: &str,
filepath_send: &mut Option<File>,
) -> io::Result<()> {
let hex_of_binary = u64::from_str_radix(binary_string, 2)
.expect("Failed to parse binary string");

let formatted_hex_str = format_hex(hex_of_binary);

if let Some(file) = filepath_send.as_mut() {
// Write binary string to the file
file.write_all(formatted_hex_str.as_bytes())?;
file.write_all(b"\n")?;
} else {
stdout().write_all(formatted_hex_str.as_bytes())?;
stdout().write_all(b"\n")?;
}

Ok(())
}

/// Converts a string representation of a binary number to its floating-point
/// format and appends the result to the specified file.
///
/// This function takes a string slice representing a binary number,
/// converts it to a 64-bit integer (`u64`), then interprets this integer as
/// the binary representation of a 64-bit floating-point number (`f64`).
/// The floating-point representation is formatted as a string and written
/// to the specified file, followed by a newline.
///
/// # Arguments
///
/// * `binary_string` - A string slice containing the binary number to be converted.
/// * `filepath_send` - A mutable reference to a `File` where the floating-point representation
/// will be appended.
///
/// # Returns
///
/// This function returns a `std::io::Result<()>` which is `Ok` if the operation
/// is successful, or an `Err` if an I/O error occurs while writing to the file.
///
/// # Panics
///
/// This function will panic if the input string cannot be parsed as a binary number.
pub fn binary_to_float(
binary_string: &str,
filepath_send: &mut Option<File>,
) -> io::Result<()> {
let binary_value = u64::from_str_radix(binary_string, 2)
.expect("Failed to parse binary string");

let formated_float_str = format!("{:?}", binary_value);

if let Some(file) = filepath_send.as_mut() {
// Write binary string to the file
file.write_all(formated_float_str.as_bytes())?;
file.write_all(b"\n")?;
} else {
stdout().write_all(formated_float_str.as_bytes())?;
stdout().write_all(b"\n")?;
}

Ok(())
}

/// Converts a string representation of a fixed-point number to its binary
/// format and appends the result to the specified file.
///
/// This function takes a string slice representing a fixed-point number,
/// multiplies it by 2 raised to the power of the negative exponent, converts the result
/// to a 64-bit integer, and then to its binary representation. The binary representation
/// is formatted as a string and written to the specified file, followed by a newline.
///
/// # Arguments
///
/// * `fixed_string` - A string slice containing the fixed-point number to be converted.
/// * `filepath_send` - A mutable reference to a `File` where the binary representation
/// will be appended.
/// * `exponent` - A floating-point number representing the exponent to be applied in the
/// conversion process.
///
/// # Returns
///
/// This function returns a `std::io::Result<()>` which is `Ok` if the operation
/// is successful, or an `Err` if an I/O error occurs while writing to the file.
///
/// # Panics
///
/// This function will panic if the input string cannot be parsed as a fixed-point number.
pub fn fixed_to_binary(

Check failure on line 230 in tools/data-conversion/src/fast_track.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

function `fixed_to_binary` is never used

Check failure on line 230 in tools/data-conversion/src/fast_track.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

function `fixed_to_binary` is never used
fixed_string: &str,
filepath_send: &mut Option<File>,
exp_int: i64,
) -> io::Result<()> {
// Convert fixed value from string to int
let fixed_value: f64;
match fixed_string.parse::<f64>() {
Ok(parsed_num) => fixed_value = parsed_num,
Err(_) => {
panic!("Bad fixed value input")
}
}

//exponent int to float so we can multiply
let exponent = exp_int as f64;

// Exponent math
let multiplied_fixed = fixed_value * 2_f64.powf(-exponent);

// Convert to a 64-bit integer
let multiplied_fixed_as_i64 = multiplied_fixed as i64;

// Convert to a binary string with 64 bits
let binary_of_fixed = format!("{:064b}", multiplied_fixed_as_i64);

if let Some(file) = filepath_send.as_mut() {
// Write binary string to the file
file.write_all(binary_of_fixed.as_bytes())?;
file.write_all(b"\n")?;
} else {
stdout().write_all(binary_of_fixed.as_bytes())?;
stdout().write_all(b"\n")?;
}

Ok(())
}

/// Converts a string representation of a binary number to its fixed-point
/// format and appends the result to the specified file.
///
/// This function takes a string slice representing a binary number,
/// converts it to a 64-bit unsigned integer, interprets this integer as
/// a floating-point number, divides it by 2 raised to the power of the negative exponent,
/// and converts the result to its fixed-point representation. The fixed-point
/// representation is formatted as a string and written to the specified file,
/// followed by a newline.
///
/// # Arguments
///
/// * `binary_string` - A string slice containing the binary number to be converted.
/// * `filepath_send` - A mutable reference to a `File` where the fixed-point representation
/// will be appended.
/// * `exponent` - A floating-point number representing the exponent to be applied in the
/// conversion process.
///
/// # Returns
///
/// This function returns a `std::io::Result<()>` which is `Ok` if the operation
/// is successful, or an `Err` if an I/O error occurs while writing to the file.
///
/// # Panics
///
/// This function will panic if the input string cannot be parsed as a binary number.
pub fn binary_to_fixed(

Check failure on line 294 in tools/data-conversion/src/fast_track.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

function `binary_to_fixed` is never used

Check failure on line 294 in tools/data-conversion/src/fast_track.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

function `binary_to_fixed` is never used
binary_string: &str,
filepath_send: &mut Option<File>,
exp_int: i64,
) -> io::Result<()> {
// Convert binary value from string to int
let binary_value = match u64::from_str_radix(binary_string, 2) {
Ok(parsed_num) => parsed_num,
Err(_) => panic!("Bad binary value input"),
};

// Convert to fixed
let int_of_binary = binary_value as f64;

//exponent int to float so we can multiply
let exponent = exp_int as f64;

// Exponent math
let divided: f64 = int_of_binary / 2_f64.powf(-exponent);

let string_of_divided = format!("{:+.8e}", divided);

if let Some(file) = filepath_send.as_mut() {
// Write binary string to the file
file.write_all(string_of_divided.as_bytes())?;
file.write_all(b"\n")?;
} else {
stdout().write_all(string_of_divided.as_bytes())?;
stdout().write_all(b"\n")?;
}

Ok(())
}

pub fn binary_to_fixed_bit_slice(

Check failure on line 328 in tools/data-conversion/src/fast_track.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

function `binary_to_fixed_bit_slice` is never used

Check failure on line 328 in tools/data-conversion/src/fast_track.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

function `binary_to_fixed_bit_slice` is never used
binary_string: &str,
filepath_send: &mut Option<File>,
exp_int: i64,
) -> io::Result<()> {
// Convert binary string to an integer (assuming binary_string is a valid binary representation)
let binary_int = u64::from_str_radix(binary_string, 2).unwrap();

// Adjust the binary point based on the exponent
let mut result = binary_int;
if exp_int < 0 {
// If exponent is negative, shift right (multiply by 2^(-exp_int))
result >>= -exp_int as u64;
} else {
// If exponent is positive, shift left (multiply by 2^(exp_int))
result <<= exp_int as u64;
}

// Convert result to a fixed-point decimal representation
let fixed_value = result as f64;

let string_of_fixed = format!("{:.8e}", fixed_value);

if let Some(file) = filepath_send.as_mut() {
// Write binary string to the file
file.write_all(string_of_fixed.as_bytes())?;
file.write_all(b"\n")?;
} else {
stdout().write_all(string_of_fixed.as_bytes())?;
stdout().write_all(b"\n")?;
}

Ok(())
}
Loading
Loading