From 565b2d0a563501ea6b72864ede1443610e139964 Mon Sep 17 00:00:00 2001 From: Joost van Zwieten Date: Wed, 28 Feb 2024 21:53:16 +0100 Subject: [PATCH] replace x - (x // y) * y with x % y in divmod `evaluable.divmod(x, y)` returns `x - (x // y) * y` as remainder. There are three issues with this: * When calling `evaluable.div`, `evaluable.mod` *and* `evaluable.divmod` we're doing extra work. This can be solved by changing the implementation of `evaluable.mod`, but: * It appears to be cheaper to compute the modulo using `numpy.mod` on x86 since the remainder is a byproduct of the [div](https://www.felixcloutier.com/x86/div) instruction. * `evaluable.Mod` (note the capital) has an implementation for the integer bounds and the remainder of `evaluable.divmod` has not. This patch changes the implementation of the remainder of `evaluable.divmod` to `evaluable.Mod`. --- nutils/evaluable.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/nutils/evaluable.py b/nutils/evaluable.py index 89f774b48..1c382b6bb 100644 --- a/nutils/evaluable.py +++ b/nutils/evaluable.py @@ -4773,9 +4773,8 @@ def ln(x): def divmod(x, y): - div = FloorDivide(*_numpy_align(x, y)) - mod = x - div * y - return div, mod + x, y = _numpy_align(x, y) + return FloorDivide(x, y), Mod(x, y) def mod(arg1, arg2):