Multiply 32b x 32b

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Multiply 32b x 32b

Jump to solution
1,869 Views
ViniciusHoff
Contributor II

I need to multiply quickly two 32 bits operands and store in a 64 bits operand with GCC. I think that the faster mode to do this, is using the SMULL instruction of the processor.

But GCC never uses the SMULL instruction.

If I just multiply two 32 bits operands and store in a 64 bits operand, the GCC uses the MULL instruction and the result will be wrong, of 32 bits.

If I cast both operands to 64 bits, the result is correct, but is much longer.

How do I do this multiplication using SMULL instruction with GCC?

Tags (2)
0 Kudos
Reply
1 Solution
1,565 Views
martynhunt
NXP Employee
NXP Employee

Thank you for your reply.

I did some testing and found that this works on my system with optimizations set to Level 2.

volatile int a;

volatile int b;

volatile long long c;

a = /* A number */;

b = /* A number */;

c = ((long long a * b);

Hopefully this works for you as well.

In case this doesn't work for you, would you mind sharing your version of GCC tools?

Thank you,

Martyn

View solution in original post

0 Kudos
Reply
4 Replies
1,565 Views
martynhunt
NXP Employee
NXP Employee

Hi,

I would try doing this:

int a;

int b;

long c;

a = /* a number*/;

b = /* a number*/;

c = ((long long) a * b);

This should invoke the SMULL instruction.

Let me know if this does or does not work for you.

Thank you,

Martyn

0 Kudos
Reply
1,565 Views
ViniciusHoff
Contributor II

Hi Martyn

This code not work too. The code "((long long) a * b)" is made with MUL instruction and result 32 bits.

Thank you

Vinicius

0 Kudos
Reply
1,566 Views
martynhunt
NXP Employee
NXP Employee

Thank you for your reply.

I did some testing and found that this works on my system with optimizations set to Level 2.

volatile int a;

volatile int b;

volatile long long c;

a = /* A number */;

b = /* A number */;

c = ((long long a * b);

Hopefully this works for you as well.

In case this doesn't work for you, would you mind sharing your version of GCC tools?

Thank you,

Martyn

0 Kudos
Reply
1,565 Views
ViniciusHoff
Contributor II

Now this works for me too. Really, the optimization must be set to Level 2. I was working with level 1 optimization, so the GCC doesn't use SMULL instruction.

I think that GCC should always use SMULL instruction, but, that way, my problem is solved.

Thank you Martyn

0 Kudos
Reply