Skip to content

Commit

Permalink
Expand support for int/float type casting
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Oct 1, 2023
1 parent f3f2dac commit cdbeb79
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion ncc/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,16 @@ impl Expr
}
}

// Int to float
// float f = (float)int_val;
(Float(32), Int(32)) => {
(Float(32), Int(n)) if *n <= 32 => {
if *n < 32 {
out.push_str(&format!("sx_i{}_i32;\n", n));
}
out.push_str("i32_to_f32;\n");
}

// Float to int
(Int(m), Float(32)) if *m <= 32 => {
out.push_str("f32_to_i32;\n");
if *m < 32 {
Expand Down
2 changes: 1 addition & 1 deletion ncc/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl Expr
(Int(m), Int(n)) => {},

// Int/float casts
(Float(32), Int(32)) => {},
(Float(32), Int(n)) if *n <= 32 => {},
(Int(m), Float(32)) if *m <= 32 => {},

// Pointer casts
Expand Down
1 change: 1 addition & 0 deletions ncc/tests/floats.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ int main()
assert((short)5000.0f == 5000);
// FIXME:
//assert((short)-5000.0f == (short)-5000);
assert((float)(short)5000 == 5000.0f);

// Global variable access
assert(g == 3.5f);
Expand Down

0 comments on commit cdbeb79

Please sign in to comment.