Skip to content

Commit

Permalink
Merge pull request numba#7848 from njriasan/nick/int_list_mul
Browse files Browse the repository at this point in the history
Support for int * list
  • Loading branch information
sklam authored Feb 17, 2022
2 parents 6e7561b + c960254 commit 775b988
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions numba/core/typing/listdecl.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ def generic(self, args, kws):
a, b = args
if isinstance(a, types.List) and isinstance(b, types.Integer):
return signature(a, a, types.intp)
elif isinstance(a, types.Integer) and isinstance(b, types.List):
return signature(b, types.intp, b)


@infer_global(operator.imul)
Expand Down
9 changes: 7 additions & 2 deletions numba/cpython/listobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,11 +714,16 @@ def list_add_inplace(context, builder, sig, args):


@lower_builtin(operator.mul, types.List, types.Integer)
@lower_builtin(operator.mul, types.Integer, types.List)
def list_mul(context, builder, sig, args):
src = ListInstance(context, builder, sig.args[0], args[0])
if isinstance(sig.args[0], types.List):
list_idx, int_idx = 0, 1
else:
list_idx, int_idx = 1, 0
src = ListInstance(context, builder, sig.args[list_idx], args[list_idx])
src_size = src.size

mult = args[1]
mult = args[int_idx]
zero = ir.Constant(mult.type, 0)
mult = builder.select(cgutils.is_neg_int(builder, mult), zero, mult)
nitems = builder.mul(mult, src_size)
Expand Down
7 changes: 7 additions & 0 deletions numba/tests/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ def list_mul(n, v):
a = list(range(n))
return a * v

def list_mul2(n, v):
a = list(range(n))
return v * a

def list_mul_inplace(n, v):
a = list(range(n))
a *= v
Expand Down Expand Up @@ -656,6 +660,9 @@ def check_mul(self, pyfunc):
def test_mul(self):
self.check_mul(list_mul)

def test_mul2(self):
self.check_mul(list_mul2)

def test_mul_inplace(self):
self.check_mul(list_mul_inplace)

Expand Down

0 comments on commit 775b988

Please sign in to comment.