Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How is the % processed?

Status
Not open for further replies.

Bones3

Programmer
Jul 27, 2003
151
US
After code with the modulus operator (%) is compiled how does the CPU process it? I mean does it have some sort of efficient way of makeing x % y take up less processing power than if we just put it in a loop that calcualtes it? Same thing with powers. If we could (I don't believe we can) do this: 4^50 would it be processed the same as

for(i = 0; i<50; i++)
{
4 * 4;
}
?

-Bones
 
There is no x86 instruction for modulus. However, the time needed for this is insignificant. A modulus is probably implemented as three operations: integer divide, integer multiply, and subtract. Think of it this way:

int operator% (int a, int b)
{ return (a/b)*b-a; )

There is no x86 instruction for general exponents, i.e. calculating ab. However, the most common exponential operation in computers is to multiply a number by 2x. In C/C++ code we use the shift operators << and >> to do this. For general exponentiation we use pow(a,b).

I REALLY hope that helps.
Will
 
Might have been with ALU(Arithematical Logic Unit) in any x86 microprocessor. You can look for more details in Liu Gibson book or in some book of microprocessor architecture
 
Thanks everyone, it was just one of those things I wonder about. Is there a place where you can find some x86 instruction documentation online?

-Bones
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top