Skip to content

Commit 66c70d7

Browse files
authored
Merge pull request #1894 from certik/u16_ptr
Fix unsigned integers in pointers
2 parents 8340b79 + 77aa0d0 commit 66c70d7

File tree

3 files changed

+71
-10
lines changed

3 files changed

+71
-10
lines changed

integration_tests/bindc_07.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
1-
from lpython import CPtr, i64, sizeof, i32, i16, i8, ccall, c_p_pointer, empty_c_void_p, Pointer, pointer
2-
from numpy import empty, int64, array
1+
from lpython import (CPtr, sizeof, ccall, c_p_pointer, empty_c_void_p,
2+
i64, i32, i16, i8,
3+
u64, u32, u16, u8,
4+
Pointer, pointer, u16)
5+
from numpy import array
36

47
@ccall
58
def _lfortran_malloc(size: i32) -> CPtr:
69
pass
710

8-
def allocate_memory(size: i32) -> tuple[CPtr, CPtr, CPtr, CPtr]:
11+
def allocate_memory(size: i32) -> tuple[CPtr, CPtr, CPtr, CPtr, \
12+
CPtr, CPtr, CPtr, CPtr]:
913
array1: CPtr = _lfortran_malloc(size * i32(sizeof(i8)))
1014
array2: CPtr = _lfortran_malloc(size * i32(sizeof(i16)))
1115
array3: CPtr = _lfortran_malloc(size * i32(sizeof(i32)))
1216
array4: CPtr = _lfortran_malloc(size * i32(sizeof(i64)))
13-
return array1, array2, array3, array4
17+
array5: CPtr = _lfortran_malloc(size * i32(sizeof(u8)))
18+
array6: CPtr = _lfortran_malloc(size * i32(sizeof(u16)))
19+
array7: CPtr = _lfortran_malloc(size * i32(sizeof(u32)))
20+
array8: CPtr = _lfortran_malloc(size * i32(sizeof(u64)))
21+
return array1, array2, array3, array4, array5, array6, array7, array8
1422

