diff options
author | Stefan Behnel <stefan_ml@behnel.de> | 2019-01-20 21:05:04 +0100 |
---|---|---|
committer | Stefan Behnel <stefan_ml@behnel.de> | 2019-01-22 19:13:35 +0100 |
commit | 525d348874ee137c97e405e0779cb7ccc8cfb183 (patch) | |
tree | 63cf8117320424c846593490a5d59d82a92bae22 /tests | |
parent | b48cf655522e632ab0193cd80b615a30cffe0dc3 (diff) | |
download | cython-525d348874ee137c97e405e0779cb7ccc8cfb183.tar.gz |
Speed up multiplication of Python numbers with small integers (<= 2**30).faster_pymultiply
Diffstat (limited to 'tests')
-rw-r--r-- | tests/run/consts.pyx | 11 | ||||
-rw-r--r-- | tests/run/mulop.pyx | 166 |
2 files changed, 176 insertions, 1 deletions
diff --git a/tests/run/consts.pyx b/tests/run/consts.pyx index 37772eaef..5f70e3ac2 100644 --- a/tests/run/consts.pyx +++ b/tests/run/consts.pyx @@ -156,8 +156,17 @@ def multiplied_lists_nonconst_left(x): """ return x * [1,2,3] + +@cython.test_fail_if_path_exists("//MulNode") +def multiplied_nonconst_list_const_int(x): + """ + >>> multiplied_nonconst_list_const_int(2) + [1, 2, 3, 1, 2, 3] + """ + return [1,x,3] * 2 + + @cython.test_fail_if_path_exists("//MulNode//ListNode") -@cython.test_assert_path_exists("//MulNode") def multiplied_lists_nonconst_expression(x): """ >>> multiplied_lists_nonconst_expression(5) == [1,2,3] * (5 * 2) diff --git a/tests/run/mulop.pyx b/tests/run/mulop.pyx new file mode 100644 index 000000000..0fba7dba9 --- /dev/null +++ b/tests/run/mulop.pyx @@ -0,0 +1,166 @@ +# mode: run +# tag: multiply + +import sys +IS_PY2 = sys.version_info[0] < 3 + + +def print_long(x): + if IS_PY2: + x = str(x).rstrip('L') + print(x) + + +def mul_10_obj(x): + """ + >>> mul_10_obj(0) + 0 + >>> mul_10_obj(10) + 100 + >>> mul_10_obj(-10) + -100 + >>> 10 * (2**14) + 163840 + >>> mul_10_obj(2**14) + 163840 + >>> mul_10_obj(-2**14) + -163840 + >>> print_long(10 * (2**29)) + 5368709120 + >>> print_long(mul_10_obj(2**29)) + 5368709120 + >>> print_long(mul_10_obj(-2**29)) + -5368709120 + >>> print_long(10 * (2**30)) + 10737418240 + >>> print_long(mul_10_obj(2**30)) + 10737418240 + >>> print_long(mul_10_obj(-2**30)) + -10737418240 + >>> print_long(10 * (2**63)) + 92233720368547758080 + >>> print_long(mul_10_obj(2**63)) + 92233720368547758080 + >>> print_long(mul_10_obj(-2**63)) + -92233720368547758080 + >>> print_long(10 * (2**128)) + 3402823669209384634633746074317682114560 + >>> print_long(mul_10_obj(2**128)) + 3402823669209384634633746074317682114560 + >>> print_long(mul_10_obj(-2**128)) + -3402823669209384634633746074317682114560 + """ + result = 10 * x + return result + + +def mul_obj_10(x): + """ + >>> mul_obj_10(0) + 0 + >>> mul_obj_10(10) + 100 + >>> mul_obj_10(-10) + -100 + >>> 10 * (2**14) + 163840 + >>> mul_obj_10(2**14) + 163840 + >>> mul_obj_10(-2**14) + -163840 + >>> print_long(10 * (2**29)) + 5368709120 + >>> print_long(mul_obj_10(2**29)) + 5368709120 + >>> print_long(mul_obj_10(-2**29)) + -5368709120 + >>> print_long(10 * (2**30)) + 10737418240 + >>> print_long(mul_obj_10(2**30)) + 10737418240 + >>> print_long(mul_obj_10(-2**30)) + -10737418240 + >>> print_long(10 * (2**63)) + 92233720368547758080 + >>> print_long(mul_obj_10(2**63)) + 92233720368547758080 + >>> print_long(mul_obj_10(-2**63)) + -92233720368547758080 + >>> print_long(10 * (2**128)) + 3402823669209384634633746074317682114560 + >>> print_long(mul_obj_10(2**128)) + 3402823669209384634633746074317682114560 + >>> print_long(mul_obj_10(-2**128)) + -3402823669209384634633746074317682114560 + """ + result = x * 10 + return result + + +def mul_bigint_obj(x): + """ + >>> mul_bigint_obj(0) + 0 + >>> print_long(mul_bigint_obj(1)) + 536870912 + >>> print_long(mul_bigint_obj(2)) + 1073741824 + >>> print_long(mul_bigint_obj(2**29)) + 288230376151711744 + >>> print_long(mul_bigint_obj(-2**29)) + -288230376151711744 + >>> print_long(mul_bigint_obj(2**30)) + 576460752303423488 + >>> print_long(mul_bigint_obj(-2**30)) + -576460752303423488 + >>> print_long(mul_bigint_obj(2**59)) + 309485009821345068724781056 + >>> print_long(mul_bigint_obj(-2**59)) + -309485009821345068724781056 + """ + result = (2**29) * x + return result + + +def mul_obj_float(x): + """ + >>> mul_obj_float(-0.0) + -0.0 + >>> mul_obj_float(0) + 0.0 + >>> mul_obj_float(1.0) + 2.0 + >>> mul_obj_float(-2.0) + -4.0 + >>> mul_obj_float(-0.5) + -1.0 + """ + result = x * 2.0 + return result + + +def mul_float_obj(x): + """ + >>> mul_float_obj(0) + 0.0 + >>> mul_float_obj(2) + 4.0 + >>> mul_float_obj(-2) + -4.0 + >>> 2.0 * (2**30-1) + 2147483646.0 + >>> mul_float_obj(2**30-1) + 2147483646.0 + >>> mul_float_obj(-(2**30-1)) + -2147483646.0 + >>> mul_float_obj(-0.0) + -0.0 + >>> mul_float_obj(1.0) + 2.0 + >>> mul_float_obj(-2.0) + -4.0 + >>> mul_float_obj(-0.5) + -1.0 + """ + result = 2.0 * x + return result |