• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 414
  • Last Modified:

muldiv64 on small cpu's

Hi.

I have a small problem.

I need to calculate  x =  (a*b)/c.

Problem is, that a,b and c are 32 bit long, and the machine doesn't have a 64/32 bit division. The code the compiler generates in this case is ugly and slow (yep, it's time-critical). Does anyone has an idea how to do a fast version with 2 or maybe 4 ordinary 32/32 divisions?

Thank you in advance,

 Nils Pipenbrinck

(P.S. Please don't quote ugly c-code from the gnu lib)
0
nils pipenbrinck
Asked:
nils pipenbrinck
1 Solution
 
sgantaCommented:
Hello !

You use
x = (a/c)*b;    /* Instead of x = (a*b)/c; */

0
 
alexoCommented:
>> x = (a/c)*b;
Which will give you an incorrect result due to premature rounding
0
 
nils pipenbrinckAuthor Commented:
sganter, as alexo already said.. this will give incorrect results.

nils
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sgantaCommented:
Hello !
x = (a/c)*b;    /* Instead of x = (a*b)/c; */
is correct because
First it will get the result for a/c since it is in the paranthesis.
i.e., 32bit/32 bit division, result will be 32 bit only

Then it will take the result and multiply with b.

The equivalent statement in the assembly language is like this
MOVE value_of_a to AX
DIV AX,value_of_c /* AX contains the RESULT of A/C */
MUL AX,value_of_b /*AX  contains the FINAL RESULT  */

You test this program. Here it works. If it does'nt work, I don't mind.
You can reject my answer
0
 
ozoCommented:
(a/c)*(b/c)*c+a/c*(b%c)+b/c*(a%c)+(a%c)*(b%c)/c
0
 
nils pipenbrinckAuthor Commented:
x = (a/c)*b;    /* Instead of x = (a*b)/c; */

 a = 1
 b = 1000
 c = 2

 x = (a/c)*b would give:

  (1/2) * 1000
  = 0 * 1000
  = 0

while my formula gives:

 x = (a*b)/c

 (1*1000)/2 = 500

your substitution won't give the correct results in this case.
anyways  we  found a fast way to solve this problem.

Thank you.

(just answer again, I reopend the question for 50 points. then 'll give you the 50 points for your work.)
0
 
sgantaCommented:
I am sorry, I will apreciate it.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now