I am a chief IT specialist who is interested in how assembler languages handle the integer division function. It seems that just adding to the numerator, giving both division and mod, is too impractical, so I came up with another way to split using bit offsets, subtraction and two lookup tables.
Basically, the function takes a denominator and makes “blocks” based on the maximum power of 2. Thus, dividing by 15 makes binary blocks out of 4, dividing by 5 makes binary blocks out of 3, etc. Then the first 2 ^ block size of the multiple denominator is generated. For each multiple, write the values AFTER the first block in the search table, using the sign of the first block.
Example: Multiplication 5 in a binary block of size 3 (octal)
000 000 **101** - 5 maps to 0 000 001 **010** - 2 maps to 1 000 001 **111** - 7 maps to 1 000 010 **100** - 4 maps to 2 000 011 **001** - 1 maps to 3 000 011 **110** - 6 maps to 3 000 100 **011** - 3 maps to 4 000 101 **000** - 0 maps to 5
Thus, the actual procedure involves obtaining the first block, shifting the left bit over the first block, and subtracting the value by which the blocks are mapped. If the resulting number goes to 0, then it is perfectly divided, and if the value becomes negative, it is not.
If you add another enumeration search table where you match the values to the counter as they become available, you can calculate the division result!
Example: multiply by 5 again
5 maps to 1 2 maps to 2 7 maps to 3 4 maps to 4 1 maps to 5 6 maps to 6 3 maps to 7 0 maps to 8
Then, all that remains is to display each block in a column, and you have an answer.
There are several problems with this method.
- If the answer is not completely divisible, the function returns the unwanted file.
- For high values of Integer, this will not work, because the size of 5 blocks will be truncated at the end of a 32-bit or 64-bit integer.
- This is about 100 times slower than standard division in C.
- If the denominator is a divisor factor, then your blocks should be matched with multiple values, and you need even more tables. This can be solved using simple factorization, but all the methods that I read about simple / quick simple factorization include separation, surpassing the purpose of this.
So, I have 2 questions: firstly, is there a similar algorithm there? I looked around and I can not find anything like it. Second, how do real assembler languages handle the Integer unit?
Sorry if there is any formatting error, this is my first post on stack overflow.