I like this, since it handles full 64 bit values. An improvement to the repeating doubling algorithm is to check how many bits at once can be calculated without an overflow. The approximation leaves a value small enough for normal 64 bit integer operations, for m up to 63 bits. To handle arbitrary a and b , add these lines before c is computed. This method is limited by the significand of a long double , which is usually 64 bits or larger.

Uploader: | Nezragore |

Date Added: | 21 October 2017 |

File Size: | 16.84 Mb |

Operating Systems: | Windows NT/2000/XP/2003/2003/7/8/10 MacOS 10/X |

Downloads: | 52216 |

Price: | Free* [*Free Regsitration Required] |

Here is a modification which has two advantages: Post as a guest Name. I don’t know about inline assembler, does it also use a loop? It will actually fail for some inputs.

Stack Overflow works best with JavaScript enabled. Have you read about the Montgomery reduction algorithm? You could try something that breaks the multiplication up into additions: Funny, this would be trivial in x64 assembly.

This method is limited by the significand of a doublewhich is usually 52 bits. Email Muulmod, but never shown.

An early exit check can be done for both arguments — speeding up the unlikely? But I assume the OP means that the implied quotient is a bit value as well.

# mulmod failing –

This solution seems inefficient: It uses a double-width multiplication and division is always double-width. The first one is the same as yours, but I changed your numbers to excplicit ULL. The actual number of iterations mu,mod be 3 with the example.

Christian Ammer 5, 5 38 That also explains harold’s comment; for such large m the stepsize becomes 1 and you make no progress. An improvement to the repeating doubling algorithm is to check how many bits at mulmood can be calculated without an overflow.

Yes, but only one, and there’s already a modulo in a loop.

By clicking “Post Your Answer”, you acknowledge that you have read our updated terms of serviceprivacy policy and cookie policyand milmod your continued use of the website is subject to these policies.

I believe that’s the only limitation on the function argument values of the present algorithm, though. Post Your Answer Discard By clicking “Post Your Answer”, you acknowledge that you have read our updated terms of serviceprivacy policy and cookie policyand that your continued use of the website is subject to these policies.

Sign up using Facebook. By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The first factor must not have its top bit set. I can suggest an improvement for your algorithm.

You should use Russian Peasant multiplication. Both are bit, but divq will result in an exception.

Is there a way to build e. Ways to do modulo multiplication with primitive types Ask Question. The integer arithmetic is limited to 63 bits.

## Method: Numeric#mulmod

Adios to Winter Bash It doesn’t need to use the modulo operation, which can be expensive on some processors. Benjamin – I’m just pointing out that the assembly implementation isn’t general. Brett 3 numers are 64bit. It’s mullmod in high level languages that the high part of the multiplication suddenly gets lost.