Skip to content

Commit

Permalink
Merge pull request #2178 from Smit-create/i-2174
Browse files Browse the repository at this point in the history
Fix unsigned to signed int cast
  • Loading branch information
certik authored Jul 18, 2023
2 parents 3d61ec0 + 6dc605a commit 31d3a29
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ RUN(NAME test_unary_op_05 LABELS cpython llvm c) # unsigned unary minus, plus
RUN(NAME test_unary_op_06 LABELS cpython llvm c) # unsigned unary bitnot
RUN(NAME test_unsigned_01 LABELS cpython llvm c) # unsigned bitshift left, right
RUN(NAME test_unsigned_02 LABELS cpython llvm c)
RUN(NAME test_unsigned_03 LABELS cpython llvm c)
RUN(NAME test_bool_binop LABELS cpython llvm c)
RUN(NAME test_issue_518 LABELS cpython llvm c NOFAST)
RUN(NAME structs_01 LABELS cpython llvm c)
Expand Down
17 changes: 11 additions & 6 deletions integration_tests/test_unsigned_02.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from lpython import u16, i32
from lpython import u16, i32, u8, u32, u64

# test issue 2170

i : i32
u : u16 = u16(32768)
x : i32
u_1 : u16 = u16(32768)
u_2 : u8 = u8(24)
u_3 : u32 = u32(32768)
u_4 : u64 = u64(32768)

for i in range(i32(u)):
x = i * 2
assert u_1 == u16(32768)
assert u_2 == u8(24)
assert u_3 == u32(32768)
assert u_4 == u64(32768)

print(u_1, u_2, u_3, u_4)
19 changes: 19 additions & 0 deletions integration_tests/test_unsigned_03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from lpython import u16, i32, u8, u16, u64, i64, u32, i8

# test issue 2174

def f():
u: u16 = u16(32768)
assert i32(u) == 32768
u1: u8 = u8(23)
assert i8(u1) == i8(23)
assert u16(u1) == u16(23)
assert u32(u1) == u32(23)
assert u64(u1) == u64(23)
print(i8(u1), u16(u1), u32(u1), u64(u1))
assert i64(u1) == i64(23)
assert i64(u) == i64(32768)
assert i32(u1) == 23
print(i64(u), i32(u))

f()
2 changes: 1 addition & 1 deletion src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6329,7 +6329,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
arg_kind != dest_kind )
{
if (dest_kind > arg_kind) {
tmp = builder->CreateSExt(tmp, llvm_utils->getIntType(dest_kind));
tmp = builder->CreateZExt(tmp, llvm_utils->getIntType(dest_kind));
} else {
tmp = builder->CreateTrunc(tmp, llvm_utils->getIntType(dest_kind));
}
Expand Down

0 comments on commit 31d3a29

Please sign in to comment.