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 8803533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:21:46+00:00 2026-06-14T01:21:46+00:00

Modern CPU’s can perform extended multiplication between two native-size words and store the low

  • 0

Modern CPU’s can perform extended multiplication between two native-size words and store the low and high result in separate registers. Similarly, when performing division, they store the quotient and the remainder in two different registers instead of discarding the unwanted part.

Is there some sort of portable gcc intrinsic which would take the following signature:

void extmul(size_t a, size_t b, size_t *lo, size_t *hi);

Or something like that, and for division:

void extdiv(size_t a, size_t b, size_t *q, size_t *r);

I know I could do it myself with inline assembly and shoehorn portability into it by throwing #ifdef’s in the code, or I could emulate the multiplication part using partial sums (which would be significantly slower) but I would like to avoid that for readability. Surely there exists some built-in function to do this?

  • 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-06-14T01:21:48+00:00Added an answer on June 14, 2026 at 1:21 am

    For gcc since version 4.6 you can use __int128. This works on most 64 bit hardware. For instance

    To get the 128 bit result of a 64×64 bit multiplication just use

    void extmul(size_t a, size_t b, size_t *lo, size_t *hi) {
        __int128 result = (__int128)a * (__int128)b;
        *lo = (size_t)result;
        *hi = result >> 64;
    }
    

    On x86_64 gcc is smart enough to compile this to

       0:   48 89 f8                mov    %rdi,%rax
       3:   49 89 d0                mov    %rdx,%r8
       6:   48 f7 e6                mul    %rsi
       9:   49 89 00                mov    %rax,(%r8)
       c:   48 89 11                mov    %rdx,(%rcx)
       f:   c3                      retq   
    

    No native 128 bit support or similar required, and after inlining only the mul instruction remains.

    Edit: On a 32 bit arch this works in a similar way, you need to replace __int128_t by uint64_t and the shift width by 32. The optimization will work on even older gccs.

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

Sidebar

Related Questions

It's common that modern CPU architectures employ performance optimizations that can result in out-of-order
In most modern IDEs there is a parameter that you can set to ensure
For desktop browser all modern browser uses Zoom functionality so we can use PX
For each of the modern browsers are there well known hooks I can tie
It appears that most modern languages and tools allow for extended regular expressions, and
I'm interesting in the time cost on a modern desktop CPU of some floating
Some modern network cards support Direct Memory Access for improved performance. How can I
In modern web browsers we can work with multiple contents in parallel by using
How much is the performance of modern FPGA relative to CPU, absolutly in (GFlops/GIops)
Many modern programming languages allow us to handle potentially infinite lists and to perform

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.