-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
255 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//! Implement parsing and generation for floating point constants used by std_float_const. | ||
use crate::{CalyxResult, Error}; | ||
|
||
pub fn parse(rep: u64, width: u64, fl: String) -> CalyxResult<u64> { | ||
if rep != 0 { | ||
return Err(Error::misc(format!( | ||
"Unknown representation: {rep}. Support representations: 0 (IEEE754)" | ||
))); | ||
} | ||
|
||
let bits: u64 = match width { | ||
32 => { | ||
let fl = fl.parse::<f32>().map_err(|e| { | ||
Error::misc(format!( | ||
"Expected valid floating point number: {e}" | ||
)) | ||
})?; | ||
fl.to_bits() as u64 | ||
} | ||
64 => { | ||
let fl = fl.parse::<f64>().map_err(|e| { | ||
Error::misc(format!( | ||
"Expected valid floating point number: {e}" | ||
)) | ||
})?; | ||
fl.to_bits() | ||
} | ||
r => { | ||
return Err(Error::misc(format!( | ||
"Unsupported floating point width: {r}. Supported values: 32, 64" | ||
))) | ||
} | ||
}; | ||
|
||
Ok(bits) | ||
} | ||
|
||
pub fn emit(bits: u64, width: u64) -> CalyxResult<String> { | ||
match width { | ||
32 => { | ||
let fl = f32::from_bits(bits as u32); | ||
Ok(format!("{}", fl)) | ||
} | ||
64 => { | ||
let fl = f64::from_bits(bits); | ||
Ok(format!("{}", fl)) | ||
} | ||
r => Err(Error::misc(format!( | ||
"Unsupported floating point width: {r}. Supported values: 32, 64" | ||
))), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** A floating-point constant with a specific representation. | ||
* NOTE: Floating point constants have special parsing and generation rules in | ||
* the frontend and the backend. If you make changes here, you will likely | ||
* need to change calyx_utils::float as well. | ||
* | ||
* PARAMETERS | ||
* - REP: The representation of the floating-point number. | ||
* 0: IEEE 754 | ||
* _: Reserved for future use. | ||
* - WIDTH=32, 64 are supported. Other values result in an error. | ||
* | ||
* PARSING | ||
* The value is converted into the bitpattern of the floating-point representation. | ||
* If the number cannot be represented exactly with the given bitwidth, we will | ||
* emit a warning. | ||
*/ | ||
comb primitive std_float_const<"share"=1>[REP, WIDTH, VALUE]() -> (out: WIDTH) { | ||
assign out = VALUE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
---CODE--- | ||
1 | ||
---STDERR--- | ||
Error: tests/errors/float/invalid-rep.futil | ||
3 | f = std_float_const(1, 32, 0.5); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Parse error | ||
Unknown representation: 1. Support representations: 0 (IEEE754) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
component main() -> () { | ||
cells { | ||
f = std_float_const(1, 32, 0.5); | ||
} | ||
wires {} | ||
control {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
---CODE--- | ||
1 | ||
---STDERR--- | ||
Error: tests/errors/float/invalid-width.futil | ||
3 | f = std_float_const(0, 16, 0.5); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Parse error | ||
Unsupported floating point width: 16. Supported values: 32, 64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
component main() -> () { | ||
cells { | ||
f = std_float_const(0, 16, 0.5); | ||
} | ||
wires {} | ||
control {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import "primitives/float.futil"; | ||
import "primitives/core.futil"; | ||
component main(@go go: 1, @clk clk: 1, @reset reset: 1) -> (@done done: 1) { | ||
cells { | ||
f = std_float_const(0, 32, 0.5); | ||
add = std_add(32); | ||
r = std_reg(32); | ||
} | ||
wires { | ||
static<1> group add_one { | ||
add.left = f.out; | ||
add.right = 32'd1; | ||
r.in = add.out; | ||
r.write_en = 1'd1; | ||
} | ||
} | ||
control { | ||
add_one; | ||
} | ||
} |