[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


Pentium III or higher: 64-bit / 128-bit integer operations

Posted on 2003-12-11
Medium Priority
Last Modified: 2008-02-26

I would like to implement various mathematical operations using relatively large integers for several highly optimized mathematical analyses in C++.

Are there simple assembly sequences, for Pentium III or higher, to program the following:

1) unsigned 64-bit * unsigned 64-bit multiply with unsigned 128-bit unsigned result.
2) unsigned 128-bit / unsigned 64-bit division with unsigned 64-bit result.
3) 64-bit unsigned + 64-bit unsigned addition with indication of carry.
4) 64-bit unsigned - 64-bit unsigned subtraction with indication of borrow.

I would really like to use SSE2 or something like that, as opposed to complicated sequences with eax:edx, etc. Most of my assembly experience is with 386 and some ID and timing stuff with CPUID and RDTSC stuff. Thus I could really use some help with the newer SSE stuff.

Are there any true 128-bit registers (or some combination registers such as the old eax:edx for old 386) for the new Pentium III and higher processors?

Thank you  very much for any assistance.

Sincerely, Chris.
Question by:dude_1967
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 9925362
The PIII only supports SSE, not SSE2.
If you want to work with integers, then you want MMX (SSE works on floats, and each 128 bit register is treated as 4 parallel 32 bit floats).

You can use MMX instructions to manipulate some 64 bit values for addition and subtraction.  I believe that you can multiply 2 64-bit numbers, but you will need to specifiy if you want the low 64-bits of the result or the high 64-bits of the result.  I don't believe you can divide 128 bits by 64 bits however.



Author Comment

ID: 9942661
Dear terageek,

I have been working a while with your suggestions. I think that my question was not clear. In addition, I would like to start with something a bit easier for me. Let's just examine the simple unsigned multiplication of unsigned 32-bit a * unsigned 32-bit b = unsigned 64-bit c.

I have coded this in a mixed C++ / ASM project using VC6, SP5 in the dialect of 386 assembler below.

Can this be done faster using something from the Pentium IV or higher? I was mistaken since I thought the newer stuff with SSE2 was in PIII as well. Let's look at the PIV architecture.

Let's start with this simple example. After mastering the 32 * 32 = 64 stuff, I would like to study the next level (64 bit * 64 bit = 128 bit).

Can you or anyone else please look at the sample and try to get this more modern that the 386 stuff?

Thanks once again for any help.

Sincerely, Chris.

int main(int argc, char* argv[])
  unsigned __int32 a = 0x33333333;
  unsigned __int32 b = 0x66666666;

  unsigned __int32 aa = 0xBBBBBBBB;
  unsigned __int32 bb = 0xEEEEEEEE;

  unsigned __int64 c, cc;

  // Multiply c  =  a *  b
  // Multiply cc = aa * bb
    mov eax, dword ptr [a]
    imul dword ptr [b]
    mov dword ptr [c], eax
    mov dword ptr [c + 4], edx

    mov eax, dword ptr [aa]
    mul dword ptr [bb]
    mov dword ptr [cc], eax
    mov dword ptr [cc + 4], edx

  // Test c  =  a *  b
  unsigned __int64 t  = static_cast<unsigned __int64>( a) *  b;

  // Test cc = aa * bb
  unsigned __int64 tt = static_cast<unsigned __int64>(aa) * bb;

  // Final check.
  const bool ok = t == c && tt == cc;

  return ok ? 0 : 1;

Assisted Solution

terageek earned 800 total points
ID: 9958278
I am not sure I can give you all of the detailed info you would need here.  Here are a couple of links to get you started...

Intel's page for the P4 has a lot of usefull articles...


You will probably want to start with the P4 instruction set reference to read up on the MMX, SSE and SSE2 instructions...


And if you want to get ahead of the curve, you can start reading up on the 13 new instructions in Prescott (the Pentium V?)


Accepted Solution

mtmike earned 1200 total points
ID: 9959981
I think the MMX/SSE/SSE2 extensions will not be very useful for what you want to accomplish. Since you are new to them, a short overview...


This adds eight 64-bit registers (labelled mm0-mm7) and 54 instructions. The mmx instructions operate on packed dwords, words or bytes. Depending on the instruction, a 64-bit mmx operand is either treated as two dwords, four words or eight bytes. For example:

paddb mm0,mm1 ; add eight packed bytes
paddw mm0,mm1 ; add four packed words
paddd mm0,mm1 ; add two packed dwords

The instruction set contains logical, (saturating) arithmetic, multiplication, data movement, packing/unpacking and comparison operations.

Multiplication is limited to signed words. You can only compute one half of the result at a time:

pmullw mm0,mm1 ; multiply four signed words and store the low word of the result
pmulhw mm0,mm1 ; multiply four signed words and store the high word of the result

Or you can do a widening multiply-add:

pmaddwd mm0,mm1; multiply four signed words and add each pair of results.

The mmx instruction set was later extended with an unsigned multiply (P3):

pmulhuw mm0,mm1 ; multiply four unsigned words and store the high word of the result

So, doing 32 x 32 -> 64 multiply word-wise is certainly going to take a lot more instructions than the plain x86 version. Since mmx has no indication of carry, adding partial results is going to be tricky.

SSE (P3):

This adds eight 128-bit registers (xmm0-xmm7). All new sse instructions operate on packed floats. There is no support for integer arithmetic (only logical and packing instructions).

SSE2 (P4):

This adds support for doubles and some integer instructions. Of interest are:

pmuludq mm0,mm1 ; multiply two unsigned dwords to a qword
pmuludq xmm0,xmm1 ; multiply four unsigned dwords to two qwords

paddq mm0,mm1 ; qword add
paddq xmm0,xmm1 ; add two packed qwords

psubq mm0,mm1 ; qword sub
psubq xmm0,xmm1 ; substract two packed qwords

These might accelerate 32 x 32 -> 64 multiplies since you can do two of them at a time.

A 64 x 64 -> 128 multiply could be implemented by doing four 32 x 32 -> 64 multiplies (two instructions). The partial results will have to be added using plain old "adc" since sse has no carry support. The multiplies will be a little bit more efficient, but you will end up having to do a lot of register transfers.

The paddq/psubq instructions are also unsuitable for your purposes since they give no indication of carry/borrow.

There are no mmx/sse integer division instructions.

Author Comment

ID: 9962644
mtmike, terageek,

Thanks for the clearly thought out and relevant suggestions as well as the advise on further reading. It seems like it could take some careful design and a bit of time to work out the optimal solution. I have split the question points and now consider this topic closed for the purpooses of the forum. I'll continue working on a decent solution.

Thanks again.

Also my complements to the forum---always a source of competent information.

Sincerely, Chris.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
What monsters are hiding in your child's room? In this article I will share with you a tech horror story that could happen to anyone, along with some tips on how you can prevent it from happening to you.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question