Skip to content

Commit

Permalink
replace x - (x // y) * y with x % y in divmod
Browse files Browse the repository at this point in the history
`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`.
  • Loading branch information
joostvanzwieten committed Feb 29, 2024
1 parent 1a10071 commit 565b2d0
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions nutils/evaluable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 565b2d0

Please sign in to comment.