Zulip Chat Archive

Stream: Is there code for X?

Topic: fast div and mod


view this post on Zulip Chris Hughes (Aug 12 2020 at 14:56):

Is it faster to compute div and mod for integers at the same time? i.e Is there a function div_mod such that let (q, r) := div_mod a b is slightly faster than let q := a / b, let r := a % b, with a and b in int?

view this post on Zulip Alex J. Best (Aug 12 2020 at 15:17):

There is divmod  for pos_num already.

view this post on Zulip Mario Carneiro (Aug 12 2020 at 15:19):

You are basically reliant on the primitives to provide this; otherwise you can just define def divmod (a b : nat) := (a / b, a % b)

view this post on Zulip Alex J. Best (Aug 12 2020 at 15:30):

Wouldn't def divmod (a b : nat) := let div := a / b in (div, a - b * div)  be faster than using both % and /?

view this post on Zulip Mario Carneiro (Aug 12 2020 at 15:51):

Maybe. I would guess that the lean overhead of calling multiple functions is already more than the GMP bignum implementation overhead


Last updated: May 07 2021 at 21:10 UTC