diff --git a/ncc/src/codegen.rs b/ncc/src/codegen.rs index 3d6f13b..b8205f5 100644 --- a/ncc/src/codegen.rs +++ b/ncc/src/codegen.rs @@ -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 { diff --git a/ncc/src/types.rs b/ncc/src/types.rs index 1095c82..f301b92 100644 --- a/ncc/src/types.rs +++ b/ncc/src/types.rs @@ -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 diff --git a/ncc/tests/floats.c b/ncc/tests/floats.c index 6648c98..2483447 100644 --- a/ncc/tests/floats.c +++ b/ncc/tests/floats.c @@ -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);