From 613c0cee4599455d767464cf9334479fa9adec68 Mon Sep 17 00:00:00 2001 From: Xuanda Yang Date: Thu, 5 Nov 2020 01:41:39 +0800 Subject: [PATCH] [mypyc] Implement float abs primitive (#9695) Related to mypyc/mypyc#644, also part of the plan to speed up rest of the slow microbenmark ops Implement builtins.abs on float. --- mypyc/primitives/float_ops.py | 8 ++++++++ mypyc/test-data/fixtures/ir.py | 2 ++ mypyc/test-data/run-floats.test | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/mypyc/primitives/float_ops.py b/mypyc/primitives/float_ops.py index 17bbdfbfe69e..fa5bbb018688 100644 --- a/mypyc/primitives/float_ops.py +++ b/mypyc/primitives/float_ops.py @@ -15,3 +15,11 @@ return_type=float_rprimitive, c_function_name='PyFloat_FromString', error_kind=ERR_MAGIC) + +# abs(float) +c_function_op( + name='builtins.abs', + arg_types=[float_rprimitive], + return_type=float_rprimitive, + c_function_name='PyNumber_Absolute', + error_kind=ERR_MAGIC) diff --git a/mypyc/test-data/fixtures/ir.py b/mypyc/test-data/fixtures/ir.py index 4ffefb7432de..da8e6986ce05 100644 --- a/mypyc/test-data/fixtures/ir.py +++ b/mypyc/test-data/fixtures/ir.py @@ -82,6 +82,7 @@ def __add__(self, n: float) -> float: pass def __sub__(self, n: float) -> float: pass def __mul__(self, n: float) -> float: pass def __truediv__(self, n: float) -> float: pass + def __neg__(self) -> float: pass class complex: def __init__(self, x: object, y: object = None) -> None: pass @@ -251,6 +252,7 @@ def zip(x: Iterable[T], y: Iterable[S]) -> Iterator[Tuple[T, S]]: ... @overload def zip(x: Iterable[T], y: Iterable[S], z: Iterable[V]) -> Iterator[Tuple[T, S, V]]: ... def eval(e: str) -> Any: ... +def abs(x: float) -> float: ... # Dummy definitions. class classmethod: pass diff --git a/mypyc/test-data/run-floats.test b/mypyc/test-data/run-floats.test index e2ab4b228861..e716949d69c4 100644 --- a/mypyc/test-data/run-floats.test +++ b/mypyc/test-data/run-floats.test @@ -10,3 +10,11 @@ assert str_to_float("1.234567") == 1.234567 assert str_to_float("44324") == 44324.0 assert str_to_float("23.4") == 23.4 assert str_to_float("-43.44e-4") == -43.44e-4 + +[case testFloatAbs] +def test_abs() -> None: + assert abs(0.0) == 0.0 + assert abs(-1.234567) == 1.234567 + assert abs(44324.732) == 44324.732 + assert abs(-23.4) == 23.4 + assert abs(-43.44e-4) == 43.44e-4