forked from viperproject/prusti-dev
-
Notifications
You must be signed in to change notification settings - Fork 9
Add support for IntToFloat and FloatToInt casts #190
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
Open
ThomasMayerl
wants to merge
7
commits into
Aurel300:rewrite-2023
Choose a base branch
from
ThomasMayerl:float_to_int
base: rewrite-2023
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
958cf39
Add support for int to float casts
ThomasMayerl 5f9b68c
add suport for float to int casts
ThomasMayerl 601f1c4
implement PR feedback
ThomasMayerl 393cc64
implement PR feedback
ThomasMayerl a4de0ea
implement PR feedback
ThomasMayerl e16f4fb
move tests to fail
ThomasMayerl ab33848
implement PR feedback
ThomasMayerl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
72 changes: 72 additions & 0 deletions
72
prusti-encoder/src/encoders/ty/interpretation/int_real_cast.rs
This file contains hidden or 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,72 @@ | ||
| use task_encoder::TaskEncoder; | ||
| use vir::{DomainGenData, FunctionIdn, ViperIdent}; | ||
|
|
||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct IntRealCastDomain<'vir> { | ||
| pub from_int: FunctionIdn<'vir, vir::Int, vir::Perm>, | ||
| pub to_int: FunctionIdn<'vir, vir::Perm, vir::Int>, | ||
| } | ||
|
|
||
| pub struct IntRealCastEnc; | ||
|
|
||
| impl TaskEncoder for IntRealCastEnc { | ||
| task_encoder::encoder_cache!(IntRealCastEnc); | ||
| const ENCODER_NAME: &'static str = "int_real_cast encoder"; | ||
|
|
||
| type TaskDescription<'vir> = (); | ||
|
|
||
| type OutputFullLocal<'vir> = &'vir DomainGenData<'vir, (), !>; | ||
|
|
||
| type OutputFullDependency<'vir> = IntRealCastDomain<'vir>; | ||
|
|
||
| type EncodingError = (); | ||
|
|
||
| fn task_to_key<'vir>(task: &Self::TaskDescription<'vir>) -> Self::TaskKey<'vir> { | ||
| *task | ||
| } | ||
|
|
||
| fn do_encode_full<'vir>( | ||
| task_key: &Self::TaskKey<'vir>, | ||
| deps: &mut task_encoder::TaskEncoderDependencies<'vir, Self>, | ||
| ) -> task_encoder::EncodeFullResult<'vir, Self> { | ||
| vir::with_vcx(|vcx| { | ||
| deps.emit_output_ref(*task_key, ())?; | ||
|
|
||
| let domain_name = "s_IntRealCast"; | ||
|
|
||
| let domain_ident = vir::ViperIdent::new(domain_name); | ||
| let from_int_name = vir::vir_format!(vcx, "{domain_name}_from_int"); | ||
|
|
||
| let from_int = FunctionIdn::new( | ||
| ViperIdent::new(from_int_name), | ||
| vir::TYPE_INT, | ||
| vir::TYPE_PERM, | ||
| ); | ||
|
|
||
| let from_int_data = vcx.mk_domain_function(from_int, false, Some("to_real")); | ||
|
|
||
| let to_int_name = vir::vir_format!(vcx, "{domain_name}_to_int"); | ||
|
|
||
| let to_int = | ||
| FunctionIdn::new(ViperIdent::new(to_int_name), vir::TYPE_PERM, vir::TYPE_INT); | ||
|
|
||
| let to_int_data = vcx.mk_domain_function(to_int, false, Some("to_int")); | ||
|
|
||
| let domain_data = vcx.mk_domain::<(), !>( | ||
| domain_ident, | ||
| &[], | ||
| &[], | ||
| vcx.alloc_slice(&[from_int_data, to_int_data]), | ||
| None, | ||
| ); | ||
|
|
||
| Ok((domain_data, IntRealCastDomain { from_int, to_int })) | ||
| }) | ||
| } | ||
|
|
||
| fn emit_outputs<'vir>(program: &mut task_encoder::Program<'vir>) { | ||
| for output in Self::all_outputs_local_no_errors(program) { | ||
| program.add_domain(output); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| pub mod bitvec; | ||
| pub mod float; | ||
| pub mod int_real_cast; |
This file contains hidden or 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 hidden or 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 hidden or 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,23 @@ | ||
| // The result of these casts can currently not be verified using Z3. | ||
| // Once Z3 supports these cases, replace pass/casts/int_to_float.rs with this file. | ||
|
|
||
| fn main() { | ||
| let x = 100; | ||
| let y = x as f32; | ||
| assert!(y == 100.0); //~ERROR: the asserted expression might not hold | ||
|
|
||
| let x = u128::MAX; | ||
| let y = x as f32; | ||
| assert!(y == f32::INFINITY); //~ERROR: the asserted expression might not hold | ||
|
|
||
| let x = u8::MAX; | ||
| let y = x as f64; | ||
| assert!(y == 255.0); //~ERROR: the asserted expression might not hold | ||
|
|
||
| let x = -5; | ||
| let y = x as f32; | ||
| assert!(y == -5.0); //~ERROR: the asserted expression might not hold | ||
|
|
||
| let x = 9007199254740993i64; | ||
| assert!(x as f32 == 9007199254740992.0); //~ERROR: the asserted expression might not hold | ||
| } |
This file contains hidden or 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,41 @@ | ||
| fn main() { | ||
| let x = 100.0; | ||
| let y = x as i32; | ||
| assert!(y == 100); | ||
|
|
||
| let x2 = 36.9; | ||
| let y2 = x2 as u32; | ||
| assert!(y2 == 36); | ||
|
|
||
| let x3 = -36.9; | ||
| let y3 = x3 as u32; | ||
| assert!(y3 == 0); | ||
|
|
||
| let x4 = -36.9; | ||
| let y4 = x4 as i32; | ||
| assert!(y4 == -36); | ||
|
|
||
| let x5 = 1000.5; | ||
| let y5 = x5 as u8; | ||
| assert!(y5 == 255); | ||
|
|
||
| let x6 = -1000.5; | ||
| let y6 = x6 as i8; | ||
| assert!(y6 == -128); | ||
|
|
||
| let x7 = -36.3; | ||
| let y7 = x7 as i32; | ||
| assert!(y7 == -36); | ||
|
|
||
| let x8 = f64::INFINITY; | ||
| let y8 = x8 as i32; | ||
| assert!(y8 == i32::MAX); | ||
|
|
||
| let x9 = f64::NEG_INFINITY; | ||
| let y9 = x9 as i32; | ||
| assert!(y9 == i32::MIN); | ||
|
|
||
| let x10 = f64::NAN; | ||
| let y10 = x10 as i32; | ||
| assert!(y10 == 0); | ||
| } |
This file contains hidden or 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,15 @@ | ||
| fn main() { | ||
| let x = 100; | ||
| let y = x as f32; | ||
|
|
||
| let x = u128::MAX; | ||
| let y = x as f32; | ||
|
|
||
| let x = u8::MAX; | ||
| let y = x as f64; | ||
|
|
||
| let x = -5; | ||
| let y = x as f32; | ||
|
|
||
| let x = 9007199254740993i64 as f32; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I imagine this is going to clash with #188.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because Reals are just Perms. This encoder merely adds functions to be used with them.