Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the modulo operator (%) actually computed?

Recently I've been confused about the modulo operator, %.

It's known that a % b == a-a/b*b when we have integers a and b where a > b, and we can do this calculation by hand if a and b are small enough.

However, when it comes to the way a processor computes it, does the processor use the same method as previously mentioned, a-a/b*b? Maybe just by translating the division into subtraction or addition, or is there some shifting involved perhaps?

like image 893
pNok Avatar asked Oct 09 '11 13:10

pNok


People also ask

What the modulus operator (%) does?

The modulo operator, denoted by %, is an arithmetic operator. The modulo division operator produces the remainder of an integer division. produces the remainder when x is divided by y.

What types does the mod (%) work on?

3) modulus operator is not just applicable to integral types e.g. byte, short, int, long but also to floating-point types like float and double. 4) You can also use the remainder operator to check if a number is even or odd, or if a year is leap year.

How does a computer calculate modulo?

Most of the time, modulus is just computed by dividing the two numbers. The quotient is stored in one register, and the remainder is stored in the other register.

How is modulo calculated in Python?

The Python modulo operator calculates the remainder of dividing two values. This operator is represented by the percentage sign (%). The syntax for the modulo operator is: number1 % number2. The first number is divided by the second then the remainder is returned.


2 Answers

Except for powers of 2, where the modulo operator can (and in most optimizing compilers is) be turned into a simple bitwise operation, I'm afraid the only way to do it is the hard way. Explanation is http://en.wikipedia.org/wiki/Modulo_operation

In another answer, @Henk Holterman points out that some CPUs do it in the microcode, leaving the remainder in a register while doing an integer divide, which means the modulo instruction can be reduced to an integer divide and return the remainder. (I'm adding that information here because this answer has already been accepted.)

like image 143
Paul Tomblin Avatar answered Sep 28 '22 11:09

Paul Tomblin


It uses the idiv assembly instruction:

    int x = a % b;
00000050  cmp         dword ptr [rsp+20h],80000000h 
00000058  jne         0000000000000061 
0000005a  cmp         dword ptr [rsp+24h],0FFFFFFFFh 
0000005f  je          0000000000000070 
00000061  mov         eax,dword ptr [rsp+20h] 
00000065  cdq              
00000066  idiv        eax,dword ptr [rsp+24h] 
0000006a  mov         dword ptr [rsp+2Ch],edx 
0000006e  jmp         0000000000000075 
00000070  call        FFFFFFFFF2620E70 
00000075  mov         eax,dword ptr [rsp+2Ch] 
00000079  mov         dword ptr [rsp+28h],eax 

idiv stores the remainder in a register.
http://pdos.csail.mit.edu/6.828/2007/readings/i386/IDIV.htm

like image 37
Steve Wellens Avatar answered Sep 28 '22 09:09

Steve Wellens