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?
Solved! Go to Solution.
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
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
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
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
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