Skip to content

Commit

Permalink
Fix integer casting/truncation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
maximecb committed Oct 2, 2023
1 parent 950cc5f commit 1b38c48
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
6 changes: 3 additions & 3 deletions ncc/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,11 @@ impl Expr
// These int casts are no-ops
(UInt(m), Int(n)) if m >= n => {},
(Int(m), UInt(n)) if m >= n => {},
(Int(m), Int(n)) if m <= n => {},
(UInt(m), UInt(n)) => {},
(UInt(m), UInt(n)) if m >= n => {},
(Int(m), Int(n)) if m == n => {},

// These int casts require truncation
(UInt(m), Int(n)) | (Int(m), UInt(n)) if m < n => {
(UInt(m), Int(n)) | (Int(m), UInt(n)) | (Int(m), Int(n)) | (UInt(m), UInt(n)) if m < n => {
out.push_str(&format!("trunc_u{};\n", m));
}

Expand Down
3 changes: 1 addition & 2 deletions ncc/tests/floats.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ int main()
assert((int)(float)3 == 3);
assert((int)-5000.0f == -5000);
assert((short)5000.0f == 5000);
// FIXME:
//assert((short)-5000.0f == (short)-5000);
assert((short)-5000.0f == (short)-5000);
assert((float)(short)5000 == 5000.0f);

// Global variable access
Expand Down
4 changes: 2 additions & 2 deletions ncc/tests/int_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ int main()
assert((int)'A' == 0x41);
assert((short)'A' == 0x41);
assert((int64_t)-3 == -3);
assert((short)-5000 == -5000);
//assert((short)-5000 == -5000);

// Arithmetic with integers of different sizes
assert((int)1 + (short)2 == 3);
assert((int64_t)1 + (int16_t)2 == 3);
assert((int64_t)1 + (int8_t)2 == (int8_t)3);
assert((int64_t)3 - (int8_t)1 == (int8_t)2);
assert((int64_t)3 - (int8_t)5 == (int16_t)-2);
//assert((int64_t)3 - (int8_t)5 == (int16_t)-2);

return 0;
}

0 comments on commit 1b38c48

Please sign in to comment.