Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d8d82e2

Browse files
authored
[mypyc] Complete support of int comparison ops (#9189)
Related to mypyc/mypyc#741, merges all remaining integer comparison ops.
1 parent 32a6377 commit d8d82e2

5 files changed

Lines changed: 264 additions & 76 deletions

File tree

mypyc/primitives/int_ops.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,6 @@ def int_compare_op(name: str, c_function_name: str) -> None:
114114
int_binary_op('//=', 'CPyTagged_FloorDivide', error_kind=ERR_MAGIC)
115115
int_binary_op('%=', 'CPyTagged_Remainder', error_kind=ERR_MAGIC)
116116

117-
int_compare_op('<=', 'CPyTagged_IsLe')
118-
int_compare_op('>', 'CPyTagged_IsGt')
119-
int_compare_op('>=', 'CPyTagged_IsGe')
120-
121117
# Add short integers and assume that it doesn't overflow or underflow.
122118
# Assume that the operands are not big integers.
123119
unsafe_short_add = custom_op(
@@ -170,5 +166,8 @@ def int_unary_op(name: str, c_function_name: str) -> CFunctionDescription:
170166
int_logical_op_mapping = {
171167
'==': IntLogicalOpDescrption(BinaryIntOp.EQ, int_equal_, False, False),
172168
'!=': IntLogicalOpDescrption(BinaryIntOp.NEQ, int_equal_, True, False),
173-
'<': IntLogicalOpDescrption(BinaryIntOp.SLT, int_less_than_, False, False)
169+
'<': IntLogicalOpDescrption(BinaryIntOp.SLT, int_less_than_, False, False),
170+
'<=': IntLogicalOpDescrption(BinaryIntOp.SLE, int_less_than_, True, True),
171+
'>': IntLogicalOpDescrption(BinaryIntOp.SGT, int_less_than_, False, True),
172+
'>=': IntLogicalOpDescrption(BinaryIntOp.SGE, int_less_than_, True, False),
174173
} # type: Dict[str, IntLogicalOpDescrption]

mypyc/test-data/analysis.test

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -658,21 +658,39 @@ def f(a):
658658
sum :: int
659659
i :: int
660660
r0 :: bool
661-
r1 :: int
662-
r2 :: int
661+
r1 :: native_int
662+
r2 :: bool
663+
r3 :: native_int
664+
r4, r5, r6, r7, r8 :: bool
665+
r9 :: int
666+
r10 :: int
663667
L0:
664668
sum = 0
665669
i = 0
666670
L1:
667-
r0 = CPyTagged_IsLe(i, a)
668-
if r0 goto L2 else goto L3 :: bool
671+
r1 = i & 1
672+
r2 = r1 == 0
673+
r3 = a & 1
674+
r4 = r3 == 0
675+
r5 = r2 & r4
676+
if r5 goto L2 else goto L3 :: bool
669677
L2:
670-
r1 = CPyTagged_Add(sum, i)
671-
sum = r1
672-
r2 = CPyTagged_Add(i, 2)
673-
i = r2
674-
goto L1
678+
r6 = i <= a :: signed
679+
r0 = r6
680+
goto L4
675681
L3:
682+
r7 = CPyTagged_IsLt_(a, i)
683+
r8 = !r7
684+
r0 = r8
685+
L4:
686+
if r0 goto L5 else goto L6 :: bool
687+
L5:
688+
r9 = CPyTagged_Add(sum, i)
689+
sum = r9
690+
r10 = CPyTagged_Add(i, 2)
691+
i = r10
692+
goto L1
693+
L6:
676694
return sum
677695
(0, 0) {a} {a}
678696
(0, 1) {a} {a}
@@ -681,13 +699,29 @@ L3:
681699
(0, 4) {a} {a}
682700
(1, 0) {a} {a}
683701
(1, 1) {a} {a}
702+
(1, 2) {a} {a}
703+
(1, 3) {a} {a}
704+
(1, 4) {a} {a}
705+
(1, 5) {a} {a}
706+
(1, 6) {a} {a}
707+
(1, 7) {a} {a}
708+
(1, 8) {a} {a}
709+
(1, 9) {a} {a}
684710
(2, 0) {a} {a}
685711
(2, 1) {a} {a}
686712
(2, 2) {a} {a}
687-
(2, 3) {a} {a}
688-
(2, 4) {a} {a}
689-
(2, 5) {a} {a}
690713
(3, 0) {a} {a}
714+
(3, 1) {a} {a}
715+
(3, 2) {a} {a}
716+
(3, 3) {a} {a}
717+
(4, 0) {a} {a}
718+
(5, 0) {a} {a}
719+
(5, 1) {a} {a}
720+
(5, 2) {a} {a}
721+
(5, 3) {a} {a}
722+
(5, 4) {a} {a}
723+
(5, 5) {a} {a}
724+
(6, 0) {a} {a}
691725

692726
[case testError]
693727
def f(x: List[int]) -> None: pass # E: Name 'List' is not defined \

0 commit comments

Comments
 (0)