Skip to content

Commit a771d87

Browse files
authored
Merge pull request #1871 from Smit-create/i-1869
Overload missing functions with unsigned int
2 parents cbda4d8 + abfd261 commit a771d87

File tree

67 files changed

+1753
-1500
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1753
-1500
lines changed

integration_tests/expr_05.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from lpython import i32, i64, f64
1+
from lpython import i32, i64, f64, u16
22

33
def test_multiply(a: i32, b: i32) -> i32:
44
return a*b
@@ -78,5 +78,12 @@ def main0():
7878
assert abs((11.0%-3.0) - (-1.0)) < eps
7979
assert abs((-11.0%3.0) - (1.0)) < eps
8080

81+
# Test issue 1869 and 1870
82+
a1: u16 = u16(10)
83+
b1: u16 = u16(3)
84+
c1: u16 = a1 % b1
85+
assert c1 == u16(1)
86+
c1 = a1 // b1
87+
assert c1 == u16(3)
8188

8289
main0()

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,12 @@ R"(#include <stdio.h>
14911491
last_expr_precedence = 2;
14921492
break;
14931493
}
1494+
case (ASR::cast_kindType::UnsignedIntegerToInteger) : {
1495+
int dest_kind = ASRUtils::extract_kind_from_ttype_t(x.m_type);
1496+
src = "(int" + std::to_string(dest_kind * 8) + "_t)(" + src + ")";
1497+
last_expr_precedence = 2;
1498+
break;
1499+
}
14941500
case (ASR::cast_kindType::ComplexToComplex) : {
14951501
break;
14961502
}

src/runtime/lpython_builtin.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from lpython import i8, i16, i32, i64, f32, f64, c32, c64, overload
1+
from lpython import (i8, i16, i32, i64, f32, f64, c32, c64, overload, u8,
2+
u16, u32, u64)
23
#from sys import exit
34

45
#: abs() as a generic procedure.
@@ -485,6 +486,10 @@ def _lpython_floordiv(a: i8, b: i8) -> i8:
485486
return result
486487
return result - i8(1)
487488

489+
@overload
490+
def _lpython_floordiv(a: u8, b: u8) -> u8:
491+
return u8(_lpython_floordiv(i8(a), i8(b)))
492+
488493
@overload
489494
def _lpython_floordiv(a: i16, b: i16) -> i16:
490495
r: f64 # f32 rounds things up and gives incorrect results
@@ -495,6 +500,10 @@ def _lpython_floordiv(a: i16, b: i16) -> i16:
495500
return result
496501
return result - i16(1)
497502

503+
@overload
504+
def _lpython_floordiv(a: u16, b: u16) -> u16:
505+
return u16(_lpython_floordiv(i16(a), i16(b)))
506+
498507
@overload
499508
def _lpython_floordiv(a: i32, b: i32) -> i32:
500509
r: f64 # f32 rounds things up and gives incorrect results
@@ -505,6 +514,10 @@ def _lpython_floordiv(a: i32, b: i32) -> i32:
505514
return result
506515
return result - 1
507516

517+
@overload
518+
def _lpython_floordiv(a: u32, b: u32) -> u32:
519+
return u32(_lpython_floordiv(i32(a), i32(b)))
520+
508521
@overload
509522
def _lpython_floordiv(a: i64, b: i64) -> i64:
510523
r: f64
@@ -515,6 +528,10 @@ def _lpython_floordiv(a: i64, b: i64) -> i64:
515528
return result
516529
return result - i64(1)
517530

531+
@overload
532+
def _lpython_floordiv(a: u64, b: u64) -> u64:
533+
return u64(_lpython_floordiv(i64(a), i64(b)))
534+
518535
@overload
519536
def _lpython_floordiv(a: bool, b: bool) -> bool:
520537
if b == False:
@@ -534,10 +551,26 @@ def _mod(a: i16, b: i16) -> i16:
534551
def _mod(a: i32, b: i32) -> i32:
535552
return a - _lpython_floordiv(a, b)*b
536553

554+
@overload
555+
def _mod(a: u8, b: u8) -> u8:
556+
return a - _lpython_floordiv(a, b)*b
557+
558+
@overload
559+
def _mod(a: u16, b: u16) -> u16:
560+
return a - _lpython_floordiv(a, b)*b
561+
562+
@overload
563+
def _mod(a: u32, b: u32) -> u32:
564+
return a - _lpython_floordiv(a, b)*b
565+
537566
@overload
538567
def _mod(a: f32, b: f32) -> f32:
539568
return a - _lpython_floordiv(a, b)*b
540569

570+
@overload
571+
def _mod(a: u64, b: u64) -> u64:
572+
return a - _lpython_floordiv(a, b)*b
573+
541574
@overload
542575
def _mod(a: i64, b: i64) -> i64:
543576
return a - _lpython_floordiv(a, b)*b

tests/reference/asr-array_01_decl-39cf894.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-array_01_decl-39cf894.stdout",
9-
"stdout_hash": "705bcf3f7012f932ba8d5f5566cb9c14cbbb8675c3b710dcee4e769b",
9+
"stdout_hash": "64bfda5839dc16bcecfe6c4e8bd1dc9348c6b28045aab06efc317f82",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)