Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7178237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:52:48+00:00 2026-05-28T16:52:48+00:00

I need to do the following operation many times: Take two integers a, b

  • 0

I need to do the following operation many times:

  1. Take two integers a, b
  2. Compute a * b mod p, where p = 1000000007 and a, b are of the same order of magnitude as p

My gut feeling is the naive

result = a * b
result %= p

is inefficient. Can I optimise multiplication modulo p much like exponentiation modulo p is optimised with pow(a, b, p)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:52:49+00:00Added an answer on May 28, 2026 at 4:52 pm

    To do this calculation in assembly, but have it callable from Python, I’d
    try inline assembly from a
    Python module written in C.
    Both GCC and
    MSVC
    compilers feature inline assembly, only with differing syntax.

    Note that our modulus p = 1000000007 just fits into 30-bits. The result
    desired (a*b)%p can be computed in Intel 80×86 registers given some weak
    restrictions on a,b not being much bigger than p.

    Restrictions on size of a,b

    (1) a,b are 32-bit unsigned integers

    (2) a*b is less than p << 32, i.e. p times 2^32

    In particular if a,b are each less than 2*p, overflow will be avoided.
    Given (1), it also suffices that either one of them is less than p.

    The Intel 80×86 instruction MUL can multiply two 32-bit unsigned integers
    and store the 64-bit result in accumulator register pair EDX:EAX. Some
    details and quirks of MUL are discussed in Section 10.2.1 of this helpful
    summary.

    The instruction DIV can then divide this 64-bit result by a 32-bit constant
    (the modulus p), storing the quotient in EAX and the remainder in EDX.
    See Section 10.2.2 of the last link. The result we want is that remainder.

    It is this division instruction DIV that entails a risk of overflow, should
    the 64-bit product in numerator EDX:EAX give a quotient larger than 32-bits
    by failing to satisfy (2) above.

    I’m working on a code snippet in C/inline assembly for “proof of concept”.
    However the maximum benefit in speed will depend on batching up arrays of
    data a,b to process, amortizing the overhead of function calls, etc. in
    Python (if that is the target platform).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We need to perform the following operation in our database : There is a
I need a query which can do the following operation. I have a table
I need a data structure which will support the following operations in a performant
I need to design a data structure that supports the following operations: search element
All, I need to write a regular expression to perform the following operations replace
I need following function (from C++ dll) available in C++/CLI extern C _declspec(dllexport) void
I need following namespaces to use native wpf property grid however, VS 2010 doesn't
I need the following C function in Python: int func(Uint8 *bytRecvBuffer, int *iRecvLen); I
I need the following xml to be made in code: <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:gravity=right
I need the following authentication script finished. I am weak at php/pdo so I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.