Multiply 32b x 32b

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Multiply 32b x 32b

跳至解决方案
1,874 次查看
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?

标记 (2)
0 项奖励
回复
1 解答
1,570 次查看
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 项奖励
回复
4 回复数
1,570 次查看
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 项奖励
回复
1,570 次查看
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 项奖励
回复
1,571 次查看
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 项奖励
回复
1,570 次查看
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 项奖励
回复