Solved

muldiv64 on small cpu's

Posted on 1998-06-24
7
372 Views
Last Modified: 2008-02-01
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
Comment
Question by:nils pipenbrinck
7 Comments
 
LVL 4

Expert Comment

by:sganta
ID: 1251396
Hello !

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

0
 
LVL 11

Expert Comment

by:alexo
ID: 1251397
>> x = (a/c)*b;
Which will give you an incorrect result due to premature rounding
0
 
LVL 4

Author Comment

by:nils pipenbrinck
ID: 1251398
sganter, as alexo already said.. this will give incorrect results.

nils
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 4

Expert Comment

by:sganta
ID: 1251399
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
 
LVL 84

Expert Comment

by:ozo
ID: 1251400
(a/c)*(b/c)*c+a/c*(b%c)+b/c*(a%c)+(a%c)*(b%c)/c
0
 
LVL 4

Author Comment

by:nils pipenbrinck
ID: 1251401
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
 
LVL 4

Accepted Solution

by:
sganta earned 100 total points
ID: 1251402
I am sorry, I will apreciate it.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question