Skip to content

[mypyc] Replace integer floor division by a power of two with a shift #12870

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 2 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions mypyc/irbuild/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ def transform_op_expr(builder: IRBuilder, expr: OpExpr) -> Value:
# Special case some int ops to allow borrowing operands.
if (is_int_rprimitive(builder.node_type(expr.left))
and is_int_rprimitive(builder.node_type(expr.right))):
if expr.op == '//':
expr = try_optimize_int_floor_divide(expr)
if expr.op in int_borrow_friendly_op:
borrow_left = is_borrow_friendly_expr(builder, expr.right)
left = builder.accept(expr.left, can_borrow=borrow_left)
Expand All @@ -419,6 +421,17 @@ def transform_op_expr(builder: IRBuilder, expr: OpExpr) -> Value:
)


def try_optimize_int_floor_divide(expr: OpExpr) -> OpExpr:
"""Replace // with a power of two with a right shift, if possible."""
if not isinstance(expr.right, IntExpr):
return expr
divisor = expr.right.value
shift = divisor.bit_length() - 1
if 0 < shift < 28 and divisor == (1 << shift):
return OpExpr('>>', expr.left, IntExpr(shift))
return expr


def transform_index_expr(builder: IRBuilder, expr: IndexExpr) -> Value:
index = expr.index
base_type = builder.node_type(expr.base)
Expand Down
38 changes: 38 additions & 0 deletions mypyc/test-data/irbuild-int.test
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,41 @@ L5:
r8 = x
L6:
return r8

[case testIntFloorDivideByPowerOfTwo]
def divby1(x: int) -> int:
return x // 1
def divby2(x: int) -> int:
return x // 2
def divby3(x: int) -> int:
return x // 3
def divby4(x: int) -> int:
return x // 4
def divby8(x: int) -> int:
return x // 8
[out]
def divby1(x):
x, r0 :: int
L0:
r0 = CPyTagged_FloorDivide(x, 2)
return r0
def divby2(x):
x, r0 :: int
L0:
r0 = CPyTagged_Rshift(x, 2)
return r0
def divby3(x):
x, r0 :: int
L0:
r0 = CPyTagged_FloorDivide(x, 6)
return r0
def divby4(x):
x, r0 :: int
L0:
r0 = CPyTagged_Rshift(x, 4)
return r0
def divby8(x):
x, r0 :: int
L0:
r0 = CPyTagged_Rshift(x, 6)
return r0
18 changes: 18 additions & 0 deletions mypyc/test-data/run-integers.test
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ assert test_isinstance_int_and_not_bool(True) == False
assert test_isinstance_int_and_not_bool(1) == True

[case testIntOps]
from typing import Any

def check_and(x: int, y: int) -> None:
# eval() can be trusted to calculate expected result
expected = eval('{} & {}'.format(x, y))
Expand Down Expand Up @@ -428,6 +430,22 @@ def test_constant_fold() -> None:
n64 = -(1 << 64) + int()
assert n64 == -(1 << 64)

def div_by_2(x: int) -> int:
return x // 2

def div_by_3(x: int) -> int:
return x // 3

def div_by_4(x: int) -> int:
return x // 4

def test_floor_divide_by_literal() -> None:
for i in range(-100, 100):
i_boxed: Any = i
assert div_by_2(i) == i_boxed // int('2')
assert div_by_3(i) == i_boxed // int('3')
assert div_by_4(i) == i_boxed // int('4')

[case testIntMinMax]
def test_int_min_max() -> None:
x: int = 200
Expand Down