Multiply 32b x 32b

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 
1,880件の閲覧回数
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,576件の閲覧回数
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,576件の閲覧回数
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,576件の閲覧回数
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,577件の閲覧回数
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,576件の閲覧回数
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 件の賞賛
返信