15-
def sum_arrays(array1: CPtr, array2: CPtr, array3: CPtr, array4: CPtr, size: i32):
23+
def sum_arrays(array1: CPtr, array2: CPtr, array3: CPtr, array4: CPtr, \
24+
array5: CPtr, array6: CPtr, array7: CPtr, array8: CPtr, size: i32):
1625
iarray1: Pointer[i8[:]] = c_p_pointer(array1, i8[:], array([size]))
1726
iarray2: Pointer[i16[:]] = c_p_pointer(array2, i16[:], array([size]))
1827
iarray3: Pointer[i32[:]] = c_p_pointer(array3, i32[:], array([size]))
1928
iarray4: Pointer[i64[:]] = c_p_pointer(array4, i64[:], array([size]))
29+
iarray5: Pointer[u8[:]] = c_p_pointer(array5, u8[:], array([size]))
30+
iarray6: Pointer[u16[:]] = c_p_pointer(array6, u16[:], array([size]))
31+
iarray7: Pointer[u32[:]] = c_p_pointer(array7, u32[:], array([size]))
32+
iarray8: Pointer[u64[:]] = c_p_pointer(array8, u64[:], array([size]))
2033
sum_array_cptr: CPtr = _lfortran_malloc(size * i32(sizeof(i64)))
2134
sum_array: Pointer[i64[:]] = c_p_pointer(sum_array_cptr, i64[:], array([size]))
2235
i: i32
@@ -26,20 +39,30 @@ def sum_arrays(array1: CPtr, array2: CPtr, array3: CPtr, array4: CPtr, size: i32
2639
iarray2[i] = i16(2 * i)
2740
iarray3[i] = i32(3 * i)
2841
iarray4[i] = i64(4 * i)
42+
iarray5[i] = u8(i)
43+
iarray6[i] = u16(6 * i)
44+
iarray7[i] = u32(7 * i)
45+
iarray8[i] = u64(8 * i)
2946

3047
for i in range(size):
31-
sum_array[i] = i64(iarray1[i]) + i64(iarray2[i]) + i64(iarray3[i]) + iarray4[i]
48+
sum_array[i] = i64(iarray1[i]) + i64(iarray2[i]) + i64(iarray3[i]) \
49+
+ iarray4[i] + i64(iarray5[i]) + i64(iarray6[i]) \
50+
+ i64(iarray7[i]) + i64(iarray8[i])
3251

3352
for i in range(size):
3453
print(i, sum_array[i])
35-
assert sum_array[i] == i64(10 * i)
54+
assert sum_array[i] == i64(32 * i)
3655

3756
def test_tuple_return():
3857
a: CPtr = empty_c_void_p()
3958
b: CPtr = empty_c_void_p()
4059
c: CPtr = empty_c_void_p()
4160
d: CPtr = empty_c_void_p()
42-
a, b, c, d = allocate_memory(50)
43-
sum_arrays(a, b, c, d, 50)
61+
e: CPtr = empty_c_void_p()
62+
f: CPtr = empty_c_void_p()
63+
g: CPtr = empty_c_void_p()
64+
h: CPtr = empty_c_void_p()
65+
a, b, c, d, e, f, g, h = allocate_memory(50)
66+
sum_arrays(a, b, c, d, e, f, g, h, 50)
4467

4568
test_tuple_return()

src/libasr/codegen/asr_to_c.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,27 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
250250
std::string dims = convert_dims_c(n_dims, m_dims, v_m_type, is_fixed_size);
251251
sub = format_type_c(dims, type_name, v.m_name, use_ref, dummy);
252252
}
253+
} else if (ASRUtils::is_unsigned_integer(*t2)) {
254+
ASR::UnsignedInteger_t *t = ASR::down_cast<ASR::UnsignedInteger_t>(ASRUtils::type_get_past_array(t2));
255+
std::string type_name = "uint" + std::to_string(t->m_kind * 8) + "_t";
256+
if( !ASRUtils::is_array(v_m_type) ) {
257+
type_name.append(" *");
258+
}
259+
if( is_array ) {
260+
bool is_fixed_size = true;
261+
std::string dims = convert_dims_c(n_dims, m_dims, v_m_type, is_fixed_size, true);
262+
std::string encoded_type_name = "u" + std::to_string(t->m_kind * 8);
263+
generate_array_decl(sub, std::string(v.m_name), type_name, dims,
264+
encoded_type_name, m_dims, n_dims,
265+
use_ref, dummy,
266+
v.m_intent != ASRUtils::intent_in &&
267+
v.m_intent != ASRUtils::intent_inout &&
268+
v.m_intent != ASRUtils::intent_out, is_fixed_size, true);
269+
} else {
270+
bool is_fixed_size = true;
271+
std::string dims = convert_dims_c(n_dims, m_dims, v_m_type, is_fixed_size);
272+
sub = format_type_c(dims, type_name, v.m_name, use_ref, dummy);
273+
}
253274
} else if (ASRUtils::is_real(*t2)) {
254275
ASR::Real_t *t = ASR::down_cast<ASR::Real_t>(t2);
255276
std::string type_name;

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
444444
el_type = getIntType(a_kind, true);
445445
break;
446446
}
447+
case ASR::ttypeType::UnsignedInteger: {
448+
el_type = getIntType(a_kind, true);
449+
break;
450+
}
447451
case ASR::ttypeType::Real: {
448452
el_type = getFPType(a_kind, true);
449453
break;
@@ -6411,6 +6415,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
64116415
ASRUtils::type_get_past_pointer(x->m_type));
64126416
switch (t2->type) {
64136417
case ASR::ttypeType::Integer:
6418+
case ASR::ttypeType::UnsignedInteger:
64146419
case ASR::ttypeType::Real:
64156420
case ASR::ttypeType::Complex:
64166421
case ASR::ttypeType::Struct:
@@ -6759,6 +6764,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
67596764
case (ASR::cast_kindType::IntegerToUnsignedInteger) : {
67606765
int arg_kind = -1, dest_kind = -1;
67616766
extract_kinds(x, arg_kind, dest_kind);
6767+
LCOMPILERS_ASSERT(arg_kind != -1 && dest_kind != -1)
67626768
if( arg_kind > 0 && dest_kind > 0 &&
67636769
arg_kind != dest_kind )
67646770
{
@@ -6771,7 +6777,18 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
67716777
break;
67726778
}
67736779
case (ASR::cast_kindType::UnsignedIntegerToInteger) : {
6774-
// tmp = tmp
6780+
int arg_kind = -1, dest_kind = -1;
6781+
extract_kinds(x, arg_kind, dest_kind);
6782+
LCOMPILERS_ASSERT(arg_kind != -1 && dest_kind != -1)
6783+
if( arg_kind > 0 && dest_kind > 0 &&
6784+
arg_kind != dest_kind )
6785+
{
6786+
if (dest_kind > arg_kind) {
6787+
tmp = builder->CreateSExt(tmp, getIntType(dest_kind));
6788+
} else {
6789+
tmp = builder->CreateTrunc(tmp, getIntType(dest_kind));
6790+
}
6791+
}
67756792
break;
67766793
}
67776794
case (ASR::cast_kindType::CPtrToUnsignedInteger) : {

0 commit comments

Comments
 (0)