8000 [mypyc] Generate faster code for bool comparisons and arithmetic by JukkaL · Pull Request #14489 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

[mypyc] Generate faster code for bool comparisons and arithmetic #14489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 5, 2023
Prev Previous commit
Next Next commit
Add run tests for mixed comparisons
  • Loading branch information
JukkaL committed Jan 21, 2023
commit 0f1f3aaec8645ceddd743c7c870bac5d1055eedf
40 changes: 40 additions & 0 deletions mypyc/test-data/run-bools.test
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,26 @@ def test_comparisons() -> None:
assert gt(x, y) == (x2 > y2)
assert ge(x, y) == (x2 >= y2)

def eq_mixed(x: bool, y: int) -> bool:
return x == y

def neq_mixed(x: int, y: bool) -> bool:
return x != y

def lt_mixed(x: bool, y: int) -> bool:
return x < y

def gt_mixed(x: int, y: bool) -> bool:
return x > y

def test_mixed_comparisons() -> None:
for x in True, False:
for n in -(1 << 70), -123, 0, 1, 1753, 1 << 70:
assert eq_mixed(x, n) == (int(x) == n)
assert neq_mixed(n, x) == (n != int(x))
assert lt_mixed(x, n) == (int(x) < n)
assert gt_mixed(n, x) == (n > int(x))

def add(x: bool, y: bool) -> int:
return x + y

Expand Down Expand Up @@ -181,3 +201,23 @@ def test_arithmetic_i64() -> None:
for n in -(1 << 62), -123, 0, 1, 1753, 1 << 62:
assert add_mixed_i64(x, n) == int(x) + n
assert sub_mixed_i64(n, x) == n - int(x)

def eq_mixed_i64(x: bool, y: i64) -> bool:
return x == y

def neq_mixed_i64(x: i64, y: bool) -> bool:
return x != y

def lt_mixed_i64(x: bool, y: i64) -> bool:
return x < y

def gt_mixed_i64(x: i64, y: bool) -> bool:
return x > y

def test_mixed_comparisons_i64() -> None:
for x in True, False:
for n in -(1 << 62), -123, 0, 1, 1753, 1 << 62:
assert eq_mixed_i64(x, n) == (int(x) == n)
assert neq_mixed_i64(n, x) == (n != int(x))
assert lt_mixed_i64(x, n) == (int(x) < n)
assert gt_mixed_i64(n, x) == (n > int(x))
0